Multi-Material Cube class for Away3d

Quite a common question I see being asked is how to have a cube with a different material on each face. Currently, you can set ’tile6′ to ‘true’ which will map a texture to the UV so that it is split onto each face correctly, but it can be a bit fiddly, and also does not allow for different material types on each face, or to set different eventListeners on each face.

This class takes variables for the dimensions of the cube, and 6 materials, and creates a cube for you, from 6 correctly sized, rotated and positioned faces. At some point I intend to do this the ‘proper’ way (ie by creating a geometry class with 6 submeshes) but for the time being this works fine:

 import away3d.containers.ObjectContainer3D;
 import away3d.entities.Mesh;
 import away3d.primitives.PlaneGeometry;

 * A quick way to create cubes with multiple material
 * @author Mark Sutton
 public class multiMatCube extends ObjectContainer3D

var planesContainer:ObjectContainer3D = new ObjectContainer3D;
 var face1:Mesh;
 var face2:Mesh;
 var face3:Mesh;
 var face4:Mesh;
 var face5:Mesh;
 var face6:Mesh;

public function multiMatCube(width, height, depth, face1Mat, face2Mat, face3Mat, face4Mat, face5mat, face6Mat):void

var faceGeometry:PlaneGeometry = new PlaneGeometry(1, 1, 1, 1 )
 face1 =  new Mesh(faceGeometry, face1Mat );
 face1.scaleX = width;
 face1.scaleZ = depth;

face1.rotationX = 0
 face1.y = height / 2;

 face2 =  new Mesh(faceGeometry, face2Mat);
 face2.scaleX = width;
 face2.scaleZ = depth;
 face2.rotationX = 180;
 face2.y = -height/2;

 face3 = new Mesh (faceGeometry, face3Mat);
 face3.scaleX = width;
 face3.scaleZ = height;
 face3.rotationX = 90
 face3.z = depth / 2;

 face4 = new Mesh (faceGeometry, face4Mat);
 face4.scaleX = width;
 face4.scaleZ = height;
 face4.rotationX = 270
 face4.z = -depth / 2;

 face5 = new Mesh (faceGeometry, face5mat);
 face5.scaleX = depth;
 face5.scaleZ = height;
 face5.rotationX = -90
 face5.rotationY = -90
 face5.x = width / 2;

 face6 = new Mesh (faceGeometry, face6Mat);
 face6.scaleX = depth;
 face6.scaleZ = height;
 face6.rotationX = -90
 face6.rotationY = 90
 face6.x = -width / 2;



Using it is simple:

var newMultiCube: multiMatCube = new multiMatCube (cubeWidth, cubeHeight, cubeDepth,  material1, material2, material3, material4, material5, material6);

and you can add eventListeners to each face too:

newMultiCube.face1.addEventListener(MouseEvent3D.MOUSE_DOWN, face1click);


7 thoughts on “Multi-Material Cube class for Away3d

  1. 2 optimisations ( well three if I would add that you should enable strict typing,but I won’t ;) ) :

    - you can drop line 15, it isn’t used.
    - you wanted to use subgeometries.
    well you still can without extra work:
    after line 78 add:

    var merge:Merge = new Merge(true, false)
    var mesh:Mesh = merge.applyToContainer(this)
    var i:int = 6;
    while ( i– ) {
    var face:Mesh = this["face" + (i + 1)];
    this["face" + (i + 1)] = null;
    PS: I’m a newbie but I tested it and works great with preservation of materials.

  2. Pingback: Flog! > » Blog Archive » away3d 4.0, cube with six materials and individual face meshes

  3. Ah yes, Line 15 is a poor orphaned leftover variable… I’ll do the kind thing and get rid of the little fella…

    Does your merged version still allow for an eventlistener on each face?

    • I have no idea about the eventlistener on individual meshes, but I came across a post on the away forum that had to do with event problems. Don’t know or this applies here to in one way or another.
      I’m a 3D newbee so if you have some example code on how to add listeners, I’m glad to learn how and test it for ya.
      What I do know is that when you merge the meshes into a new one, the result has less than 6 submeshes when you used similar materials multiple cubes sides, so I can imagine that it won’t work as expected ?

  4. I’m not totally sure about having eventlisteners on submeshes, hopefully they can be referenced as usual..

    I’m going to leave this class for the time being I think, apart from deleteing the orphan variable as you suggested, and also I think I might use your naming convention for the sides, makes it easier to follow…

    The ultimate aim I think would be to replace, and construct the cube at the vertex level..

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>