Intertactive 3D Cube with Dynamic Textures

Intro

We are moving along with assignment 2 of chapter 2 of Professional Papervisoin3D. And in the first video below you learned how to build the standard Papervision3D class presented  in Chapter 2. And in the next three videos you learned how to use BasicView to build an interactive cube with dynamic textures. Dynamic means that you can change the textures while the program is running just by clicking on them.

PV3D Class

The Papervision3D Class PV3D Book

BasicView Interactive Cube Part 1

Building an Interactive Cube Part 1

BasicView Interactive Cube Part 2

Interactive Cube Part 2


To see the code for the interactive cube Part 1 click the link below:

package {

import flash.events.Event;

import org.papervision3d.events.InteractiveScene3DEvent;

import org.papervision3d.materials.ColorMaterial;

import org.papervision3d.materials.utils.MaterialsList;

import org.papervision3d.objects.primitives.Cube;

import org.papervision3d.view.BasicView;

//SWF metadata tag

[SWF(width=“800”, height=“600”, backgroundColor=“#000000”, frameRate=“60”)]

public class CubeExample extends BasicView

{

private var myCube:Cube;

private var myMaterials1:MaterialsList;

//Primary colors

private var cred: ColorMaterial = new ColorMaterial(0x00ffff);

private var cgreen: ColorMaterial = new ColorMaterial(0xff00ff);

private var cblue: ColorMaterial = new ColorMaterial(0xffff00);

private var myTickNumber:uint = 0;

private var myBooleanStart:Boolean = true;

//myClick Number

private var myClickIs:uint=0;

public function CubeExample()

{

super(0,0,true,true);

initPV3D();

initListeners();

startRendering();

}

private function initPV3D():void

{

//Set Viewport interactivity

viewport.buttonMode = true;

//Set Material Interactivity

cred.interactive = true;

cgreen.interactive = true;

cblue.interactive = true;

//Dictionary class, which represents a dictionary.

//A dictionary is an array that allows you to use

//any type of object as a key to distinguish between elements.

//An array whose items consist of pairs of objects,

//known as the key and the value. The key is used instead

//of a numeric index to identify a single element.

//Primary colors Material List

myMaterials1 = new MaterialsList();

myMaterials1.addMaterial( cred, “front” );

myMaterials1.addMaterial( cred, “back” );

myMaterials1.addMaterial( cblue, “left” );

myMaterials1.addMaterial( cblue, “right” );

myMaterials1.addMaterial( cgreen, “top” );

myMaterials1.addMaterial( cgreen, “bottom” );

myCube = new Cube(myMaterials1,400,400,400);

scene.addChild(myCube);

}

//Add Some Interactivity

private function initListeners():void

{

myCube.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK, myClick);

}

//Cube click method

private function myClick(event:InteractiveScene3DEvent):void{

//Increment click number

myClickIs++;

//Change cube color

if(myClickIs%4==1){

myCube.materials.getMaterialByName(“front”).fillColor= 0xff0000;

myCube.materials.getMaterialByName(“back”).fillColor= 0xff0000;

myCube.materials.getMaterialByName(“left”).fillColor= 0x00ff00;

myCube.materials.getMaterialByName(“right”).fillColor= 0x00ff00;

myCube.materials.getMaterialByName(“top”).fillColor= 0x0000ff;

myCube.materials.getMaterialByName(“bottom”).fillColor= 0x0000ff;

}

if(myClickIs%4==3){

myCube.materials.getMaterialByName(“front”).fillColor= 0x00ffff;

myCube.materials.getMaterialByName(“back”).fillColor= 0x00ffff;

myCube.materials.getMaterialByName(“left”).fillColor= 0xff00ff;

myCube.materials.getMaterialByName(“right”).fillColor= 0xff00ff;

myCube.materials.getMaterialByName(“top”).fillColor= 0xffff00;

myCube.materials.getMaterialByName(“bottom”).fillColor= 0xffff00;

}

}

//Animartion Loop

override protected function onRenderTick(e:Event=null):void

{

if(myClickIs%2 != 1){

myCube.rotationX++;

myCube.rotationY++;

myCube.rotationZ++;

}

super.onRenderTick();

}

}

}

//Dictionary Stub Code

/*import flash.display.Sprite;

import flash.utils.Dictionary;

var groupMap:Dictionary = new Dictionary();

// objects to use as keys

var spr1:Sprite = new Sprite();

var spr2:Sprite = new Sprite();

var spr3:Sprite = new Sprite();

// objects to use as values

var groupA:Object = new Object();

var groupB:Object = new Object();

// Create new key-value pairs in dictionary.

groupMap[spr1] = groupA;

groupMap[spr2] = groupB;

groupMap[spr3] = groupB;

if (groupMap[spr1] == groupA)

{

trace(“spr1 is in groupA”);

}

if (groupMap[spr2] == groupB)

{

trace(“spr2 is in groupB”);

}

if (groupMap[spr3] == groupB)

{

trace(“spr3 is in groupB”);

}*/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: