
![]()
|
It Shall Be ScaledThe first and second lessons of this Transform tutorial taught about moving and rotating things, respectively. Amazingly, the power of the Transform node extends further than those two functions. It can also scale its coordinate systems.
Remember that each time the Transform node is used, it creates a new coordinate system, which has your basic X, Y, and Z coordinates. By using the
Transform {
children [] # exposedField MFNode
translation 0.0 0.0 0.0 # exposedField SFVec3f
rotation 0.0 0.0 0.0 0.0 # exposedField SFRotation
scale 1.0 1.0 1.0 # exposedField SFVec3f
scaleOrientation 0.0 0.0 1.0 0.0 # exposedField SFRotation
bboxCenter 0.0 0.0 0.0 # field SFVec3f
bboxSize 0.0 0.0 0.0 # field SFVec3f
center 0.0 0.0 0.0 # exposedField SFVec3f
addChildren # eventIn MFNode
removeChildren # eventIn MFNode
}
Using the
#VRML V2.0 utf8
Transform {
children [ # Build the sphere
Shape {
appearance Appearance {
material Material {} # Microsoft-like greyish color
}
geometry Sphere {
radius 2.0
}
}
]
scale 2.0 1.0 1.0
}
scale 1.0 2.0 1.0. Likewise, to make the sphere longer going back and forth, you'd manipulate the Z axis: scale 1.0 1.0 2.0. Remember that scale factors can be anything. As explained before, you can scale all or any combination of the axes at once.
Using Scale OrientationSimply scaling around the three axes is great, but there is a point where you find you can only do so much. To remedy this problem, VRML includes the convenientscaleOrientation field, which allows you to put in a specific axis and a radian measure to tell the browser exactly how you want to scale your objects. It's much like using the rotation field; in fact, both are of the same field type--SFRotation.When you define a custom orientation, the browser performs three steps to build the children of that particular Transform node:
rotation field, first you need to define which axis you'll be manipulating. For the example above, you'll use the Z axis: scaleOrientation 0.0 0.0 1.0. Now, rotate around that axis by forty-five degrees (view a Javascript degree/radian converter). Your finished code is scaleOrientation 0.0 0.0 1.0 0.785. From there, all you have to do is plug it in to a world and see if it works.
#VRML V2.0 utf8
Transform {
children [
Shape {
appearance Appearance {
material Material {} # Grey!
}
geometry Sphere { # The sphere
radius 2.0
}
}
]
scale 1.0 3.0 1.0 # To show the difference, make the sphere 3 times taller
scaleOrientation 0.0 0.0 1.0 0.785 # Then, it's rotated to what's in here
}
And, as expected, the resulting world. The sphere is three times as tall as it was originally, and it's rotated around the Z axis by forty-five degrees (0.785 radians) so that it's tilted. You could also add a line to rotate the coordinate system that just got finished scaling:
rotation 0.0 1.0 0.0 1.571 The result is a scaled sphere, like the one above, that has been rotated ninety degrees around the Y axis so that the upper end points towards you. Scale Around the CenterScaling around a center point other than the coordinate system's origin is a very easy concept. You can use it on objects where some point should stay fixed while the rest of the system is scaled. For example, a tree:
#VRML V2.0 utf8
# Help us build a tree. Recycle your bytes.
Transform {
children [
Transform {
children [
Shape { # The trunk
appearance Appearance {
material Material {}
}
geometry Cylinder {
radius 1.0
height 3.0
}
}
]
translation 0.0 -3.0 0.0 # A little underground
},
Shape { # The tree's upper portion
appearance Appearance {
material Material {}
}
geometry Cone {
bottomRadius 4.0
height 5.0
}
}
]
}
Now here's the same tree, with this additional line added to the parent
scale 1.0 3.0 1.0 # Three times as tall as it was
Trees, however, grow upwards from the trunk as the trunk stays where it is. So, we need to add a
center 0.0 -5.0 0.0 To view the full effect of this change, load this centered tree, which is three times as tall as its original height, and then load a tree seven times its original height. As you can see, the bottom of the trunk remains stationary, and the scaling is applied to everything above the lowest point. Another mystery of the Transform node unveiled.
|