Jump to content

Custom material with shadows


Recommended Posts

I'm using a custom material which is pretty similar to the ground material used in WorldMonger demo.

The problem is that I don't get a shadows on it. Well it's not a big surprise, because I'm overriding the _effect property of the base material.

The question is how can I get both my effect with a base material effect without copy-pasting the code from base material.

I mean how can I get shadows while using additional effects?

Link to comment
Share on other sites

If you want shadows in your own shader, you have to use the light.getShadowGenerator().getShadowMap() shadowMap


Then do like in this shader:


float computeShadow(vec4 vPositionFromLight, sampler2D shadowSampler, float darkness){	vec3 depth = vPositionFromLight.xyz / vPositionFromLight.w;	vec2 uv = 0.5 * depth.xy + vec2(0.5, 0.5);	if (uv.x < 0. || uv.x > 1.0 || uv.y < 0. || uv.y > 1.0)	{		return 1.0;	}	float shadow = unpack(texture2D(shadowSampler, uv));	if (depth.z > shadow)	{		return darkness;	}	return 1.;}
Link to comment
Share on other sites

  • 1 year later...


Following this topic, I'm trying to get shadows in my custom shader, but can't manage to make it work.

I setup a shadowGenerator and add the mesh to the render list. It know it works ok because with a StandardMaterial I can see the shadows.


With my ShaderMaterial, I pass a mat4 lightMatrix0, a sampler2D shadowSampler0 and a vec3 shadowsInfo0 (I only want one light)


I feed this data in the next way:

customMaterial.setTexture("shadowSampler0", light.getShadowGenerator().getShadowMapForRendering());customMaterial.setMatrix("lightMatrix0", light.getShadowGenerator().getTransformMatrix());customMaterial.setVector3("shadowsInfo0", light.getShadowGenerator().getDarkness(), light.getShadowGenerator().getShadowMap().getSize().width, light.getShadowGenerator().bias);

With this uniforms I feed the computeShadow function, as in the default shader code.


I have tested lightMatrix0 and shadowsInfo0 have the correct values. But nothing is displaying. The returned value of computeShadow is always 1.0


Any idea on what am I missing? I have tried with .getShadowMapForRendering() and getShadowMap() and nothing works.


Any clue?

Link to comment
Share on other sites

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...