Files
cocos4/cocos/renderer/utils.js
T
Yun Hsiao Wu da885ac40a effect improvements (#3480)
* effect (de)serialization

* more GL params to configure in effect

* uniform tags

* material (de)serialization

* switch to json solution

* effect: pass default value to editor too

* minor fix

* use default (de)serialization procedure

* async builtin init only
2018-11-13 15:12:02 +08:00

61 lines
1.4 KiB
JavaScript

// 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]);
}
}
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 });
}
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);
}