Three.js,在网格之间共享ShaderMaterial但具有不同的统一集 [英] Three.js, sharing ShaderMaterial between meshes but with different uniform sets

查看:21
本文介绍了Three.js,在网格之间共享ShaderMaterial但具有不同的统一集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我想为不同的网格重用给定的 ShaderMaterial,但每个网格有一组不同的制服(实际上,有些制服可能因网格而异,但不一定是全部):是可能吗?

As the title says, I would like to reuse a given ShaderMaterial for different meshes, but with a different set of uniforms for each mesh (in fact, some uniforms may vary between meshes, but not necessarily all of them): is it possible ?

在这种情况下必须为每个网格创建一个完整的 ShaderMaterial 对我来说似乎是一种资源浪费,这个想法是有一个单一的顶点/片段着色器程序,但通过不同的制服来配置它,其值会根据在网格上.如果我为每个网格创建一个新的 ShaderMaterial,我最终会得到很多重复(顶点 + 片段程序 + Material/ShaderMaterial 类的所有其他数据成员).

It seems a waste of resources to me to have to create a full ShaderMaterial for each mesh in this circumstance, the idea being to have a single vertex/fragment shader program but to configurate it through different uniforms, whose values would change depending on the mesh. If I create a new ShaderMaterial for each mesh, I will end up with a lots of duplications (vertex+fragment programs + all other data members of the Material / ShaderMaterial classes).

如果引擎能够在绘制网格之前调用回调,我就可以更改制服并实现我想要做的事情.另一种可能性是拥有一个LiteShaderMaterial",它会保存一个指向共享 ShaderMaterial 的指针 + 仅我的网格的特定制服.

If the engine was able to call a callback before drawing a mesh, I could change the uniforms and achieve what I want to do. Another possibility would be to have a "LiteShaderMaterial" which would hold a pointer to the shared ShaderMaterial + only the specific uniforms for my mesh.

请注意,我的问题与此有关 许多具有相同几何形状和材料的网格,我可以更改它们的颜色吗? 但仍然不同,因为我最关心的是资源浪费 - 性能方面我不知道认为拥有多个 ShaderMaterial 或单个 ShaderMaterial 不会有太大不同,因为引擎应该足够聪明,可以注意到所有材质都具有相同的程序,并且不会将它们重新发送到 gfx 卡.

Note that my question is related to this one Many meshes with the same geometry and material, can I change their colors? but is still different, as I'm mostly concerned about the waste of resources - performance wise I don't think it would be a lot different between having multiple ShaderMaterial or a single one, as the engine should be smart enough to note that all materials have the same programs and don't resend them to the gfx card.

谢谢

推荐答案

在克隆 ShaderMaterial 时,属性和顶点/片段程序是通过引用复制的.只有制服是按值复制的,这就是你想要的.

When cloning a ShaderMaterial, the attributes and vertex/fragment programs are copied by reference. Only the uniforms are copied by value, which is what you want.

这应该有效.

您可以通过创建一个 ShaderMaterial 然后使用 ShaderMaterial.clone() 为每个网格克隆它来向自己证明这一点.然后为每个材料分配唯一的统一值.

You can prove it to yourself by creating a ShaderMaterial and then using ShaderMaterial.clone() to clone it for each mesh. Then assign each material unique uniform values.

在控制台输入render.info".它应该显示 1 个程序.

In the console type "render.info". It should show 1 program.

three.js r.64

three.js r.64

这篇关于Three.js,在网格之间共享ShaderMaterial但具有不同的统一集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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