引擎版本:creator 3.5.2
本人首次接触3D,对于模型的理解几近于无,遂从网上找了一些关于mesh的说明,打算写一个对立方块缩放的demo,但是按照文章的描述去把顶点数据缩小1/2后,模型并没有任何变化,希望论坛中的各路大佬不吝指教!
文章链接:https://zhuanlan.zhihu.com/p/385296910
@property(Node)
private cube: Node = null;
private cubeMesh: Mesh = null;
protected onLoad() {
input.on(Input.EventType.TOUCH_START, this.touchStart, this);
}
protected start() {
const initMesh: Function = (node: Node) => {
const renderer: MeshRenderer = node.getComponent(MeshRenderer);
return renderer.mesh;
}
this.cubeMesh = initMesh(this.cube);
}
private touchStart(e: EventTouch) {
const data: Uint8Array = this.cubeMesh.data;//网格二进制字节流数据
const subMeshCount: number = this.cubeMesh.renderingSubMeshes.length;//子网格数量
const struct: Mesh.IStruct = this.cubeMesh.struct;//mesh网格结构
const primitives: Mesh.ISubMesh[] = struct.primitives;//此网格的所有子网格
const vertexBundles: Mesh.IVertexBundle[] = struct.vertexBundles;//此网格所有的顶点块
const newData: { [key: string]: { [key: string]: any } }[] = [];
for (let i = 0; i < subMeshCount; i++) {
const vView: Mesh.IBufferView = vertexBundles[i].view;
const pView: Mesh.IBufferView = primitives[i].indexView;
for (let j = 0; j < vView.count; j++) {
const temp: Float32Array = new Float32Array(data.slice(j * vView.stride + vView.offset, (j + 1) * vView.stride + vView.offset).buffer);
if (undefined === newData[i] || null === newData[i]) newData[i] = { v: { arr: [], stride: vView.stride } };
newData[i].v.arr.push(temp);
}
for (let j = 0; j < pView.count; j++) {
const temp: Float32Array = new Float32Array(data.slice(j * pView.stride + pView.offset, (j + 1) * pView.stride + pView.offset).buffer);
if (undefined === newData[i].p || null === newData[i].p) newData[i].p = { arr: [], stride: pView.stride };
newData[i].p.arr.push(temp);
}
this.scaleMoudle(0.5, newData[i].v);
}
const _newData: number[] = [];
for (let i = 0; i < subMeshCount; i++) {
for (const item of newData[i].v.arr)
new Uint8Array(item.buffer).forEach(e => _newData.push(e));
for (const item of newData[i].p.arr)
new Uint8Array(item.buffer).forEach(e => _newData.push(e));
}
const result:Uint8Array=Uint8Array.from(_newData);
let mesh = new Mesh();
mesh.reset({ struct: this.cubeMesh.struct, data: result });
mesh.initialize();
this.cubeMesh = mesh;
}
private scaleMoudle(scale: number, data: { [key: string]: any }) {
const vCount: number = data.stride / 4 - 11;
for (const item of data.arr) {
for (let i = 0; i < vCount; i++) {
item[i] = item[i] * scale;
}
}
}