简体   繁体   English

将裁剪添加到 THREE.ShaderMaterial

[英]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.MeshLambertMaterialTHREE.MeshPhongMaterialTHREE.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:所以,有几个问题:

  1. Should THREE.ShaderMaterial include Clipping Planes out of the box? THREE.ShaderMaterial 应该包括开箱即用的剪切平面吗? (there is a clipping property, but not sure what it enables) (有一个剪辑属性,但不确定它启用了什么)
  2. If not, how could I modify this shader to include the necessary params and shader chunks to enable clipping?如果没有,我如何修改此着色器以包含必要的参数和着色器块以启用剪辑?

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 个“着色器块”

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.它应该工作。

Check this fiddle .检查这个小提琴


Note笔记

To make your shader work, I also added begin_vertex.glsl and project_vertex.glsl .为了让你的着色器工作,我还添加了begin_vertex.glslproject_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.

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