Using CubeMaterialsData to map different BitMapMaterial to sides of a Cube

Software: Away3D 3.x

James RM, Newbie
Posted: 05 August 2011 06:24 PM   Total Posts: 1

Hey Guys,

I’m a pretty seasoned AS3 dev and I was looking to create bitmaps from textfields and apply them to different sides of a cube primitive. However, when I’m running into trouble when it comes to using the CubeMaterialsData object to assign different BitMapMaterial to each side.

The front side comes out as expected, however the bottom and top sides appear to have different orientations. I’ve searched the forum, tried implementing a matrix transform and a few other things and have not found a solution.

Anyone got any ideas?

package
{
 import away3d
.containers.View3D;
 
import away3d.materials.BitmapMaterial;
 
import away3d.primitives.Cube;
 
import away3d.primitives.data.CubeMaterialsData;
 
 
import flash.display.Bitmap;
 
import flash.display.BitmapData;
 
import flash.display.Graphics;
 
import flash.display.Sprite;
 
import flash.events.Event;
 
import flash.events.MouseEvent;
 
import flash.geom.Matrix;
 
import flash.geom.Rectangle;
 
import flash.text.TextField;
 
import flash.text.TextFieldAutoSize;
 
import flash.text.TextFormat;
 
 public class 
CubeEx3 extends Sprite
 {
  
public var bg:Sprite = new Sprite
  public var 
view:View3D;
  public var 
cube:Cube;
  
  
  public var 
firstSideData:BitmapData;
  public var 
firstSideText:TextField = new TextField;
  public var 
firstSideTextFormat:TextFormat = new TextFormat;
  
  public var 
secondSideData:BitmapData;
  public var 
secondSideText:TextField = new TextField;
  public var 
secondSideTextFormat:TextFormat = new TextFormat;
  
  public var 
thirdSideData:BitmapData;
  public var 
thirdSideText:TextField = new TextField;
  public var 
thirdSideTextFormat:TextFormat = new TextFormat;
  
  public var 
w:int 300;
  public var 
h:int 100;
  
  public function 
CubeEx3()
  
{
   bg
.graphics.beginFill(0xf2f2f2,1);
   
bg.graphics.drawRect(0,0,500,200);
   
bg.graphics.endFill();
   
addChild(bg);
   
drawSideOne();
   
drawSideTwo();
   
drawSideThree();
   
init3D();
   
this.addEventListener(Event.ENTER_FRAME,loop);
  
}
  
  
private function init3D():void
  {
   view 
= new View3D({x:250,y:150});
   
addChild(view);
   
   
cube = new Cube({width:w,height:h,depth:h,bothsides:true});
   var 
cData:CubeMaterialsData = new CubeMaterialsData;
   
cData.front = new BitmapMaterial(firstSideData);
   
cData.bottom = new BitmapMaterial(secondSideData);
   
cData.top = new BitmapMaterial(thirdSideData);
   
cube.cubeMaterials cData;
   
   
view.scene.addChild(cube);
   
view.render();
  
}
  
  
private function drawSideOne():void
  {
   
   firstSideTextFormat
.font "Myriad Pro";
   
firstSideTextFormat.color 0xF2F2F2;
   
   
firstSideText.text "First Side";
   
firstSideText.setTextFormat(firstSideTextFormat);
   
firstSideText.autoSize TextFieldAutoSize.CENTER;
   
   var 
cubeMatrix:Matrix = new Matrix;
   
   
firstSideData = new BitmapData(w,h,false,0x00000000);
   
firstSideData.draw(firstSideText,cubeMatrix);
  
}
  
  
public function drawSideTwo():void{

   secondSideTextFormat
.font "Myriad Pro";
   
secondSideTextFormat.color 0xF2F2F2;
   
secondSideText.text "Second Side";
   
secondSideText.setTextFormat(secondSideTextFormat);
   
secondSideText.autoSize TextFieldAutoSize.CENTER;   
   
   var 
cubeMatrix2:Matrix = new Matrix;
   
cubeMatrix2
   
   secondSideData 
= new BitmapData(h,w,false,0x00000000);
   
secondSideData.draw(secondSideText,cubeMatrix2,null,null,null,true);
  
}

  
private function drawSideThree():void
  {
   
   thirdSideTextFormat
.font "Myriad Pro";
   
thirdSideTextFormat.color 0xF2F2F2;
   
thirdSideText.text "Third Side";
   
thirdSideText.setTextFormat(thirdSideTextFormat);
   
thirdSideText.autoSize TextFieldAutoSize.CENTER;
   
thirdSideData = new BitmapData(h,w,false,0x00000000);
   
thirdSideData.draw(thirdSideText);
  
}
  
  
public function loop(e:Event):void{
   cube
.rotationX += -5;
   
view.render();
  
}
 }
   

James RM, Newbie
Posted: 05 August 2011 07:53 PM   Total Posts: 1   [ # 1 ]

NM, I achieved the desired effect by patching together 4 planes and rotating them in an obj group:

package
{
 import away3d
.containers.ObjectContainer3D;
 
import away3d.containers.View3D;
 
import away3d.primitives.Cube;
 
import away3d.primitives.Plane;
 
 
import flash.display.Bitmap;
 
import flash.display.BitmapData;
 
import flash.display.Graphics;
 
import flash.display.Sprite;
 
import flash.events.Event;
 
import flash.events.MouseEvent;
 
import flash.geom.Matrix;
 
import flash.geom.Vector3D;
 
import flash.text.TextField;
 
import flash.text.TextFieldAutoSize;
 
import flash.text.TextFormat;
 
 
// http://www.actionscript.org/forums/showthread.php3?t=150918
 
 
public class CubeEx4 extends Sprite
 {
  
public var bg:Sprite = new Sprite
  public var 
view:View3D;
  public var 
w:int 300;
  public var 
h:int 100;
  public var 
origin:Vector3D = new Vector3D(0,0,0);
  public var 
cCont:ObjectContainer3D = new ObjectContainer3D;
  public var 
c1:away3d.primitives.Plane;
  public var 
c2:away3d.primitives.Plane;
  public var 
c3:away3d.primitives.Plane;
  public var 
c4:away3d.primitives.Plane;
  
  public function 
CubeEx4()
  
{
   
   
//initBMP();
   
init3D();
   
this.addEventListener(Event.ENTER_FRAME,loop);
  
}
  
  
private function init3D():void
  {
   
   view 
= new View3D({x:250,y:150});
   
addChild(view);
   
   
c1 = new Plane({name:"c1",segmentsW:2,segmentsH:2,width:w,height:h,x:0,y:0,z:0,bothsides:true});
   
c2 = new Plane({name:"c2",segmentsW:2,segmentsH:2,width:w,height:h,x:0,y:h,z:0,bothsides:true});
   
c3 = new Plane({name:"c3",segmentsW:2,segmentsH:2,width:w,height:h,x:0,y:0,z:0,bothsides:true});
   
c3.rotationX 90;
   
c4 = new Plane({name:"c4",segmentsW:2,segmentsH:2,width:w,height:h,x:0,y:0,z:h,bothsides:true});
   
c4.rotationX 90;
   
c3.h/2;
   
c3.h/2;
   
c4.h/2;
   
c4.= -h/2;
   
   
cCont.addChild(c1);
   
cCont.addChild(c2);
   
cCont.addChild(c3);
   
cCont.addChild(c4);
   
   
view.scene.addChild(cCont);
   
view.render();
  
}
  
  
public function loop(e:Event):void{
   cCont
.rotationX+=5;
   
cCont.
   
view.render();
  
}
 }
   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X