Created
March 22, 2014 11:37
-
-
Save andreiRS/9705783 to your computer and use it in GitHub Desktop.
Away3d example how to use the "depth of field" filter/effect.
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
package { | |
import away3d.containers.View3D; | |
import away3d.debug.AwayStats; | |
import away3d.entities.Mesh; | |
import away3d.filters.DepthOfFieldFilter3D; | |
import away3d.lights.DirectionalLight; | |
import away3d.lights.PointLight; | |
import away3d.materials.ColorMaterial; | |
import away3d.materials.lightpickers.StaticLightPicker; | |
import away3d.primitives.SphereGeometry; | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
/** | |
* Away3d example how to use the "depth of field" filter/effect. | |
* | |
* Note: The implementation was adapted from the example of David Lenaerts to work with the latest | |
* Away3d version (4.1.6). | |
*/ | |
[SWF(width="1920", height="1024", backgroundColor="0x000000", frameRate="60")] | |
public class DOFTest extends Sprite { | |
private static const GRID_NUM_X: int = 10; | |
private static const GRID_NUM_Y: int = 10; | |
private static const GRID_NUM_Z: int = 10; | |
private static const GRID_SPACING: int = 200; | |
private static const GRID_ELEMENT_SIZE: int = 20; | |
private var _view: View3D; | |
private var _roamer: Mesh; | |
private var _count1: Number = 0; | |
private var _count2: Number = 0; | |
public function DOFTest() { | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
stage.align = StageAlign.TOP_LEFT; | |
_initView3D(); | |
stage.addEventListener(Event.ENTER_FRAME, _onEnterFrame); | |
} | |
private function _initView3D(): void { | |
_createAndAddView3D(); | |
_updateCameraPosition(); | |
var roamerLight: PointLight = _createRoamerLight(); | |
_roamer = _createRoamer(roamerLight); | |
_addRoamer(_roamer); | |
var ambientLight: DirectionalLight = _createAmbientLight(); | |
_addAmbientLight(ambientLight); | |
var lightPicker: StaticLightPicker = _createLightPicker(roamerLight, ambientLight); | |
_createElements(lightPicker); | |
_addFilters(); | |
_addAwayStats(); | |
} | |
private function _addFilters(): void { | |
_view.filters3d = [_createDepthOfFieldFilter()]; | |
} | |
private function _createDepthOfFieldFilter(): DepthOfFieldFilter3D { | |
// for changing the strength of the filter update the constructor parameters | |
var dofFilter: DepthOfFieldFilter3D = new DepthOfFieldFilter3D(32, 32); | |
dofFilter.range = 500; | |
dofFilter.focusTarget = _roamer; | |
return dofFilter; | |
} | |
private function _createLightPicker(roamerLight: PointLight, | |
ambientLight: DirectionalLight): StaticLightPicker { | |
return new StaticLightPicker([roamerLight, ambientLight]); | |
} | |
private function _createElementMaterial(lightPicker: StaticLightPicker): ColorMaterial { | |
var elementMaterial: ColorMaterial = new ColorMaterial(0x808080); | |
elementMaterial.ambientColor = 0x303040; | |
elementMaterial.ambient = 1; | |
elementMaterial.lightPicker = lightPicker; | |
return elementMaterial; | |
} | |
private function _addRoamer(roamer: Mesh): void { | |
_view.scene.addChild(roamer); | |
} | |
private function _addAmbientLight(ambientLight: DirectionalLight): void { | |
_view.scene.addChild(ambientLight); | |
} | |
private function _createAndAddView3D(): void { | |
_view = new View3D(); | |
addChild(_view); | |
} | |
private function _updateCameraPosition(): void { | |
_view.camera.x = 0; | |
_view.camera.y = 0; | |
_view.camera.z = 0; | |
} | |
private function _createRoamer(roamerLight: PointLight): Mesh { | |
var roamer: Mesh = new Mesh(new SphereGeometry(GRID_ELEMENT_SIZE)); | |
roamer.material = new ColorMaterial(0xff0000); | |
roamer.addChild(roamerLight); | |
return roamer; | |
} | |
private function _createElements(lightPicker: StaticLightPicker): void { | |
var elementMaterial: ColorMaterial = _createElementMaterial(lightPicker); | |
for (var gx: uint = 0; gx < GRID_NUM_X; ++gx) { | |
for (var gy: uint = 0; gy < GRID_NUM_Y; ++gy) { | |
for (var gz: uint = 0; gz < GRID_NUM_Z; ++gz) { | |
var element: Mesh = new Mesh(new SphereGeometry(GRID_ELEMENT_SIZE), elementMaterial); | |
element.x = (gx - GRID_NUM_X * .5) * GRID_SPACING; | |
element.y = (gy - GRID_NUM_Y * .5) * GRID_SPACING; | |
element.z = (gz - GRID_NUM_Z * .5) * GRID_SPACING; | |
_view.scene.addChild(element); | |
} | |
} | |
} | |
} | |
private function _createAmbientLight(): DirectionalLight { | |
var light: DirectionalLight = new DirectionalLight(-1, 1, -1); | |
light.color = 0x9090aa; | |
light.ambient = 100; | |
light.diffuse = 2; | |
return light; | |
} | |
private function _createRoamerLight(): PointLight { | |
var light: PointLight = new PointLight(); | |
light.color = 0xff0000; | |
light.radius = 100; | |
light.fallOff = 700 | |
light.diffuse = 2; | |
light.ambient = 1000; | |
return light; | |
} | |
private function _addAwayStats(): void { | |
addChild(new AwayStats(_view)); | |
} | |
private function _onEnterFrame(event: Event): void { | |
_count1 += .01; | |
_count2 += .008; | |
_roamer.x = Math.sin(Math.cos(_count1)) * GRID_NUM_X * .5 * GRID_SPACING; | |
_roamer.y = Math.sin(_count2) * Math.cos(_count1) * GRID_NUM_Y * .5 * GRID_SPACING; | |
_roamer.z = Math.cos(_count2 * .97) * GRID_NUM_Z * .5 * GRID_SPACING; | |
_view.camera.lookAt(_roamer.position); | |
_view.render(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment