Hi all. I created a simple test app with away3d. It work nice in the web and mobile actionscript projects. But when I use the same code in flex mobile project, the coordinates of MouseEvent3D are incorrect. Bug was reproduced only on Android devices (iOS did not try yet), on AIR simulator everything is fine.
Here is video with my app.
I searched a lot but did not find the solution of this problem.
private function init(e:Event = null):void {
if (e) removeEventListener(Event.ADDED_TO_STAGE, init);
_isRotationLocked = true;
_lastMouseX = 0;
_lastMouseY = 0;
_lastTiltAngle = 0;
_lastPanAngle = 0;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
_camera = new Camera3D();
_scene = new Scene3D();
_view = new View3D();
_view.backgroundColor = 0x000000;
_view.buttonMode = true;
_view.scene = _scene;
_view.camera = _camera;
_view.forceMouseMove = true;
var light:DirectionalLight = new DirectionalLight(-1, -1, 1);
light.color = 0xFFFFFF;
light.ambient = 0;
light.diffuse = 1;
light.specular = 1;
_scene.addChild(light);
_lightPicker = new StaticLightPicker([light]);
_cameraController = new HoverController(_camera);
_cameraController.distance = START_CAMERA_DISTANCE;
_cameraController.minTiltAngle = 0;
_cameraController.maxTiltAngle = 90;
onStageResize();
stage.addEventListener(Event.RESIZE, onStageResize);
stage.addEventListener(TransformGestureEvent.GESTURE_ZOOM, onStageZoom);
stage.addEventListener(TransformGestureEvent.GESTURE_PAN, onStagePan);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
addChild(_view);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
Parsers.enableAllBundled();
AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete);
_loader = new Loader3D();
_loader.addEventListener(LoaderEvent.LOAD_ERROR, onLoadError);
_loader.addEventListener(LoaderEvent.RESOURCE_COMPLETE, onLoadComplete);
_view.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
_view.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
_view.renderer.antiAlias = 4;
_view.mousePicker = PickingType.RAYCAST_BEST_HIT;
_loader.loadData(BatmanAWD);
pickingPositionTracer = new Mesh( new SphereGeometry( 2 ), new ColorMaterial( 0x00FF00, 0.5 ) );
pickingPositionTracer.visible = false;
pickingPositionTracer.mouseEnabled = false;
pickingPositionTracer.mouseChildren = false;
_scene.addChild(pickingPositionTracer);
}
private function onMouseMove(e:MouseEvent):void {
if (Math.abs(_lastMouseX - stage.mouseX) > 50 || Math.abs(_lastMouseY - stage.mouseY) > 50) lockPicker(true, 100);
}
private function onMouseDown(e:MouseEvent):void {
_lastMouseX = stage.mouseX;
_lastMouseY = stage.mouseY;
_lastPanAngle = _cameraController.panAngle;
_lastTiltAngle = _cameraController.tiltAngle;
_isRotationLocked = false;
stage.addEventListener(Event.MOUSE_LEAVE, onMouseLeave);
}
private function onMouseUp(e:MouseEvent):void {
_isRotationLocked = true;
}
private function onMouseLeave(e:Event):void {
_isRotationLocked = true;
stage.removeEventListener(Event.MOUSE_LEAVE, onMouseLeave);
}
private function onStageResize(e:Event = null):void {
_view.width = stage.fullScreenWidth;
_view.height = stage.fullScreenHeight;
}
private function onLoadComplete(e:LoaderEvent):void {
_loader.removeEventListener(LoaderEvent.LOAD_ERROR, onLoadError);
_loader.removeEventListener(LoaderEvent.RESOURCE_COMPLETE, onLoadComplete);
_view.scene.addChild(_loader);
_loader.y = START_OFFSET_Y;
_loader.x = START_OFFSET_X;
_batmanBodyMesh.pickingCollider = PickingColliderType.AS3_FIRST_ENCOUNTERED;
_batmanHeadMesh.pickingCollider = PickingColliderType.AS3_FIRST_ENCOUNTERED;
_batmanBodyMesh.mouseEnabled = true;
_batmanBodyMesh.mouseChildren = true;
_batmanBodyMesh.shaderPickingDetails = true;
_batmanBodyMesh.addEventListener(MouseEvent3D.CLICK, onBatmanClick);
_batmanHeadMesh.mouseEnabled = true;
_batmanHeadMesh.mouseChildren = true;
_batmanHeadMesh.shaderPickingDetails = true;
_batmanHeadMesh.addEventListener(MouseEvent3D.CLICK, onBatmanClick);
_cameraController.tiltAngle = 0;
_cameraController.panAngle = 180;
}
private function onEnterFrame(e:Event):void {
if (!_isRotationLocked && !_isCameraLocked) {
_cameraController.panAngle = .3 * (stage.mouseX - _lastMouseX) + _lastPanAngle;
_cameraController.tiltAngle= .3 * (stage.mouseY - _lastMouseY) + _lastTiltAngle;
}
_lightPicker.lights[0].rotationX = _camera.rotationX;
_lightPicker.lights[0].rotationY = _camera.rotationY;
_lightPicker.lights[0].rotationZ = _camera.rotationZ;
if (_isCameraLocked) {
trace("rotation locked");
}
_view.render()
}
private function onAssetComplete(e:AssetEvent):void {
if (e.asset.assetType == AssetType.MESH) {
switch (e.asset.name) {
case "objBatman_skin":
_batmanBodyMesh = e.asset as Mesh;
_batmanBodyMesh.material.lightPicker = _lightPicker;
break;
case "objBatman_Head_Skin":
_batmanHeadMesh = e.asset as Mesh;
_batmanHeadMesh.material.lightPicker = _lightPicker;
break;
}
}
}
private function onBatmanClick(e:MouseEvent3D):void {
pickingPositionTracer.visible = true;
pickingPositionTracer.position = e.scenePosition;
if (!_isPickerLocked) {
if (e.currentTarget.material.ambientTexture) e.currentTarget.material.ambientTexture = null;
else e.currentTarget.material.ambientTexture = e.currentTarget.material.normalMap;
}
}