- Home /
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:
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!
bump... any taker? would much appreciate help on this.
Answer by clever · Jun 08, 2015 at 03:43 AM
There's a shader the does a mask which can be found here: http://wiki.unity3d.com/index.php/TextureMask
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: http://wiki.unity3d.com/index.php/Dissolve_With_Texture
Hope this helps
thanks! this is helpful. let me try a low resolution mask and see how it works. :-)
Hey Hamlet Arche can you give me your code I am facing same problem I don't have used setPixels I have tried evertying on internet and finally I am here. can can you give me your code so that I can scratch the image. deeply thanks if you give me you code.
hi @bhargav, i ended up just reducing the resolution and using more or less the same codes in my original question.
Your answer
Follow this Question
Related Questions
Combining 2 black-white textures 2 Answers
Why is Unity generating grayscale normal maps? 0 Answers
textureQueryLod/CalculateLevelOfDetail in Fragment Shader 2 Answers
Change the color of a black sprite?,How can i change the color of a black sprite? 1 Answer
Converting a RenderTexture to a Texture2D for use in a shader 2 Answers