Skip to content

Commit

Permalink
[opt] physx mesh collider shrinkPositions() performance x50 (#16283)
Browse files Browse the repository at this point in the history
  • Loading branch information
lealzhan authored Sep 19, 2023
1 parent c58cb54 commit 8366af4
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions cocos/physics/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
THE SOFTWARE.
*/

import { equals, Vec3, IVec3Like } from '../../core';
import { equals, Vec3, IVec3Like, murmurhash2_32_gc } from '../../core';
import { CharacterController, CharacterTriggerEventType, Collider, CollisionEventType, IContactEquation, TriggerEventType } from '../framework';

export { cylinder } from '../../primitive';
Expand Down Expand Up @@ -69,23 +69,21 @@ export const CollisionEventObject = {

export function shrinkPositions (buffer: Float32Array | number[]): number[] {
const pos: number[] = [];
const posHashMap = {};
if (buffer.length >= 3) {
// eslint-disable-next-line no-unused-expressions
pos[0] = buffer[0], pos[1] = buffer[1], pos[2] = buffer[2];
pos[0] = buffer[0];
pos[1] = buffer[1];
pos[2] = buffer[2];
const len = buffer.length;
for (let i = 3; i < len; i += 3) {
const p0 = buffer[i];
const p1 = buffer[i + 1];
const p2 = buffer[i + 2];
const len2 = pos.length;
let isNew = true;
for (let j = 0; j < len2; j += 3) {
if (equals(p0, pos[j]) && equals(p1, pos[j + 1]) && equals(p2, pos[j + 2])) {
isNew = false;
break;
}
}
if (isNew) {
const str = String(p0) + String(p1) + String(p2);
//todo: directly use buffer as input
const hash = murmurhash2_32_gc(str, 666);
if (posHashMap[hash] !== str) {
posHashMap[hash] = str;
pos.push(p0); pos.push(p1); pos.push(p2);
}
}
Expand Down

0 comments on commit 8366af4

Please sign in to comment.