scratchcard type blend between two textures

we are building a scratchcard lottery type effect like in this image:

at this point, we start with a mesh with the unscratched texture (backTexture), and then we use a raycast to find the input hit point (pixelX, pixelY), and blend the scratched texture (frontTexture) in based on the distance from the hit point using Texture2D.SetPixels. i.e. something like this:

			for (int x = Mathf.Max (0, pixelX - radius); x < Mathf.Min (backTexture.width, pixelX + radius); x++) {
				for (int y = Mathf.Max (0, pixelY - radius); y < Mathf.Min (backTexture.height, pixelY + radius); y++) {
					float discount = (new Vector2 (pixelX - x, pixelY - y)).magnitude / (float)radius;
					if (discount < 1f) {
						int index = x + y * backTexture.width;
						backPixels [index] = Color.Lerp (backPixels [index], frontPixels [index], Time.deltaTime * strength * (1f - discount));
		backTexture.SetPixels32 (backPixels);
		backTexture.Apply ();

the problem is that the performance of Texture2D.SetPixels32() and Texture2D.Apply() are too low.

we have done some research to find people suggesting a shader-based solution, where a shader with 2 materials will draw the corresponding blend of textures. unfortunately i’m afraid we know very little about shaders, and in particular, we’d appreciate help on:

  1. how to tell the shader to draw something based on pixel position, and
  2. how to set the array parameter of the shader from script.

many thanks!

There’s a shader the does a mask which can be found here:

Sadly you’ll still have to use SetPixels to “scratch” the mask texture. One way you can limit the performance hit is to make the “scratch surface” texture rather small (like use a 256x256 or 512x512)

There’s also a dissolve shader on the wiki but I haven’t played around with it to see if it could help you:

Hope this helps


Here is what you can use.