From 30d534b3320b14252a2db7c9fa6a0a8e12d23de8 Mon Sep 17 00:00:00 2001 From: yyc <395976266@qq.com> Date: Sat, 23 Feb 2019 17:46:36 +0800 Subject: [PATCH] feat(focus): fix currentSceneTreeNode and its all children has no geometry component if currentSceneTreeNode and its all children has no geometry component: use currentSceneTreeNode->position as target; use fixed value as distance; --- .../job/init/initHotKeysJob/FocusUtils.re | 36 ++++--- test/integration/job/initHotKeysJob_test.re | 97 ++++++++++++++----- 2 files changed, 96 insertions(+), 37 deletions(-) diff --git a/src/core/utils/engine/job/init/initHotKeysJob/FocusUtils.re b/src/core/utils/engine/job/init/initHotKeysJob/FocusUtils.re index c4e9b27fc..b45e20d20 100644 --- a/src/core/utils/engine/job/init/initHotKeysJob/FocusUtils.re +++ b/src/core/utils/engine/job/init/initHotKeysJob/FocusUtils.re @@ -35,18 +35,30 @@ let _buildAllPointsAndLocalToWolrdMatrices = (targetGameObject, engineState) => ); }); -let _calcGeometrySphereCenterAndRadius = (targetGameObject, engineState) => { - let aabb = - AABBShapeUtils.setFromAllPointsAndLocalToWolrdMatrices( - _buildAllPointsAndLocalToWolrdMatrices(targetGameObject, engineState), - ); +let _getFixedDistance = () => 3.; - let center = AABBShapeUtils.getCenter(aabb); +let _calcCenterAndDistance = (targetGameObject, engineState) => + switch ( + _buildAllPointsAndLocalToWolrdMatrices(targetGameObject, engineState) + ) { + | allPointsAndLocalToWolrdMatrices + when Js.Array.length(allPointsAndLocalToWolrdMatrices) === 0 => ( + TransformGameObjectEngineService.getPosition( + targetGameObject, + engineState, + ), + _getFixedDistance(), + ) + | allPointsAndLocalToWolrdMatrices => + let aabb = + AABBShapeUtils.setFromAllPointsAndLocalToWolrdMatrices( + _buildAllPointsAndLocalToWolrdMatrices(targetGameObject, engineState), + ); - (center, AABBShapeUtils.calcRadiusOfAABB(aabb, center)); -}; + let center = AABBShapeUtils.getCenter(aabb); -let _calcArcballCameraControllerDistance = distance => distance *. 2.5; + (center, AABBShapeUtils.calcRadiusOfAABB(aabb, center) *. 2.5); + }; let setEditorCameraFocusTargetGameObject = (editCamera, targetGameObject, editorState, engineState) => { @@ -85,12 +97,12 @@ let setEditorCameraFocusTargetGameObject = targetGameObject, ); - let (center, radius) = - engineState |> _calcGeometrySphereCenterAndRadius(targetGameObject); + let (center, distance) = + engineState |> _calcCenterAndDistance(targetGameObject); _setArcballCameraControllerFocusRelatedAttribute( editorCameraArcballControllerComponent, - (_calcArcballCameraControllerDistance(radius), center), + (distance, center), engineState, ); }; \ No newline at end of file diff --git a/test/integration/job/initHotKeysJob_test.re b/test/integration/job/initHotKeysJob_test.re index edd58ae72..51bd6b7c0 100755 --- a/test/integration/job/initHotKeysJob_test.re +++ b/test/integration/job/initHotKeysJob_test.re @@ -155,7 +155,29 @@ let _ = }) ); - describe("test bind focus hot-key", () => + describe("test bind focus hot-key", () => { + let _getDistance = ((editorState, engineState)) => + editorState + |> SceneViewEditorService.unsafeGetEditCamera + |. GameObjectComponentEngineService.unsafeGetArcballCameraControllerComponent( + engineState, + ) + |. ArcballCameraEngineService.unsafeGetArcballCameraControllerDistance( + engineState, + ) + |> FloatService.truncateFloatValue(_, 3); + + let _getTarget = ((editorState, engineState)) => + editorState + |> SceneViewEditorService.unsafeGetEditCamera + |. GameObjectComponentEngineService.unsafeGetArcballCameraControllerComponent( + engineState, + ) + |. ArcballCameraEngineService.unsafeGetArcballCameraControllerTarget( + engineState, + ) + |> Vector3Service.truncate(3); + describe( {| calc currentSceneTreeNode's all children and its self->aabb; @@ -163,28 +185,6 @@ let _ = use aabb's radius * factor as arcball camera controller distance; |}, () => { - let _getDistance = ((editorState, engineState)) => - editorState - |> SceneViewEditorService.unsafeGetEditCamera - |. GameObjectComponentEngineService.unsafeGetArcballCameraControllerComponent( - engineState, - ) - |. ArcballCameraEngineService.unsafeGetArcballCameraControllerDistance( - engineState, - ) - |> FloatService.truncateFloatValue(_, 3); - - let _getTarget = ((editorState, engineState)) => - editorState - |> SceneViewEditorService.unsafeGetEditCamera - |. GameObjectComponentEngineService.unsafeGetArcballCameraControllerComponent( - engineState, - ) - |. ArcballCameraEngineService.unsafeGetArcballCameraControllerTarget( - engineState, - ) - |> Vector3Service.truncate(3); - test("test the currentSceneTreeNode is scene gameObject", () => { MainEditorSceneTool.unsafeGetScene() |> GameObjectTool.setCurrentSceneTreeNode; @@ -257,7 +257,54 @@ let _ = |> expect == (4.146, (2., 0., 0.)); }); }, - ) - ); + ); + + describe("fix bug", () => + describe( + "if currentSceneTreeNode and its all children has no geometry component", + () => { + let _prepareAndExec = () => { + GameObjectTool.setCurrentSceneTreeNode( + MainEditorSceneTool.getCameraInDefaultScene + |> StateLogicService.getEngineStateToGetData, + ); + + let engineState = StateEngineService.unsafeGetState(); + + let camera = + MainEditorSceneTool.getCameraInDefaultScene(engineState); + + let pos = (2., 0., 0.); + let engineState = + engineState + |> TransformGameObjectEngineService.setLocalPosition( + camera, + pos, + ); + engineState |> StateEngineService.setState |> ignore; + + triggerFocusHotKeyEvent(); + + pos; + }; + + test("use currentSceneTreeNode->position as target", () => { + let pos = _prepareAndExec(); + + _getTarget + |> StateLogicService.getStateToGetData + |> expect == pos; + }); + + test("use fixed value as distance", () => { + let _ = _prepareAndExec(); + + _getDistance + |> StateLogicService.getStateToGetData + |> expect == 3.; + }); + }) + ); + }); }); }); \ No newline at end of file