diff --git a/src/core/composable_component/utils/FocusDataUtils.re b/src/core/composable_component/utils/FocusDataUtils.re index 8d89eb73a..1ac2a039a 100644 --- a/src/core/composable_component/utils/FocusDataUtils.re +++ b/src/core/composable_component/utils/FocusDataUtils.re @@ -1,3 +1,3 @@ -let getSceneGameObjectArcballCameraDistance = () => 20.0; +let getSceneGameObjectArcballCameraDistance = () => 30.; -let getSceneChildrenArcballCameraDistance = () => 6.0; \ No newline at end of file +let getSceneChildrenArcballCameraDistance = () => 10.; \ No newline at end of file diff --git a/src/core/utils/engine/job/init/initPickingJob/SphereShapeUtils.re b/src/core/utils/engine/job/init/initPickingJob/SphereShapeUtils.re index bd8ccea30..e97e28e49 100755 --- a/src/core/utils/engine/job/init/initPickingJob/SphereShapeUtils.re +++ b/src/core/utils/engine/job/init/initPickingJob/SphereShapeUtils.re @@ -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), @@ -42,7 +42,7 @@ let setFromPoints = vertices => { { center, radius: - _findMaxDistanceOfPointsToCenter(center, vertices) |> Js.Math.sqrt, + findMaxDistanceOfPointsToCenter(center, vertices) |> Js.Math.sqrt, }; }; diff --git a/src/service/stateTuple/logic/ArcballCameraControllerLogicService.re b/src/service/stateTuple/logic/ArcballCameraControllerLogicService.re index f1b9ecbd7..ee1ce654b 100755 --- a/src/service/stateTuple/logic/ArcballCameraControllerLogicService.re +++ b/src/service/stateTuple/logic/ArcballCameraControllerLogicService.re @@ -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, @@ -203,6 +200,7 @@ let _setArcballCameraControllerFocusRelatedAttribute = distance, arcballCameraController, ); +}; let setEditorCameraFocusTargetGameObject = (targetGameObject, editorState, engineState) => { @@ -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, );