generate complex objects with JAVASCRIPT 
Author Message
 generate complex objects with JAVASCRIPT

hallo everyone!

i have a problem with javascript.
i want to create complex objects with javascript.

the following code generate only one object and
i don't know why.

#  :
---------------------------------------------------------------------------

PROTO test [
          field   SFVec3f     position             0.0 0.0 0.0
          field   SFInt32     numOfObjects      1
          field   SFColor     selectAnObjectColor  .8 .4 .7
          field   SFColor     ObjectColor          .8 .4 .7
          field   SFColor     TrommelColor         .5 .5 .5
          field   SFFloat     spacing 3.0
          field   MFNode      theNode    []
] {
    DEF aGroup Group {}
    Script {
        directOutput TRUE
        field   SFInt32         numOfObjects IS numOfObjects
        field   SFNode        myGroup USE aGroup
        field   SFFloat         spacing IS spacing
        field   MFNode          accumulatedTransforms []
        field   MFNode          myNode IS theNode

        url "javascript:
        function initialize() {
           for(i=0;i<numOfObjects;i++) {
              xform = new MFNode() ;
              xform = myNode ;
              xform[0].set_translation = new SFVec3f(spacing*i,0,0);
              accumulatedTransforms[i] = xform[0];
           }
           myGroup.addChildren = accumulatedTransforms;
   }
  "
 }

Quote:
} # END PROTO ConeTree

#  :
///////////////////////////////////////////////////////////////////////////

test {
        numOfObjects 2
        theNode Transform {
           children [
                   Shape {
                           appearance Appearance {
                                   material Material {
                                           diffuseColor 1 0 0
                                   }
                           }
                           geometry Sphere {}
                   }
           ]
      }
  }# END of Code

but i don't want to generate the object like this term

url "javascript:
                         function initialize() {
                                 sphereString = 'Transform { '+
                                                                  '
children [ '+

'               Shape { '+

'                       appearance Appearance { '+

'                               material Material { '+

'                                       diffuseColor 1 0 0 '+

'                               } '+

'                       } '+

'                       geometry Sphere {} '+

'               } '+
                                                                 '
] '+
                                                                 '} ';
                                 for(i=0;i<numberOfSpheres;i++) {
                                         xform = new MFNode();
                                         xform =
Browser.createVrmlFromString(sphereString);

                                         xform[0].set_translation = new
SFVec3f(spacing*i,0,0);
                                         accumulatedTransforms[i] =
xform[0];

                                 }
                                 myGroup.addChildren =
accumulatedTransforms;

                         }
                 "

Any tips or suggestions?

peter



Mon, 02 Jul 2001 03:00:00 GMT  
 generate complex objects with JAVASCRIPT

Quote:

>     Script {
>         directOutput TRUE
>         field   SFInt32         numOfObjects IS numOfObjects
>         field   SFNode        myGroup USE aGroup
>         field   SFFloat         spacing IS spacing
>         field   MFNode          accumulatedTransforms []
>         field   MFNode          myNode IS theNode

>         url "javascript:
>         function initialize() {
>            for(i=0;i<numOfObjects;i++) {
>               xform = new MFNode() ;
>               xform = myNode ;
>               xform[0].set_translation = new SFVec3f(spacing*i,0,0);
>               accumulatedTransforms[i] = xform[0];
>            }
>            myGroup.addChildren = accumulatedTransforms;
>    }
>   "
>  }

> Any tips or suggestions?

> peter

Preallocate memory instead of doing it in a for-loop.
Also you do not need a dummy variable "xform".
When you declare a scalar field in a Script node,
you automatically are allocating space for it.  For
multi-fields, all you need to do is adjust the length.
This triggers automatically the memory allocation,
so do it once if you know the length ahead of time.

        function initialize() {
           accumulatedTransforms.length = numOfObjects;
           for(i=0;i<numOfObjects;i++) {
              accumulatedTransforms[i].x = spacing*i;
              accumulatedTransforms[i].y = 0;
              accumulatedTransforms[i].z = 0;
           }
           myGroup.addChildren = accumulatedTransforms;
   }

This probably doesn't answer your question...  but people
seem to make their JavaScript programs unnecessarilly complex.
That's my explanation for why things don't work.

--
  James S?rlie

  sorliej.vcf
< 1K Download


Mon, 02 Jul 2001 03:00:00 GMT  
 generate complex objects with JAVASCRIPT

Quote:

> that's a good idea,

> but it doesn't work
> look at my modifications:

> #VRML V2.0 utf8

> # ConeTreePROTO.wrl
> # This file was created by peter wibberg
> # in 1999

> #  : ---------------------------------------------------------------------------
> PROTO test [
>         field   SFVec3f     position             0.0 0.0 0.0
>   field   SFInt32     numOfObjects      1
>   field   SFColor     selectAnObjectColor  .8 .4 .7
>   field   SFColor     ObjectColor          .8 .4 .7
>   field   SFColor     TrommelColor         .5 .5 .5
>         field   SFFloat     spacing 3.0
>   field   MFNode      theNode    []

> ] {
>     DEF aGroup Group {}
>  Script {
>   directOutput TRUE
>   field   SFInt32         numOfObjects IS numOfObjects
>   field   SFNode          myGroup USE aGroup
>   field   SFFloat         spacing IS spacing
>   field   MFNode          accumulatedTransforms []
>   field   MFNode          myNode IS theNode

>   url "javascript:
>    function initialize() {
>     accumulatedTransforms.length = numOfObjects;
>              for(i=0;i<numOfObjects;i++) {
>      accumulatedTransforms[i] = myNode[0] ;
>      accumulatedTransforms[i].translation.x = spacing*i;
>      accumulatedTransforms[i].translation.y = 0;
>      accumulatedTransforms[i].translation.z = 0;
>     }
>     myGroup.addChildren = accumulatedTransforms;

>    }
>   "
>  }
> } # END PROTO ConeTree

> #  : ///////////////////////////////////////////////////////////////////////////

> test {
>   numOfObjects 2
>   theNode Transform {
>            children [
>                    Shape {
>                            appearance Appearance {
>                                    material Material {
>                                            diffuseColor 1 0 0
>                                    }
>                            }
>                            geometry Sphere {}
>                    }
>            ]
>   }
>  }# END of code

> when i set the "numOfObjects" to 5, i can only see the object with the
> last calculated position.
> all the other objects generate before the last position are not visible.

> peter wibberg

Pete,

I wasn't intending to solve your problem, just direct
you to less complex and more efficient programming.
If I understand what you are trying to do, you will need
to add the "theNode" to the children of each transform.
This is not what you have done by:

     accumulatedTransforms[i] = myNode[0] ;

You probably need a line that affects the following change:

   accumulatedTransforms[i].children[0] = myNode[0];

It might be something like:

   accumulatedTransforms[i].addChildren = myNode[0];

I hope you're not confusing the semantics of OpenInventor's
scene graph with that of vrml 2.0.  Perhaps you need to
read more of the language reference...?

  http://www.vrml.org/Specifications/VRML97/part1/concepts.html
  http://www.vrml.org/Specifications/VRML97/part1/nodesRef.html

Please don't feel offended.  I'm trying to sound constructive.

--
  James S?rlie

  sorliej.vcf
< 1K Download


Mon, 02 Jul 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Javascript on PHP generated page

2. Why Object-Oriented JavaScript is ideal for GOFAI

3. build object with JAVASCRIPT

4. change object color using Javascript, HELP!

5. newby: Timeout error when using javascript confirm object

6. patterns on creating complex objects?

7. Fly-over of complex object yields missing faces on close approach

8. IE6 +VRML + javascript in VRML trouble (was: problem using javascript window.open in VRML)

9. Storing a complex object in a session

10. Complex Arithmetic Object/Methods?

11. Storing/printing a complex object

12. Complex integration with oscillting Exp(complex argument) factor.

 

 
Powered by phpBB® Forum Software