-
-
Save JohnBrookes/d9abb3c3eb504fa3b038 to your computer and use it in GitHub Desktop.
Demo of cascade shadowmaps issue.
If light has no z value shadow fails.
See initLight function in this
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//See initLights function comments | |
package | |
{ | |
import away3d.cameras.Camera3D; | |
import away3d.containers.Scene3D; | |
import away3d.containers.View3D; | |
import away3d.controllers.HoverController; | |
import away3d.debug.AwayStats; | |
import away3d.entities.Mesh; | |
import away3d.lights.DirectionalLight; | |
import away3d.lights.PointLight; | |
import away3d.lights.shadowmaps.CascadeShadowMapper; | |
import away3d.materials.lightpickers.StaticLightPicker; | |
import away3d.materials.methods.CascadeShadowMapMethod; | |
import away3d.materials.methods.FilteredShadowMapMethod; | |
import away3d.materials.TextureMultiPassMaterial; | |
import away3d.primitives.CubeGeometry; | |
import away3d.primitives.PlaneGeometry; | |
import away3d.textures.BitmapTexture; | |
import flash.display.BitmapData; | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.events.MouseEvent; | |
public class Main extends Sprite | |
{ | |
private var view:View3D; | |
private var scene:Scene3D; | |
private var camera:Camera3D; | |
//camera controls | |
private var camController:HoverController; | |
private var move:Boolean = false; | |
private var lastPanAngle:Number; | |
private var lastTiltAngle:Number; | |
private var lastMouseX:Number; | |
private var lastMouseY:Number; | |
//lights | |
private var slp:StaticLightPicker; | |
private var dlight:DirectionalLight; | |
private var cascadeShadowMapper:CascadeShadowMapper; | |
private var mainLight:PointLight; | |
public function Main() | |
{ | |
if (stage) | |
init(); | |
else | |
addEventListener(Event.ADDED_TO_STAGE, init); | |
} | |
private function init(e:Event = null):void | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, init); | |
// entry point | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
stage.align = StageAlign.TOP_LEFT; | |
init3D(); | |
initLights(); | |
initObjects(); | |
initListeners(); | |
} | |
private function init3D():void | |
{ | |
view = new View3D(); | |
view.backgroundColor = 0xfcfcfc; | |
view.antiAlias = 8; | |
scene = view.scene; | |
camera = view.camera; | |
addChild(view); | |
camController = new HoverController(camera, null, 180, 35, 4000); | |
camController.yFactor = 3; | |
camController.steps = 32 | |
camController.minTiltAngle = -90 | |
camController.distance = 4000 | |
camera.lens.far = 25000; | |
addChild(new AwayStats(view)); | |
} | |
private function initLights():void | |
{ | |
cascadeShadowMapper = new CascadeShadowMapper(4); | |
//Comment one of these to test | |
//dlight = new DirectionalLight(1, -1, 0); //This will fail | |
dlight = new DirectionalLight(0, -1, 1); //Works | |
scene.addChild(dlight); | |
dlight.castsShadows = true; | |
dlight.shadowMapper = cascadeShadowMapper; | |
dlight.shadowMapper.depthMapSize = 2048; | |
slp = new StaticLightPicker([dlight]); | |
} | |
private function initObjects():void | |
{ | |
var baseShadowMethod:FilteredShadowMapMethod = new FilteredShadowMapMethod(slp.lights[0]); | |
var csm:CascadeShadowMapMethod = new CascadeShadowMapMethod(baseShadowMethod); | |
csm.epsilon = 0.006; | |
var groundPlaneGeom:PlaneGeometry = new PlaneGeometry(100000, 100000, 1, 1); | |
var groundMaterial:TextureMultiPassMaterial = new TextureMultiPassMaterial(new BitmapTexture(new BitmapData(256, 256, false, 0xff0000))); | |
groundMaterial.lightPicker = slp; | |
groundMaterial.shadowMethod = csm | |
var groundPlaneMesh:Mesh = new Mesh(groundPlaneGeom, groundMaterial) | |
scene.addChild(groundPlaneMesh); | |
var cubeGeom:CubeGeometry = new CubeGeometry(1000, 2000, 1000); | |
var cubeMultiMaterial:TextureMultiPassMaterial = new TextureMultiPassMaterial(new BitmapTexture(new BitmapData(256, 256, false, 0xffff00))); | |
cubeMultiMaterial.lightPicker = slp; | |
cubeMultiMaterial.shadowMethod = csm | |
// | |
var cube:Mesh = new Mesh(cubeGeom, cubeMultiMaterial); | |
scene.addChild(cube); | |
cube.castsShadows = true; | |
cube.y = 1000; | |
} | |
private function handleEnterFrame(e:Event):void | |
{ | |
if (move) | |
{ | |
camController.panAngle = 0.3 * (stage.mouseX - lastMouseX) + lastPanAngle; | |
camController.tiltAngle = 0.3 * (stage.mouseY - lastMouseY) + lastTiltAngle; | |
} | |
//dlight.direction = view.camera.forwardVector; | |
view.render(); | |
} | |
private function onMouseDown(event:MouseEvent):void | |
{ | |
lastPanAngle = camController.panAngle; | |
lastTiltAngle = camController.tiltAngle; | |
lastMouseX = stage.mouseX; | |
lastMouseY = stage.mouseY; | |
move = true; | |
stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave); | |
} | |
/** | |
* Mouse up listener for navigation | |
*/ | |
private function onMouseUp(event:MouseEvent):void | |
{ | |
move = false; | |
stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave); | |
} | |
private function onMouseWheel(event:MouseEvent):void | |
{ | |
camController.distance -= event.delta * 10; | |
} | |
/** | |
* Mouse stage leave listener for navigation | |
*/ | |
private function onStageMouseLeave(event:Event):void | |
{ | |
move = false; | |
stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave); | |
} | |
private function onStageResize(event:Event):void | |
{ | |
view.width = stage.stageWidth; | |
view.height = stage.stageHeight; | |
} | |
private function initListeners():void | |
{ | |
addEventListener(Event.ENTER_FRAME, handleEnterFrame); | |
stage.addEventListener(Event.RESIZE, onStageResize); | |
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); | |
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); | |
stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment