Skip to content

Commit

Permalink
feat(focus): now calc currentSceneTreeNode's all children and its sel…
Browse files Browse the repository at this point in the history
…f->aabb
  • Loading branch information
yyc-git committed Feb 23, 2019
1 parent df71793 commit 5e96641
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 259 deletions.
3 changes: 0 additions & 3 deletions src/core/composable_component/utils/FocusDataUtils.re

This file was deleted.

3 changes: 2 additions & 1 deletion src/core/job/init/InitHotKeysJob.re
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ let _getHandleFuncByHotKeyAction = hotKeyAction => {
switch (editorState |> SceneTreeEditorService.getCurrentSceneTreeNode) {
| None => ()
| Some(currentSceneTreeNode) =>
ArcballCameraControllerLogicService.setEditorCameraFocusTargetGameObject(
FocusUtils.setEditorCameraFocusTargetGameObject(
editorState |> SceneViewEditorService.unsafeGetEditCamera,
currentSceneTreeNode,
editorState,
StateEngineService.unsafeGetState(),
Expand Down
116 changes: 116 additions & 0 deletions src/core/utils/engine/job/init/initHotKeysJob/FocusUtils.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
let _setArcballCameraControllerFocusRelatedAttribute =
(arcballCameraController, (distance, target), engineState) => {
Js.log(distance);

engineState
|> ArcballCameraEngineService.setArcballCameraControllerTarget(
arcballCameraController,
target,
)
|> ArcballCameraEngineService.setArcballCameraControllerDistance(
distance,
arcballCameraController,
);
};

let _getTargetGameObjectMaxScale = (targetGameObjectTransform, engineState) => {
let (scaleX, scaleY, scaleZ) =
engineState |> TransformEngineService.getScale(targetGameObjectTransform);

Js.Math.max_float(scaleX, scaleY) |> Js.Math.max_float(scaleZ);
};

let _buildAllPointsAndLocalToWolrdMatrices = (targetGameObject, engineState) =>
engineState
|> HierarchyGameObjectEngineService.getAllGameObjects(targetGameObject)
|> Js.Array.filter(gameObject =>
GameObjectComponentEngineService.hasGeometryComponent(
gameObject,
engineState,
)
)
|> Js.Array.map(gameObject => {
let geometry =
engineState
|> GameObjectComponentEngineService.unsafeGetGeometryComponent(
gameObject,
);

(
engineState |> GeometryEngineService.getGeometryVertices(geometry),
engineState
|> TransformGameObjectEngineService.getLocalToWorldMatrixTypeArray(
gameObject,
),
);
});

let _calcGeometrySphereCenterAndRadius = (targetGameObject, engineState) => {
let aabb =
AABBShapeUtils.setFromAllPointsAndLocalToWolrdMatrices(
_buildAllPointsAndLocalToWolrdMatrices(targetGameObject, engineState),
);
let center = AABBShapeUtils.getCenter(aabb);

(center, AABBShapeUtils.calcRadiusOfAABB(aabb, center));
};

let _calcArcballCameraControllerDistance =
(distance, targetGameObjectTransform, engineState) =>
_getTargetGameObjectMaxScale(targetGameObjectTransform, engineState)
*. distance
*. 2.5;

let setEditorCameraFocusTargetGameObject =
(editCamera, targetGameObject, editorState, engineState) => {
WonderLog.Contract.requireCheck(
() =>
WonderLog.(
Contract.(
Operators.(
test(
Log.buildAssertMessage(
~expect=
{j|the editor camera should has arcballCameraController component|j},
~actual={j|not|j},
),
() =>
GameObjectComponentEngineService.hasArcballCameraControllerComponent(
editCamera,
engineState,
)
|> assertTrue
)
)
)
),
StateEditorService.getStateIsDebug(),
);

let editorCameraArcballControllerComponent =
GameObjectComponentEngineService.unsafeGetArcballCameraControllerComponent(
editCamera,
engineState,
);
let targetGameObjectTransform =
engineState
|> GameObjectComponentEngineService.unsafeGetTransformComponent(
targetGameObject,
);

let (center, radius) =
engineState |> _calcGeometrySphereCenterAndRadius(targetGameObject);

_setArcballCameraControllerFocusRelatedAttribute(
editorCameraArcballControllerComponent,
(
engineState
|> _calcArcballCameraControllerDistance(
radius,
targetGameObjectTransform,
),
center,
),
engineState,
);
};
149 changes: 1 addition & 148 deletions src/service/stateTuple/logic/ArcballCameraControllerLogicService.re
Original file line number Diff line number Diff line change
Expand Up @@ -187,151 +187,4 @@ let unbindGameViewActiveCameraArcballCameraControllerEvent = engineState =>
engineState =>
_checkSceneAllArcballCameraControllersNotBindEvent(engineState),
StateEditorService.getStateIsDebug(),
);

let _setArcballCameraControllerFocusRelatedAttribute =
(arcballCameraController, (distance, target), engineState) => {
Js.log(distance);

engineState
|> ArcballCameraEngineService.setArcballCameraControllerTarget(
arcballCameraController,
target,
)
|> ArcballCameraEngineService.setArcballCameraControllerDistance(
distance,
arcballCameraController,
);
};

let _getTargetGameObjectMaxScale = (targetGameObjectTransform, engineState) => {
let (scaleX, scaleY, scaleZ) =
engineState |> TransformEngineService.getScale(targetGameObjectTransform);

Js.Math.max_float(scaleX, scaleY) |> Js.Math.max_float(scaleZ);
};

let _calcGeometrySphereCenterAndRadius =
(targetGameObject, targetGameObjectTransform, engineState) => {
let pointsAndLocalToWolrdMatricesArray =
engineState
|> HierarchyGameObjectEngineService.getAllGameObjects(targetGameObject)
|> Js.Array.map(gameObject =>
switch (
engineState
|> GameObjectComponentEngineService.getGeometryComponent(
gameObject,
)
) {
| None => (None, None)
| Some(geometry) => (
engineState
|> GeometryEngineService.getGeometryVertices(geometry)
|. Some,
engineState
|> TransformEngineService.getLocalToWorldMatrixTypeArray(
targetGameObjectTransform,
)
|. Some,
)
}
)
|> Js.Array.filter(((vertices, localToWolrdMatrices)) =>
vertices |> Js.Option.isSome
)
|> Js.Array.map(((vertices, localToWolrdMatrices)) =>
(
vertices |> OptionService.unsafeGet,
localToWolrdMatrices |> OptionService.unsafeGet,
)
);

let {min, max} =
AABBShapeUtils.setFromAllPointsAndLocalToWolrdMatrices(
pointsAndLocalToWolrdMatricesArray,
);

/* TODO the min and max not change with position */
WonderLog.Log.print((min, max)) |> ignore;

let center = AABBShapeUtils.getCenter({min, max});

(center, AABBShapeUtils.calcRadiusOfAABB({min, max}, center));
};

let _calcArcballCameraControllerDistance =
(distance, targetGameObject, targetGameObjectTransform, engineState) =>
_getTargetGameObjectMaxScale(targetGameObjectTransform, engineState)
*. distance
*. 2.
+. FocusDataUtils.getSmallGameObjectFocusDeviation();
let setEditorCameraFocusTargetGameObject =
(targetGameObject, editorState, engineState) => {
WonderLog.Contract.requireCheck(
() =>
WonderLog.(
Contract.(
Operators.(
test(
Log.buildAssertMessage(
~expect=
{j|the editor camera should has arcballCameraController component|j},
~actual={j|not|j},
),
() =>
editorState
|> SceneViewEditorService.unsafeGetEditCamera
|. GameObjectComponentEngineService.hasArcballCameraControllerComponent(
engineState,
)
|> assertTrue
)
)
)
),
StateEditorService.getStateIsDebug(),
);

let editorCameraArcballControllerComponent =
editorState
|> SceneViewEditorService.unsafeGetEditCamera
|. GameObjectComponentEngineService.unsafeGetArcballCameraControllerComponent(
engineState,
);
let targetGameObjectTransform =
engineState
|> GameObjectComponentEngineService.unsafeGetTransformComponent(
targetGameObject,
);

let (center, radius) =
engineState
|> _calcGeometrySphereCenterAndRadius(
targetGameObject,
targetGameObjectTransform,
);

let arcballCameraControllerDistance =
engineState
|> _calcArcballCameraControllerDistance(
radius,
targetGameObject,
targetGameObjectTransform,
);

engineState |> SceneEngineService.isSceneGameObject(targetGameObject) ?
_setArcballCameraControllerFocusRelatedAttribute(
editorCameraArcballControllerComponent,
(
arcballCameraControllerDistance
+. FocusDataUtils.getSceneGameObjectArcballCameraDistance(),
center,
),
engineState,
) :
_setArcballCameraControllerFocusRelatedAttribute(
editorCameraArcballControllerComponent,
(arcballCameraControllerDistance, center),
engineState,
);
};
);
Loading

0 comments on commit 5e96641

Please sign in to comment.