javascript - How do you "combine"a ShaderMaterial and LambertMaterial? - Stack Overflow

First, thank you so much for this great work and munity.I am trying to write a simple low poly style wa

First, thank you so much for this great work and munity.

I am trying to write a simple low poly style water material.

I know there are different ways of writing this kind of thing, and I would like as much as possible to do it in a js script (I e from C# dev', and I am not super fy with frontend yet).

The one way I can understand after hours of piling infos on the Net is this one:

var waterVertexShader =  
                "attribute float vertexDisplacement;" +
                "uniform float time;" +
                "varying vec3 vUv;" +
                "void main() {" +
                "   vUv = position;" +
                "   vUv.y += sin(time) * 0.01;" +
                "   gl_Position = projectionMatrix * modelViewMatrix * vec4(vUv, 1.0);"+     
                "}"
            ;

            var waterFragmentShader =

                "void main() {" +
                "gl_FragColor = vec4(0.65, 0.88, 1, 1);" +
                "}";


                },
                flatShading : true,
                vertexShader: waterVertexShader,

That gives me the beginning of the wanted behavior, but this is not my issue.

Now, if I want to inherit let's say the Lambert or Phong material properties, what is the right way to proceed?

It sounds very basic, but this is the only relevant link I found: ThreeJS - Extend Lambert Shader with Custom VertexShader and there is no answer.

Thank you for your attention.

First, thank you so much for this great work and munity.

I am trying to write a simple low poly style water material.

I know there are different ways of writing this kind of thing, and I would like as much as possible to do it in a js script (I e from C# dev', and I am not super fy with frontend yet).

The one way I can understand after hours of piling infos on the Net is this one:

var waterVertexShader =  
                "attribute float vertexDisplacement;" +
                "uniform float time;" +
                "varying vec3 vUv;" +
                "void main() {" +
                "   vUv = position;" +
                "   vUv.y += sin(time) * 0.01;" +
                "   gl_Position = projectionMatrix * modelViewMatrix * vec4(vUv, 1.0);"+     
                "}"
            ;

            var waterFragmentShader =

                "void main() {" +
                "gl_FragColor = vec4(0.65, 0.88, 1, 1);" +
                "}";


                },
                flatShading : true,
                vertexShader: waterVertexShader,

That gives me the beginning of the wanted behavior, but this is not my issue.

Now, if I want to inherit let's say the Lambert or Phong material properties, what is the right way to proceed?

It sounds very basic, but this is the only relevant link I found: ThreeJS - Extend Lambert Shader with Custom VertexShader and there is no answer.

Thank you for your attention.

Share Improve this question edited Feb 3, 2020 at 5:57 Edric 26.9k13 gold badges87 silver badges95 bronze badges asked Mar 13, 2019 at 15:30 GoupilSystemGoupilSystem 711 silver badge10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

There are several ways to enhance a built-in material in three.js:

  • You can use the callback Material.onBeforeCompile(). This approach is demonstrated in the following example. If you want to add just minor enhancements to the shader code, this should be your first choice.
  • Create a custom material with RawShaderMaterial or ShaderMaterial from scratch and reuse the existing shader chunks from the library. This approach is very flexible but it requires good knowledge about the internals of three.js's material system.
  • Of course you could modify/monkey patch the existing shader chunks but this approach is in general to remended (you effectively change the library code with all consequences).
  • Another approach is to use the promising but experimental NodeMaterial. However, this technique is not documented and no part of the core so far. There are some examples that demonstrate the usage like this one.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745564262a4633297.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信