Saturday, 14 January 2012

Add texture to cube face in Away3DLite

I recently tried to add a different texture to the faces of a cube in Away3DLite. I noticed that you can only add a single texture to the whole cube. I stumbled across this post the author has written a class that allows you to add different textures to the faces of the cube. To use the class you simply put the files into the relevant Away3DLite folders. Here is a quick example I made. I have six different images files in the library with the image class set to: Texture1, Texture2 etc.

package 
{
 import away3dlite.cameras.*
 import away3dlite.containers.*;
 import away3dlite.materials.*;
 import away3dlite.primitives.*;
 import away3dlite.primitives.Data.CubeMaterialsData;
 import flash.display.MovieClip;
 import flash.events.Event;
 import flash.events.MouseEvent;

 public class CubeFace extends MovieClip 
 {
  private var view:View3D;
  private var camera:Camera3D;
  private var faceCube:Cube;
  
  public function CubeFace() {
   createView();
   createScene();
  }
  
  private function createView():void 
  {
   camera = new Camera3D();
   camera.z = -1000;
   
   view = new View3D();
   view.x = stage.stageWidth / 2;
   view.y = stage.stageHeight / 2;
   view.camera = camera;
   addChild(view);
   
   stage.addEventListener(Event.ENTER_FRAME, ef);
  }
  
  
  private function createScene():void 
  {
   var cmd:CubeMaterialsData = new CubeMaterialsData();
   cmd.back = new BitmapMaterial( new MyTexture1(0,0) );
   cmd.bottom = new BitmapMaterial( new MyTexture2(0,0) );
   cmd.front = new BitmapMaterial( new MyTexture3(0,0) );
   cmd.left = new BitmapMaterial( new MyTexture4(0,0) );
   cmd.right = new BitmapMaterial( new MyTexture5(0,0) );
   cmd.top = new BitmapMaterial( new MyTexture6(0,0) );
   
   faceCube = new Cube(cmd, 100, 100, 100);
   view.scene.addChild(faceCube);
  }
  
  private function ef(e:Event):void 
  {
   
   faceCube.rotationY += 3;
   faceCube.rotationX += 3;
   
   view.render();
  }
 }
}



Update

There is also a lightweight 3D engine called ND3D that you can add different textures to the faces of the cube. Here is a quick example.

package 
{
 import flash.display.MovieClip;
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.events.MouseEvent;
 import de.nulldesign.nd3d.material.SWFMaterial;
 import de.nulldesign.nd3d.objects.Mesh;
 import de.nulldesign.nd3d.objects.PointCamera;
 import de.nulldesign.nd3d.renderer.Renderer;
 import de.nulldesign.nd3d.objects.Cube;

 public class CubeFace extends MovieClip
 {
  private var SWF_WIDTH:uint = 400;
  private var SWF_HEIGHT:uint = 300;

  private var camera:PointCamera;
  private var renderer:Renderer;
  private var renderList:Array;
  private var materialsArray:Array;

  private var swfMatTop:SWFMaterial;
  private var swfMatRight:SWFMaterial;
  private var swfMatLeft:SWFMaterial;
  private var swfMatFront:SWFMaterial;
  private var swfMatBottom:SWFMaterial;
  private var swfMatBack:SWFMaterial;

  public function Cube3D()
  {
   renderer = new Renderer(Sprite(addChild(new Sprite())));

   camera = new PointCamera(SWF_WIDTH,SWF_HEIGHT);
   camera.zOffset = 1000;
   
   renderList = [];

   swfMatFront   = new SWFMaterial(new Texture1(), true);
   swfMatTop     = new SWFMaterial(new Texture2(), true);
   swfMatBack    = new SWFMaterial(new Texture3(), true);
   swfMatBottom  = new SWFMaterial(new Texture4(), true);
   swfMatRight   = new SWFMaterial(new Texture5(), true);
   swfMatLeft    = new SWFMaterial(new Texture6(), true);
   
   var c:Mesh = new Cube([swfMatFront, swfMatTop, swfMatBack, swfMatBottom, swfMatRight, swfMatLeft],200);
   
   renderList.push(c);

   addEventListener(Event.ENTER_FRAME, ef);
  }
  
  private function ef(e:Event):void
  {
   swfMatTop.update();
   swfMatRight.update();
   swfMatLeft.update();
   swfMatFront.update();
   swfMatBottom.update();
   swfMatBack.update();

   renderer.render(renderList, camera);
  }
 }
}

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP