Search code examples
unity-game-engineshader

How to apply a shader on multiple sprites' rendering in Unity3D?


In Unity3d, I'm trying to make a simple metaball effect using multiple sprites of a blurred round, and those sprites move randomly. Thus I'd like the shader to perform a pixel color change from the rendering of all the sprites all together and not one by one.

Here is an example :

unity shader sample

The picture on the left shows four sprites with the blurred sprite ; the picture on the right is the result of the shader.

And I have no clue of how to do this.


Solution

  • If I understand your question correctly, what you can do is

    1. Create a new RenderTexture
    2. Move these sprites off-screen, out of the main camera's view.
    3. Point a new orthographic camera at all of the sprites that you've moved off-screen and set this camera's Target Texture field (in the Inspector view) to the render texture. This will save whatever the camera is seeing to that texture.
    4. From here you can render that texture onto the surface of another game object (maybe a Quad?)
    5. Attach a custom shader material to that quad that takes the render texture as input.
    6. Perform whatever operations you wish to the render texture within this shader
    7. Position this quad object in front of your main camera so that the final result gets rendered to screen

    Does this make sense?