许多网格具有相同的几何和材料,我可以改变他们的颜色吗? [英] Many meshes with the same geometry and material, can I change their colors?

查看:249
本文介绍了许多网格具有相同的几何和材料,我可以改变他们的颜色吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大量的($ 1000) THREE.Mesh 对象从相同的 THREE.Geometry THREE.MeshPhongMaterial (其中有一张地图)。





Naïvely,我尝试更改 mesh.material.color 属性,任何对象立即改变所有对象的颜色。这是有道理的,因为只有一种材料在所有对象之间共享。



我的下一个想法是创建一个单独的 THREE.MeshPhongMaterial 。所以,现在我有大量的从 THREE.Geometry 构建的 THREE.Mesh 对象,但是有个人 THREE.MeshPhongMaterials (共享相同的纹理)。这允许我单独更改颜色,但性能更糟。 chrome的profilier显示,应用程序花了大量的时间做材料,如切换纹理。



材质颜色只是一个统一的着色器。

有一种方法可以覆盖网格层的材质颜色?



如果有,我相信我可以在所有对象之间共享材料,并让我的表演回来,而仍然单独改变颜色。



[我已在v49和v54上测试,它们具有相同的性能和降级]



更新:

这里有两个链接:





在第一种情况下,只有两种材料,在第二种情况下,每个立方体都有自己的材料。我在本机上测量第一种情况的帧率为53fps,第二种的帧率为46fps。这是大约15%的下降。



在这两种情况下,每个立方体的材质的颜色每帧改变。在许多材料的情况下,我们实际上看到每个立方体获得它自己的颜色,在只有两种材料的情况下,我们看到它们都具有相同的颜色(如预期)。

解决方案

是的。每个对象,使用 material.clone()克隆您的材料,修改其发射颜色,并将对象的材质设置为此克隆。着色器和属性通过引用被复制,所以不要担心你每次都克隆整个材质;事实上,通过值复制的唯一的东西是制服(例如 emissive color )。所以你可以改变每个单独的对象。



我个人将原始材料存储在一个单独的,对象的自定义属性,以便我以后可以轻松地切换回它;取决于你的需要。


I have a large number (~1000) of THREE.Mesh objects that have been constructed from the same THREE.Geometry and THREE.MeshPhongMaterial (which has a map).

I would like to tint (color) these objects individually.

Naïvely, I tried changing the mesh.material.color property, but changing this property on any of the objects changes the color of all the objects at once. This makes sense, since there is only one material that is shared among all the objects.

My next idea was to create a separate THREE.MeshPhongMaterial for each object. So, now I have a large number of THREE.Mesh objects that been constructed from the same THREE.Geometry, but have individual THREE.MeshPhongMaterials (that share the same texture). This allows me to change the colors individually, but the performance is worse. The chrome profilier shows that the app is spending significant time doing material-things like switching textures.

The material color is just a uniform in the shader. So, updating that uniform should be quite quick.

question: Is there a way to override a material color from the mesh level?

If there was, I believe I could share the material among all my objects and get my performance back, while still changing the colors individually.

[I have tested on v49 and v54, they have identical performance and degredation]

update: I have built a test case, and the performance drop due to this is smaller than I thought it was, but is still measurable.

Here are two links:

In the first case, there are only two materials, in the second case each cube has it's own material. I measure the framerate of the first case to be 53fps on this machine, and the framerate of the second is 46fps. This is about a 15% drop.

In both cases, the color of the material of every cube is changed every frame. In the case with many materials, we actually see each cube getting it's own color, in the case with only two materials, we see them all having the same color (as expected).

解决方案

Yes. Per object, clone your material using material.clone(), modify its emissive and color, and set the object's material to this clone. Shaders and attributes are copied by reference, so do not worry that you are cloning the entire material each time; in fact the only things that are copied by value are the uniforms (such as emissive and color). So you can change these per individual object.

Personally I store the original material on a separate, custom property of the object so that I can easily switch back to it later; depends what your needs are.

这篇关于许多网格具有相同的几何和材料,我可以改变他们的颜色吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆