mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
proper IA data extraction (#3323)
This commit is contained in:
@@ -104,8 +104,7 @@ export class GltfMeshResource extends MeshResource {
|
||||
return;
|
||||
}
|
||||
|
||||
const vbData = new _compType2Array[positionType](this.gltfAsset.buffers[vertexBufferView.buffer].data, vertexBufferView.byteOffset,
|
||||
vertexBufferView.byteLength / gfx.attrTypeBytes(positionType));
|
||||
const vbData = new DataView(this.gltfAsset.buffers[vertexBufferView.buffer].data, vertexBufferView.byteOffset, vertexBufferView.byteLength);
|
||||
const vb = new gfx.VertexBuffer(
|
||||
app.device,
|
||||
new gfx.VertexFormat(vfmt),
|
||||
@@ -118,8 +117,7 @@ export class GltfMeshResource extends MeshResource {
|
||||
if (primitive.indices !== undefined) {
|
||||
let ibAcc = gltfAccessors[primitive.indices];
|
||||
let ibView = gltf.bufferViews[ibAcc.bufferView];
|
||||
let ibData = new _compType2Array[ibAcc.componentType](this.gltfAsset.buffers[ibView.buffer].data, ibView.byteOffset,
|
||||
ibView.byteLength / gfx.attrTypeBytes(ibAcc.componentType));
|
||||
let ibData = new DataView(this.gltfAsset.buffers[ibView.buffer].data, ibView.byteOffset, ibView.byteLength);
|
||||
|
||||
ib = new gfx.IndexBuffer(
|
||||
app.device,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { enums } from './enums';
|
||||
|
||||
export default class IndexBuffer {
|
||||
class IndexBuffer {
|
||||
/**
|
||||
* @constructor
|
||||
* @param {Device} device
|
||||
@@ -29,7 +29,6 @@ export default class IndexBuffer {
|
||||
// update
|
||||
this._glID = device._gl.createBuffer();
|
||||
this.update(0, data);
|
||||
if (CC_EDITOR) this._data = data || 0;
|
||||
|
||||
// stats
|
||||
device._stats.ib += this._bytes;
|
||||
@@ -91,4 +90,45 @@ export default class IndexBuffer {
|
||||
get count () {
|
||||
return this._numIndices;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (CC_EDITOR) {
|
||||
let _type2fn = {
|
||||
[enums.ATTR_TYPE_INT8]: 'getInt8',
|
||||
[enums.ATTR_TYPE_UINT8]: 'getUint8',
|
||||
[enums.ATTR_TYPE_INT16]: 'getInt16',
|
||||
[enums.ATTR_TYPE_UINT16]: 'getUint16',
|
||||
[enums.ATTR_TYPE_INT32]: 'getInt32',
|
||||
[enums.ATTR_TYPE_UINT32]: 'getUint32',
|
||||
[enums.ATTR_TYPE_FLOAT32]: 'getFloat32'
|
||||
};
|
||||
let littleEndian = (function() {
|
||||
let buffer = new ArrayBuffer(2);
|
||||
new DataView(buffer).setInt16(0, 256, true);
|
||||
// Int16Array uses the platform's endianness.
|
||||
return new Int16Array(buffer)[0] === 256;
|
||||
})();
|
||||
let createCache = function(ib, data) {
|
||||
if (!data) return [];
|
||||
let icount = data.byteLength / ib._bytesPerIndex;
|
||||
let result = new Array(icount);
|
||||
for (let i = 0; i < icount; i++) {
|
||||
let fname = _type2fn[ib._format];
|
||||
result[i] = data[fname](i * ib._bytesPerIndex, littleEndian);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
IndexBuffer.prototype.do_update = IndexBuffer.prototype.update;
|
||||
IndexBuffer.prototype.update = function(offset, data) {
|
||||
this.do_update(offset, data);
|
||||
// don't need to create for IA pools
|
||||
if (data && this._bytes !== data.byteLength) return;
|
||||
if (data instanceof ArrayBuffer)
|
||||
data = new DataView(data);
|
||||
else if (ArrayBuffer.isView(data) && !(data instanceof DataView))
|
||||
data = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
||||
this._data = createCache(this, data);
|
||||
};
|
||||
}
|
||||
|
||||
export default IndexBuffer;
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
export default class VertexBuffer {
|
||||
import { enums } from './enums';
|
||||
|
||||
class VertexBuffer {
|
||||
/**
|
||||
* @constructor
|
||||
* @param {Device} device
|
||||
@@ -19,7 +21,6 @@ export default class VertexBuffer {
|
||||
// update
|
||||
this._glID = device._gl.createBuffer();
|
||||
this.update(0, data);
|
||||
if (CC_EDITOR) this._data = data || 0;
|
||||
|
||||
// stats
|
||||
device._stats.vb += this._bytes;
|
||||
@@ -80,4 +81,57 @@ export default class VertexBuffer {
|
||||
get count () {
|
||||
return this._numVertices;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (CC_EDITOR) {
|
||||
let _type2fn = {
|
||||
[enums.ATTR_TYPE_INT8]: 'getInt8',
|
||||
[enums.ATTR_TYPE_UINT8]: 'getUint8',
|
||||
[enums.ATTR_TYPE_INT16]: 'getInt16',
|
||||
[enums.ATTR_TYPE_UINT16]: 'getUint16',
|
||||
[enums.ATTR_TYPE_INT32]: 'getInt32',
|
||||
[enums.ATTR_TYPE_UINT32]: 'getUint32',
|
||||
[enums.ATTR_TYPE_FLOAT32]: 'getFloat32'
|
||||
};
|
||||
let littleEndian = (function() {
|
||||
let buffer = new ArrayBuffer(2);
|
||||
new DataView(buffer).setInt16(0, 256, true);
|
||||
// Int16Array uses the platform's endianness.
|
||||
return new Int16Array(buffer)[0] === 256;
|
||||
})();
|
||||
let getAttrAt = function(data, idx, fmt, out = []) {
|
||||
let offset = idx * fmt.stride + fmt.offset;
|
||||
let bytes = fmt.bytes / fmt.num;
|
||||
let fname = _type2fn[fmt.type];
|
||||
for (let i = 0; i < fmt.num; i++) {
|
||||
out[i] = data[fname](offset + i * bytes, littleEndian);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
let createAttrCache = function(vb, data, attr) {
|
||||
if (!data) return [];
|
||||
let fmt = vb._format.element(attr);
|
||||
let vcount = data.byteLength / fmt.stride;
|
||||
let result = new Array(vcount * fmt.num);
|
||||
let t = new Array(fmt.num);
|
||||
for (let i = 0; i < vcount; i++) {
|
||||
getAttrAt(data, i, fmt, t);
|
||||
for (let j = 0; j < fmt.num; j++)
|
||||
result[i * fmt.num + j] = t[j];
|
||||
}
|
||||
return result;
|
||||
};
|
||||
VertexBuffer.prototype.do_update = VertexBuffer.prototype.update;
|
||||
VertexBuffer.prototype.update = function(offset, data) {
|
||||
this.do_update(offset, data);
|
||||
// don't need to create for IA pools
|
||||
if (data && this._bytes !== data.byteLength) return;
|
||||
if (data instanceof ArrayBuffer)
|
||||
data = new DataView(data);
|
||||
else if (ArrayBuffer.isView(data) && !(data instanceof DataView))
|
||||
data = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
||||
this[enums.ATTR_POSITION] = createAttrCache(this, data, enums.ATTR_POSITION);
|
||||
};
|
||||
}
|
||||
|
||||
export default VertexBuffer;
|
||||
|
||||
@@ -20,7 +20,6 @@ export default class VertexFormat {
|
||||
this._attr2el = {};
|
||||
this._elements = [];
|
||||
this._bytes = 0;
|
||||
this._count = 0;
|
||||
|
||||
for (let i = 0, len = infos.length; i < len; ++i) {
|
||||
let info = infos[i];
|
||||
@@ -33,21 +32,17 @@ export default class VertexFormat {
|
||||
num: info.num,
|
||||
normalize: (info.normalize === undefined) ? false : info.normalize,
|
||||
bytes: info.num * attrTypeBytes(info.type),
|
||||
arrayOffset: this._count,
|
||||
arrayStride: 0
|
||||
};
|
||||
|
||||
this._attr2el[el.name] = el;
|
||||
this._elements.push(el);
|
||||
|
||||
this._count += el.num;
|
||||
this._bytes += el.bytes;
|
||||
}
|
||||
|
||||
for (let i = 0, len = this._elements.length; i < len; ++i) {
|
||||
let el = this._elements[i];
|
||||
el.stride = this._bytes;
|
||||
el.arrayStride = this._count;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -215,19 +215,13 @@ if (CC_EDITOR) {
|
||||
let modelRay = ray.create();
|
||||
let m4 = mat4.create();
|
||||
let distance = Infinity;
|
||||
let cmp = (a, b) => {
|
||||
return a.distance - b.distance;
|
||||
};
|
||||
let do_test = tri => {
|
||||
let dist = intersect.ray_triangle(modelRay, tri);
|
||||
if (dist <= 0 || dist > distance) return;
|
||||
distance = dist;
|
||||
};
|
||||
let tri = triangle.create();
|
||||
return function(worldRay, results, mask = Layers.RaycastMask) {
|
||||
results.reset();
|
||||
for (let i = 0; i < this._models.length; i++) {
|
||||
let m = this._models.data[i];
|
||||
if ((m._node._layer & mask) === 0) continue;
|
||||
if ((m._node._layer & mask) === 0 ||
|
||||
m._inputAssembler._primitiveType !== gfx.PT_TRIANGLES) continue;
|
||||
// transform ray back to model space
|
||||
mat4.invert(m4, m._node.getWorldMatrix(m4));
|
||||
vec3.transformMat4(modelRay.o, worldRay.o, m4);
|
||||
@@ -236,37 +230,26 @@ if (CC_EDITOR) {
|
||||
if (intersect.ray_aabb(modelRay, m._bsModelSpace) <= 0) continue;
|
||||
// narrowphase
|
||||
distance = Infinity;
|
||||
this._forEachTriangleIn(m._inputAssembler, do_test);
|
||||
let vb = m._inputAssembler._vertexBuffer[gfx.ATTR_POSITION];
|
||||
let ib = m._inputAssembler._indexBuffer._data;
|
||||
for (let j = 0; j < ib.length; j += 3) {
|
||||
let i0 = ib[j] * 3, i1 = ib[j + 1] * 3, i2 = ib[j + 2] * 3;
|
||||
vec3.set(tri.a, vb[i0], vb[i0 + 1], vb[i0 + 2]);
|
||||
vec3.set(tri.b, vb[i1], vb[i1 + 1], vb[i1 + 2]);
|
||||
vec3.set(tri.c, vb[i2], vb[i2 + 1], vb[i2 + 2]);
|
||||
let dist = intersect.ray_triangle(modelRay, tri);
|
||||
if (dist <= 0 || dist > distance) continue;
|
||||
distance = dist;
|
||||
}
|
||||
if (distance < Infinity) {
|
||||
let res = results.add();
|
||||
res.entity = m._node;
|
||||
res.distance = distance;
|
||||
}
|
||||
}
|
||||
results.sort(cmp);
|
||||
return results.length;
|
||||
};
|
||||
})();
|
||||
|
||||
Scene.prototype._forEachTriangleIn = (function() {
|
||||
let tri = triangle.create();
|
||||
return function(ia, callback) {
|
||||
if (ia._primitiveType !== gfx.PT_TRIANGLES) return;
|
||||
let vb = ia._vertexBuffer._data;
|
||||
let ib = ia._indexBuffer._data;
|
||||
let fmt = ia._vertexBuffer._format.element(gfx.ATTR_POSITION);
|
||||
let offset = fmt.arrayOffset, stride = fmt.arrayStride;
|
||||
for (let i = 0; i < ib.length; i += 3) {
|
||||
let idx = ib[i] * stride + offset;
|
||||
vec3.set(tri.a, vb[idx], vb[idx + 1], vb[idx + 2]);
|
||||
idx = ib[i + 1] * stride + offset;
|
||||
vec3.set(tri.b, vb[idx], vb[idx + 1], vb[idx + 2]);
|
||||
idx = ib[i + 2] * stride + offset;
|
||||
vec3.set(tri.c, vb[idx], vb[idx + 1], vb[idx + 2]);
|
||||
if (callback(tri)) break;
|
||||
}
|
||||
};
|
||||
})();
|
||||
}
|
||||
|
||||
export default Scene;
|
||||
|
||||
Reference in New Issue
Block a user