fix when use uint32arrat as index buffer (#3961)

* fix when use uint32arrat as index buffer

* add collide event
This commit is contained in:
Leslie Leigh
2019-03-05 10:27:15 +08:00
committed by Weiliang Wang
parent 5fcb1b5de3
commit dee10308a4
3 changed files with 31 additions and 13 deletions
+1 -1
View File
@@ -119,7 +119,7 @@ function getIndexUnitStride (indexUnit: IndexUnit) {
switch (indexUnit) {
case IndexUnit.UINT8: return 1;
case IndexUnit.UINT16: return 2;
case IndexUnit.UINT32: return 3;
case IndexUnit.UINT32: return 4;
}
return 1;
}
@@ -5,7 +5,7 @@ import { Quat, Vec3 } from '../../../../core/value-types';
import { quat, vec3 } from '../../../../core/vmath';
import { Node } from '../../../../scene-graph/node';
import { BoxColliderComponent, ColliderComponentBase, SphereColliderComponent } from '../collider-component';
import { setWrap } from '../util';
import { getWrap, setWrap } from '../util';
import { PhysicsWorld } from '../world';
export class PhysicsBasedComponent extends Component {
@@ -245,8 +245,10 @@ class SharedRigidBody {
if (!this._node) {
return;
}
this._node.emit('collided', {});
// console.log(`Collided {${getWrap<PhysicsBody>(event.body)._node.name}} and {${getWrap<PhysicsBody>(event.target)._node.name}}.`);
this._node.emit('collided', {
source: getWrap<Node>(event.body),
target: getWrap<Node>((event as any).target),
});
}
private _onWorldBeforeStep (event: CANNON.IEvent) {
+25 -9
View File
@@ -41,6 +41,7 @@ export function createMesh (geometry: IGeometry) {
for (const channel of channels) {
_writeVertices(vertexBufferView, channel.offset, stride, channel.attribute, channel.data);
}
bufferBlob.setNextAlignment(0);
const vertexBundle: IVertexBundle = {
verticesCount,
attributes,
@@ -54,9 +55,9 @@ export function createMesh (geometry: IGeometry) {
// Fill index buffer.
let indexBuffer: ArrayBuffer | null = null;
const targetIndexUnit = IndexUnit.UINT16;
const indexUnitBytesLength = 2;
if (geometry.indices) {
const { indices } = geometry;
const indexUnitBytesLength = 2;
indexBuffer = new ArrayBuffer(indexUnitBytesLength * indices.length);
const indexBufferView = new DataView(indexBuffer);
const writer = _getIndicesWriter(indexBufferView, targetIndexUnit);
@@ -73,6 +74,7 @@ export function createMesh (geometry: IGeometry) {
// geometric info for raycasting
if (primitive.primitiveMode >= GFXPrimitiveMode.TRIANGLE_LIST) {
const geomInfo = Float32Array.from(geometry.positions);
bufferBlob.setNextAlignment(4);
primitive.geometricInfo = {
doubleSided: geometry.doubleSided,
range: { offset: bufferBlob.getLength(), length: geomInfo.byteLength },
@@ -81,6 +83,7 @@ export function createMesh (geometry: IGeometry) {
}
if (indexBuffer) {
bufferBlob.setNextAlignment(indexUnitBytesLength);
primitive.indices = {
indexUnit: targetIndexUnit,
range: {
@@ -141,12 +144,23 @@ const predefinedAttributes = {
};
class BufferBlob {
private _arrayBuffers: ArrayBuffer[] = [];
private _arrayBufferOrPaddings: Array<ArrayBuffer | number> = [];
private _length = 0;
public setNextAlignment (align: number) {
if (align !== 0) {
const remainder = this._length % align;
if (remainder !== 0) {
const padding = align - remainder;
this._arrayBufferOrPaddings.push(padding);
this._length += padding;
}
}
}
public addBuffer (arrayBuffer: ArrayBuffer) {
const result = this._length;
this._arrayBuffers.push(arrayBuffer);
this._arrayBufferOrPaddings.push(arrayBuffer);
this._length += arrayBuffer.byteLength;
return result;
}
@@ -156,13 +170,15 @@ class BufferBlob {
}
public getCombined () {
let length = 0;
this._arrayBuffers.forEach((arrayBuffer) => length += arrayBuffer.byteLength);
const result = new Uint8Array(length);
const result = new Uint8Array(this._length);
let counter = 0;
this._arrayBuffers.forEach((arrayBuffer) => {
result.set(new Uint8Array(arrayBuffer), counter);
counter += arrayBuffer.byteLength;
this._arrayBufferOrPaddings.forEach((arrayBufferOrPadding) => {
if (typeof arrayBufferOrPadding === 'number') {
counter += arrayBufferOrPadding;
} else {
result.set(new Uint8Array(arrayBufferOrPadding), counter);
counter += arrayBufferOrPadding.byteLength;
}
});
return result.buffer;
}