package com.freesome.papervision { import org.papervision3d.core.*; import org.papervision3d.core.proto.*; import org.papervision3d.core.geom.*; import org.papervision3d.objects.Mesh; import org.papervision3d.materials.ColorMaterial; import com.freesome.util.ColorConversion; import org.papervision3d.scenes.*; import flash.display.BitmapData; import flash.display.Bitmap; import flash.display.Sprite; public class Sprite3D2 extends Mesh { /** * Default value of segments. */ static public var DEFAULT_SCALE :Number = 1; static public var materials:Array = new Array(); public var cubes:Array; public var meta:Array; private var lxm:Number=0; private var lym:Number=0; public function Sprite3D2( bitmap:Bitmap, material:MaterialObject3D=null, scale:Number=0, initObject:Object=null ){ super( material, new Array(), new Array(), initObject ); scale = scale || DEFAULT_SCALE; var w:Number = bitmap.width; var h:Number = bitmap.height; cubes = new Array(); meta = new Array(); for (var yp:uint = 0; yp < h;yp++) { for (var xp:uint = 0; xp< w; xp++) { if ((bitmap.bitmapData.getPixel32(xp,yp) & 0xff000000 )!= 0) { var c = bitmap.bitmapData.getPixel32(xp,yp); buildCube( scale,c,xp-(w/2),yp-(h/2),0 ); } } } } public function addMaterials(scene:Scene3D):void { for (var m in materials) scene.push(materials[m]); } public function addCubes(scene:Scene3D):void { for (var c in cubes) scene.push(cubes[c]); } public function explode():void { for (var c in cubes) { var cu:Mesh = cubes[c]; meta[c].vx = 100-Math.random() * 200; meta[c].vy = 100-Math.random() * 200; meta[c].vz = 100-Math.random() * 200; } } private function dim(col:Number):Number { var o:Object = ColorConversion.hex24torgb(col); o["red"] = Math.min(0xff,o["red"]+0x11); o["blue"] = Math.min(0xff,o["blue"]+0x11); o["green"] = Math.min(0xff,o["green"]+0x11); return ColorConversion.rgbtohex24(o["red"], o["green"], o["blue"]); } private function makeMaterials(col):Array { var m:Array = new Array; for (var a:Number= 0; a<6;a++) { var fi:String = ColorConversion.toHexadecimalString(col); if (Sprite3D.materials[fi] == null) { var cm:ColorMaterial = new ColorMaterial(col,100, {oneSide:true}); //cm.oneSide = true; Sprite3D.materials[fi] = cm; } m.push(Sprite3D.materials[fi]); col = this.dim(col); } return m; } public function remove() { } private function buildCube( scale:Number, col:Number, xp:Number, yp:Number,zp:Number ):void{ var a :Number = 10 * scale; var mxp:int; var myp:int; mxp = xp*(10*scale) +4; myp = yp*(10*scale) +4 ; xp=0; yp=0; var mats:Array = this.makeMaterials(col); var v:Array = [ new Vertex3D( xp, yp, zp ), //0 new Vertex3D( xp+a, yp, zp ), //1 new Vertex3D( xp, yp+a, zp ), //2 new Vertex3D( xp+a, yp+a, zp ), //3 new Vertex3D( xp, yp, zp+a ), //4 new Vertex3D( xp+a, yp, zp+a ), //5 new Vertex3D( xp, yp+a, zp+a ), //6 new Vertex3D( xp+a, yp+a, zp+a ), //7 ]; //this.vertices = v.concat(this.vertices);; var f:Array = [ new Face3D( [v[1], v[0], v[2]], mats[0] ), //front new Face3D( [v[1], v[2], v[3]], mats[0]), new Face3D( [v[4], v[5], v[6]], mats[5] ), //back new Face3D( [v[5], v[7], v[6]], mats[5]), new Face3D( [v[5], v[4], v[0]], mats[1] ), //top new Face3D( [v[0], v[1], v[5]], mats[1]), new Face3D( [v[6], v[7], v[2]], mats[4] ), //bottom new Face3D( [v[7], v[3], v[2]], mats[4]), new Face3D( [v[0], v[4], v[6]], mats[2] ), //left new Face3D( [v[6], v[2], v[0]], mats[2]), new Face3D( [v[5], v[1], v[7]], mats[3] ), //right new Face3D( [v[7], v[1], v[3]], mats[3]), ]; var cube:Mesh = new Mesh(null,v,f); cube.x = mxp; cube.y = myp; this.cubes.push(cube); this.meta.push({vz:0, az:1, vx:0, ax:0, ay:0, vy:0, sx:mxp, sy:myp}); //this.faces = f.concat(this.faces); //this.projectTexture( "x", "z" ); } } }