Skip to content

Commit

Permalink
feat(focus): calc focus distance with geometry
Browse files Browse the repository at this point in the history
  • Loading branch information
AmyOrz committed Feb 22, 2019
1 parent 448bde7 commit 4811419
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/core/composable_component/utils/FocusDataUtils.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
let getSceneGameObjectArcballCameraDistance = () => 20.0;
let getSceneGameObjectArcballCameraDistance = () => 30.;

let getSceneChildrenArcballCameraDistance = () => 6.0;
let getSceneChildrenArcballCameraDistance = () => 10.;
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ let _forEachVertices = (vertices, verticesCount, func) => {
maxRadiusSqRef^;
};

let _findMaxDistanceOfPointsToCenter = (center, vertices) =>
let findMaxDistanceOfPointsToCenter = (center, vertices) =>
_forEachVertices(
vertices,
Js.Typed_array.Float32Array.length(vertices),
Expand All @@ -42,7 +42,7 @@ let setFromPoints = vertices => {
{
center,
radius:
_findMaxDistanceOfPointsToCenter(center, vertices) |> Js.Math.sqrt,
findMaxDistanceOfPointsToCenter(center, vertices) |> Js.Math.sqrt,
};
};

Expand Down
64 changes: 54 additions & 10 deletions src/service/stateTuple/logic/ArcballCameraControllerLogicService.re
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,10 @@ let unbindGameViewActiveCameraArcballCameraControllerEvent = engineState =>
StateEditorService.getStateIsDebug(),
);

let _getGameObjectPosition = (gameObject, engineState) =>
engineState
|> GameObjectComponentEngineService.unsafeGetTransformComponent(gameObject)
|. TransformEngineService.getPosition(engineState);

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

engineState
|> ArcballCameraEngineService.setArcballCameraControllerTarget(
arcballCameraController,
Expand All @@ -203,6 +200,7 @@ let _setArcballCameraControllerFocusRelatedAttribute =
distance,
arcballCameraController,
);
};

let setEditorCameraFocusTargetGameObject =
(targetGameObject, editorState, engineState) => {
Expand Down Expand Up @@ -237,21 +235,67 @@ let setEditorCameraFocusTargetGameObject =
|. GameObjectComponentEngineService.unsafeGetArcballCameraControllerComponent(
engineState,
);
let targetGameObjectPosition =
engineState
|> GameObjectComponentEngineService.unsafeGetTransformComponent(
targetGameObject,
)
|. TransformEngineService.getPosition(engineState);

let maxDistanceofPointsToCenter =
engineState
|> HierarchyGameObjectEngineService.getAllGameObjects(targetGameObject)
|> Js.Array.map(gameObject =>
switch (
engineState
|> GameObjectComponentEngineService.getGeometryComponent(
gameObject,
)
) {
| None => None
| Some(geometry) =>
engineState
|> GeometryEngineService.getGeometryVertices(geometry)
|. Some
}
)
|> WonderCommonlib.ArrayService.reduceOneParam(
(. maxValue, vertices) => {
let distanceOfPointsToCenter =
switch (vertices) {
| None => 0.
| Some(vertices) =>
SphereShapeUtils.findMaxDistanceOfPointsToCenter(
(0., 0., 0.),
vertices,
)
};

distanceOfPointsToCenter > maxValue ?
distanceOfPointsToCenter : maxValue;
},
0.,
)
|> Js.Math.sqrt;

Js.log2(targetGameObjectPosition, maxDistanceofPointsToCenter);

engineState |> SceneEngineService.isSceneGameObject(targetGameObject) ?
_setArcballCameraControllerFocusRelatedAttribute(
editorCameraArcballControllerComponent,
(
FocusDataUtils.getSceneGameObjectArcballCameraDistance(),
engineState |> _getGameObjectPosition(targetGameObject),
maxDistanceofPointsToCenter
+. FocusDataUtils.getSceneGameObjectArcballCameraDistance(),
targetGameObjectPosition,
),
engineState,
) :
_setArcballCameraControllerFocusRelatedAttribute(
editorCameraArcballControllerComponent,
(
FocusDataUtils.getSceneChildrenArcballCameraDistance(),
engineState |> _getGameObjectPosition(targetGameObject),
maxDistanceofPointsToCenter
+. FocusDataUtils.getSceneChildrenArcballCameraDistance(),
targetGameObjectPosition,
),
engineState,
);
Expand Down

0 comments on commit 4811419

Please sign in to comment.