# Shadow map - how to properly create a render target? (SOLVED)

**URL:** <https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816>\
**Category:** Questions\
**Created:** [August 19, 2024, 9:57am UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816 "2024-08-19T09:57:27Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Dragosha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dragosha/32/5103_2.png) [@Dragosha](https://forum.defold.com/u/Dragosha)\
**Post date:** [August 19, 2024, 6:14pm UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/21 "2024-08-19T18:14:51Z")

</div>

> [@Pawel](#):
>
> how would you describe

In simple case by steps:

1. Set the camera at the point where the ‘shadow source’ is and aim the camera in the desired direction. Camera is an abstract concept, in 3d graphics it is matrices.
2. Render the view from this camera on all objects that cast a shadow, but put in the texture is not a picture, but the distance from the source of the shadow to the point of the object.  
[defold-light-and-shadows/light\_and\_shadows/materials/shadow/shadow.fp at main · Dragosha/defold-light-and-shadows · GitHub](https://github.com/Dragosha/defold-light-and-shadows/blob/main/light_and_shadows/materials/shadow/shadow.fp#L16)
3. Get a texture with a ‘depth map’ in the ‘shadow source’ coordinate system or shadowmap as a result.  
[defold-light-and-shadows/light\_and\_shadows/light\_and\_shadows.lua at main · Dragosha/defold-light-and-shadows · GitHub](https://github.com/Dragosha/defold-light-and-shadows/blob/main/light_and_shadows/light_and_shadows.lua#L142)
4. Calculate a special matrix of our camera ‘shadow source’, multiplying by which we can translate any coordinate of the world into the coordinate system of the camera ‘shadow source’.  
[defold-light-and-shadows/light\_and\_shadows/light\_and\_shadows.lua at main · Dragosha/defold-light-and-shadows · GitHub](https://github.com/Dragosha/defold-light-and-shadows/blob/main/light_and_shadows/light_and_shadows.lua#L107)
5. Switch to a regular camera and render the world again, but in its whole form. In the shader we pass the texture with shadowmap and a special matrix from the previous points.
6. In the vertex shader we translate 3d coordinates of the vertex into 2d coordinates on the screen. And additionally we translate into 2d coordinates in the shadowmap (multiplied by the special matrix).  
[https://github.com/Dragosha/defold-light-and-shadows/blob/main/light\_and\_shadows/materials/model/model.vp#L33](https://github.com/Dragosha/defold-light-and-shadows/blob/main/light_and_shadows/materials/model/model.vp#L33)
7. In the fragment shader we compare the depth of our fragment with the value from the shadowmap texture. And this is where the ‘magic’ happens. If the value is greater, then our fragment was far away from the ‘shadow source’ than some other fragment, i.e. it is in the shadow. Something was nearer to the light bulb and we colour this pixel with the shadow in mind. This is what we will find out in step 2. If the value is smaller, it means that our pixel does not overshadow any object and it is in the light zone.  
[defold-light-and-shadows/light\_and\_shadows/materials/model/model.fp at main · Dragosha/defold-light-and-shadows · GitHub](https://github.com/Dragosha/defold-light-and-shadows/blob/main/light_and_shadows/materials/model/model.fp#L73)
8. Going for a beer to rethink the above.

---

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [August 19, 2024, 7:45pm UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/22 "2024-08-19T19:45:26Z")

</div>

Thank you very much @Dragosha ! ❤

**Regarding 1. :**  
Do you have some tips on how to define view and projection matrices for given source of light? Camera component gives me matrices, but am I thinking correctly if I think about utilizing some cameras placed in point\_light.go just for the purpose of giving me matrices? OR is there a better approach?

EDIT: Ok, both Lights and Shadows and Jhonny’s Shadow mapping example create those matrices in code. I wonder if my idea of utilising a camera component for this is impossible/nonoptimal to use? Isn’t the camera component mainly for this - doing a hard work of giving you the correct matrices from what you can set up in the Editor?

---

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [August 19, 2024, 7:55pm UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/23 "2024-08-19T19:55:35Z")

</div>

And regarding this:

> [@jhonny.goransson](#):
>
> Have you tried the RT resource instead?

@jhonny.goransson is there any advantage of this over creating it in init()? I see in @Dragosha Lights and Shadows the render target for shadowmap is created in this way - there is a “shadowmap” render target resource and it’s linked to the render (and then used in render script):

 ![image](https://forum-defold-uploads.b-cdn.net/original/3X/f/b/fbce2f8f7cbab14e1c1b655fd2a0455c08ab96eb.png)

I can see that width and height are defined here, so it can’t be initialized with initial window size. Either has to be at least same as the window size from game.project, or it should be recreated in render script when window size changes.

Beside that, one can add as many color attachments as we want in Render Target Editor, but from what I know, we can only create up to 4 color attachments using API - is it a bug?

 ![image](https://forum-defold-uploads.b-cdn.net/original/3X/7/2/725df6d68b97150a0fc73fd5096b73c77103b62f.png)

---

<div class="post-metadata">

**Author:** ![Dragosha](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/dragosha/32/5103_2.png) [@Dragosha](https://forum.defold.com/u/Dragosha)\
**Post date:** [August 20, 2024, 5:47am UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/24 "2024-08-20T05:47:58Z")

</div>

> [@Pawel](#):
>
> I can see that width and height are defined here, so it can’t be initialized with initial window size. E

It doesn’t seem to matter where exactly to make RT, I just tried to see how RT creation will work in the editor, after adding this feature in some version of Defold, before that RT was created in code.  
The thing is that the size of Shadow map RT does not depend directly on the size of the window, they are not even in the window proportions, it is its own view from the light source, it is projected on the view of the regular camera. But this resolution affects the accuracy with which shadows are drawn. The higher the RT resolution, the less ‘laddering’, especially if the light source is at a high angle to the surface in the regular camera view. There is such a thing as bias in the code to eliminate the moire effect (acne), several passes over the shadow map and averaging with noise, all this is done just to improve the final look of the shadow, to make it less ugly.

As for using camera component to get matrices, I guess you can, why not, I haven’t tried to use its new version with extended API in my work yet.

It is also interesting to look at materials on this topic, besides explaining the principles of work, there is the main thing - illustrations.

> **[Tutorial 16 : Shadow mapping](https://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/)**
>
> Free tutorials for modern Opengl (3.3 and later) in C/C++

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [August 20, 2024, 6:31am UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/25 "2024-08-20T06:31:27Z")

</div>

> [@Pawel](#):
>
> Do you have some tips on how to define view and projection matrices for given source of light?

I’m using little bit easier and lazy(cause I’m) way for light’s point of view calculation.

```lua
local light_position = vmath.vector3(37.0, 50.0, 12.0) -- light current position -> go.get_position('/light')|
local light_look_at = vmath.vector3(0, 0, 0) -- light target pos -> go.get_position('/light_target')|
local up = vmath.vector3(0, 1, 0)|
self.light_transform = vmath.matrix4_look_at(light_position, light_look_at, up)|
render.set_view(self.light_transform)|

```

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [August 20, 2024, 8:08am UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/26 "2024-08-20T08:08:00Z")

</div>

> [@selimanac](#):
>
> I’m using little bit easier and lazy(cause I’m) way for light’s point of view calculation.

And I got the shadows 😃

 ![Screenshot 2024-08-20 at 11.06.51](https://forum-defold-uploads.b-cdn.net/original/3X/3/6/3606f76aaf46ad0b3d835de280b642fcd6c50cd8.png)

---

<div class="post-metadata">

**Author:** ![jhonny.goransson](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jhonny.goransson/32/3864_2.png) [@jhonny.goransson](https://forum.defold.com/u/jhonny.goransson)\
**Post date:** [August 20, 2024, 8:34am UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/27 "2024-08-20T08:34:36Z")

</div>

> [@Pawel](#):
>
> @jhonny.goransson is there any advantage of this over creating it in init()

Yes, the advantage is that since it is a resource it can act as a texture elsewhere in the engine. Meaning you can attach it in the editor to a model for example, so you can create debug panels or simplify writing effects. Or wrap it into an atlas that can be used for particles or sprites.

An RT created in a render script needs to be either used only in the render script, or passed into a script to be wrapped into a texture resource which is just more tedious.

In a shadow mapping example perhaps it doesn’t make things that much easier, except for the creation, but RTs can be used for so much more than just shadow mapping.

In my example we didn’t have this functionality nor the camera api, if I did it today I’d use both. But you don’t have to 🙂

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [August 20, 2024, 8:40am UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/28 "2024-08-20T08:40:47Z")

</div>

> [@jhonny.goransson](#):
>
> Yes, the advantage is that since it is a resource it can act as a texture elsewhere in the engine.

Any ‘know’ performance differences between using render resources for RT(using it elsewhere) and creating in render script?

---

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [August 20, 2024, 8:47am UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/29 "2024-08-20T08:47:12Z")

</div>

> [@jhonny.goransson](#):
>
> Meaning you can attach it in the editor to a model for example, so you can create debug panels or simplify writing effects. Or wrap it into an atlas that can be used for particles or sprites.

Nice indeed! One can then display anything on sprite or model, e.g. make virtual banner/tv screen or anything like this with dynamic texture like a view from a surveillance camera 🤩 or mirrors! 😅

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [August 20, 2024, 8:49am UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/30 "2024-08-20T08:49:25Z")

</div>

> [@Pawel](#):
>
> One can then display anything on sprite or model, e.g. make virtual banner/tv screen or anything like this with dynamic texture like a view from a surveillance camera

Or mini map 🙂 (just posting again for future searches)

> **[my-public-defold-examples/render-target-resource at master ·...](https://github.com/Jhonnyg/my-public-defold-examples/tree/master/render-target-resource)**
>
> Contribute to Jhonnyg/my-public-defold-examples development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![jhonny.goransson](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jhonny.goransson/32/3864_2.png) [@jhonny.goransson](https://forum.defold.com/u/jhonny.goransson)\
**Post date:** [August 20, 2024, 8:50am UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/31 "2024-08-20T08:50:46Z")

</div>

Not really no, it’s one more resource to load but using it in the engine is no different than creating from a render script

---

<div class="post-metadata">

**Author:** ![jhonny.goransson](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jhonny.goransson/32/3864_2.png) [@jhonny.goransson](https://forum.defold.com/u/jhonny.goransson)\
**Post date:** [August 20, 2024, 8:52am UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/32 "2024-08-20T08:52:26Z")

</div>

> [@Pawel](#):
>
> Nice indeed! One can then display anything on sprite or model, e.g. make virtual banner/tv screen or anything like this with dynamic texture like a view from a surveillance camera 🤩 or mirrors! 😅

Yes exactly. And I think we want to add a “target” property to cameras that can take RTs, so you can hook most of these thing up in the editor without using render scripts

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [August 20, 2024, 2:32pm UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/33 "2024-08-20T14:32:01Z")

</div>

> [@jhonny.goransson](#):
>
> Yeah it looks correct! It’s ok, but if you want you can open a ticket and I’ll take a look later when I’m back from vacation

hmm… new [graphics](https://defold.com/ref/stable/graphics/) module doesn’t have `.TEXTURE_BIT` or I can’t find the equivalent…

---

<div class="post-metadata">

**Author:** ![jhonny.goransson](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jhonny.goransson/32/3864_2.png) [@jhonny.goransson](https://forum.defold.com/u/jhonny.goransson)\
**Post date:** [August 20, 2024, 2:51pm UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/34 "2024-08-20T14:51:00Z")

</div>

No, because it’s not part of the graphics API, it’s a render enum still

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [August 20, 2024, 2:54pm UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/35 "2024-08-20T14:54:41Z")

</div>

Ah! ok, sorry

---

<div class="post-metadata">

**Author:** ![jhonny.goransson](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jhonny.goransson/32/3864_2.png) [@jhonny.goransson](https://forum.defold.com/u/jhonny.goransson)\
**Post date:** [August 20, 2024, 2:58pm UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/36 "2024-08-20T14:58:15Z")

</div>

Yeah no problem, I was debating wether or not to add it to the graphics api but decided not to, at least not yet

---

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [August 20, 2024, 5:12pm UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/37 "2024-08-20T17:12:43Z")

</div>

Beside LearnOpenGL, there is also a great video (and whole tutorial series):

Shadow-mapping:

[![](https://img.youtube.com/vi/kCCsko29pv0/maxresdefault.jpg "Basic Shadow Mapping // OpenGL Tutorial #35") ](https://www.youtube.com/watch?v=kCCsko29pv0)

---

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [August 20, 2024, 5:32pm UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/38 "2024-08-20T17:32:54Z")

</div>

> [@jhonny.goransson](#):
>
> nor the camera api, if I did it today I’d use both.

@jhonny.goransson Camera component looks great for spotlight and directional light (orthographic projection), but afaik we don’t have yet a possibility to get a frustum from camera API, right? Is it planned?

Another crucial thing to fully use camera components is that when there is more than one camera sending `"set_view_projection"` messages to the render script - we get its _id_ only, not a whole _URL_. I noticed it, because usually I don’t name components uniquely, so I had 2 game objects, with same components `"camera"` and both messages received had `id = “camera”. There is no possibility now in such case to differentiate from which camera the matrices for view and projection are received.

---

<div class="post-metadata">

**Author:** ![jhonny.goransson](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jhonny.goransson/32/3864_2.png) [@jhonny.goransson](https://forum.defold.com/u/jhonny.goransson)\
**Post date:** [August 20, 2024, 5:42pm UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/39 "2024-08-20T17:42:31Z")

</div>

I don’t use that message personally, you already have all the cameras so I don’t know why you would want to respond to that message?

---

<div class="post-metadata">

**Author:** ![Pawel](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pawel/32/24623_2.png) [@Pawel](https://forum.defold.com/u/Pawel)\
**Post date:** [August 21, 2024, 11:44am UTC](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816/40 "2024-08-21T11:44:34Z")

</div>

So it’s not only me that sees spotlight as “camera” 😅

 ![image](https://forum-defold-uploads.b-cdn.net/original/3X/7/8/78cc2a0204c0775adfbfd5e8770c81bbe904385e.jpeg)

It’s a lenghty, but very good explanation:  
 ![](https://img.youtube.com/vi/Jhopq2lkzMQ/maxresdefault.jpg)https://www.youtube.com/embed/Jhopq2lkzMQ?feature=oembed&wmode=opaque

> [@jhonny.goransson](#):
>
> I don’t use that message personally, you already have all the cameras so I don’t know why you would want to respond to that message?

 ![image](https://forum-defold-uploads.b-cdn.net/original/3X/2/f/2f97bd07914e5cedc5487655eb9651465356d53c.png)

😅 Yeah, you can get everything from API or use `.set_camera()`

And when we `render.draw()` we should pass a frustum for culling, right? Uhm, but we can calculate it from `proj*view` from that camera, right? So yeah, no question 😃

[Previous page](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816.md?page=1)

[Next page](https://forum.defold.com/t/shadow-map-how-to-properly-create-a-render-target-solved/77816.md?page=3)
