I’m having an issue where my shadows are disappearing on one side of a plane depending on what direction the directional light is “shining”.
In the code below a red sphere is created that casts a shadow on a green plane below it.
If the directional light is created like this:
_light = new DirectionalLight(0.5, -1, -0.2);
then the shadow disappears as you approach the left side of the plane.
If the directional light is created like this (change in x direction):
_light = new DirectionalLight(-0.5, -1, -0.2);
then the shadow disappears as you approach the right side of the plane.
Use the left/right arrow keys to move the object.
Edit: Just wanted to add, I had smaller values before for objects sizes where the shadows didn’t disappear on the edges but I had a different issue where as my object approached the ground the shadow would disappear. Scaling the size of all objects and camera locations fixed the issue but introduced this new issue I’m having now…
package
{
import away3d.containers.View3D;
import away3d.entities.Mesh;
import away3d.lights.DirectionalLight;
import away3d.materials.ColorMaterial;
import away3d.materials.lightpickers.StaticLightPicker;
import away3d.materials.methods.FilteredShadowMapMethod;
import away3d.primitives.PlaneGeometry;
import away3d.primitives.SphereGeometry;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Vector3D;
import flash.ui.Keyboard;
[SWF(width='1024',height='768',backgroundColor='#c3d6ea',frameRate='60')]
public class Main extends Sprite
{
private var _view:View3D;
private var _plane:Mesh;
private var _sphere:Mesh;
private var _lightPicker:StaticLightPicker;
private var _light:DirectionalLight;
private var _shadowMapMethod:FilteredShadowMapMethod;
private var _dir:Number;
public function Main()
{
addEventListener(Event.ADDED_TO_STAGE,addedToStage);
}
private function addedToStage(ev:Event):void
{
_view = new View3D();
addChild(_view);
_dir = 0;
_light = new DirectionalLight(0.5, -1, -0.2);
_view.scene.addChild(_light);
_lightPicker = new StaticLightPicker([_light]);
_shadowMapMethod = new FilteredShadowMapMethod(_light);
var greenMaterial:ColorMaterial = new ColorMaterial(0x00FFF00);
greenMaterial.lightPicker = _lightPicker;
greenMaterial.specular = 0;
greenMaterial.shadowMethod = _shadowMapMethod;
_plane = new Mesh(new PlaneGeometry(50000,50000), greenMaterial);
_view.scene.addChild(_plane);
var redMaterial:ColorMaterial = new ColorMaterial(0xFF0000);
redMaterial.lightPicker = _lightPicker;
redMaterial.shadowMethod = _shadowMapMethod;
_sphere = new Mesh(new SphereGeometry(1000), redMaterial);
_sphere.castsShadows = true;
_sphere.position = new Vector3D(0,2000,0);
_view.scene.addChild(_sphere);
_view.camera.position = new Vector3D(0,50000,0);
_view.camera.lookAt(new Vector3D(0,0,0));
_view.camera.lens.far = 100000;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
addEventListener(Event.ENTER_FRAME, update);
}
private function keyDown(k:KeyboardEvent):void
{
switch(k.keyCode)
{
case Keyboard.LEFT:
_dir = -1;
break;
case Keyboard.RIGHT:
_dir = 1;
break;
default:
_dir = 0;
break;
}
}
private function keyUp(k:KeyboardEvent):void
{
switch(k.keyCode)
{
case Keyboard.LEFT:
case Keyboard.RIGHT:
_dir = 0;
break;
}
}
private function update(ev:Event):void
{
_sphere.position = new Vector3D( _sphere.position.x+_dir*100, _sphere.position.y, _sphere.position.z);
_view.render();
}
}
}