Hi,
Beign a newbie, I work myself through the Away3D book by FriendsOf Ed
and fooling around with the samples. Now I am confused, because there
is a sample with three objects in it (a cube, a sphere, and a monkey),
using DirectionalLight3D and some Dot3BitmapMaterial.
The thing that appears wrong to me is that the monkey is lit correctly
from the top (camera.tiltAngle = 10), but the primitives are lit from
below. The picture in the book shows a correct result though.
Can anybody give me a hint where my mistake is?
This is the code:
// Base Class (to be extended)
package
{
import away3d.cameras.*;
import away3d.containers.*;
import away3d.primitives.*;
import assets.*;
import flash.display.*;
import flash.events.*;
[SWF(width = "800",height = "600")]
public class Away3D_BaseClass5 extends Sprite
{
protected var _view:View3D;
protected var _camera:HoverCamera3D;
protected var _cube:Cube;
protected var _sphere:Sphere;
protected var _ape:MonkeyMesh;
protected var _state:int = 0;
public function Away3D_BaseClass5 ()
{
_createView ();
_createScene ();
_createMaterials ();
trace("Constructor called");
}
protected function _createView ():void
{
// create the Camera
_camera = new HoverCamera3D;
_camera.distance = 150;
_camera.tiltAngle = 10;
_view = new View3D();
_view.x = 400;
_view.y = 300;
_view.camera = _camera;
addChild (_view);
stage.addEventListener (Event.ENTER_FRAME,
_onEnterFrame);
stage.addEventListener (MouseEvent.CLICK, _onClick);
}
protected function _createScene ():void
{
// create the primiteves
_cube = new Cube();
_cube.width = 30;
_cube.height = 30;
_cube.depth = 30;
_cube.x = -70;
_view.scene.addChild (_cube);
_sphere = new Sphere({material:"blue#cyan"});
_sphere.radius = 25;
_sphere.x = 70;
_sphere.y = 0;
_sphere.rotationX = 180;
_sphere.segmentsW = 16;
_sphere.segmentsH = 10;
//_sphere.yUp = false;
_view.scene.addChild (_sphere);
_ape = new MonkeyMesh();
_ape.scale(2.5);
_view.scene.addChild (_ape);
}
protected function _createMaterials ():void
{
// to be overridden
}
protected function _toggle ():void
{
// overridden
}
protected function _onClick (ev:MouseEvent):void
{
_toggle();
}
protected function _onEnterFrame (ev:Event):void
{
// define camera bahaviour and render scene
if(stage.mouseX < 130 || stage.mouseX > 450)
{
_camera.panAngle -= (stage.mouseX -
stage.stageWidth/2) / 100;
_camera.hover();
}
else
{
_camera.hover();
}
_view.render();
}
}
}
// Normal Maps
package
{
import away3d.core.math.*;
import away3d.core.utils.*;
import away3d.lights.*;
import away3d.materials.*;
import away3d.primitives.*;
import flash.display.*;
import flash.events.*;
[SWF(width = "800",height = "600")]
public class Away3D_NormalMaps extends Away3D_BaseClass5
{
private var _dirLight:DirectionalLight3D;
private var _angle:Number = 0;
public static const RADS_PRE_DEG:Number = Math.PI / 180;
[Embed("assets/ch05/sphere_normals.jpg")]
private var SphereNormal:Class;
[Embed("assets/ch05/ape_normals.jpg")]
private var ApeNormal:Class;
[Embed("assets/ch05/cube_normals.jpg")]
private var CubeNormal:Class;
[Embed("assets/ch05/redapple.jpg")]
private var AppleImage:Class;
public function Away3D_NormalMaps ()
{
super ();
_cube.mappingType = CubeMappingType.MAP6;
_createLights ();
}
protected function _createLights ():void
{
_dirLight = new DirectionalLight3D();
_dirLight.direction = new Number3D(70,500,70);
_dirLight.ambient = 0.1;
_dirLight.diffuse = 0.5;
_dirLight.specular = 1;
_dirLight.brightness = 2;
_view.scene.addLight (_dirLight);
}
override protected function _createMaterials ():void
{
var texture:BitmapData = Cast.bitmap(AppleImage);
var sphereMap:BitmapData = Cast.bitmap(SphereNormal);
var apeMap:BitmapData = Cast.bitmap(ApeNormal);
var cubeMap:BitmapData = Cast.bitmap(CubeNormal);
var sphereMaterial:Dot3BitmapMaterial;
sphereMaterial = new
Dot3BitmapMaterial(texture,sphereMap);
sphereMaterial.specular = 0xFFFFFF;
var apeMaterial:Dot3BitmapMaterial;
apeMaterial = new Dot3BitmapMaterial(texture,apeMap);
apeMaterial.specular = 0xFFFFFF;
var cubeMaterial:Dot3BitmapMaterial;
cubeMaterial = new Dot3BitmapMaterial(texture,cubeMap);
cubeMaterial.specular = 0xFFFFFF;
_cube.material = cubeMaterial;
_cube.ownCanvas = true;
_sphere.material = sphereMaterial;
_sphere.ownCanvas = true;
_ape.material = apeMaterial;
_ape.ownCanvas = true;
}
override protected function _onEnterFrame(ev:Event):void
{
/*
_angle = (_angle + 5) % 360;
var x:Number = 100 * Math.cos(RADS_PER_DEG * _angle);
var y:Number = 50 * Math.cos(RADS_PER_DEG * _angle);
var z:Number = 100 * Math.sin(RADS_PER_DEG * _angle);
_dirLight.direction = new Number3D(-x,-y,-z);
*/
super._onEnterFrame(ev);
}
}
}
Thanks a lot!!