// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. import gfx from './gfx'; import InputAssembler from './core/input-assembler'; /** * @param {gfx.Device} device * @param {Object} data */ export function createIA(device, data) { if (!data.positions) { console.error('The data must have positions field'); return null; } let verts = []; let vcount = data.positions.length / 3; for (let i = 0; i < vcount; ++i) { verts.push(data.positions[3 * i], data.positions[3 * i + 1], data.positions[3 * i + 2]); if (data.normals) { verts.push(data.normals[3 * i], data.normals[3 * i + 1], data.normals[3 * i + 2]); } if (data.uvs) { verts.push(data.uvs[2 * i], data.uvs[2 * i + 1]); } if (data.colors) { verts.push(data.colors[3 * i], data.uvs[3 * i + 1], data.colors[3 * i + 2]); } } let vfmt = []; vfmt.push({ name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 3 }); if (data.normals) { vfmt.push({ name: gfx.ATTR_NORMAL, type: gfx.ATTR_TYPE_FLOAT32, num: 3 }); } if (data.uvs) { vfmt.push({ name: gfx.ATTR_UV0, type: gfx.ATTR_TYPE_FLOAT32, num: 2 }); } if (data.colors) { vfmt.push({ name: gfx.ATTR_COLOR, type: gfx.ATTR_TYPE_FLOAT32, num: 3 }); } let vb = new gfx.VertexBuffer( device, new gfx.VertexFormat(vfmt), gfx.USAGE_STATIC, new Float32Array(verts), vcount ); let ib = null; if (data.indices) { ib = new gfx.IndexBuffer( device, gfx.INDEX_FMT_UINT16, gfx.USAGE_STATIC, new Uint16Array(data.indices), data.indices.length ); } return new InputAssembler(vb, ib, data.primitiveType); }