private var view:View3D; private var camera:HoverCamera3D; private var thing:Object3D; private var thingIndex:uint; //loading materials images private var loader:Loader; private var loader2:Loader; private var isLoad:Boolean; private var isLoad2:Boolean; private var isImagesLoaded:Boolean; //lights private var lightDirect:DirectionalLight3D; private var lightDirect2:DirectionalLight3D; private var sphere:Sphere; private var sphere2:Sphere; private var lightColors:Array = [0xff0000, 0x00ff00, 0x0000ff, 0xffffff, 0x00ffff, 0xff00ff, 0xffff00]; private var lightColorIndex:uint = 0; //lights dragging private var clickTarget:uint; private var isMousePress:Boolean; private var dragPositionX:int; private var dragPositionZ:int; //navigation variables private var lastPanAngle:Number; private var lastTiltAngle:Number; private var lastMouseX:Number; private var lastMouseY:Number; private var isDragging:Boolean = false; public function NormalMapTest():void { prepareAway3d(); createLights(); createObjects(); addListeners(); } private function prepareAway3d():void { camera = new HoverCamera3D(); view = new View3D({camera:camera}); view.x = 250; view.y = 250; addChild(view); } private function createLights():void { lightDirect = new DirectionalLight3D( { color:0xFFFFFF, ambient:0.25, diffuse:0.75, specular:0.9 } ); lightDirect2 = new DirectionalLight3D( { color:0xffffff, ambient:0.25, diffuse:0.75, specular:0.9 } ); lightDirect.x = -200; lightDirect.z = -200; lightDirect2.x = 200; lightDirect2.z = 200; lightDirect.y = 200; lightDirect2.y = 200; view.scene.addChild( lightDirect ); view.scene.addChild( lightDirect2 ); } private function createObjects():void { sphere = new Sphere({material: new ColorMaterial(0xffffff), radius:40, segmentsW:4, segmentsH:4, y:50}); sphere2 = new Sphere( { material: new ColorMaterial(0xffffff), radius:40, segmentsW:4, segmentsH:4, y:50 } ); thingIndex = 1; createObject3D(new WireframeMaterial(), true); sphere.name = "light1"; sphere2.name = "light2"; view.scene.addChild(sphere); view.scene.addChild(sphere2); } private function createObject3D(material:*, changeMat:Boolean):void { if (thing) { view.scene.removeChild(thing); } switch (thingIndex) { case 1 : thing = new Plane( { material:material, width:512, height:512, segmentsW:2, segmentsH:2, ownCanvas:true } ); if (changeMat) { //Cobble Stone by Nutsy http://www.filterforge.com/filters/3643.html loadMaterial("3643-diffuse.jpg", "3643-normal.jpg"); } break; case 2 : thing = new Sphere( { material: material, radius:256, segmentsW:12, segmentsH:12, y: -128, ownCanvas:true } ); thing.rotationX = -90; if (changeMat) { //Wooden Wave by Constantin Malkov http://www.filterforge.com/filters/5043.html loadMaterial("5043-diffuse.jpg", "5043-normal.jpg"); } break; case 3 : thing = new Cube( { material: material, width:400, height:200, depth:400, ownCanvas:true } ); if (changeMat) { //Lounge Lizards by Crapadilla http://www.filterforge.com/filters/1596.html loadMaterial("1596-diffuse.jpg", "1596-normal.jpg"); } break; case 4 : thing = new Torus( { material: material, radius:150, tube:100, segmentsR:8, segmentsT:12, ownCanvas:true } ); if (changeMat) { //Snow_02 by jensp http://www.filterforge.com/filters/442.html loadMaterial("442-diffuse.jpg", "442-normal.jpg"); } break; } view.scene.addChild(thing); } private function addListeners():void { addEventListener(Event.ENTER_FRAME, onEnterFrame); stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler); stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler); stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler); view.scene.addOnMouseMove(dragElement); view.scene.addOnMouseUp(dropElement); sphere.addOnMouseDown(clickElement); sphere2.addOnMouseDown(clickElement); thing.addOnMouseDown(dropElement);// drop light when click on thing } private function loadMaterial(urlDiffuse:String, urlNormal:String):void { isImagesLoaded = false; isLoad = false; loader = new Loader( ); loader.contentLoaderInfo.addEventListener( Event.COMPLETE, handleComplete ); loader.load( new URLRequest( urlDiffuse ) ); isLoad2 = false; loader2 = new Loader( ); loader2.contentLoaderInfo.addEventListener( Event.COMPLETE, handleComplete2 ); loader2.load( new URLRequest( urlNormal ) ); } private function handleComplete( event:Event ):void { isLoad = true; if (isLoad2) { setupMaterial(); } } private function handleComplete2( event:Event ):void { isLoad2 = true; if (isLoad) { setupMaterial(); } } private function setupMaterial():void { isImagesLoaded = true; var image1:Bitmap = loader.content as Bitmap; var image2:Bitmap = loader2.content as Bitmap; var material:Dot3BitmapMaterial = new Dot3BitmapMaterial(image1.bitmapData, { normalMap:image2.bitmapData } ); createObject3D(material, false); } private function onEnterFrame(event:Event):void { if (clickTarget == 1) { lightDirect.x = dragPositionX; lightDirect.z = dragPositionZ; } else if (clickTarget == 2) { lightDirect2.x = dragPositionX; lightDirect2.z = dragPositionZ; } view.render(); sphere.position = lightDirect.position; sphere2.position = lightDirect2.position; if (isDragging && isMousePress == false) { camera.targetpanangle = 0.3*(stage.mouseX - lastMouseX) + lastPanAngle; camera.targettiltangle = 0.3*(stage.mouseY - lastMouseY) + lastTiltAngle; } camera.hover(); } private function clickElement(event:MouseEvent3D):void { isMousePress = true; if (event.object.name == "light1") { clickTarget = 1; } else if (event.object.name == "light2") { clickTarget = 2 ; if (lightColorIndex >= lightColors.length) { lightColorIndex = 0; } lightDirect2.color = lightColors[lightColorIndex]; //trace("lightColors[lightColorIndex]",lightColors[lightColorIndex]) sphere2.material = new ColorMaterial(lightColors[lightColorIndex]); lightColorIndex++; } } private function dropElement(event:MouseEvent3D):void { isMousePress = false; clickTarget = 0; } private function dragElement(event:MouseEvent3D):void { dragPositionX = event.sceneX; dragPositionZ = event.sceneZ; } private function onMouseUpHandler(event:MouseEvent):void { isDragging = false; } private function onMouseDownHandler(event:MouseEvent):void { lastPanAngle = camera.targetpanangle; lastTiltAngle = camera.targettiltangle; lastMouseX = stage.mouseX; lastMouseY = stage.mouseY; isDragging = true; } private function keyUpHandler(e:KeyboardEvent):void { if (isChar(e.charCode, "1")) { thingIndex = 1; createObject3D(new WireframeMaterial(), true); } else if (isChar(e.charCode, "2")) { thingIndex = 2; createObject3D(new WireframeMaterial(), true); } else if (isChar(e.charCode, "3")) { thingIndex = 3; createObject3D(new WireframeMaterial(), true); } else if (isChar(e.charCode, "4")) { thingIndex = 4; createObject3D(new WireframeMaterial(), true); } } private function isChar(code:Number, str:String):Boolean { if (code == str.charCodeAt()) { return true; } return false; }
17.05.2008. 06:19
Hi. I'm slightly confused by the DirectionalLight3D light in Away3D. Do you know if it works in the same way as, for instance, the primitive OpenGL and Direct3D directional lights? You have placeholders that can be moved about in your demo here, but I don't normally associate directional lights with a tangible position. Do the x, y and z properties actually refer to the position of the DirectionalLight3D, and if so, what is the light directing at? The origin? Is it pointing from 0, 0, 0 to x, y, z?
Thanks for your time.
Yes, as we see, in away3d sources(DirectionalLight.as), it referer to position:
//update direction vector
direction.x = light.x;
direction.y = light.y;
direction.z = light.z;
Unfortunatly direction is private property.
direction vector as public have small 3d engine ??” AS3D:
http://www.libspark.org/browser/as3/AS3D/src/as3d/light/DirectionalLight3D.as
this article help me to understand away3d.....really thanks
Write a comment
* = required field