[英]Add clipping to THREE.ShaderMaterial
I'm trying to create a shader that takes into account the clipping planes I'm defining in the scene.我正在尝试创建一个着色器,该着色器考虑到我在场景中定义的剪裁平面。 These clipping planes work fine for all of the 'vanilla' materials I'm using:
THREE.MeshLambertMaterial
, THREE.MeshPhongMaterial
, and THREE.MeshPhysicalMaterial
, but THREE.ShaderMaterial
is missing this implementation.这些剪切平面适用于我正在使用的所有“普通”材质:
THREE.MeshLambertMaterial
、 THREE.MeshPhongMaterial
和THREE.MeshPhysicalMaterial
,但THREE.ShaderMaterial
缺少此实现。 This is an example of what I mean: https://jsfiddle.net/fraguada/27LrLsv5/这是我的意思的一个例子: https : //jsfiddle.net/fraguada/27LrLsv5/
In this example there are two cubes, one with a THREE.MeshStandardMaterial
and one with a material defined by THREE.ShaderMaterial
.在此示例中,有两个立方体,一个具有
THREE.MeshStandardMaterial
,另一个具有由THREE.ShaderMaterial
定义的THREE.ShaderMaterial
。 The cube with a THREE.MeshStandardMaterial
clips ok.带有
THREE.MeshStandardMaterial
的立方体可以剪辑。 The cube with THREE.ShaderMaterial does not clip.带有 THREE.ShaderMaterial 的立方体不会剪裁。
(I'm not typically defining the vertex/fragment shader in script tags as I show in the jsfiddle, instead I'm defining them in a similar manner to this: https://github.com/mrdoob/three.js/blob/dev/examples/js/shaders/BasicShader.js .) (我通常不会像在 jsfiddle 中显示的那样在脚本标签中定义顶点/片段着色器,而是以与此类似的方式定义它们: https : //github.com/mrdoob/three.js/blob /dev/examples/js/shaders/BasicShader.js 。)
So, a few questions:所以,有几个问题:
Actually, clipping is done inside the Three.js shaders .实际上,裁剪是在Three.js着色器中完成的。
To make it work, you have to handle it inside your shader, by adding those 4 "shader chunks" :为了让它工作,你必须在你的着色器中处理它,通过添加这 4 个“着色器块” :
clipping_planes_pars_vertex.glsl
at the top of your vertex shader ; clipping_planes_pars_vertex.glsl
在你的顶点着色器的顶部;clipping_planes_vertex.glsl
inside the main()
of your vertex shader ; clipping_planes_vertex.glsl
在你的顶点着色器的main()
中;clipping_planes_pars_fragment.glsl
at the top of your fragment shader ; clipping_planes_pars_fragment.glsl
在片段着色器的顶部;clipping_planes_fragment.glsl
inside the main()
of your fragment shader. clipping_planes_fragment.glsl
在片段着色器的main()
中。 You can access those chunks by simply adding #include <(chunk name)>
to your shaders.您可以通过简单地将
#include <(chunk name)>
到您的着色器来访问这些块。
Then, set material.clipping = true;
然后,设置
material.clipping = true;
and it should work.它应该工作。
Note笔记
To make your shader work, I also added begin_vertex.glsl
and project_vertex.glsl
.为了让你的着色器工作,我还添加了
begin_vertex.glsl
和project_vertex.glsl
。
I checked the current phong shader implementation to understand where to put those chunks.我检查了当前的phong 着色器实现以了解将这些块放在哪里。
Note 2笔记2
This code should work with a shader implemented with an array of strings but note that you can also reference shader chunks with THREE.ShaderChunk[(chunk name)]
.此代码应与使用字符串数组实现的着色器一起使用,但请注意,您还可以使用
THREE.ShaderChunk[(chunk name)]
引用着色器块。
This should be more suitable in your case.这应该更适合您的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.