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:
- how to tell the shader to draw something based on pixel position, and
- how to set the array parameter of the shader from script.
many thanks!