Simple light and shadow example?

Software: Away3D 4.x

Tricky Widget, Newbie
Posted: 16 June 2012 06:31 PM   Total Posts: 19

I’m trying to learn how to use light and shadow in Away3D, but all the examples and tutorials I can find are for 3.x.  Can anyone point me to something made with 4.0?  Or paste in a snippet of minimal code illustrating a lit object casting a shadow?

Thank you very much!  smile

   

Tricky Widget, Newbie
Posted: 17 June 2012 02:40 AM   Total Posts: 19   [ # 1 ]

OK, after much trial and error, I’ve managed to sort out a simple light.  Here it is in case anyone else is wondering (or if anyone sees a problem with doing it this way!).  I’m still not sure how to work shadows yet, though.  Any help would be appreciated!

package
{
 import away3d
.containers.View3D;
 
import away3d.entities.Mesh;
 
import away3d.lights.PointLight;
 
import away3d.materials.ColorMaterial;
 
import away3d.materials.lightpickers.StaticLightPicker;
 
import away3d.primitives.CubeGeometry;
 
import flash.display.Sprite;
 
import flash.events.Event;
 
 public class 
Main extends Sprite
 {
  
public var view:View3D = new View3D();
  
  public function 
Main():void
  {
   
var pointLight:PointLight = new PointLight();
   
pointLight.specular .5;
   
view.scene.addChild(pointLight);
   
   var 
colorMaterial:ColorMaterial = new ColorMaterial(0xff0000);
   
colorMaterial.lightPicker = new StaticLightPicker([pointLight]);
   
   var 
cube:Mesh = new Mesh(new CubeGeometry(), colorMaterial);
   
cube.100;
   
view.scene.addChild(cube);
   
   
view.camera.= -100;
   
   
addChild(view);
   
   
this.addEventListener(Event.ENTER_FRAMEonEnterFrame);
  
}
  
  
private function onEnterFrame(event:Event):void
  {
   view
.render();
  
}
 }
   

Richard Olsson, Administrator
Posted: 17 June 2012 09:49 AM   Total Posts: 1192   [ # 2 ]

There’s an example called Basic_Load3DS in the examples repository on GitHub that loads an ant model from a 3DS file, and sets it up for lighting and shadow-casting. You can find the latest revision of the example through the following direct link:

https://github.com/away3d/away3d-examples-fp11/blob/master/src/Basic_Load3DS.as

   

Tricky Widget, Newbie
Posted: 17 June 2012 11:04 AM   Total Posts: 19   [ # 3 ]

Ah, thank you!  That did the trick.  smile

Here’s my completed snippet:

package
{
 import away3d
.containers.View3D;
 
import away3d.entities.Mesh;
 
import away3d.lights.DirectionalLight;
 
import away3d.lights.PointLight;
 
import away3d.materials.lightpickers.StaticLightPicker;
 
import away3d.materials.ColorMaterial;
 
import away3d.materials.methods.HardShadowMapMethod;
 
import away3d.primitives.CubeGeometry;
 
import flash.display.Sprite;
 
import flash.events.Event;
 
 public class 
Main extends Sprite
 {
  
public var view:View3D = new View3D();
  
  public function 
Main():void
  {
   
var directonalLight:DirectionalLight = new DirectionalLight(0010);
   
directonalLight.specular .5;
   
view.scene.addChild(directonalLight);
   
   var 
plainColorMaterial:ColorMaterial = new ColorMaterial(0xff0000);
   
plainColorMaterial.lightPicker = new StaticLightPicker([directonalLight]);
   
   var 
plainCube:Mesh = new Mesh(new CubeGeometry(), plainColorMaterial);
   
plainCube.100;
   
view.scene.addChild(plainCube);
   
   var 
shadowColorMaterial:ColorMaterial = new ColorMaterial(0x00ff00);
   
shadowColorMaterial.lightPicker = new StaticLightPicker([directonalLight]);
   
// Note that the shadowMethod goes on the material displaying the shadow, not the material casting it!
   
shadowColorMaterial.shadowMethod = new HardShadowMapMethod(directonalLight);
   
   var 
shadowCube:Mesh = new Mesh(new CubeGeometry(100010001000), shadowColorMaterial);
   
shadowCube.1200;
   
view.scene.addChild(shadowCube);
   
   
view.camera.= -100;
   
view.camera.= -100;
   
   
addChild(view);
   
   
this.addEventListener(Event.ENTER_FRAMEonEnterFrame);
  
}
  
  
private function onEnterFrame(event:Event):void
  {
   view
.render();
  
}
 }
   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X