Skip to content

Commit

Permalink
feat(geometry): setTexCoords add check: texCoords should in [0.0, 1.0]
Browse files Browse the repository at this point in the history
  • Loading branch information
yyc-git committed Sep 27, 2020
1 parent 584b815 commit 5e3932f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,33 @@ let getTexCoords = geometry => {
};

let setTexCoords = (geometry, texCoords) => {
DpContainer.unsafeGetGeometryRepoDp().setTexCoords(
geometry->GeometryEntity.value,
texCoords->TexCoordsVO.value,
);
Contract.requireCheck(
() => {
Contract.(
Operators.(
test(
Log.buildAssertMessage(
~expect={j|texCoords in [0.0, 1.0]|j},
~actual={j|not|j},
),
() => {
texCoords
->TexCoordsVO.value
->TypeArrayCPRepoUtils.reduceFloat32Array(true, (. result, value) => {
result && value >=. 0.0 && value <=. 1.0
})
})
)
)
},
DpContainer.unsafeGetOtherConfigDp().getIsDebug(),
)
->Result.bind(() => {
DpContainer.unsafeGetGeometryRepoDp().setTexCoords(
geometry->GeometryEntity.value,
texCoords->TexCoordsVO.value,
)
});
};

let hasTexCoords = geometry => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,7 @@ let fillUint32ArrayWithOffset = (targetTypeArr, sourceTypeArr, offset) => {
|> Uint32Array.setArrayOffset(Obj.magic(sourceTypeArr), offset)
});
};

let reduceFloat32Array = (typeArr, acc, f) => {
Float32Array.reduce(f, acc, typeArr);
};
35 changes: 26 additions & 9 deletions test/construct/unit/scene/component/geometry/Geometry_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ let _ =
(getFunc, setFunc, createVertexDataVOFunc) =>
test("directly set it", () => {
let geometry = create()->ResultTool.getExnSuccessValue;

setFunc(
geometry,
Float32Array.make([|1., 2., 3.|])->createVertexDataVOFunc,
Expand All @@ -48,7 +47,6 @@ let _ =

let newData =
Float32Array.make([|3., 5., 5.|])->createVertexDataVOFunc;

setFunc(geometry, newData)->ResultTool.getExnSuccessValueIgnore;

getFunc(geometry)->ResultTool.getExnSuccessValue->expect == newData;
Expand All @@ -62,13 +60,32 @@ let _ =
)
);

describe("set texCoords with type array", () =>
_testSetVertexDataWithTypeArray(
getTexCoords,
setTexCoords,
TexCoordsVO.create,
)
);
describe("set texCoords with type array", () => {
test("directly set it", () => {
let geometry = create()->ResultTool.getExnSuccessValue;
setTexCoords(
geometry,
Float32Array.make([|0., 1.|])->TexCoordsVO.create,
)
->ResultTool.getExnSuccessValueIgnore;

let newData = Float32Array.make([|0.5, 0.2|])->TexCoordsVO.create;
setTexCoords(geometry, newData)
->ResultTool.getExnSuccessValueIgnore;

getTexCoords(geometry)->ResultTool.getExnSuccessValue->expect
== newData;
});
test("texCoords should in [0.0, 1.0]", () => {
let geometry = create()->ResultTool.getExnSuccessValue;

setTexCoords(
geometry,
Float32Array.make([|1., 2., (-0.1), 0.5|])->TexCoordsVO.create,
)
->ExpectTool.toFail("expect texCoords in [0.0, 1.0]");
});
});

describe("set normals with type array", () =>
_testSetVertexDataWithTypeArray(
Expand Down

0 comments on commit 5e3932f

Please sign in to comment.