Merging cubes causes their shading to become weird/dark…

Software: Away3D 4.x

Mr Margaret Scratcher, Sr. Member
Posted: 09 October 2013 09:19 PM   Total Posts: 344

I’ve been playing around with merging some cubes, but getting a weird result, the cubes come out really dark, or weirdly shaded, as if all faces are pointing in the same direction

See the attachments for how they look before and after merging, or have a go for yourself here:


******************************
Merge test
******************************

Here’s my code:

package 
{
 import away3d
.containers.ObjectContainer3D;
 
import away3d.controllers.HoverController;
 
import away3d.entities.Mesh;
 
import away3d.lights.PointLight;
 
import away3d.materials.lightpickers.StaticLightPicker;
 
import flash.display.MovieClip;
 
import flash.display.Shape;
 
import flash.display.Sprite;
 
import flash.events.Event;
 
import flash.events.MouseEvent;
 
import flash.geom.Point;
 
import flash.text.TextField;
 
 
import away3d.containers.View3D;
 
import away3d.cameras.Camera3D;
 
import away3d.debug.AwayStats;
 
import away3d.primitives.CubeGeometry;
 
import away3d.materials.ColorMaterial;
 
 
import flash.display.StageScaleMode;
 
import flash.display.StageAlign;
 
import flash.display.StageQuality;
 
 
import away3d.filters.MotionBlurFilter3D;
 
import away3d.filters.BloomFilter3D;
 
 
import away3d.tools.commands.Merge;
 
/**
  * ...
  * @author Mark
  */
 
public class Main extends Sprite 
 {
  
var view:View3D;
  var 
camera:Camera3D;
  var 
_light:PointLight;
  var 
lightPicker:StaticLightPicker;
  
  var 
testCube:Mesh;
  
  var 
cubeArray:Array = new Array();
  var 
cubesContainer:ObjectContainer3D = new ObjectContainer3D();
  var 
mBlurFilter:MotionBlurFilter3D;
  var 
bloomFilt:BloomFilter3D;
  
  var 
camController:HoverDragController;
  
  public function 
Main():void 
  {
   
if (stageinit();
   else 
addEventListener(Event.ADDED_TO_STAGEinit);
   
init3D();
   
makeMergeButton();
   
  
}
  
  
public function init3D():void
  {
   trace 
("init 3D");
   
view = new View3D();
   
   
   
   
mBlurFilter = new MotionBlurFilter3D(.3);
   
bloomFilt = new BloomFilter3D (1515.903);
  
   
   
   
this.addChild(view);
   
   
view.antiAlias 10;
   
//view.filters3d = [mBlurFilter, bloomFilt];
   
view.backgroundColor 0xffffff;
   
   
   
   
camera view.camera;
   
camController = new HoverDragController(camerastage);
    
   
addChild(new AwayStats(view));
   
_light = new PointLight();
   
_light.= -600;
   
_light.= -400;
   
_light.300;
   
_light.color 0xfffffff;
   
view.scene.addChild(_light);
   
lightPicker = new StaticLightPicker([_light]);
   
   
stage.scaleMode StageScaleMode.NO_SCALE;
   
stage.align StageAlign.TOP_LEFT;
   
stage.addEventListener(Event.ENTER_FRAMEonEnterFrame);
   
view.scene.addChild(cubesContainer);
  
   
makeCubes();
  
}
  
  
private function makeMergeButton():void
  {
   trace 
("Making MergeButton");
   var 
mergeButton:MovieClip = new MovieClip();
   var 
mergeButtonShape:Shape = new Shape();
   
mergeButtonShape.graphics.lineStyle(2);
   
mergeButtonShape.graphics.beginFill(0xaaffaa);
   
mergeButtonShape.graphics.drawRoundRect(00200405);
   
mergeButtonShape.graphics.endFill;
   
mergeButton.addChild(mergeButtonShape);
   var 
mergeButtonText:TextField = new TextField ();
   
mergeButtonText.text "Merge Cubes";
   
mergeButtonText.mouseEnabled false;
   
mergeButtonText.= (mergeButton.width 2) - (mergeButtonText.width 2);
   
mergeButton.addChild(mergeButtonText);
   
   
mergeButton.addEventListener(MouseEvent.CLICKmergeCubes);
   
mergeButton.buttonMode true;
   
mergeButton.stage.stageWidth mergeButton.width 20;
   
mergeButton.20;
   
addChild(mergeButton);
   
  
}
  
  
  
private function mergeCubes(e:Event):void
  {
   
   trace 
("merging Cubes");
   for (var 
i:uint 0cubesContainer.numChildreni++)
   
{
    trace 
("Found cube number " i);
    
   
}
   
   
    
var merged:Merge = new Merge
    
    
var merger:Merge = new Merge(truefalse);
    var 
mergedCubes:Mesh = new Mesh(null);
    
merger.applyToContainer(mergedCubescubesContainer);
    
cubesContainer.visible false;
    
view.scene.addChild(mergedCubes)
   
  
}

  
private function test3D():void
  {
   
var testMat:ColorMaterial = new ColorMaterial ();
   
testMat.lightPicker lightPicker;
   var 
cubeGeom:CubeGeometry = new CubeGeometry (1000100100);
   
testCube = new Mesh (cubeGeomtestMat);
   
view.scene.addChild(testCube);
  
}
  
  
private function makeCubes():void
  {
   
for (var i:uint10i++)
   
{
   
var testMat:ColorMaterial = new ColorMaterial (Math.random()*0xFFFFFF);
   
testMat.lightPicker lightPicker;
   var 
cubeGeom:CubeGeometry = new CubeGeometry (100100100);
   var 
Cube = new Mesh (cubeGeomtestMat);
   
Cube.= (Math.random() * 1000) - 500;
   
Cube.= (Math.random() * 1000)-500;
   
Cube.= (Math.random() * 1000)-500;
   
cubesContainer.addChild(Cube);
   
cubeArray.push (Cube);
   
}
   
   
  }
  
 
  
private function onEnterFrame(:Event):void
  {
   view
.render();
   
   
  
}
  
 }
 

 

 

 

   

Mr Margaret Scratcher, Sr. Member
Posted: 09 October 2013 09:32 PM   Total Posts: 344   [ # 1 ]

OKay, so if I disable the lightpicker either by not applying it in the first place, or cycling through like this:

for (var i:uint 0cubesContainer.numChildreni++)
   
{
    trace 
("Found cube number " i);
    var 
mesh Mesh(cubesContainer.getChildAt(i));
    
mesh.material.lightPicker null;
    
    
   

Then I get a load of flat-shaded shapes as expected, but after that I can’t apply a lightpicker to the material, or even make a new material and apply that to the mergedCubes mesh…

Confused…

 

   

John Brookes, Moderator
Posted: 09 October 2013 09:51 PM   Total Posts: 732   [ # 2 ]

Its because of this line
https://github.com/away3d/away3d-core-fp11/blob/dev/src/away3d/tools/commands/Merge.as#L274
Its adding position data to the normals.
So
var m:Matrix3D = mesh.sceneTransform.clone();
m.appendTranslation( -mesh.scenePosition.x, -mesh.scenePosition.y, -mesh.scenePosition.z);
m.transformVectors(normals, normals);
or something.

   

Mr Margaret Scratcher, Sr. Member
Posted: 09 October 2013 10:06 PM   Total Posts: 344   [ # 3 ]
JohnBrookes - 09 October 2013 09:51 PM

Its because of this line
https://github.com/away3d/away3d-core-fp11/blob/dev/src/away3d/tools/commands/Merge.as#L274
Its adding position data to the normals.
So
var m:Matrix3D = mesh.sceneTransform.clone();
m.appendTranslation( -mesh.scenePosition.x, -mesh.scenePosition.y, -mesh.scenePosition.z);
m.transformVectors(normals, normals);
or something.


Not sure I follow you, you’re going to have to fill in some blanks in between the above bit of code and the solution, ha! smile

   

John Brookes, Moderator
Posted: 09 October 2013 10:25 PM   Total Posts: 732   [ # 4 ]

Basically replace the line thats highlighted in that url (Merge.as) with what I posted.

Think thats it.

   

Mr Margaret Scratcher, Sr. Member
Posted: 10 October 2013 06:17 PM   Total Posts: 344   [ # 5 ]
JohnBrookes - 09 October 2013 10:25 PM

Basically replace the line thats highlighted in that url (Merge.as) with what I posted.

Think thats it.

Yep, works for me! Is that a fix for what I’m doing in my case, or a fix for a general bug? Proud to have pointed it out/got it fixed if so :D

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X