Skip to content

Instantly share code, notes, and snippets.

@andreiRS
Created January 28, 2014 21:23
Show Gist options
  • Save andreiRS/8676837 to your computer and use it in GitHub Desktop.
Save andreiRS/8676837 to your computer and use it in GitHub Desktop.
Away3D Simple Shadow
package {
import away3d.containers.ObjectContainer3D;
import away3d.containers.View3D;
import away3d.debug.AwayStats;
import away3d.entities.Mesh;
import away3d.lights.DirectionalLight;
import away3d.materials.ColorMaterial;
import away3d.materials.lightpickers.StaticLightPicker;
import away3d.materials.methods.SoftShadowMapMethod;
import away3d.primitives.CylinderGeometry;
import away3d.primitives.PlaneGeometry;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.Vector3D;
[SWF(backgroundColor = "#000000", width = "1000", height = "600", frameRate = "60", quality = "HIGH")]
public class Main extends Sprite {
private var _view: View3D;
private var _mainLight: DirectionalLight;
private var _mainContainer: ObjectContainer3D;
public function Main() {
_initializeEngine();
_initializeStage();
_initializeLights();
_initializeCamera();
_setupScene();
}
private function _initializeStage(): void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, onResize);
_updateViewSize();
}
private function _setupScene(): void {
_mainContainer = new ObjectContainer3D();
_mainContainer.addChild(_createGround());
_mainContainer.addChild(_createCoin());
_view.scene.addChild(_mainContainer);
}
private function _createGround(): Mesh {
var planeMaterial: ColorMaterial = new ColorMaterial(0xFFFFFF);
planeMaterial.lightPicker = new StaticLightPicker([_mainLight]);
planeMaterial.specular = .1;
planeMaterial.gloss = 0;
// better performance but lower shadow quality
planeMaterial.shadowMethod = new SoftShadowMapMethod(_mainLight);
// better shadow quality
// planeMaterial.shadowMethod = new SoftShadowMapMethod(_mainLight, 32, 8);
return new Mesh(new PlaneGeometry(1000, 1000), planeMaterial);
}
private function _createCoin(): Mesh {
var coinMaterial: ColorMaterial = new ColorMaterial(0xFFD700);
coinMaterial.lightPicker = new StaticLightPicker([_mainLight]);
coinMaterial.specular = .5;
coinMaterial.gloss = 20;
var coin: Mesh = new Mesh(new CylinderGeometry(50, 50, 10), coinMaterial);
coin.y = 100;
return coin;
}
private function _initializeCamera(): void {
//setup the camera
_view.camera.z = -600;
_view.camera.y = 500;
_view.camera.lookAt(new Vector3D());
}
private function _initializeEngine(): void {
_view = new View3D();
_view.antiAlias = 16;
addChild(_view);
addChild(new AwayStats(_view));
//setup the render loop
addEventListener(Event.ENTER_FRAME, _onEnterFrame);
}
private function _initializeLights(): void {
//setup lights
_mainLight = new DirectionalLight(-0.5, -1, -1);
_mainLight.castsShadows = true;
_mainLight.y = 100;
_mainLight.color = 0xFFFFFF;
_mainLight.diffuse = 1;
_mainLight.specular = 1;
//_mainLight.radius = 400;
//_mainLight.fallOff = 500;
_mainLight.ambient = 0xA0A0A0;
_mainLight.ambient = .5;
_view.scene.addChild(_mainLight);
}
/**
* render loop
*/
private function _onEnterFrame(e: Event): void {
_mainContainer.rotationY += .1;
_view.render();
}
/**
* stage listener for resize events
*/
private function onResize(event: Event = null): void {
_updateViewSize();
}
private function _updateViewSize(): void {
_view.width = stage.stageWidth;
_view.height = stage.stageHeight;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment