Thanks for the tip. I applied the fix, and it did seem to help somewhat, but the locations are still very off.
Here is a link to a sample application:
Drag3D Sample Application
And here is the source code:
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Vector3D;
import flash.text.TextField;
import flash.ui.Keyboard;
import away3d.cameras.lenses.OrthographicLens;
import away3d.cameras.lenses.PerspectiveLens;
import away3d.containers.View3D;
import away3d.entities.Mesh;
import away3d.materials.ColorMaterial;
import away3d.primitives.PlaneGeometry;
import away3d.primitives.WireframePlane;
import away3d.tools.utils.Drag3D;
[SWF(backgroundColor="#777777", frameRate="60", width="1024", height="768")]
public class Main extends Sprite
{
private const MESH_SIZE:uint = 16;
private const GRID_SIZE:uint = 32;
private var _view:View3D;
private var _dragMesh:Mesh;
private var _drag3D:Drag3D;
private var _lensPersp:PerspectiveLens = new PerspectiveLens();
private var _lensOrtho:OrthographicLens = new OrthographicLens();
private var _orthoView:Boolean = false;
private var _txtLensType:TextField = new TextField();
private var _txtLocation:TextField = new TextField();
public function Main()
{
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;
// Create our 3D view
stage.addChild(_view = new View3D());
// Create our Orthographic camera and position it nicely
_view.camera.lens = _lensPersp;
_view.camera.x = (MESH_SIZE * GRID_SIZE) + ((MESH_SIZE * GRID_SIZE) / 4);
_view.camera.y = (MESH_SIZE * GRID_SIZE);
_view.camera.z = (MESH_SIZE * GRID_SIZE) + ((MESH_SIZE * GRID_SIZE) / 4);
_view.camera.lookAt(new Vector3D( ((MESH_SIZE * GRID_SIZE) >> 1), 0, ((MESH_SIZE * GRID_SIZE) >> 1) ));
// Create a white wireframe grid on the XZ plane
var grid:WireframePlane;
grid = new WireframePlane(MESH_SIZE * GRID_SIZE, MESH_SIZE * GRID_SIZE, MESH_SIZE, MESH_SIZE, 0xFFFFFF, 1, "xz");
grid.x = (MESH_SIZE * GRID_SIZE) >> 1;
grid.z = (MESH_SIZE * GRID_SIZE) >> 1;
_view.scene.addChild(grid);
// Setup our mesh that we will drag around
var dragMaterial:ColorMaterial = new ColorMaterial(0x0000FF, 0.6);
var dragPlane:PlaneGeometry = new PlaneGeometry(GRID_SIZE, GRID_SIZE, 1, 1, true, false);
_dragMesh = new Mesh(dragPlane, dragMaterial);
_dragMesh.y = 1;
_dragMesh.visible = false;
_view.scene.addChild(_dragMesh);
// Initialize Drag3D to get our mouse position
_drag3D = new Drag3D(_view);
_drag3D.debug = true; // This shows what plane it's using
// Setup our display
_txtLensType.text = "Current Lens: Perspective";
_txtLensType.textColor = 0xFFFFFF;
_txtLensType.width = 400;
_txtLensType.y = _view.stage.stageHeight - 40;
_view.addChild(_txtLensType);
_txtLocation.text = "X: 0 Z: 0";
_txtLocation.textColor = 0xFFFFFF;
_txtLocation.width = 400;
_txtLocation.y = _view.stage.stageHeight - 20;
_view.addChild(_txtLocation);
// Setup our listeners
_view.stage.addEventListener(Event.RESIZE, onResize );
_view.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
_view.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp );
// Size our screen properly
onResize();
}
protected function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{
_orthoView = !_orthoView;
if (_orthoView)
{
_view.camera.lens = _lensOrtho;
_txtLensType.text = "Camera Lens: Orthographic";
}
else
{
_view.camera.lens = _lensPersp;
_txtLensType.text = "Camera Lens: Perspective";
}
}
}
protected function onResize(event:Event = null):void
{
_view.width = _view.stage.stageWidth;
_view.height = _view.stage.stageHeight;
}
protected function onEnterFrame(event:Event):void
{
var intersect:Vector3D = _drag3D.getIntersect();
var hoverX:uint = Math.floor(intersect.x / GRID_SIZE);
var hoverZ:uint = Math.floor(intersect.z / GRID_SIZE);
if (hoverX < 0 || hoverX >= MESH_SIZE || hoverZ < 0 || hoverZ >= MESH_SIZE )
{
_dragMesh.visible = false;
_txtLocation.text =
"GRID - X: -- Z: -- " +
"DRAG3D - X: " + intersect.x.toFixed() + " Z: " + intersect.z.toFixed();
}
else
{
_dragMesh.visible = true;
_dragMesh.x = (hoverX * GRID_SIZE) + (GRID_SIZE >> 1);
_dragMesh.z = (hoverZ * GRID_SIZE) + (GRID_SIZE >> 1);
_txtLocation.text =
"GRID - X: " + hoverX + " Z: " + hoverZ + " " +
"DRAG3D - X: " + intersect.x.toFixed() + " Z: " + intersect.z.toFixed();
}
_view.render();
}
}
}