2
\$\begingroup\$

Let's say what I have is a planet like so:

enter image description here

What I want to do is to add a scorch mark like this:

enter image description here

Now, if this was a one time thing, it wouldn't be a problem. I'd make a sprite for this specific occasion and move on. The problem is however, I want the scorch marks to look different and to not use all that much memory (because there might be too many of them on screen and this is for mobile).

The scorch marks will almost always be the same size but the planets will be different sizes, so one sprite that is scaled is not what I want (it would look too big on bigger planets and too small on smaller ones). Also, I want to be able to rotate the sprite to make it look different.

I've thought about adding another sprite on top of the planet (a border-like hack) to hide this but there can be other planets that are really close by to this one and they would be hidden too.

I've looked at a lot of stuff but nothing like this exists.

I've managed to create some stuff with a stencil shader and the use of a sphere with zero Z scale. This does create the problem of showing the sprite on other planets and I haven't been able to push forward on this.

This is what I can currently make:

enter image description here

This is what I need to make:

enter image description here

\$\endgroup\$
  • \$\begingroup\$ I worked around the issue by making the planets further apart, but it's not a pretty solution. \$\endgroup\$ – John Hamilton Mar 13 '17 at 18:08
-1
\$\begingroup\$

IMO You can use Alpha Blending for this. After reading your planet texture and the spot texture alpha blend them, you can find multiple script for alpha blending.for example : You can read texture of any image using Sprite.texture.

 public RawImage ResultantImage;
 public Texture2D planetTexture, spotTexture; // you can get these from Image or make it public and assign in inspector.

 public void Merge(){

        Texture2D resultantTexture = new Texture2D(texWidth,texHeight, TextureFormat.RGBA32, false);
        resultantTexture.SetPixels (AlphaBlend(planetTexture,spotTexture).GetPixels());
        resultantTexture.Apply ();
        ResultantImage.texture =  resultantTexture; // to show resultant image or replace with existing image
 }

 public  Texture2D AlphaBlend(Texture2D aBottom, Texture2D aTop)
    {
        if (aBottom.width != aTop.width || aBottom.height != aTop.height)
            throw new System.InvalidOperationException("AlphaBlend only works with two equal sized images");
        var bData = aBottom.GetPixels();
        var tData = aTop.GetPixels();
        int count = tData.Length;
        var rData = new Color[count];
        for(int i = 0; i < count; i++)
        {
            Color B = bData[i];
            Color T = tData[i];
            float srcF = T.a;
            float destF = 1f - T.a;
            float alpha = srcF + destF * B.a;
            Color R = (T * srcF + B * B.a * destF)/alpha;
            R.a = alpha;
            rData[i] = R;
        }
        var res = new Texture2D(aTop.width, aTop.height);
        res.SetPixels(rData);
        res.Apply();
        return res;
    }

This code isn't tested. You can use this idea to move forward.

\$\endgroup\$
  • \$\begingroup\$ The code says "AlphaBlend only works with two equal sized images". Also, won't this just change the texture itself, and even if it doesn't, won't it lead to performance problems when there are many planets and scorch marks present? I'm asking for scorch marks on planets that are left there by "bullets". This means different numbers of marks on different parts of different planets and also different sized scorch marks. \$\endgroup\$ – John Hamilton Nov 9 '17 at 12:22
  • \$\begingroup\$ First of all we can make same canvas size as planet with a spot and remaining area transparent. Yes , it will be performance hit to make spot size equal to planet. but this code isnt ready to use you have to play with it. Its just for sake of direction how you can manipulate images. you can use docs.unity3d.com/ScriptReference/Texture2D.SetPixel.html to change specific pixels of you planet. \$\endgroup\$ – Faisal Imran Nov 9 '17 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.