Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StageProxy + 2 views mouse picking ( picking only works on the second added child view ) #483

Closed
LucaEstiva opened this issue Mar 10, 2013 · 3 comments
Assignees
Labels

Comments

@LucaEstiva
Copy link

package
{
import away3d.containers.ObjectContainer3D;
import away3d.containers.View3D;
import away3d.controllers.HoverController;
import away3d.core.managers.Stage3DManager;
import away3d.core.managers.Stage3DProxy;
import away3d.core.pick.*;
import away3d.debug.AwayStats;
import away3d.entities.Mesh;
import away3d.events.MouseEvent3D;
import away3d.materials.ColorMaterial;
import away3d.materials.TextureMaterial;
import away3d.primitives.CubeGeometry;
import away3d.primitives.SphereGeometry;
import away3d.primitives.WireframePlane;

import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.text.TextField;
import flash.text.TextFormat;

[SWF(width="800", height="600", frameRate="60")]

public class StageProxyPickingTest extends Sprite
{

    // Stage manager and proxy instances
    private var stage3DManager:Stage3DManager;
    private var stage3DProxy:Stage3DProxy;

    // Away3D view instances
    private var away3dView1:View3D;
    private var away3dView2:View3D;

    // Camera controllers 
    private var hoverController:HoverController;

    // Materials

    private var UnselectedMaterial:ColorMaterial;
    private var SelectedMaterial:ColorMaterial;

    // Objects
    private var cube1:Mesh;
    private var cube2:Mesh;
    private var cube3:Mesh;
    private var cube4:Mesh;
    private var cube5:Mesh;
    private var cubeContainer:ObjectContainer3D;

    private var sphere1:Mesh;
    private var sphere2:Mesh;
    private var sphere3:Mesh;
    private var sphere4:Mesh;
    private var sphere5:Mesh;
    private var sphereContainer:ObjectContainer3D;

    // Runtime variables
    private var lastPanAngle:Number = 0;
    private var lastTiltAngle:Number = 0;
    private var lastMouseX:Number = 0;
    private var lastMouseY:Number = 0;
    private var mouseDown:Boolean;

    private var View2Enabled:Boolean = true;
    private var AddViewChildOrder:int = 1;


    // Constants                
    private const CUBES_SPHERES:int = 0;
    private const SPHERES_CUBES:int = 1;


    public function StageProxyPickingTest()
    {
        init();
    }

    /**
     * Global initialise function
     */
    private function init():void
    {
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;

        initProxies();
        initAway3D();
        initMaterials();
        initObjects();
        initListeners();

    }

    /**
     * Initialise the Stage3D proxies
     */
    private function initProxies():void
    {
        // Define a new Stage3DManager for the Stage3D objects
        stage3DManager = Stage3DManager.getInstance( stage );

        // Create a new Stage3D proxy to contain the separate views
        stage3DProxy = stage3DManager.getFreeStage3DProxy();
        stage3DProxy.antiAlias = 8;
        stage3DProxy.color = 0x000000;
    }

    /**
     * Initialise the Away3D views
     */
    private function initAway3D() : void
    {
        // Create the first Away3D view which holds the cube objects.
        away3dView1 = new View3D();
        away3dView1.stage3DProxy = stage3DProxy;
        away3dView1.shareContext = true;

        hoverController = new HoverController( away3dView1.camera, null, 45, 30, 1200, 5, 89.999 );



        addChild( new AwayStats( away3dView1 ) );

        // Uses the GPU, considers gpu animations, and suffers from Stage3D's drawToBitmapData()'s bottleneck.
        // MainView1.mousePicker = PickingType.SHADER;
        // Uses the CPU, fast, but might be inaccurate with intersecting objects.
        away3dView1.mousePicker = PickingType.RAYCAST_FIRST_ENCOUNTERED;
        // Uses the CPU, guarantees accuracy with a little performance cost.
        // away3dView1.mousePicker = PickingType.RAYCAST_BEST_HIT;



        if( View2Enabled )
        {
            //Create the second Away3D view which holds the sphere objects
            away3dView2 = new View3D();
            away3dView2.stage3DProxy = stage3DProxy;
            away3dView2.shareContext = true;

            away3dView2.camera.z = -2000;




            // Uses the GPU, considers gpu animations, and suffers from Stage3D's drawToBitmapData()'s bottleneck.
            // MainView1.mousePicker = PickingType.SHADER;
            // Uses the CPU, fast, but might be inaccurate with intersecting objects.
            away3dView2.mousePicker = PickingType.RAYCAST_FIRST_ENCOUNTERED;
            // Uses the CPU, guarantees accuracy with a little performance cost.
            // away3dView2.mousePicker = PickingType.RAYCAST_BEST_HIT;
        }

        if( !AddViewChildOrder )
        {
            addChild( away3dView1 );
            addChild( away3dView2 );
        }
        else
        {
            addChild( away3dView2 );
            addChild( away3dView1 );
        }


    }

    /**
     * Initialise the materials
     */
    private function initMaterials():void
    {
        UnselectedMaterial = new ColorMaterial( 0xff0000 );
        SelectedMaterial = new ColorMaterial( 0x00ff00 );
    }

    /**
     * Initialise the lights
     */
    private function initObjects():void
    {

        // Build the cubes for view 1
        var cG:CubeGeometry = new CubeGeometry( 300, 300, 300 );
        cube1 = new Mesh( cG, UnselectedMaterial );
        cube1.mouseEnabled = true;
        cube1.addEventListener( MouseEvent3D.MOUSE_OVER, onObjectMouseOver );
        cube1.addEventListener( MouseEvent3D.MOUSE_OUT, onObjectMouseOut );

        cube2 = new Mesh( cG, UnselectedMaterial );
        cube2.mouseEnabled = true;
        cube2.addEventListener( MouseEvent3D.MOUSE_OVER, onObjectMouseOver );
        cube2.addEventListener( MouseEvent3D.MOUSE_OUT, onObjectMouseOut );

        cube3 = new Mesh( cG, UnselectedMaterial );
        cube3.mouseEnabled = true;
        cube3.addEventListener( MouseEvent3D.MOUSE_OVER, onObjectMouseOver );
        cube3.addEventListener( MouseEvent3D.MOUSE_OUT, onObjectMouseOut );

        cube4 = new Mesh( cG, UnselectedMaterial );
        cube4.mouseEnabled = true;
        cube4.addEventListener( MouseEvent3D.MOUSE_OVER, onObjectMouseOver );
        cube4.addEventListener( MouseEvent3D.MOUSE_OUT, onObjectMouseOut );

        cube5 = new Mesh( cG, UnselectedMaterial );
        cube5.mouseEnabled = true;
        cube5.addEventListener( MouseEvent3D.MOUSE_OVER, onObjectMouseOver );
        cube5.addEventListener( MouseEvent3D.MOUSE_OUT, onObjectMouseOut );

        // Arrange them in a circle with one on the center
        cube1.x = -750; 
        cube2.z = -750;
        cube3.x = 750;
        cube4.z = 750;
        cube1.y = cube2.y = cube3.y = cube4.y = cube5.y = 150;

        cubeContainer = new ObjectContainer3D();
        cubeContainer.addChild( cube1 );
        cubeContainer.addChild( cube2 );
        cubeContainer.addChild( cube3 );
        cubeContainer.addChild( cube4 );
        cubeContainer.addChild( cube5 );
        //
        away3dView1.scene.addChild( cubeContainer );

        //
        away3dView1.scene.addChild( new WireframePlane( 2500, 2500, 20, 20, 0x77aaaa, 1.5, WireframePlane.ORIENTATION_XZ ) );

        if( View2Enabled )
        {
            // Build the spheres for view 2
            var sG:SphereGeometry = new SphereGeometry( 200 );

            sphere1 = new Mesh( sG, UnselectedMaterial );
            sphere1.mouseEnabled = true;
            sphere1.addEventListener( MouseEvent3D.MOUSE_OVER, onObjectMouseOver );
            sphere1.addEventListener( MouseEvent3D.MOUSE_OUT, onObjectMouseOut );

            sphere2 = new Mesh( sG, UnselectedMaterial );
            sphere2.mouseEnabled = true;
            sphere2.addEventListener( MouseEvent3D.MOUSE_OVER, onObjectMouseOver );
            sphere2.addEventListener( MouseEvent3D.MOUSE_OUT, onObjectMouseOut );

            sphere3 = new Mesh( sG, UnselectedMaterial );
            sphere3.mouseEnabled = true;
            sphere3.addEventListener( MouseEvent3D.MOUSE_OVER, onObjectMouseOver );
            sphere3.addEventListener( MouseEvent3D.MOUSE_OUT, onObjectMouseOut );

            sphere4 = new Mesh( sG, UnselectedMaterial );
            sphere4.mouseEnabled = true;
            sphere4.addEventListener( MouseEvent3D.MOUSE_OVER, onObjectMouseOver );
            sphere4.addEventListener( MouseEvent3D.MOUSE_OUT, onObjectMouseOut );

            sphere5 = new Mesh( sG, UnselectedMaterial );
            sphere5.mouseEnabled = true;
            sphere5.addEventListener( MouseEvent3D.MOUSE_OVER, onObjectMouseOver );
            sphere5.addEventListener( MouseEvent3D.MOUSE_OUT, onObjectMouseOut );

            // Arrange them in a circle with one on the center
            sphere1.x = -1000; 
            sphere2.y = -1000;
            sphere3.x = 1000;
            sphere4.y = 1000;

            sphereContainer = new ObjectContainer3D();
            sphereContainer.addChild( sphere1 );
            sphereContainer.addChild( sphere2 );
            sphereContainer.addChild( sphere3 );
            sphereContainer.addChild( sphere4 );
            sphereContainer.addChild( sphere5 );
            //
            away3dView2.scene.addChild( sphereContainer );
        }


    }

    /**
     * Set up the rendering processing event listeners
     */
    private function initListeners():void
    {
        stage3DProxy.addEventListener( Event.ENTER_FRAME, onEnterFrame );
        addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
        addEventListener( MouseEvent.MOUSE_UP, onMouseUp );
    }

    /**
     * The main rendering loop
     */
    private function onEnterFrame( event:Event ):void
    {
        // Update the hovercontroller for view 1
        if( mouseDown )
        {
            hoverController.panAngle = 0.3 * ( stage.mouseX - lastMouseX ) + lastPanAngle;
            hoverController.tiltAngle = 0.3 * ( stage.mouseY - lastMouseY ) + lastTiltAngle;
        }


        // Render the Away3D layer 1
        away3dView1.render();

        if( View2Enabled )
        {
            // Rotate view 2
            sphereContainer.rotationZ += 0.5;
            // Render the Away3D layer 2
            away3dView2.render();
        }

    }

    /** Se il mouse è su un oggetto:
     * Se l'oggetto non è selezionato evidenzia l'oggetto
     */
    private function onObjectMouseOver( event:MouseEvent3D ):void
    {
        event.target.material = SelectedMaterial;
    }

    /** Se il mouse non è più su un oggetto:
     * Se l'oggetto non è selezionato non evidenzia l'oggetto
     */
    private function onObjectMouseOut( event:MouseEvent3D ):void
    {
        event.target.material = UnselectedMaterial;
    }

    /**
     * Handle the mouse down event and remember details for hovercontroller
     */
    private function onMouseDown( event:MouseEvent ):void
    {
        mouseDown = true;
        lastPanAngle = hoverController.panAngle;
        lastTiltAngle = hoverController.tiltAngle;
        lastMouseX = stage.mouseX;
        lastMouseY = stage.mouseY;
    }

    /**
     * Clear the mouse down flag to stop the hovercontroller
     */
    private function onMouseUp( event:MouseEvent ):void
    {
        mouseDown = false; 
    }
}

}

@ghost ghost assigned eternauta1337 Mar 23, 2013
@JohnBrookes
Copy link
Member

can be closed checked and works.

@ringodotnl
Copy link
Member

thank you!;)

@LucaEstiva
Copy link
Author

thx guys :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants