PointLight Test
Reference -
http://www.allforthecode.co.uk/aftc/forum/user/modules/forum/article.php?index=5&subindex=5&aid=308
HSV code from
http://actionsnippet.com/?p=797
/**
* Copyright greentec ( http://wonderfl.net/user/greentec )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/i7tH
*/
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.entities.SegmentSet;
import away3d.lights.DirectionalLight;
import away3d.lights.PointLight;
import away3d.materials.ColorMaterial;
import away3d.materials.lightpickers.StaticLightPicker;
import away3d.primitives.CubeGeometry;
import away3d.primitives.LineSegment;
import away3d.primitives.SphereGeometry;
import com.bit101.components.PushButton;
import flash.geom.Vector3D;
import flash.display.*;
import flash.events.Event;
import flash.events.MouseEvent;
public class FlashTest extends Sprite {
private var view:View3D;
private var scene:Scene3D;
private var camera:Camera3D;
private var awayStats:AwayStats;
private var cameraController:HoverController;
private var lightPicker:StaticLightPicker;
public var lightNum:int = 5;
public var lightArray:Array = [];
public var lightMeshArray:Array = [];
public var lightSegmentArray:Array = [];
//navigation variables
private var move:Boolean = false;
private var lastPanAngle:Number;
private var lastTiltAngle:Number;
private var lastMouseX:Number;
private var lastMouseY:Number;
public var button:PushButton;
//private var source:BitmapData = new BitmapData(465, 465, false, 0x000000);
public function FlashTest() {
// write as3 code here..
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
Wonderfl.disable_capture();
//addChild(new Bitmap(source));
initEngine();
initLight();
initObject();
initListener();
button = new PushButton(this, 465 - 110, 10, "Reset", onReset);
}
private function onReset(e:Event):void
{
var i:int;
var light:PointLight;
var color:Array;
for (i = 0; i < lightNum; i += 1)
{
light = lightArray[i];
color = hsv(Math.random() * 360, 1, 1);
light.color = color[0] << 16 | color[1] << 8 | color[2];
light.x = (Math.random() * 50 + 120) * (Math.random() < 0.5 ? -1 : 1);
light.y = (Math.random() * 50 + 120) * (Math.random() < 0.5 ? -1 : 1);
light.z = (Math.random() * 50 + 120) * (Math.random() < 0.5 ? -1 : 1);
lightMeshArray[i].x = light.x;
lightMeshArray[i].y = light.y;
lightMeshArray[i].z = light.z;
lightMeshArray[i].material.color = light.color;
lightSegmentArray[i].start = new Vector3D(0, 0, 0);
lightSegmentArray[i].end = new Vector3D(light.x, light.y, light.z);
lightSegmentArray[i].startColor = light.color;
lightSegmentArray[i].endColor = light.color;
}
}
private function initListener():void
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.addEventListener(Event.RESIZE, resizeHandler);
addEventListener(Event.ENTER_FRAME, render);
addChild(view);
addChild(awayStats);
}
private function render(e:Event):void
{
if (move)
{
cameraController.panAngle = 0.3 * (stage.mouseX - lastMouseX) + lastPanAngle;
cameraController.tiltAngle = 0.3 * (stage.mouseY - lastMouseY) + lastTiltAngle;
}
view.render();
//view.renderer.queueSnapshot(source);
}
private function onMouseDown(e:MouseEvent):void
{
lastPanAngle = cameraController.panAngle;
lastTiltAngle = cameraController.tiltAngle;
lastMouseX = stage.mouseX;
lastMouseY = stage.mouseY;
move = true;
}
private function onMouseUp(e:MouseEvent):void
{
move = false;
}
private function resizeHandler(e:Event):void
{
view.width = stage.stageWidth;
view.height = stage.stageHeight;
}
private function initEngine():void
{
view = new View3D();
view.antiAlias = 4;
scene = view.scene;
camera = view.camera;
camera.lens.far = 5000;
cameraController = new HoverController(camera);
cameraController.distance = 500;
cameraController.panAngle = 45;
cameraController.tiltAngle = 20;
awayStats = new AwayStats(view);
}
private function initLight():void
{
var light:PointLight;
var i:int;
//var lightArray:Array = [dirLight, backLight];
var lightMesh:Mesh;
var lightSegment:SegmentSet;
var color:Array;
for (i = 0; i < lightNum; i += 1)
{
light = new PointLight();
color = hsv(Math.random() * 360, 1, 1);
light.color = color[0] << 16 | color[1] << 8 | color[2];
//light.ambient = 1;
light.specular = 1;
light.diffuse = 0.8;
light.x = (Math.random() * 50 + 120) * (Math.random() < 0.5 ? -1 : 1);
light.y = (Math.random() * 50 + 120) * (Math.random() < 0.5 ? -1 : 1);
light.z = (Math.random() * 50 + 120) * (Math.random() < 0.5 ? -1 : 1);
scene.addChild(light);
lightArray.push(light);
lightMesh = new Mesh(new SphereGeometry(10), new ColorMaterial(light.color));
lightMesh.x = light.x;
lightMesh.y = light.y;
lightMesh.z = light.z;
scene.addChild(lightMesh);
lightMeshArray.push(lightMesh);
lightSegment = new SegmentSet();
lightSegment.addSegment(new LineSegment(new Vector3D(0, 0, 0), new Vector3D(light.x, light.y, light.z), light.color, light.color));
scene.addChild(lightSegment);
lightSegmentArray.push(lightSegment.getSegment(0));
//lightPicker.lights.push(light);
}
lightPicker = new StaticLightPicker(lightArray);
}
private function initObject():void
{
var mesh:Mesh;
var colorMaterial:ColorMaterial;
colorMaterial = new ColorMaterial(0xcccccc);
colorMaterial.lightPicker = lightPicker;
//mesh = new Mesh(new CubeGeometry(100, 100, 100, 1, 1, 1, false), colorMaterial);
mesh = new Mesh(new SphereGeometry(120, 32, 24), colorMaterial);
scene.addChild(mesh);
}
private function hsv(h:Number, s:Number, v:Number):Array{
var r:Number, g:Number, b:Number;
var i:int;
var f:Number, p:Number, q:Number, t:Number;
if (s == 0){
r = g = b = v;
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
h /= 60;
i = Math.floor(h);
f = h - i;
p = v * (1 - s);
q = v * (1 - s * f);
t = v * (1 - s * (1 - f));
switch( i ) {
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
default: // case 5:
r = v;
g = p;
b = q;
break;
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
}
}