Search code examples
actionscript-33dpapervision3d

mapping planes onto primitives


I've looped through the vertices and mapped a plane to each one. I'm having problems orientating the planes correctly. I can get it working with a sphere but when i make any alterations to the the primitive - positions are correct but they don't face/tilt the right way.

EDIT: Note - the alternation to the sphere was done before the sphere was created. I have updated the Sphere class to create an elongated sphere.

The code I'm using to place the planes are as follows:

pivotDO3D = new DisplayObject3D();
scene.addChild(pivotDO3D);
var bigSphere:Sphere = new Sphere(null, 500, 20, 20);

for each (var v:Vertex3D in bigSphere.geometry.vertices)
{
 var __seatmaterial:ColorMaterial = new ColorMaterial(0x000000);
 __seatmaterial.doubleSided = true;
 var p:Plane = new Plane(__seatmaterial, 20, 20, 2, 2);
 pivotDO3D.addChild(p);
 p.position = v.toNumber3D();
 p.lookAt(bigSphere);
}

Solution

  • The following demo shows how to minimize the problem. I changed the multiplication factor of 0.6 to 2.0 as well as the sphere size in order to exaggerate the effect so you can see it easily. Make sure to change 0.6 to 2.0 in your Sphere.as as well.

    The key is in varying the z location of the target point with the z location of the point on the sphere.

    To compare, run it as-is to see the "fixed" version, and change the lookAt target from pivotDO3D2 to bigSphere to see the old version.

    package
    {
        import flash.display.Sprite;
        import flash.events.Event;
    
        import org.papervision3d.cameras.*;
        import org.papervision3d.core.geom.renderables.*;
        import org.papervision3d.materials.*;
        import org.papervision3d.objects.*;
        import org.papervision3d.objects.primitives.*;
        import org.papervision3d.render.*;
        import org.papervision3d.scenes.*;
        import org.papervision3d.view.*;
    
        [SWF(width='400', height='400', backgroundColor='0x000000', frameRate='30')]
    
        public class PlaneOrientationDemo extends Sprite
        {
            private var scene:Scene3D;
            private var camera:Camera3D;
            private var renderer:BasicRenderEngine;
            private var viewport:Viewport3D;
            private var pivotDO3D:DisplayObject3D;
    
            public function PlaneOrientationDemo()
            {
                viewport = new Viewport3D(0, 0, true, true);
                addChild( viewport );
    
                renderer = new BasicRenderEngine();                         
                scene = new Scene3D( );
    
                camera = new Camera3D();
                camera.z = -700;
                camera.zoom = 50;
    
                pivotDO3D = new DisplayObject3D();
                scene.addChild(pivotDO3D);
    
                var pivotDO3D2:DisplayObject3D = new DisplayObject3D();
    
                var bigSphere:Sphere = new Sphere(null, 150, 20, 20);
    
                for each (var v:Vertex3D in bigSphere.geometry.vertices)
                {
                     var __seatmaterial:ColorMaterial = new ColorMaterial(0x00FF00);
                     __seatmaterial.doubleSided = true;
                     var p:Plane = new Plane(__seatmaterial, 20, 20, 2, 2);
                     pivotDO3D.addChild(p);
                     p.position = v.toNumber3D();
    
                     // This number should match the fx multiplication factor in Sphere.as.
                     var xFactor:Number = 2.0;
    
                     pivotDO3D2.z = v.z / (Math.PI / xFactor);
                     p.lookAt(pivotDO3D2);
                }
    
                stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
            }
    
            private function onEnterFrame(event: Event): void
            {
                pivotDO3D.rotationX += 1;
                pivotDO3D.rotationY += 1;
                renderer.renderScene(scene, camera, viewport);
            }
        }
    }