Fix primitive.scale (#12836)

* Fix primitive.scale

* Test

* Tweak
This commit is contained in:
Leslie Leigh (李的序)
2022-09-21 14:49:41 +08:00
committed by GitHub
parent bb5aa2523f
commit db3a56fa1e
2 changed files with 51 additions and 12 deletions
+30 -12
View File
@@ -69,9 +69,9 @@ export function translate (geometry: IGeometry, offset: { x?: number; y?: number
* @param value @zh 缩放量。@en The scaling size
*/
export function scale (geometry: IGeometry, value: { x?: number; y?: number; z?: number }) {
const x = value.x || 0;
const y = value.y || 0;
const z = value.z || 0;
const x = value.x ?? 1.0;
const y = value.y ?? 1.0;
const z = value.z ?? 1.0;
const nVertex = Math.floor(geometry.positions.length / 3);
for (let iVertex = 0; iVertex < nVertex; ++iVertex) {
const iX = iVertex * 3;
@@ -81,17 +81,35 @@ export function scale (geometry: IGeometry, value: { x?: number; y?: number; z?:
geometry.positions[iY] *= y;
geometry.positions[iZ] *= z;
}
if (geometry.minPos) {
geometry.minPos.x *= x;
geometry.minPos.y *= y;
geometry.minPos.z *= z;
const {
minPos,
maxPos,
} = geometry;
if (minPos) {
minPos.x *= x;
minPos.y *= y;
minPos.z *= z;
}
if (geometry.maxPos) {
geometry.maxPos.x *= x;
geometry.maxPos.y *= y;
geometry.maxPos.z *= z;
if (maxPos) {
maxPos.x *= x;
maxPos.y *= y;
maxPos.z *= z;
}
if (minPos && maxPos) {
// Negative scaling causes min-max to be swapped.
if (x < 0) {
const tmp = minPos.x; minPos.x = maxPos.x; maxPos.x = tmp;
}
if (y < 0) {
const tmp = minPos.y; minPos.y = maxPos.y; maxPos.y = tmp;
}
if (z < 0) {
const tmp = minPos.z; minPos.z = maxPos.z; maxPos.z = tmp;
}
}
if (typeof geometry.boundingRadius !== 'undefined') {
geometry.boundingRadius *= Math.max(Math.max(Math.abs(x), Math.abs(y)), Math.abs(z));
}
geometry.boundingRadius = Math.max(Math.max(x, y), z);
return geometry;
}
+21
View File
@@ -0,0 +1,21 @@
import { IGeometry } from '../../cocos/primitive/define';
import { scale } from '../../cocos/primitive/transform';
import '../utils/matcher-deep-close-to';
test('Scaling', () => {
expect(scale({ positions: [2.0, 3.0, 4.0] }, { x: 1.2, })).toBeDeepCloseTo({
positions: [2.4, 3.0, 4.0],
});
expect(scale({
positions: [2.0, 3.0, 4.0],
minPos: { x: -1.0, y: -2.0, z: -3.0 },
maxPos: { x: 2.0, y: 3.2, z: 4.4 },
boundingRadius: 6.6,
}, { x: 1.2, y: 1.3, z: -3.0 })).toBeDeepCloseTo({
positions: [2.0 * 1.2, 3.0 * 1.3, 4.0 * -3.0],
minPos: { x: -1.0 * 1.2, y: -2.0 * 1.3, z: 4.4 * -3.0 },
maxPos: { x: 2.0 * 1.2, y: 3.2 * 1.3, z: -3.0 * -3.0 },
boundingRadius: 6.6 * 3.0,
});
});