Answers for "3d objects in cocos crearoe"

0

3d objects in cocos crearoe

function createMesh (data, color) {
    let gfx = cc.gfx;
    let vfmt = new gfx.VertexFormat([
        { name: gfx.ATTR_POSITION, type: gfx.ATTR_TYPE_FLOAT32, num: 3 },
        { name: gfx.ATTR_NORMAL, type: gfx.ATTR_TYPE_FLOAT32, num: 3 },
        { name: gfx.ATTR_COLOR, type: gfx.ATTR_TYPE_UINT8, num: 4, normalize: true },
    ]);

    let colors = [];
    for (let i = 0; i < data.positions.length; i++) {
        colors.push(color);
    }

    let mesh = new cc.Mesh();
    mesh.init(vfmt, data.positions.length);
    mesh.setVertices(gfx.ATTR_POSITION, data.positions);
    mesh.setVertices(gfx.ATTR_NORMAL, data.normals);
    mesh.setVertices(gfx.ATTR_COLOR, colors);
    mesh.setIndices(data.indices);
    mesh.setBoundingBox(data.minPos, data.maxPos);

    return mesh;
}

// Create box vertex data
let data = cc.primitive.box(100, 100, 100);
// Create mesh base on vertex data
let mesh = createMesh(data, cc.color(100, 100, 100));
// Set the created mesh to the Mesh Renderer
let renderer = this.getComponent(cc.MeshRenderer);
renderer.mesh = mesh;
Posted by: Guest on November-13-2020

Code answers related to "TypeScript"

Browse Popular Code Answers by Language