Hi!
I’m new here and just started testing Away3D for an upcoming project. I’ve having problems with updating the lights when rotating a mesh. I’ve tried searching the forum and google but haven’t found a working solution. Here is the full source:
package {
import away3d.containers.View3D;
import away3d.cameras.lenses.PerspectiveLens;
import away3d.loaders.parsers.Parsers;
import away3d.events.LoaderEvent;
import away3d.loaders.Loader3D;
import away3d.entities.Mesh;
import away3d.materials.TextureMaterial;
import away3d.textures.BitmapTexture;
import away3d.lights.DirectionalLight;
import away3d.materials.lightpickers.StaticLightPicker;
import flash.display.MovieClip;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.geom.Vector3D;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.ui.*;
import flash.events.*;
import com.greensock.*;
import com.greensock.easing.*;
public class Main extends MovieClip {
private var view:View3D;
private var modelLoader:Loader3D;
private var model:Mesh;
private var textureLoader:Loader;
private var lightPicker:StaticLightPicker;
private var startRotationY:Number = 0;
private var moveObject:Boolean = false;
private var startX:Number = 0;
private var dragOffsetX:Number = 0;
public function Main() {
// Create view
view = new View3D();
view.background = new BitmapTexture(new BG());
view.camera.lens = new PerspectiveLens(30);
view.camera.position = new Vector3D(50,0,0);
view.camera.lookAt(new Vector3D());
view.antiAlias = 2;
addChild(view);
// Add lights
var light1 = new DirectionalLight();
light1.direction = new Vector3D(0, -1, 0);
light1.ambient = 0.1;
light1.diffuse = 0.7;
view.scene.addChild(light1);
var light2 = new DirectionalLight();
light2.direction = new Vector3D(0, -1, 0);
light2.color = 0x00FFFF;
light2.ambient = 0.1;
light2.diffuse = 0.7;
view.scene.addChild(light2);
lightPicker = new StaticLightPicker([light1]);
// Load the model
loadModel();
}
private function mouseHandler(event:MouseEvent):void {
switch (event.type) {
case 'mouseDown':
moveObject = true;
startX = event.stageX;
startRotationY = model.rotationY;
break;
case 'mouseMove':
if (moveObject) {
dragOffsetX = startX - event.stageX;
dragOffsetX /= 2;
TweenMax.to(model, 0.5, {rotationY:startRotationY + dragOffsetX, ease: Cubic.easeOut, onUpdate: function(){
view.render();
}});
}
break;
case 'mouseUp':
moveObject = false;
break;
}
}
///////////////////////////////////////////////////////////////////
//
// Texture
//
///////////////////////////////////////////////////////////////////
private function loadTexture():void {
textureLoader = new Loader();
textureLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, textureLoaded);
textureLoader.load(new URLRequest('Army_boot_texture.png'));
}
private function textureLoaded(event:Event):void {
var bitmap:Bitmap = textureLoader.content as Bitmap;
var bitmapTexture:BitmapTexture = new BitmapTexture(bitmap.bitmapData);
var texture:TextureMaterial = new TextureMaterial(bitmapTexture, true, true);
texture.lightPicker = lightPicker;
model.material = texture;
}
///////////////////////////////////////////////////////////////////
//
// Model
//
///////////////////////////////////////////////////////////////////
private function loadModel():void {
Parsers.enableAllBundled();
modelLoader = new Loader3D();
modelLoader.addEventListener(LoaderEvent.RESOURCE_COMPLETE, modelLoaded);
modelLoader.load(new URLRequest('Army_boots.obj'));
view.scene.addChild(modelLoader);
view.camera.lookAt(modelLoader.position);
}
private function modelLoaded(event:LoaderEvent):void {
// Add loaded model to scene
model = Mesh(modelLoader.getChildAt(0));
model.y -= 5;
view.scene.addChild(model);
// Dispose loader
modelLoader.dispose();
modelLoader = null;
// Load texture and apply light
loadTexture();
// Add listeners
addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
addEventListener(MouseEvent.MOUSE_MOVE, mouseHandler);
addEventListener(MouseEvent.MOUSE_UP, mouseHandler);
addEventListener(Event.ENTER_FRAME, renderView);
}
private function renderView(event:Event):void {
view.render();
}
}
}
Thanks!
/ Erik