Skip to content

Commit

Permalink
feat(transform): lookAt
Browse files Browse the repository at this point in the history
Add lookAt API
  • Loading branch information
Wang Ying committed Sep 22, 2020
1 parent 5891d70 commit bdc330a
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/construct/application_layer/scene/TransformApService.re
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,7 @@ let getNormalMatrix = transform => {
let getMaxIndex = IndexTransformDoService.getMaxIndex;

let mutableUpdate = UpdateTransformDoService.mutableUpdate;

let lookAt = (transform, target) => {
UpdateTransformDoService.setLookAt(transform, target);
}
1 change: 1 addition & 0 deletions src/construct/domain_layer/dependency/interface/IRepoDp.re
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type transformRepo = {
addChild: (parent, child) => unit,
removeChild: (parent, child) => unit,
getLocalToWorldMatrix: transform => Js.Typed_array.Float32Array.t,
setLocalToWorldMatrix: (transform, Js.Typed_array.Float32Array.t) => Result.t2(unit),
getLocalPosition: transform => position,
setLocalPosition: (transform, position) => Result.t2(unit),
getLocalRotation: transform => rotation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ let getLocalToWorldMatrix = transform => {
->LocalToWorldMatrixVO.create;
};

let setLocalToWorldMatrix = (transform, localToWorldMatrix) => {
DpContainer.unsafeGetTransformRepoDp().setLocalToWorldMatrix(
transform->TransformEntity.value,
localToWorldMatrix
)
}

let getNormalMatrix = transform => {
DpContainer.unsafeGetGlobalTempRepoDp().getFloat9Array()
->Matrix4.invertTo3x3(
Expand Down Expand Up @@ -107,4 +114,4 @@ let setScale = (transform, parent, scale) => {
->ScaleVO.create,
)
});
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,16 @@ let updateAndSetEulerAngles = (transform, eulerAngles) =>
transform,
eulerAngles->EulerAnglesVO.convertToQuaternion->RotationVO.create,
);

let setLookAt = (transform, target) => {
ModelMatrixTransformDoService.setLocalToWorldMatrix(
transform,
Matrix4.setLookAt(
updateAndGetPosition(
transform,
) -> PositionVO.value,
target,
(0., 1., 0.),
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,5 @@ let getNormalMatrix = transform => {
let getMaxIndex = TransformApService.getMaxIndex;

let mutableUpdate = TransformApService.mutableUpdate;

let lookAt = TransformApService.lookAt;
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ let _injectDependencies = () => {
addChild: TransformCPRepoDp.addChild,
removeChild: TransformCPRepoDp.removeChild,
getLocalToWorldMatrix: TransformCPRepoDp.getLocalToWorldMatrix,
setLocalToWorldMatrix: TransformCPRepoDp.setLocalToWorldMatrix,
getLocalPosition: TransformCPRepoDp.getLocalPosition,
setLocalPosition: TransformCPRepoDp.setLocalPosition,
getLocalRotation: TransformCPRepoDp.getLocalRotation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ let getLocalToWorldMatrix = transform => {
);
};

let setLocalToWorldMatrix = (transform, localToWorldMatrix) => {
OperateTypeArrayTransformCPRepoUtils.setLocalToWorldMatrix(
transform,
TypeArrayCPRepoUtils.getFloat16Tuple(0, localToWorldMatrix),
CPRepo.getExnTransform().localToWorldMatrices,
)
}

let getLocalPosition = transform => {
OperateTypeArrayTransformCPRepoUtils.getLocalPositionTuple(
transform,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,27 @@ let getFloat4Tuple = (index: int, typeArray: Float32Array.t) => (
Float32Array.unsafe_get(typeArray, index + 3),
);

let getFloat16Tuple = (index: int, typeArray: Float32Array.t) => {
(
Float32Array.unsafe_get(typeArray, index),
Float32Array.unsafe_get(typeArray, index + 1),
Float32Array.unsafe_get(typeArray, index + 2),
Float32Array.unsafe_get(typeArray, index + 3),
Float32Array.unsafe_get(typeArray, index + 4),
Float32Array.unsafe_get(typeArray, index + 5),
Float32Array.unsafe_get(typeArray, index + 6),
Float32Array.unsafe_get(typeArray, index + 7),
Float32Array.unsafe_get(typeArray, index + 8),
Float32Array.unsafe_get(typeArray, index + 9),
Float32Array.unsafe_get(typeArray, index + 10),
Float32Array.unsafe_get(typeArray, index + 11),
Float32Array.unsafe_get(typeArray, index + 12),
Float32Array.unsafe_get(typeArray, index + 13),
Float32Array.unsafe_get(typeArray, index + 14),
Float32Array.unsafe_get(typeArray, index + 15),
);
}

let getUint32_1 = (index: int, typeArray: Uint32Array.t) =>
Uint32Array.unsafe_get(typeArray, index);

Expand Down
33 changes: 33 additions & 0 deletions test/construct/unit/scene/component/Transform_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -761,4 +761,37 @@ let _ =
);
});
});

describe("lookAt", () => {
test("set lookAt will change localToWorld matrix", () => {
let transform = create()->ResultTool.getExnSuccessValue;

transform->lookAt((0., -1.0, 0.0))->ResultTool.getExnSuccessValue;

let changedLocalToWorldMatrix = transform->getLocalToWorldMatrix;

changedLocalToWorldMatrix->LocalToWorldMatrixVO.value->expect
== Js.Typed_array.Float32Array.make([|
0.0,
0.0,
0.0,
0.0,

0.0,
0.0,
0.0,
0.0,

0.0,
1.0,
0.0,
0.0,

0.0,
0.0,
0.0,
1.0
|]);
})
})
});

0 comments on commit bdc330a

Please sign in to comment.