package { import away3d.containers.View3D; import away3d.debug.AwayStats; import away3d.lights.DirectionalLight; import away3d.materials.ColorMaterial; import away3d.materials.methods.SoftShadowMapMethod; import away3d.primitives.Cube; import away3d.primitives.Plane; import flash.events.Event; import flash.geom.Vector3D; import flash.utils.getTimer; [SWF(width="720",height="480",frameRate="60",backgroundColor="#FFFFFF")] public class Main extends View3D { static private const CUBE_ROUND:uint = 500; static private const CUBE_SIZE:uint = 100; static private const CUBE_SEGMENT:uint = 1; static private const CUBE_AMOUNT:uint = 150; static private const CAMERA_POSITION:uint = 500; static private const LIGHT_POSITION:uint = 1000; static private const PLANE_SIZE:uint = 2000; private var light:DirectionalLight; private var plane:Plane; public function Main() { backgroundColor = 0xFFFFFF; antiAlias = 8; addChild(new AwayStats(this)); light = new DirectionalLight(); light.color = 0xFFFFFF; light.specular = .2; light.diffuse = 1; scene.addChild(light); var mf:ColorMaterial = new ColorMaterial(0xFFFFFF); mf.shadowMethod = new SoftShadowMapMethod(light); mf.lights = [light]; plane = new Plane(mf, PLANE_SIZE, PLANE_SIZE, 1, 1, true); plane.y = -CUBE_ROUND; plane.castsShadows = true; scene.addChild(plane); var i:int = CUBE_AMOUNT; var mc:ColorMaterial = new ColorMaterial(0xFFFFFF); mc.lights = [light]; while (i--) { var cube:Cube = new Cube(mc, CUBE_SIZE, CUBE_SIZE, CUBE_SIZE, CUBE_SEGMENT, CUBE_SEGMENT, CUBE_SEGMENT); cube.x = CUBE_ROUND * Math.random() - CUBE_ROUND / 2; cube.y = CUBE_ROUND * Math.random() - CUBE_ROUND / 2; cube.z = CUBE_ROUND * Math.random() - CUBE_ROUND / 2; cube.scale(.8 * Math.random() + .2); cube.rotationX = 360 * Math.random(); cube.rotationY = 360 * Math.random(); cube.rotationZ = 360 * Math.random(); cube.castsShadows = true; scene.addChild(cube); } addEventListener(Event.ENTER_FRAME, enterFrameHandler); } private function enterFrameHandler(e:Event):void { camera.x = CAMERA_POSITION * Math.sin(-getTimer() / 10000); camera.z = CAMERA_POSITION * Math.cos(-getTimer() / 10000); camera.y = 500 * Math.sin(getTimer() / 3000) + 800; camera.lookAt(new Vector3D()); light.position = new Vector3D(LIGHT_POSITION * Math.sin(getTimer() / 2000), LIGHT_POSITION * 1, LIGHT_POSITION * Math.cos(getTimer() / 2000)); var d:Vector3D = light.position.clone(); d.negate(); d.normalize(); light.direction = d; render(); } } }