Skip to content

Commit 5937321

Browse files
committed
Merge branch 'main' into dev/1.2
* main: Fix gltf accessor's bufferView could be undefined (#2013) Fix `IPhysicsScene.removeColliderShape` not cleaned up `eventMap` (#2008)
2 parents 98f7d9e + 57f6c8a commit 5937321

File tree

4 files changed

+100
-20
lines changed

4 files changed

+100
-20
lines changed

packages/loader/src/gltf/GLTFUtils.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ export class GLTFUtils {
134134
accessor: IAccessor
135135
): Promise<BufferInfo> {
136136
const componentType = accessor.componentType;
137-
const bufferView = bufferViews[accessor.bufferView];
137+
const bufferViewIndex = accessor.bufferView ?? 0;
138+
const bufferView = bufferViews[bufferViewIndex];
138139

139140
return context.get<Uint8Array>(GLTFParserType.BufferView, accessor.bufferView).then((bufferViewData) => {
140141
const bufferIndex = bufferView.buffer;
@@ -152,7 +153,7 @@ export class GLTFUtils {
152153
// According to the glTF official documentation only byteStride not undefined is allowed
153154
if (bufferStride !== undefined && bufferStride !== elementStride) {
154155
const bufferSlice = Math.floor(byteOffset / bufferStride);
155-
const bufferCacheKey = accessor.bufferView + ":" + componentType + ":" + bufferSlice + ":" + accessorCount;
156+
const bufferCacheKey = bufferViewIndex + ":" + componentType + ":" + bufferSlice + ":" + accessorCount;
156157
const accessorBufferCache = context.accessorBufferCache;
157158
bufferInfo = accessorBufferCache[bufferCacheKey];
158159
if (!bufferInfo) {
@@ -205,7 +206,7 @@ export class GLTFUtils {
205206
*/
206207
static getAccessorData(glTF: IGLTF, accessor: IAccessor, buffers: ArrayBuffer[]): TypedArray {
207208
const bufferViews = glTF.bufferViews;
208-
const bufferView = bufferViews[accessor.bufferView];
209+
const bufferView = bufferViews[accessor.bufferView ?? 0];
209210
const arrayBuffer = buffers[bufferView.buffer];
210211
const accessorByteOffset = accessor.hasOwnProperty("byteOffset") ? accessor.byteOffset : 0;
211212
const bufferViewByteOffset = bufferView.hasOwnProperty("byteOffset") ? bufferView.byteOffset : 0;

packages/physics-lite/src/LitePhysicsScene.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ICharacterController, IPhysicsScene } from "@galacean/engine-design";
21
import { BoundingBox, BoundingSphere, CollisionUtil, Ray, Vector3 } from "@galacean/engine";
2+
import { ICharacterController, IPhysicsScene } from "@galacean/engine-design";
33
import { DisorderedArray } from "./DisorderedArray";
44
import { LiteCollider } from "./LiteCollider";
55
import { LiteHitResult } from "./LiteHitResult";
@@ -65,16 +65,21 @@ export class LitePhysicsScene implements IPhysicsScene {
6565
* {@inheritDoc IPhysicsManager.removeColliderShape }
6666
*/
6767
removeColliderShape(colliderShape: LiteColliderShape): void {
68-
const { _eventPool: eventPool, _currentEvents: currentEvents } = this;
69-
const { _id: shapeID } = colliderShape;
68+
const { _eventPool: eventPool, _currentEvents: currentEvents, _eventMap: eventMap } = this;
69+
const { _id: id } = colliderShape;
7070
for (let i = currentEvents.length - 1; i >= 0; i--) {
7171
const event = currentEvents.get(i);
72-
if (event.index1 == shapeID || event.index2 == shapeID) {
72+
if (event.index1 == id) {
73+
currentEvents.deleteByIndex(i);
74+
eventPool.push(event);
75+
} else if (event.index2 == id) {
7376
currentEvents.deleteByIndex(i);
7477
eventPool.push(event);
78+
// If the shape is big index, should clear from the small index shape subMap
79+
eventMap[event.index1][id] = undefined;
7580
}
7681
}
77-
delete this._eventMap[shapeID];
82+
delete eventMap[id];
7883
}
7984

8085
/**

packages/physics-physx/src/PhysXPhysicsScene.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,21 @@ export class PhysXPhysicsScene implements IPhysicsScene {
116116
*/
117117
removeColliderShape(colliderShape: PhysXColliderShape) {
118118
const { _eventPool: eventPool, _currentEvents: currentEvents } = this;
119-
const { _id: shapeID } = colliderShape;
119+
const { _id: id } = colliderShape;
120+
const { _eventMap: eventMap } = this._physXManager;
120121
for (let i = currentEvents.length - 1; i >= 0; i--) {
121122
const event = currentEvents.get(i);
122-
if (event.index1 == shapeID || event.index2 == shapeID) {
123+
if (event.index1 == id) {
123124
currentEvents.deleteByIndex(i);
124125
eventPool.push(event);
126+
} else if (event.index2 == id) {
127+
currentEvents.deleteByIndex(i);
128+
eventPool.push(event);
129+
// If the shape is big index, should clear from the small index shape subMap
130+
eventMap[event.index1][id] = undefined;
125131
}
126132
}
127-
delete this._physXManager._eventMap[shapeID];
133+
delete eventMap[id];
128134
}
129135

130136
/**

tests/src/core/physics/PhysicsManager.test.ts

+77-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import {
2-
Camera,
32
BoxColliderShape,
4-
Layer,
5-
StaticCollider,
6-
DynamicCollider,
7-
HitResult,
3+
Camera,
84
CharacterController,
9-
CapsuleColliderShape,
10-
SphereColliderShape,
11-
Script,
5+
Collider,
126
ColliderShape,
7+
DynamicCollider,
138
Entity,
14-
Collider
9+
HitResult,
10+
Layer,
11+
Script,
12+
SphereColliderShape,
13+
StaticCollider
1514
} from "@galacean/engine-core";
1615
import { Ray, Vector3 } from "@galacean/engine-math";
1716
import { LitePhysics } from "@galacean/engine-physics-lite";
@@ -136,6 +135,75 @@ describe("Physics Test", () => {
136135
engineLite.run();
137136
});
138137

138+
it("removeShape", () => {
139+
const scene = engineLite.sceneManager.activeScene;
140+
const root = scene.createRootEntity("root");
141+
const removeShapeRoot1 = root.createChild("root");
142+
removeShapeRoot1.transform.position = new Vector3(1000, 1000, 1000);
143+
144+
const enterEvent = [];
145+
const collider1 = removeShapeRoot1.addComponent(StaticCollider);
146+
const box1 = new BoxColliderShape();
147+
enterEvent[box1.id] = [];
148+
collider1.addShape(box1);
149+
removeShapeRoot1.addComponent(
150+
class extends Script {
151+
onTriggerEnter(other: ColliderShape): void {
152+
++enterEvent[box1.id][other.id];
153+
}
154+
}
155+
);
156+
const removeShapeRoot2 = root.createChild("root");
157+
removeShapeRoot2.transform.position = new Vector3(1000, 1000, 1000);
158+
const collider2 = removeShapeRoot2.addComponent(StaticCollider);
159+
const box2 = new BoxColliderShape();
160+
enterEvent[box2.id] = [];
161+
collider2.addShape(box2);
162+
removeShapeRoot2.addComponent(
163+
class extends Script {
164+
onTriggerEnter(other: ColliderShape) {
165+
++enterEvent[box2.id][other.id];
166+
}
167+
}
168+
);
169+
// @ts-ignore
170+
engineLite.physicsManager._update(8);
171+
// Remove collider shape.
172+
removeShapeRoot2.isActive = false;
173+
const removeShapeRoot3 = root.createChild("root");
174+
removeShapeRoot3.transform.position = new Vector3(1000, 1000, 1000);
175+
const collider3 = removeShapeRoot3.addComponent(StaticCollider);
176+
const box3 = new BoxColliderShape();
177+
enterEvent[box3.id] = [];
178+
collider3.addShape(box3);
179+
removeShapeRoot3.addComponent(
180+
class extends Script {
181+
onTriggerEnter(other: ColliderShape) {
182+
++enterEvent[box3.id][other.id];
183+
}
184+
}
185+
);
186+
removeShapeRoot2.isActive = true;
187+
enterEvent[box1.id][box2.id] = 0;
188+
enterEvent[box1.id][box3.id] = 0;
189+
enterEvent[box2.id][box1.id] = 0;
190+
enterEvent[box2.id][box3.id] = 0;
191+
enterEvent[box3.id][box1.id] = 0;
192+
enterEvent[box3.id][box2.id] = 0;
193+
// @ts-ignore
194+
engineLite.physicsManager._update(8);
195+
expect(enterEvent[box1.id][box2.id]).to.eq(1);
196+
expect(enterEvent[box1.id][box3.id]).to.eq(1);
197+
expect(enterEvent[box2.id][box1.id]).to.eq(1);
198+
expect(enterEvent[box2.id][box3.id]).to.eq(1);
199+
expect(enterEvent[box3.id][box1.id]).to.eq(1);
200+
expect(enterEvent[box3.id][box2.id]).to.eq(1);
201+
202+
removeShapeRoot1.destroy();
203+
removeShapeRoot2.destroy();
204+
removeShapeRoot3.destroy();
205+
});
206+
139207
it("constructor", () => {
140208
expect(engineLite.physicsManager.gravity.y).to.eq(-9.81);
141209
expect(engineLite.physicsManager.fixedTimeStep).to.eq(1 / 60);

0 commit comments

Comments
 (0)