Rotate object, update lights

Software: Away3D 4.x

xiw89, Newbie
Posted: 02 April 2014 09:07 AM   Total Posts: 2

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, -10);
      
light1.ambient 0.1;
      
light1.diffuse 0.7;
      
view.scene.addChild(light1);

      var 
light2 = new DirectionalLight();
      
light2.direction = new Vector3D(0, -10);
      
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(model0.5{rotationY:startRotationY dragOffsetXeaseCubic.easeOutonUpdate: function(){
              view
.render();
            
}});
          
}
          
break;

        case 
'mouseUp':
          
moveObject false;
          break;
      
}
    }

    
///////////////////////////////////////////////////////////////////
    //
    //  Texture
    //
    ///////////////////////////////////////////////////////////////////

    
private function loadTexture():void {
      textureLoader 
= new Loader();
      
textureLoader.contentLoaderInfo.addEventListener(Event.COMPLETEtextureLoaded);
      
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(bitmapTexturetruetrue);
      
texture.lightPicker lightPicker;
      
model.material texture;
    
}


    
///////////////////////////////////////////////////////////////////
    //
    //  Model
    //
    ///////////////////////////////////////////////////////////////////

    
private function loadModel():void {
      Parsers
.enableAllBundled();
      
modelLoader = new Loader3D();
      
modelLoader.addEventListener(LoaderEvent.RESOURCE_COMPLETEmodelLoaded);
      
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.-= 5;
      
view.scene.addChild(model);

      
// Dispose loader
      
modelLoader.dispose();
      
modelLoader null;

      
// Load texture and apply light
      
loadTexture();

      
// Add listeners
      
addEventListener(MouseEvent.MOUSE_DOWNmouseHandler);
      
addEventListener(MouseEvent.MOUSE_MOVEmouseHandler);
      
addEventListener(MouseEvent.MOUSE_UPmouseHandler);

      
addEventListener(Event.ENTER_FRAMErenderView);
    
}

    
private function renderView(event:Event):void {
      view
.render();
    
}
  }

Thanks!
/ Erik

   

Mr Margaret Scratcher, Sr. Member
Posted: 02 April 2014 12:05 PM   Total Posts: 344   [ # 1 ]

What do you mean by update the lights?

What is it that you want to happen, and what is currently happening?

   

Avatar
theMightyAtom, Sr. Member
Posted: 02 April 2014 01:29 PM   Total Posts: 669   [ # 2 ]

Lights don’t need any explicit updating, which is probably why you don’t find any info about that wink

They are placed at 0,0,0, though that doesn’t matter for directional lights.
If you’re getting no lighting on your object (i.e it’s black), check that your normals are the right way around…

Good Luck!

   

xiw89, Newbie
Posted: 02 April 2014 01:39 PM   Total Posts: 2   [ # 3 ]

I have no idea what solved it, it solved it self :D Right after I posted, it started working.

Thanks anyways!

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X