Better way to do dynamic texture

Hey guys,

I am trying to find a better/faster way of doing dynamic textures, essentially the texture is used as a lighting cookie to create a fog of war type effect on a top down view.

At the moment I create the 2d texture once in code which is quite small 128x128 pixels and add it to the light as a cookie then every fixedupdate (to help with performance) only the changes from a 2d array are applied to the texture using set pixel and apply, and the problem with doing it this way is that it is quite slow and I can understand why, as your pushing new data to the gfx card when you apply.

So as for why I’m posting this in shaders forum is that I am wondering if a shader would be able to do this in a more effective way?

Thank you,
Jackyd

You could either directly render into the texture (Camera.setTargetTexture for example) or use a compute shader to change the contents of the texture. This might improve performance if uploading the texture data is actually the bottle neck. I say if, since a 128x128x4 texture is not that much data. Its just 128x128x4 bytes = 65kb which amounts to about 4MB/s if you are running at 60Hz.

What happens if you lower/raise the resolution? How much is the actual upload and how much is changing the texture?

Hi, I got a same request as yours.
There’s a quad which it’s picture is calculated by a shader, then I need to use it as a light’s cookie.

I don’t know how to get a Texture from a Shader or a Material, so I used a Camera facing the quad to get a RenderTexture, then I tried to convert the RenderTexture into a Texture2D, finally assign it to light.cookie, but it doesn’t work well.

The effect I got by that way looks like assign an existing texture to light.cookie but the texture settings are wrong, if set the texture’s “type” to “cookie”, then it works well, or if set the texture’s type to Default, then set the AlphaSource to FromGrayScale, it works too. But if I do not do these settings, the final effect is wrong, just like what I got before.

So, could you please tell me how did you realized dynamic texture light cookie?

here is my codes:

using UnityEngine;

//testing codes, it can run but can't get the right result
public class DynamicCookie : MonoBehaviour
{
    private Light light;
    private Texture2D t2d;
    public RenderTexture renderTexture; //drag the RenderTexture rendered by a camera facing to the quad to assign it
  
    void Start ()
    {
        light = GetComponent<Light>();

        t2d = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32 , false);
        t2d.wrapMode = TextureWrapMode.Clamp;
    }
  
    void Update ()
    {   
        if (Input.GetKeyDown(KeyCode.A))
        {
            RenderTexture.active = renderTexture;
            t2d.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
            light.cookie = t2d;
        }  
    }
}

A more simple question:
How to “create the 2d texture once in code and add it to the light as a cookie”?

then , the 2nd question:
How to get a 2d texture from a quad rendered by a shader?