Papervision3D cubes x240

April 30th, 2008 by Adrian

I’ve just been having a play with Papervision3D 2.0 (Great White) and here are the results of my first test. I create 240 cubes inside a loop, positioning them in rows across the stage. Each cube uses six movieclips from the library as their materials. Each cube then rotates depending on the cursor position on the stage.

Here is the code for the document class.

package
{
    import flash.display.MovieClip;
    import flash.events.Event;

    import flash.display.StageQuality;
   
    import org.papervision3d.cameras.Camera3D;
    import org.papervision3d.render.BasicRenderEngine;
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.view.Viewport3D;
   
    import org.papervision3d.materials.*;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.materials.utils.*;

    public class Main03 extends MovieClip
    {
       
        private var face1:MovieClip;
        private var face2:MovieClip;
        private var face3:MovieClip;
        private var face4:MovieClip;
        private var face5:MovieClip;
        private var face6:MovieClip;
       
        private var viewport:Viewport3D;
        private var renderer:BasicRenderEngine;
        private var scene:Scene3D;
        private var camera:Camera3D;
        private var mm1:MovieMaterial;
        private var mm2:MovieMaterial;
        private var mm3:MovieMaterial;
        private var mm4:MovieMaterial;
        private var mm5:MovieMaterial;
        private var mm6:MovieMaterial;

        private var cube1:Cube;
        private var cube2:Cube;
        private var cube3:Cube;
        private var cube4:Cube;
        private var cube5:Cube;
        private var cube6:Cube;
        private var cube7:Cube;
        private var cube8:Cube;
        private var cube9:Cube;
        private var cube10:Cube;
       
        private var cube11:Cube;
        private var cube12:Cube;
        private var cube13:Cube;
        private var cube14:Cube;
        private var cube15:Cube;
        private var cube16:Cube;
        private var cube17:Cube;
        private var cube18:Cube;
        private var cube19:Cube;
        private var cube20:Cube;
       
        private var cube21:Cube;
        private var cube22:Cube;
        private var cube23:Cube;
        private var cube24:Cube;
        private var cube25:Cube;
        private var cube26:Cube;
        private var cube27:Cube;
        private var cube28:Cube;
        private var cube29:Cube;
        private var cube30:Cube;
       
        private var cube31:Cube;
        private var cube32:Cube;
        private var cube33:Cube;
        private var cube34:Cube;
        private var cube35:Cube;
        private var cube36:Cube;
        private var cube37:Cube;
        private var cube38:Cube;
        private var cube39:Cube;
        private var cube40:Cube;
       
        private var cube41:Cube;
        private var cube42:Cube;
        private var cube43:Cube;
        private var cube44:Cube;
        private var cube45:Cube;
        private var cube46:Cube;
        private var cube47:Cube;
        private var cube48:Cube;
        private var cube49:Cube;
        private var cube50:Cube;
       
        private var cube51:Cube;
        private var cube52:Cube;
        private var cube53:Cube;
        private var cube54:Cube;
        private var cube55:Cube;
        private var cube56:Cube;
        private var cube57:Cube;
        private var cube58:Cube;
        private var cube59:Cube;
        private var cube60:Cube;
       
        private var cube61:Cube;
        private var cube62:Cube;
        private var cube63:Cube;
        private var cube64:Cube;
        private var cube65:Cube;
        private var cube66:Cube;
        private var cube67:Cube;
        private var cube68:Cube;
        private var cube69:Cube;
        private var cube70:Cube;
       
        private var cube71:Cube;
        private var cube72:Cube;
        private var cube73:Cube;
        private var cube74:Cube;
        private var cube75:Cube;
        private var cube76:Cube;
        private var cube77:Cube;
        private var cube78:Cube;
        private var cube79:Cube;
        private var cube80:Cube;
       
        private var cube81:Cube;
        private var cube82:Cube;
        private var cube83:Cube;
        private var cube84:Cube;
        private var cube85:Cube;
        private var cube86:Cube;
        private var cube87:Cube;
        private var cube88:Cube;
        private var cube89:Cube;
        private var cube90:Cube;
       
        private var cube91:Cube;
        private var cube92:Cube;
        private var cube93:Cube;
        private var cube94:Cube;
        private var cube95:Cube;
        private var cube96:Cube;
        private var cube97:Cube;
        private var cube98:Cube;
        private var cube99:Cube;
        private var cube100:Cube;
       
        private var cube101:Cube;
        private var cube102:Cube;
        private var cube103:Cube;
        private var cube104:Cube;
        private var cube105:Cube;
        private var cube106:Cube;
        private var cube107:Cube;
        private var cube108:Cube;
        private var cube109:Cube;
        private var cube110:Cube;
       
        private var cube111:Cube;
        private var cube112:Cube;
        private var cube113:Cube;
        private var cube114:Cube;
        private var cube115:Cube;
        private var cube116:Cube;
        private var cube117:Cube;
        private var cube118:Cube;
        private var cube119:Cube;
        private var cube120:Cube;

        private var cube121:Cube;
        private var cube122:Cube;
        private var cube123:Cube;
        private var cube124:Cube;
        private var cube125:Cube;
        private var cube126:Cube;
        private var cube127:Cube;
        private var cube128:Cube;
        private var cube129:Cube;
        private var cube130:Cube;
       
        private var cube131:Cube;
        private var cube132:Cube;
        private var cube133:Cube;
        private var cube134:Cube;
        private var cube135:Cube;
        private var cube136:Cube;
        private var cube137:Cube;
        private var cube138:Cube;
        private var cube139:Cube;
        private var cube140:Cube;
       
        private var cube141:Cube;
        private var cube142:Cube;
        private var cube143:Cube;
        private var cube144:Cube;
        private var cube145:Cube;
        private var cube146:Cube;
        private var cube147:Cube;
        private var cube148:Cube;
        private var cube149:Cube;
        private var cube150:Cube;
       
        private var cube151:Cube;
        private var cube152:Cube;
        private var cube153:Cube;
        private var cube154:Cube;
        private var cube155:Cube;
        private var cube156:Cube;
        private var cube157:Cube;
        private var cube158:Cube;
        private var cube159:Cube;
        private var cube160:Cube;
       
        private var cube161:Cube;
        private var cube162:Cube;
        private var cube163:Cube;
        private var cube164:Cube;
        private var cube165:Cube;
        private var cube166:Cube;
        private var cube167:Cube;
        private var cube168:Cube;
        private var cube169:Cube;
        private var cube170:Cube;
       
        private var cube171:Cube;
        private var cube172:Cube;
        private var cube173:Cube;
        private var cube174:Cube;
        private var cube175:Cube;
        private var cube176:Cube;
        private var cube177:Cube;
        private var cube178:Cube;
        private var cube179:Cube;
        private var cube180:Cube;
       
        private var cube181:Cube;
        private var cube182:Cube;
        private var cube183:Cube;
        private var cube184:Cube;
        private var cube185:Cube;
        private var cube186:Cube;
        private var cube187:Cube;
        private var cube188:Cube;
        private var cube189:Cube;
        private var cube190:Cube;
       
       
        private var cube191:Cube;
        private var cube192:Cube;
        private var cube193:Cube;
        private var cube194:Cube;
        private var cube195:Cube;
        private var cube196:Cube;
        private var cube197:Cube;
        private var cube198:Cube;
        private var cube199:Cube;
        private var cube200:Cube;
       
        private var cube201:Cube;
        private var cube202:Cube;
        private var cube203:Cube;
        private var cube204:Cube;
        private var cube205:Cube;
        private var cube206:Cube;
        private var cube207:Cube;
        private var cube208:Cube;
        private var cube209:Cube;
        private var cube210:Cube;
       
        private var cube211:Cube;
        private var cube212:Cube;
        private var cube213:Cube;
        private var cube214:Cube;
        private var cube215:Cube;
        private var cube216:Cube;
        private var cube217:Cube;
        private var cube218:Cube;
        private var cube219:Cube;
        private var cube220:Cube;
       
        private var cube221:Cube;
        private var cube222:Cube;
        private var cube223:Cube;
        private var cube224:Cube;
        private var cube225:Cube;
        private var cube226:Cube;
        private var cube227:Cube;
        private var cube228:Cube;
        private var cube229:Cube;
        private var cube230:Cube;
       
        private var cube231:Cube;
        private var cube232:Cube;
        private var cube233:Cube;
        private var cube234:Cube;
        private var cube235:Cube;
        private var cube236:Cube;
        private var cube237:Cube;
        private var cube238:Cube;
        private var cube239:Cube;
        private var cube240:Cube;

        private var cubeSize:Number;
        private var spacing:Number;
        private var polys:Number;
        private var numCubes:Number;
        private var numCols:Number;
        private var numRows:Number;
       
        public function Main03():void
        {
            var i:Number;
            var j:Number;
           
            cubeSize = 15;
            spacing = 25;
            polys = 1;
            numCols = 20;
            numRows = 12;
            numCubes = numCols * numRows;
           
            stage.quality = StageQuality.LOW;
           
            var offset:Number = spacing/2;
            var startX:Number = ((stage.stageWidth/2) * -1) + offset;
            var startY:Number = ((stage.stageHeight/2)) - offset;
           
            viewport = new Viewport3D(0, 0, true, true);
            addChild(viewport);
            viewport.buttonMode = false;
                                   
            renderer = new BasicRenderEngine();
                                   
            scene = new Scene3D();
                                   
            camera = new Camera3D();
            camera.zoom = 11;
            camera.focus = 100;                
           
            face1 = new Face1();
            face2 = new Face2();
            face3 = new Face3();
            face4 = new Face4();
            face5 = new Face5();
            face6 = new Face6();
           
            for (i=1; i< =6; i++) {
                addChild(this["face"+i]);
                this["mm"+i] = new MovieMaterial(this["face"+i]);
                this["mm"+i].interactive = false;
                this["mm"+i].smooth = false;
                this["mm"+i].animated = false;
            }

            var counter:Number = 1;
            for (i=1; i<=numCols; i++) {
                for (j=1; j<=numRows; j++) {
                    this["cube"+counter] = new Cube(new MaterialsList({front:mm1, back:mm2, left:mm3, right:mm4,top:mm5, bottom:mm6}), cubeSize, cubeSize, cubeSize, polys, polys, polys);
                    scene.addChild(this["cube"+counter]);
                    this["cube"+counter].x = startX + (spacing * (i - 1));
                    this["cube"+counter].y = startY - (spacing * (j - 1));
                    counter++;
                }
            }
           
            for (i=1; i<=6; i++) {
                removeChild(this["face"+i]);
            }

            stage.addEventListener(Event.ENTER_FRAME, stage_ENTER_FRAME);
        }
       
       
        private function stage_ENTER_FRAME(event:Event):void
        {
            var xDist:Number = mouseX - stage.stageWidth * 0.5;
            var yDist:Number = mouseY - stage.stageHeight * 0.5;
           
            for (var i:Number = 1; i <= numCubes; i++) {
                this["cube"+i].rotationY += xDist * 0.05;
                this["cube"+i].rotationX += -yDist * 0.05;
            }
           
            renderer.renderScene(scene, camera, viewport);
        }
       
    }
}

If anyone knows how you can declare the private variables for the cubes in a loop then I’d love to know how you do it.

Remember to download and install the Papervision 2.0 (Great White) classes from GoogleCode and modify the class path in the publish settings dialogue box.

You can download the zipped up source files here.

Posted in Flash, Papervision 3D | 6 Comments »

Installing Papervision3D on Flash and Flex

April 24th, 2008 by Adrian

Papervision3D, Flex and Flash Logos

Fabian Vercuiel has posted a useful entry on his blog titled ‘Installing Papervision3D on Flash and Flex‘ which outlines how to get up and running with Papervision3D using Flash and Flex.

He covers, downloading and installing a subversion client such as TortoiseSVN, getting the Great White Papervision3D files from GoogleCode, downloading and installing the Flash Extension (mxp file), adding the Papervision3D class-path, adding the SWC file in Flex Builder.

This is all pretty useful information if you haven’t done it before.

Posted in Flash, Flex, Papervision 3D | 1 Comment »