简体   繁体   English

Three.js:我可以创建网格来替换很多对象吗?

[英]Three.js : Can i create mesh to replace lot of object

this is the link of my work : maison.whiteplay.fr 这是我的工作的链接: maison.whiteplay.fr

What i am trying to do is a 3D PacMan, but look at th code, i'm using mesh to build my level, except for bubbles (yellow circle), that you need to eat to win. 我正在尝试做的是3D PacMan,但是看一下代码,我正在使用网格物体来构建我的关卡,除了气泡(黄色圆圈)外,您需要吃点东西才能赢。 They are all different objects, but because they are a lot, it's lagging, can i use the same technologie (mesh i think) to the bubbles ? 它们都是不同的对象,但是由于它们很多,所以很滞后,我可以对气泡使用相同的技术(我认为是网格)吗? If yes, how ? 如果是,怎么办?

code: 码:

var geometrySphere = new THREE.SphereGeometry( 5, 32, 32 ); 
var bille = function(x,z){ 
    this.bille = new THREE.Mesh(
        geometrySphere, 
        new THREE.MeshBasicMaterial( {color: 0xffff00} )
    );
    this.bille.position.x = (x-15.5)*100; this.bille.position.y = 100;  
    this.bille.position.z = (z-15.5)*100; scene.add(this.bille); 
}

Thank your for reading, and if you have any suggestions about my code, don't hesistate :D 感谢您的阅读,如果您对我的代码有任何建议,请不要犹豫:D

You can also reuse your material instead of making a new instance all the time: 您还可以重复使用材质,而不必一直创建新实例:

var geometrySphere = new THREE.SphereGeometry( 5, 32, 32 ); 
var billeMaterial = new THREE.MeshBasicMaterial( {color: 0xffff00} );
var bille = function(x,z){ 
    this.bille = new THREE.Mesh(
        geometrySphere, 
        billeMaterial
    );
    this.bille.position.x = (x-15.5)*100; this.bille.position.y = 100;  
    this.bille.position.z = (z-15.5)*100; scene.add(this.bille); 
}

Reusing materials has good influence on performance. 重复使用材料对性能有很好的影响。

How do you duplicate your meshes/objects? 如何复制网格/对象?

You're code is almost right. 您的代码几乎是正确的。

In your specific case, with N equal balls, you must have N mesh but just one material. 在您的特定情况下,对于N个相等的球,您必须具有N个网格,但只有一种材质。

In this way, if you want to colorize (just for example) only one ball, you have to apply it one new material, otherwise you will apply new color to all the balls. 这样,如果只想对一个球进行着色(例如,仅对一个球进行着色),则必须将其应用一种新材料,否则,您将对所有球应用新颜色。

In your case lagging may be due to the sphere construction. 在您的情况下,滞后可能是由于球体构造造成的。

You clearly copy and paste from the documentation without read it before. 您可以清楚地从文档中复制和粘贴,而无需先阅读。

var geometrySphere = new THREE.SphereGeometry( 5, 32, 32 );

Where, as explained in the documentation : 文档中所述:

radius — sphere radius. Default is 50.
widthSegments — number of horizontal segments. Minimum value is 3, and the default is 8.
heightSegments — number of vertical segments. Minimum value is 2, and the default is 6.

32 * 32 is too much for your small-monocolor bubbles, doesn't have sense. 32 * 32对于您的小单色气泡来说太大了,没有道理。

The higher are this values, the higher is the complexity for draw it for each frame. 该值越高,则每帧绘制它的复杂度越高。

I suggest you to create sphere with a minor number of vertical/horizontal segments (8*8 may be ok). 我建议您创建具有少量垂直/水平线段的球体(8 * 8可能没问题)。

Take a look at this demo . 看一下这个演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM