# Custom transparent ground that receives shadows, show mirror and use diffuseTexture

**URL:** <https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066>\
**Category:** Questions\
**Created:** [October 20, 2020, 4:35pm UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066 "2020-10-20T16:35:35Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![samevision](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@samevision](https://forum.babylonjs.com/u/samevision)\
**Post date:** [October 20, 2020, 4:35pm UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/1 "2020-10-20T16:35:35Z")

</div>

Hello guys,

I am facing again a situation I can’t solve on my own. I try to combine several things into one plane material.

The ground should:

1. … be transparent.
2. … be able to receive shadows.
3. … be able to mirror.
4. … be able to show a diffuseTexture with alpha.

It’s exactly the setup of the default environment ([https://www.babylonjs-playground.com/#10D6YT#35](https://www.babylonjs-playground.com/#10D6YT#35)) just with transparent ground.

I created a playground here: [https://playground.babylonjs.com/#9KLUBA#1](https://playground.babylonjs.com/#9KLUBA#1)

Thank you very much!!

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [October 20, 2020, 5:43pm UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/2 "2020-10-20T17:43:05Z")

</div>

pinging @Evgeni_Popov

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 20, 2020, 8:03pm UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/3 "2020-10-20T20:03:45Z")

</div>

You should set `useAlphaFromDiffuseTexture` to `true` for the ground material to enable alpha blending:

[https://playground.babylonjs.com/#9KLUBA#2](https://playground.babylonjs.com/#9KLUBA#2)

---

<div class="post-metadata">

**Author:** ![samevision](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@samevision](https://forum.babylonjs.com/u/samevision)\
**Post date:** [October 20, 2020, 9:09pm UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/4 "2020-10-20T21:09:37Z")

</div>

Thank you. That works great.

Am I able to easily disable the diffuse texture while keeping shadows and mirror on transparent ground? Or do I have to replace the diffuse texture to imitate a transparent effect?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 21, 2020, 9:13am UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/5 "2020-10-21T09:13:09Z")

</div>

You have to either use a diffuse texture with transparency or an opacity texture to get transparent effect in your use case.

---

<div class="post-metadata">

**Author:** ![samevision](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@samevision](https://forum.babylonjs.com/u/samevision)\
**Post date:** [October 21, 2020, 9:58am UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/6 "2020-10-21T09:58:53Z")

</div>

I tried it with a transparent diffuseTexture. The problem is that the ground gets completely transparent. So the mirror as well as the shadow are not visible anymore.

EDIT:

In my project it’s woks after setting alpha mode to multiply. In playground the ground is still visible though: [https://playground.babylonjs.com/#9KLUBA#3](https://playground.babylonjs.com/#9KLUBA#3)

Any chance to keep the mirror if the light is shining on it?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 21, 2020, 10:31am UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/7 "2020-10-21T10:31:51Z")

</div>

I think what you want is to keep the shadow and the mirror and have everything else transparent?

Maybe this will work for you:

[https://playground.babylonjs.com/#9KLUBA#4](https://playground.babylonjs.com/#9KLUBA#4)

If the `shadow` variable is strictly less than 1 it means the pixel is at least partly in shadow. And the `if (color.r != 0.2 || color.g != 0.2 || color.b != 0.3)` check is to test against the clear color.

---

<div class="post-metadata">

**Author:** ![samevision](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@samevision](https://forum.babylonjs.com/u/samevision)\
**Post date:** [October 21, 2020, 10:56am UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/8 "2020-10-21T10:56:51Z")

</div>

Thanks for your effort. With just the clearColor background it looks transparent but unfortunately it isn’t.  
[https://playground.babylonjs.com/#9KLUBA#5](https://playground.babylonjs.com/#9KLUBA#5)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 21, 2020, 12:08pm UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/9 "2020-10-21T12:08:08Z")

</div>

My mistake, testing for the clear color is wrong as it is not the color computed in the shader.

One way to do it would be to clear the mirror with an alpha=0 color and use this alpha value as the final alpha value of the shader, except when shadow is \< 1:

[https://playground.babylonjs.com/#9KLUBA#6](https://playground.babylonjs.com/#9KLUBA#6)

---

<div class="post-metadata">

**Author:** ![samevision](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@samevision](https://forum.babylonjs.com/u/samevision)\
**Post date:** [October 22, 2020, 7:11am UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/10 "2020-10-22T07:11:50Z")

</div>

We are getting close. I put all codes together and now I am able to show all combinations:

- gradient
- gradient, shadow
- gradient, mirror
- gradient, shadow, mirror
- shadow
- shadow, mirror
- mirror

I encounter two weird behaviours though:

- If using the custom material to create the combination “shadow, gradient” the shadow breaks after enabling blur.
- The mirror is mirrored 😀 This is only after appending a glb model and adjusting scale and position.

[https://playground.babylonjs.com/#9KLUBA#10](https://playground.babylonjs.com/#9KLUBA#10)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 22, 2020, 8:48am UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/11 "2020-10-22T08:48:07Z")

</div>

> [@samevision](#):
>
> If using the custom material to create the combination “shadow, gradient” the shadow breaks after enabling blur.

It’s because `blurScale` and/or `blurKernel` is too high: lower one of them (or both) to correct the problem.

> [@samevision](#):
>
> The mirror is mirrored 😀 This is only after appending a glb model and adjusting scale and position.

You should enable backface culling on the material of the sphere to get rid of the artifacts:

> **[Babylon.js Playground](https://playground.babylonjs.com/#9KLUBA%2311)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![samevision](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@samevision](https://forum.babylonjs.com/u/samevision)\
**Post date:** [October 22, 2020, 9:52am UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/12 "2020-10-22T09:52:44Z")

</div>

Thanks again for your help.

Backface culling works fine. But setting the blurScale or blurKernel down corrects the shadow in terms of size but it is not blurry anymore. With blurScale = 4 and blurKernel = 64 it should look like this:

 ![Screenshot 2020-10-22 at 11.52.16](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/1/19189bc2e110614b342396e467ff82f3e28982e0.png)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 22, 2020, 10:54am UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/13 "2020-10-22T10:54:19Z")

</div>

That’s because I set a straight black when in shadow: `color = vec4(0., 0., 0., 1.);`

Improved PG:

[https://playground.babylonjs.com/#9KLUBA#12](https://playground.babylonjs.com/#9KLUBA#12)

---

<div class="post-metadata">

**Author:** ![samevision](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@samevision](https://forum.babylonjs.com/u/samevision)\
**Post date:** [October 22, 2020, 11:00am UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/14 "2020-10-22T11:00:54Z")

</div>

This is perfect. Thank you!!

---

<div class="post-metadata">

**Author:** ![samevision](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@samevision](https://forum.babylonjs.com/u/samevision)\
**Post date:** [October 31, 2020, 1:39pm UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/15 "2020-10-31T13:39:21Z")

</div>

I am sorry, but I have to come back to this thread.

I noticed two things:

1. The initial problem with smooth shadow still exists in the mirror reflection of the sphere ([https://playground.babylonjs.com/#9KLUBA#13](https://playground.babylonjs.com/#9KLUBA#13)).

2. It is not possible to combine the shadow of multiple lights ([https://playground.babylonjs.com/#9KLUBA#14](https://playground.babylonjs.com/#9KLUBA#14)). I had the same problem before with shadowOnlyMaterial. Earlier you helped me solving this with using backgroundMaterial like this:

Unfortunately that doesn’t work while using mirror as well.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 31, 2020, 2:25pm UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/16 "2020-10-31T14:25:05Z")

</div>

Here’s the best I can do for #1: [https://playground.babylonjs.com/#9KLUBA#16](https://playground.babylonjs.com/#9KLUBA#16)

Regarding #2, unfortunately `globalShadow` does not exist in the standard material. But with a bit of work we can reimplement it:

[https://playground.babylonjs.com/#9KLUBA#17](https://playground.babylonjs.com/#9KLUBA#17)

---

<div class="post-metadata">

**Author:** ![samevision](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@samevision](https://forum.babylonjs.com/u/samevision)\
**Post date:** [October 31, 2020, 4:57pm UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/17 "2020-10-31T16:57:01Z")

</div>

Thanks again. That is really helpful.

#1 It couldn’t be better 🙂

#2 This works great. Is there a way to make the shadows stronger similar to this backgroundMaterial shadow? [https://playground.babylonjs.com/#9KLUBA#18](https://playground.babylonjs.com/#9KLUBA#18)

#3 Do you know why this happens when shadow blur is disabled? [https://playground.babylonjs.com/#9KLUBA#19](https://playground.babylonjs.com/#9KLUBA#19)

 ![Screenshot 2020-10-31 at 18.01.56](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/b/b4dd6845634cd01301d85e655c9c8b19427d2460.png)

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 31, 2020, 6:26pm UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/18 "2020-10-31T18:26:18Z")

</div>

> [@samevision](#):
>
> #2 This works great. Is there a way to make the shadows stronger similar to this backgroundMaterial shadow? [https://playground.babylonjs.com/#9KLUBA#18](https://playground.babylonjs.com/#9KLUBA#18)

Yes, in the line:

```glsl
color = mix(vec4(0., 0., 0., 1. - globalShadow),
            vec4(color.rgb, reflectionColor.a),
            pow(globalShadow, 0.25));

```

Raise the value `0.25`. Try `2.0` for eg.

> [@samevision](#):
>
> #3 Do you know why this happens when shadow blur is disabled?

It’s the bias value of the shadow generators. Try to lower it:

> **[Babylon.js Playground](https://playground.babylonjs.com/#9KLUBA%2320)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![samevision](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@samevision](https://forum.babylonjs.com/u/samevision)\
**Post date:** [November 1, 2020, 8:11am UTC](https://forum.babylonjs.com/t/custom-transparent-ground-that-receives-shadows-show-mirror-and-use-diffusetexture/15066/19 "2020-11-01T08:11:57Z")

</div>

I am happy. Thank you for your help!
