2012-04-12 1 views
2

그래서 나는 Alternativa3d에서 개체를 끌어 마우스를 사용하는 몇 가지 좋은 작업의 예를 참조하십시오Alternativa3d 8의 마우스 드래그?

http://wonderfl.net/c/hrsq/read

http://www.thetechlabs.com/3d/dragging-3d-objects-in-flex-3-using-alternativa3d-and-actionscript-3/

을하지만 그들은 엔진의 이전 버전이며, 그들은 코드를 포함하는 원한다면 간단히 앞으로의 번역이 필요하지 않게되었습니다. 도와주세요!

답변

1

게시 한 wonderfl 예제를 기반으로 alternativa3d 8.27.0을 사용하여 만든 버전입니다. 희망은 도움이 ...

package { 

import alternativa.engine3d.core.events.MouseEvent3D; 
import alternativa.engine3d.core.RayIntersectionData; 
import flash.display.Sprite; 
import flash.events.Event; 
import flash.display.Stage3D; 
import flash.display.StageAlign; 
import flash.display.StageScaleMode; 
import alternativa.engine3d.primitives.Box; 
import alternativa.engine3d.core.Resource; 
import alternativa.engine3d.controllers.SimpleObjectController; 
import alternativa.engine3d.core.Camera3D; 
import alternativa.engine3d.core.Object3D; 
import alternativa.engine3d.core.View; 
import alternativa.engine3d.materials.FillMaterial; 
import flash.events.MouseEvent; 
import flash.geom.Vector3D; 

/** 
* ... 
* @author David E Jones 
*/ 
public class Main extends Sprite 
{ 
    private var scene:Object3D = new Object3D(); 
    private var camera:Camera3D; 
    private var controller:SimpleObjectController; 
    private var stage3D:Stage3D; 
    private var box:Box; 
    private var boxSelected:Boolean = false; 
    private var startPoint:Vector3D; 

    public function Main():void 
    { 
     if (stage) init(); 
     else addEventListener(Event.ADDED_TO_STAGE, init); 
    } 

    private function init(e:Event = null):void 
    { 
     removeEventListener(Event.ADDED_TO_STAGE, init); 

     stage.align = StageAlign.TOP_LEFT; 
     stage.scaleMode = StageScaleMode.NO_SCALE; 

     camera = new Camera3D(1, 1000); 
     camera.view = new View(stage.stageWidth, stage.stageHeight, false, 0, 0, 4); 
     camera.view.backgroundColor = 0x000000; 
     addChild(camera.view); 
     addChild(camera.diagram); 

     camera.x = -30; 
     camera.y = -30; 
     camera.z = 0; 
     controller = new SimpleObjectController(stage, camera, 200); 
     controller.lookAt(new Vector3D(0,0,0)); 
     scene.addChild(camera); 

     stage3D = stage.stage3Ds[0]; 
     stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreate); 
     stage3D.requestContext3D(); 
    } 

    private function onContextCreate(e:Event):void { 
     stage3D.removeEventListener(Event.CONTEXT3D_CREATE, onContextCreate); 

     box = new Box(10, 10, 10, 1, 1, 1, false, null); 
     var material:FillMaterial = new FillMaterial(0xFF0000); 
     box.setMaterialToAllSurfaces(material); 
     scene.addChild(box); 
     uploadResources(box.getResources(true)); 

     box.addEventListener(MouseEvent3D.MOUSE_DOWN, onMouseDown); 

     stage.addEventListener(Event.ENTER_FRAME, onEnterFrame); 
     stage.addEventListener(Event.RESIZE, onResize); 
     onResize(); 
    } 

    private function onMouseDown(e:MouseEvent3D):void 
    { 
     stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); 
     stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); 

     var object:Object3D = e.target as Object3D; 
     var origin:Vector3D = new Vector3D(); 
     var direction:Vector3D = new Vector3D(); 
     camera.calculateRay(origin, direction, stage.mouseX, stage.mouseY); 
     var data:RayIntersectionData = object.intersectRay(origin, direction); 

     if (data != null) { 
      startPoint = data.point; 
     } 
    } 

    private function onMouseUp(e:MouseEvent):void 
    { 
     stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove); 
     stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp); 
    } 

    private function onMouseMove(e:MouseEvent):void 
    { 
     var origin:Vector3D = new Vector3D; 
     var directionA:Vector3D = new Vector3D; 
     var directionB:Vector3D = new Vector3D; 
     var direction:Vector3D = new Vector3D; 

     camera.calculateRay(origin, directionA, camera.view.width/2, camera.view.height/2); 
     camera.calculateRay(origin, directionB, mouseX, mouseY); 

     var pos : Vector3D = intersectionPoint(origin, directionB, new Vector3D(0, startPoint.y, 0), new Vector3D(0, 1, 0)); 
     box.x = pos.x-startPoint.x; 
     box.y = pos.y-startPoint.y; 
     box.z = pos.z-startPoint.z; 
    } 

    public static function intersectionPoint(lineStart:Vector3D, lineDirection:Vector3D, planePosition:Vector3D, planeNormal:Vector3D):Vector3D { 

     var result:Vector3D = new Vector3D(); 
     var w:Vector3D = lineStart.subtract(planePosition); 
     var d:Number = planeNormal.dotProduct(lineDirection); 
     var n:Number = -planeNormal.dotProduct(w); 

     if (Math.abs(d) < 0.0000001) return result;  

     var sI:Number = n/d; 

     result.x = lineStart.x + (lineDirection.x * sI); 
     result.y = lineStart.y + (lineDirection.y * sI); 
     result.z = lineStart.z + (lineDirection.z * sI); 

     return result;  
    } 

    private function uploadResources(resources:Vector.<Resource>):void { 
     for each (var resource:Resource in resources) { 
      resource.upload(stage3D.context3D); 
     } 
    } 

    private function onEnterFrame(e:Event):void { 
     if (boxSelected) { 

     } 
     camera.render(stage3D); 
    } 

    private function onResize(e:Event = null):void { 
     camera.view.width = stage.stageWidth; 
     camera.view.height = stage.stageHeight; 
    } 
} 

}

+0

브릴리언트! 또한 귀하의 솔루션을 귀하 사이트의 게시물 (다른 사람들을 위해 - http://davidejones.com/blog/1566-dragging-3d-object-alternativa3d-8/)로 만든 것을 보니 매우 기쁩니다. 또한 Alternativa가 풍부합니다. 거기에 정보! 훌륭한 일!! –

관련 문제