Second Life Tree in Papervision3D

January 17, 2009

Intro

A Second Life tree in Papervision3D is pretty easy to make. It’s made up of two planes intersecting at 90 degrees. As you rotate around the tree, it appears to be 3D since you see its sister plane. Of course it’s just an optical illusion, and as you get closer to the tree the planes become more apparent. But given the processing savings of creating a tree with just one primitive-it’s worth it! When you get to the Chapter on virtual tours you’ll use this trick to create an entire grove of trees on a Google Map.

Second Life Tree using Two Intersecting Planes

Second Life Tree using Two Intersecting Planes

Demo

Source

YouTube (Note: Discussion of Primitive does not occur until 1/4 of video has been  played)

Discussion

The trick to creating this illusion is to take a cube, which is made up of six planes, eliminate four of the sides and translate two of them into an intersection point.

From the Cube primitive available in Papervision3D, create a Tree primitive: as was done in the hourglass case (see the book). Then navigate to the plane building functions of the cube (now named tree) and comment out the four unneeded planes. In this case you’ll keep front and right planes.

if( ! (excludeFaces & FRONT) )
buildPlane( “front”, “x”, “y”, width, height, depth2, ! Boolean( insideFaces & FRONT ) );

//Remove if( ! (excludeFaces & BACK) )
//Remove buildPlane( “back”, “x”, “y”, width, height, -depth2, Boolean(
//Remove insideFaces & BACK ) );

if( ! (excludeFaces & RIGHT) )
buildPlane( “right”, “z”, “y”, depth, height, width2, Boolean( insideFaces & RIGHT ) );

//Remove if( ! (excludeFaces & LEFT) )
//Remove buildPlane( “left”, “z”, “y”, depth, height, -width2, !
//Remove Boolean( insideFaces & LEFT ) );

//Remove if( ! (excludeFaces & TOP) )
//Remove buildPlane( “top”, “x”, “z”, width, depth, height2, Boolean(
//Remove insideFaces & TOP ) );

//Remove if( ! (excludeFaces & BOTTOM) )
//Remove buildPlane( “bottom”, “x”, “z”, width, depth, -height2, !
//Remove Boolean( insideFaces & BOTTOM ) );

Then navigate down to the vertex generation equations. Removing the “rev” term from the vertex(u) equation below will bring the front and right planes together.

vertex[ u ] = (iu * incU – textureU)[Remove * rev];
vertex[ v ] = iv * incV – textureV;
vertex[ w ] = depth;

Your primitive is ready to run. But remember the cube uses the materials list. In this case, since you have only two planes, you’ll create a material list of only two “png” image files. You use “png” image files since your tree must have the area around its branches transparent and “png” files have a transparent channel.

//Create a BitmapFileMaterial for your first png
myBitmapFileMaterial = new BitmapFileMaterial(“assets/TreeBigFront.png”);
myBitmapFileMaterial.doubleSided = true;

//Create a BitmapFileMaterial for your first png
myBitmapFileMaterial2 = new BitmapFileMaterial(“assets/TreeBigSide.png”);
myBitmapFileMaterial2.doubleSided = true;

//Add a material list containing your two bitmaps
var materialsList:MaterialsList = new MaterialsList();
materialsList.addMaterial(myBitmapFileMaterial, “front” );
materialsList.addMaterial(myBitmapFileMaterial2, “right” );

//Instantiate your Tree and add it to the scene
mySLTree = new Tree(materialsList,400,400,400,4,4,4);
scene.addChild(mySLTree);
startRendering();

As mentioned above, you must use the material list to add your “png” images to the planes. In the next Chapter (see the book) on Dressing up Prims you’ll learn more about materials and how to apply them. But for now all you do is create two bitmap file materials, add your images to them, and then add them to your material list. Then the material list is added to your Second Life Tree.

Note: Second Life Trees actually have 3 planes. This example gives the lowest possible CPU hit for the biggest bang! It’s part of optimizing Papervision3D.

The rest is the same as before (see the book), just instantiate the tree primitive and add it to your scene using the addChild method: as demonstrated in the code above.

Click the more button below to see the entire Second Life Tree Primitive and BasicView Wrapper file.

Read the rest of this entry »


Follow

Get every new post delivered to your Inbox.