bugs in Light and ColorMaterial

Software: Away3D 4.x

Zerone, Newbie
Posted: 09 October 2011 10:10 PM   Total Posts: 22

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, -11);
   
_direction = new Vector3D(-1, -11);
   
   
// 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(_cameranull4510100010);
   
   
addChild(_view);     // add 3D view
   
addChild(new AwayStats(_view));  // add 3D stats
  
}
  
  
/**
   * Initialise the listeners
   */
  
private function initListeners():void
  {
   addEventListener
(Event.ENTER_FRAMEonEnterFrame);
   
_view.addEventListener(MouseEvent.MOUSE_DOWNonMouseDown);
   
_view.addEventListener(MouseEvent.MOUSE_UPonMouseUp);
   
_view.addEventListener(Event.RESIZEonResize);
   
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=0iamounti++)
   
{
    
// random color
    
randomColor Math.random() * uint.MAX_VALUE;
    
material = new ColorMaterial(randomColor0.5);
    
    
// same color
//    material = new ColorMaterial(0x00FF00, 0.5);
    
    // plane object
    
plane = new Plane(material400300);
    
plane.= (i*10);
    
    
// add into scene
    
_scene.addChild(plane);
   
}
  }
  
  
/**
   * Navigation and render loop
   */
  
private function onEnterFrame(event:Event):void
  {
   
// light direction
   
_direction.= -Math.sin(getTimer()/4000);
   
_direction.= -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_LEAVEonStageMouseLeave);
  
}
  
  
/**
   * Mouse up listener for navigation
   */
  
private function onMouseUp(event:MouseEvent):void
  {
   _move 
false;
   
stage.removeEventListener(Event.MOUSE_LEAVEonStageMouseLeave);
  
}
  
  
/**
   * Mouse stage leave listener for navigation
   */
  
private function onStageMouseLeave(event:Event):void
  {
   _move 
false;
   
stage.removeEventListener(Event.MOUSE_LEAVEonStageMouseLeave);
  
}
  
  
/**
   * stage listener for resize events
   */
  
private function onResize(event:Event null):void
  {
   _view
.width stage.stageWidth;
   
_view.height stage.stageHeight;
  
}
 }
   

Zerone, Newbie
Posted: 09 October 2011 10:15 PM   Total Posts: 22   [ # 1 ]

what I want is a [WireColorMaterial] not [ColorMaterial].
But looks like in current away3D 4.0 still not yet have [WireColorMaterial], hope will have it soon.

Thanks. I like Away3D. Good stuff.

   

Avatar
80prozent, Sr. Member
Posted: 09 October 2011 11:31 PM   Total Posts: 430   [ # 2 ]

hi

i believe the problem why over 1000 different colored triangles slowing down, is because the gpu can work faster with a few big objects than with a lot of small objects.
you would have to find a way to put all your triangles into one big submesh.
the only way to keep the different colors would be to use uv-mapping with a bitmapmaterial.

in an 3d-application you can create a plane with an proper uv-mapping (the plane textured by a quadratic texture).
than subdivide and triangulate it, so you have a lot of connected triangles, builing a plane (both as mesh and as uv).

using this uv-map as guide, you can create your texture (the different colored triangles) in photoshop.

than you can use a command like “split faces” or “break faces appart” to make shure each triangel uses its own 3 points.
than you could arange your different triangles, without changing the uv-mapping).


exporting as single object (*.3ds or *.obj) and import in away3d.

appling the map to the object.
add some light.
animate the mesh as whole object or by moving the different triangles.

hope that helps

 Signature 

sorry…i hope my actionscript is better than my english…

   

Zerone, Newbie
Posted: 10 October 2011 12:34 AM   Total Posts: 22   [ # 3 ]

if you change the above code into below:

/**
   * Initialise the scene objects
   */
  
private function initObjects():void
  {
   
//var material:ColorMaterial;
   
   // Shadow effect
   
var material:ColorMaterial = new ColorMaterial(0x00FF000.5);
//   material.lights = [_light];
//   material.specular = 0;
//   material.shadowMethod = new FilteredShadowMapMethod(_light);
//   material.smooth = true;
   
   
var plane:Plane;
   var 
randomColor:uint;
   var 
amount:int 2000;   // amount of objects
   
for(var i:int=0iamounti++)
   
{
    
// 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(material400300);
    
plane.= (i*10);
    
    
// add into scene
    
_scene.addChild(plane);
   
}
  } 

you will see event the amount of object is 2000. it can still created in 2-3 seconds

but if you made it like:

var material:ColorMaterial;
var 
amount:int 2000;
for(var 
i:int=0iamounti++)
{
  
//.....
  
material = new ColorMaterial(0x00FF00);
  
//.....

this it is too slow

   

Zerone, Newbie
Posted: 10 October 2011 01:05 AM   Total Posts: 22   [ # 4 ]

looks like a bug in [MaterialBase].

Just tested the code below in Away3d 3.6 and current Away3D 4.0

var bm:ColorMaterial;
var 
time:Number getTimer();
for(var 
i:int=0i<1000i++){
  bm 
= new ColorMaterial(0x000000);
}
trace
("Used Time: ", (getTimer()-time)); 

Away3D 3.6:  Used Time: 14
Away3D 4.0: Used Time: 6916

WOW!!

 

   

Zerone, Newbie
Posted: 25 October 2011 04:06 AM   Total Posts: 22   [ # 5 ]

Yes it is a bug in [MaterialBase] line 76
_materialLibrary.registerMaterial(this);

please see
Made Away3d 4.x create material 20 times faster

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X