Skip to content

Commit

Permalink
fix(api): fix CoordinateAPI->convertWorldToScreen: change result to o…
Browse files Browse the repository at this point in the history
…ption
  • Loading branch information
yyc-git committed May 9, 2019
1 parent 8567055 commit 9dd6c50
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/api/CoordinateAPI.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ let convertWorldToScreen =
cameraProjection,
(worldX, worldY, worldZ, screenWidth, screenHeight),
state,
);
)
|> Js.Nullable.fromOption;
5 changes: 3 additions & 2 deletions src/service/state/main/coordinate/CoordinateMainService.re
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ let convertWorldToScreen =
|> Vector4Service.transformMat4Tuple((worldX, worldY, worldZ, 1.));

w < 0. ?
((-100.), (-100.)) :
None :
{
let (x, y, z) as ndcSpacePos = (x /. w, y /. w, z /. w);

(
Js.Math.round((x +. 1.) /. 2. *. screenWidth),
Js.Math.round((1. -. y) /. 2. *. screenHeight),
);
)
->Some;
};
};
2 changes: 1 addition & 1 deletion src/service/state/main/data/StateDataMainType.re
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ and apiRecord = {
(component, state) => GameObjectPrimitiveType.gameObject,
"convertWorldToScreen":
(int, int, (float, float, float, float, float), state) =>
(float, float),
Js.Nullable.t((float, float)),
"getRenderWorkerCustomData":
state => CustomWorkerDataType.customDataFromRenderWorkerToMainWorker,
},
Expand Down
20 changes: 14 additions & 6 deletions test/unit/api/CoordinateAPI_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let _ =
describe("convertWorldToScreen", () =>
describe("convert world coordinate to screen coordinate", () => {
let _test =
(localPosition, (worldX, worldY, worldZ), (screenX, screenY)) => {
(localPosition, (worldX, worldY, worldZ), screenCoordinate) => {
let screenWidth = 1000.;
let screenHeight = 2000.;
let (
Expand Down Expand Up @@ -61,24 +61,32 @@ let _ =
(worldX, worldY, worldZ, screenWidth, screenHeight),
state,
)
|> expect == (screenX, screenY);
|> expect == screenCoordinate;
};

test("test1", () =>
_test((0., 1., 1.), (0., 0., 0.), (500., 1000.))
_test(
(0., 1., 1.),
(0., 0., 0.),
Js.Nullable.return((500., 1000.)),
)
);
test("test2", () =>
_test((0., 0., 1.), (1., 0.5, 0.), (2232., 134.))
_test(
(0., 0., 1.),
(1., 0.5, 0.),
Js.Nullable.return((2232., 134.)),
)
);

describe("fix bug", () =>
describe(
"if world coordinate is in the reverse direction, screen coordinate should be out of screen",
() =>
test(
"if the w(the coordinate(x,y,z,w) after perspective transform) < 0.0, set screen coordinate to be (-100.0, -100.0)",
"if the w(the coordinate(x,y,z,w) after perspective transform) < 0.0, set screen coordinate to be undefined",
() =>
_test((0., 0., 1.), (0., 0., 100.), ((-100.), (-100.)))
_test((0., 0., 1.), (0., 0., 100.), Js.Nullable.undefined)
)
)
);
Expand Down

0 comments on commit 9dd6c50

Please sign in to comment.