升级到3.X后,感觉变化很大啊。碰到一个很诡异的问题:使用cc.primitive.box时候,报错primitive为undefined。烦请论坛大佬看看是什么问题。我把代码贴到下面
—————————————————————————————————————
import { _decorator, Component, Node, Label, gfx, color, MeshRenderer, Mesh, primitive } from “cc”;
const { ccclass, property } = _decorator;
@ccclass(‘TestModel’)
export class TestModel extends Component {
@property(Label)
tip: Label = null!;
@property(Node)
cube: Node = null!;
createMesh (data, color) {
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 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;
}
onLoad () {
this.tip.string = "1";
// 创建长方体顶点数据
let data = primitive.box(100, 100, 100); // 这里报错,难道新版的CC引入方式不对吗?!
this.tip.string = "2";
// 根据顶点数据创建网格
var mesh = createMesh(data, (255, 97, 0));
this.tip.string = "3";
// 将创建的网格设置到 Mesh Renderer 上
var renderer = this.cube.getComponent(MeshRenderer);
renderer.mesh = mesh;
this.tip.string = "12123";
}
}