Hi, I want to draw many planes or triangles with different colors.
but there are bugs:
(1) if not add [Light] object in scene then the mouse event are not working
(2) if the amount of objects are more then 1000 then the ColorMaterial will too slow for creation
Thanks for help.
below is my example code:
package
{
import away3d.cameras.Camera3D;
import away3d.cameras.lenses.PerspectiveLens;
import away3d.containers.Scene3D;
import away3d.containers.View3D;
import away3d.controllers.HoverController;
import away3d.debug.AwayStats;
import away3d.lights.DirectionalLight;
import away3d.materials.ColorMaterial;
import away3d.primitives.Plane;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Vector3D;
import flash.utils.getTimer;
[SWF(width="1600", height="900", backgroundColor="0xffffff")]
public class test extends Sprite
{
//engine variables
private var _view:View3D;
private var _camera:Camera3D;
private var _scene:Scene3D;
private var _cameraController:HoverController;
private var _light:DirectionalLight;
private var _direction:Vector3D;
//navigation variables
private var _move:Boolean = false;
private var _lastPanAngle:Number;
private var _lastTiltAngle:Number;
private var _lastMouseX:Number;
private var _lastMouseY:Number;
private const origin:Vector3D = new Vector3D(0,0,0);
public function test()
{
initEngine();
initObjects();
initListeners();
}
/**
* Initialise the engine
*/
private function initEngine():void
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
// setup light
_light = new DirectionalLight(-1, -1, 1);
_direction = new Vector3D(-1, -1, 1);
// setup scene
_scene = new Scene3D();
_scene.addChild(_light); // add light: Bug (if not add light layer, then the mouse effect is not working)
// setup camera
_camera = new Camera3D();
_camera.lens = new PerspectiveLens();
_camera.lens.far = 2100;
// setup view
_view = new View3D();
_view.camera = _camera;
_view.scene = _scene;
_view.backgroundColor = 0xffffff;
//setup controller to be used on the camera
_cameraController = new HoverController(_camera, null, 45, 10, 1000, 10);
addChild(_view); // add 3D view
addChild(new AwayStats(_view)); // add 3D stats
}
/**
* Initialise the listeners
*/
private function initListeners():void
{
addEventListener(Event.ENTER_FRAME, onEnterFrame);
_view.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
_view.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
_view.addEventListener(Event.RESIZE, onResize);
onResize();
}
/**
* Initialise the scene objects
*/
private function initObjects():void
{
var material:ColorMaterial;
// Shadow effect
// var material:ColorMaterial = new ColorMaterial(0x00FF00, 0.5);
// material.lights = [_light];
// material.specular = 0;
// material.shadowMethod = new FilteredShadowMapMethod(_light);
// material.smooth = true;
var plane:Plane;
var randomColor:uint;
var amount:int = 200; // amount of objects
for(var i:int=0; i< amount; i++)
{
// random color
randomColor = Math.random() * uint.MAX_VALUE;
material = new ColorMaterial(randomColor, 0.5);
// same color
// material = new ColorMaterial(0x00FF00, 0.5);
// plane object
plane = new Plane(material, 400, 300);
plane.y = (i*10);
// add into scene
_scene.addChild(plane);
}
}
/**
* Navigation and render loop
*/
private function onEnterFrame(event:Event):void
{
// light direction
_direction.x = -Math.sin(getTimer()/4000);
_direction.z = -Math.cos(getTimer()/4000);
_light.direction = _direction;
if (_move) {
_cameraController.panAngle = 0.3 * (stage.mouseX - _lastMouseX) + _lastPanAngle;
_cameraController.tiltAngle = 0.3 * (stage.mouseY - _lastMouseY) + _lastTiltAngle;
}else{
_camera.position = origin;
_camera.rotationY += 1;
_camera.moveBackward(1000);
}
_view.render();
}
/**
* Mouse down listener for navigation
*/
private function onMouseDown(event:MouseEvent):void
{
_lastPanAngle = _cameraController.panAngle;
_lastTiltAngle = _cameraController.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);
}
/**
* Mouse stage leave listener for navigation
*/
private function onStageMouseLeave(event:Event):void
{
_move = false;
stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
}
/**
* stage listener for resize events
*/
private function onResize(event:Event = null):void
{
_view.width = stage.stageWidth;
_view.height = stage.stageHeight;
}
}
}