Skip to content

Commit

Permalink
feat(focus): fix currentSceneTreeNode and its all children has no geo…
Browse files Browse the repository at this point in the history
…metry component

if currentSceneTreeNode and its all children has no geometry component:
use currentSceneTreeNode->position as target;
use fixed value as distance;
  • Loading branch information
yyc-git authored and AmyOrz committed Feb 23, 2019
1 parent c476be3 commit 30d534b
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 37 deletions.
36 changes: 24 additions & 12 deletions src/core/utils/engine/job/init/initHotKeysJob/FocusUtils.re
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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,
);
};
97 changes: 72 additions & 25 deletions test/integration/job/initHotKeysJob_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -155,36 +155,36 @@ 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;
use aabb's center as arcball camera controller target;
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;
Expand Down Expand Up @@ -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.;
});
})
);
});
});
});

0 comments on commit 30d534b

Please sign in to comment.