# Shadows for transparent textures

**URL:** https://forum.castle-engine.io/t/shadows-for-transparent-textures/1020
**Category:** 3D
**Created:** [December 9, 2023, 3:13pm UTC](https://forum.castle-engine.io/t/shadows-for-transparent-textures/1020 "2023-12-09T15:13:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![MBa](https://forum.castle-engine.io/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@MBa](https://forum.castle-engine.io/u/MBa)
#### Post date: [December 9, 2023, 3:13pm UTC](https://forum.castle-engine.io/t/shadows-for-transparent-textures/1020/1 "2023-12-09T15:13:13Z")

</div>

Michalis said in another topic: “Having lots of 3D shapes, each with 2 quads arranged to form a cross, will be both easier to do and faster to render.”  
I am now working with cross-quads to draw grass, as I asked. The textures are transparent. However, when I activate the shadow, it simply draws the quad as the shadow and not the transparent blades of grass. So, the shadow is rectangular and does not show the blades.  
Is there an example for this?

---

<div class="post-metadata">

### Author: ![MBa](https://forum.castle-engine.io/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@MBa](https://forum.castle-engine.io/u/MBa)
#### Post date: [December 21, 2023, 7:20pm UTC](https://forum.castle-engine.io/t/shadows-for-transparent-textures/1020/2 "2023-12-21T19:20:34Z")

</div>

A second question in this context would be: when I create a cube in a scene, the back cubes are actually hidden. However, when I use a PNG texture that has transparent areas (for the grass), grass behind it is sometimes drawn in front of the foreground grass. Do I need to enable different depth checks compared to the cube, just because I’m using transparent textures?

---

<div class="post-metadata">

### Author: ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)
#### Post date: [December 23, 2023, 11:46pm UTC](https://forum.castle-engine.io/t/shadows-for-transparent-textures/1020/3 "2023-12-23T23:46:50Z")

</div>

> [@MBa](#):
>
> I am now working with cross-quads to draw grass, as I asked. The textures are transparent. However, when I activate the shadow, it simply draws the quad as the shadow and not the transparent blades of grass. So, the shadow is rectangular and does not show the blades.

That is indeed the unavoidable problem of the “shadow volumes” approach, which I understand you use, as it is used when you just toggle the `Shadows` checkbox on a light source. See [Shadow Volumes | Manual | Castle Game Engine](https://castle-engine.io/shadow_volumes) , in particular sentence there _" Applying a texture with transparency (alpha testing) on the object has no effect on the shadows cast."_ .

This is unfortunately not fixable with shadow volumes, that necessarily need to follow the geometry.

We have alternative shadows algorithm, shadow maps, [Shadow Maps | Castle Game Engine](https://castle-engine.io/x3d_extensions_shadow_maps.php) , that doesn’t have this limitation. The textures with alpha (using alpha testing) are then accounted OK, e.g. tree leaves can cast correct shadow.

But using “shadow maps” in CGE is right now much harder. You need to add a light source, to the same scene (TCastleScene) where the shadow casting objects (like grass) are, as X3D light node.

Exposing the shadow maps algorithm better is a work in progress. I’d like to have a simple `ShadowsMethod` choice at each light source, and just allow you to choose shadow volumes or shadow maps, but for now this still requires some work to be possible – namely, our shadow maps should be able to cast from one scene to another, this is tracked in ticket [MainScene's light source does not cast shadow (shadow maps) to other scenes · Issue #284 · castle-engine/castle-engine · GitHub](https://github.com/castle-engine/castle-engine/issues/284) .

So, unfortunately, the simple answer to this is that there’s no easy fix. You can change your approach to use shadow maps, but that will be a complicated change for now, you can follow the docs from [Shadow Maps | Castle Game Engine](https://castle-engine.io/x3d_extensions_shadow_maps.php) . I want to make it trivial in the future in CGE, but this takes time.

> [@MBa](#):
>
> A second question in this context would be: when I create a cube in a scene, the back cubes are actually hidden. However, when I use a PNG texture that has transparent areas (for the grass), grass behind it is sometimes drawn in front of the foreground grass. Do I need to enable different depth checks compared to the cube, just because I’m using transparent textures?

I don’t understand this issue completely. If you have a simple testcase (that can be run) it would be helpful.

Is it possible you use blending for your grass texture? In this case, see [Blending (Rendering Partially-Transparent Objects) | Manual | Castle Game Engine](https://castle-engine.io/blending) how blending works. The simplest advise is to not use blending for the grass, you should use alpha testing, that doesn’t require any sorting. How to set it up, depends on how you construct your grass – if you do it in Blender, set “Blend Mode” to “Alpha Clip”. If you set it up in code, set `TApperanceNode.AlphaMode` to `amMask`.

But it is possible my advise is wrong here – it would help if you can explain / demonstrate the problem better, so that I can better advise 🙂

---

<div class="post-metadata">

### Author: ![MBa](https://forum.castle-engine.io/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@MBa](https://forum.castle-engine.io/u/MBa)
#### Post date: [December 24, 2023, 3:05pm UTC](https://forum.castle-engine.io/t/shadows-for-transparent-textures/1020/4 "2023-12-24T15:05:21Z")

</div>

Blending is working now. Thank you so much for informing me about that switch. It was easy enough to fix 😉  
Regarding the shadows: I might wait for your engine to have a more straightforward approach. I’m not sure if I would be able to get “shadow mapping” to work.  
Thanks again.
