第10节课 加入货物的显示,讨论两种方式的性能
如果场景中需要加入的对象有很多,为了提高效率,我们推荐下面两种方式:
1、尽量重用Material和Geometry
这里以Material和Geometry为例(使用比较频繁)
for (var i = 0; i < 100; i++) {
var material = new THREE.MeshBasicMaterial();
var geometry = new THREE.BoxGeometry(10, 10, 10);
var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
}
改为
var material = new THREE.MeshBasicMaterial();
var geometry = new THREE.BoxGeometry(10, 10, 10);
for (var i = 0; i < 100; i++) {
var mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
}
案例代码详见工程目录
2、尽量使用clone方法
这个的原理其实跟2是一样的
添加货物的核心代码如下,注意我们使用的Clone的方式:
StoreGoods.prototype.create=function () {
let optionCube = {
length: this.length,
width: this.width,
height: this.height,
position: {
x: this.positionX,
y: this.positionY,
z: this.positionZ
},
style: {
transparent:1,
opacity:this.opacity,
color: this.color,
depthTest:1
},
};
let cube = new Cube(optionCube);
cube.uuid=this.no;
cube.name=this.name;
cube.type="StoreGoods";
return cube;
}
StoreGoods.prototype.clone=function (object) {
let goods=object.clone();
goods.position.set(this.positionX,this.positionY,this.positionZ);
goods.uuid=this.no;
goods.name=this.name;
return goods;
}
对于货物材质相同的我们使用clone的代码。