Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using custom Vertex Shader with MeshLambertMaterial? #834

Closed
maurrubio opened this issue Dec 2, 2011 · 9 comments
Closed

Using custom Vertex Shader with MeshLambertMaterial? #834

maurrubio opened this issue Dec 2, 2011 · 9 comments
Labels

Comments

@maurrubio
Copy link

Hi I'm trying to do some kind of a custom shader, but I just want to modify the vertex position and not the material. MeshLambert already have light, texture and everything that I need, but I do modify the vertex do give an organic look do the object.

Someone can give me a direction?

Thanks !!

@maurrubio
Copy link
Author

Someone?

@alteredq
Copy link
Contributor

The easiest would be to create custom ShaderMaterial that would be like lambert, just adding your custom code to vertex shader.

Internally, standard materials use exactly the same machinery as ShaderMaterial, so you would just need to copy and paste code from here:

https://github.com/mrdoob/three.js/blob/master/src/renderers/WebGLShaders.js#L1187

@maurrubio
Copy link
Author

Hi @alteredq thanks for responding.

This is what you've refereed to?
http://www.interactiveuniverse.net/Shaders.js

I'm creating objects from the arrays that are provided in the lambert shader and passing them to a new ShaderMaterial instance.

Doing that I just get a black material without being affected by point lights.

@alteredq
Copy link
Contributor

Almost there ;)

For features from standard materials to work in ShaderMaterial you need also to set material flags (lights, fog, etc):

https://github.com/mrdoob/three.js/blob/master/src/materials/ShaderMaterial.js#L40

shaderMaterial = new THREE.ShaderMaterial( {

    uniforms:       uniforms,
    vertexShader:   vertexShader,
    fragmentShader: fragmentShader,
    lights:         true
    }); 

@maurrubio
Copy link
Author

It worked !!! Thank you so much, when I finish my work I will share with you guys.

@maurrubio maurrubio reopened this Dec 14, 2011
@maurrubio
Copy link
Author

Just one last question @alteredq the purpose of doing that was also using a environment map in the object so I can give it a texture. How I'm suppose to implement this kind of feature that MeshLambertMaterial contains?

@alteredq
Copy link
Contributor

Hmmm. I think it should be enough to set proper uniform (envMap) and also you would need to stick somewhere at the top of both vertex and fragment shaders #define USE_ENVMAP .

uniforms[ "envMap" ].texture = myCubeTexture;
vertexShader= [

"#define USE_ENVMAP",

"varying vec3 vLightWeighting;",

THREE.ShaderChunk[ "map_pars_vertex" ],
THREE.ShaderChunk[ "lightmap_pars_vertex" ],

...
fragmentShader = [

"#define USE_ENVMAP",

"uniform float opacity;",

...

Just be aware that if you modify vertex positions in a shader, normals coming from static geometry will not be anymore correct and everything will be still shaded according to old positions.

@maurrubio
Copy link
Author

It worked :D

You guys are really amazing, and I know that devs love to see what other people do with their tools, I hope I can finish my project soon ( It's my personal portfólio and I'm using websockets to connect users in a space interface with spheres ( each is a different project ) and users navigate through the space and each user is a different dynamic point light that moves across my works.

The engine is pretty functional now I need to work more on designing the stuff.

Thanks you again !

@dylnmc
Copy link

dylnmc commented Dec 18, 2017

@maurrubio did you finish your project 👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants