Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[opt] physx mesh collider shrinkPositions() performance x50 #16283

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading