所以我看到了几个使用鼠标在 Alternativa3d 中拖动对象的好例子:
http://wonderfl.net/c/hrsq/read
http://www.thetechlabs.com/3d/dragging-3d-objects-in-flex-3-using-alternativa3d-and-actionscript-3/
但它们适用于引擎的早期版本,并且它们包含现在已弃用的代码,如果您愿意的话,没有直接的正向翻译。请帮忙!
所以我看到了几个使用鼠标在 Alternativa3d 中拖动对象的好例子:
http://wonderfl.net/c/hrsq/read
http://www.thetechlabs.com/3d/dragging-3d-objects-in-flex-3-using-alternativa3d-and-actionscript-3/
但它们适用于引擎的早期版本,并且它们包含现在已弃用的代码,如果您愿意的话,没有直接的正向翻译。请帮忙!
这是我根据您发布的 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;
}
}
}