Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

setPixels with alpha?

Discussion in 'General Discussion' started by tomjoad, Apr 23, 2007.

  1. tomjoad

    tomjoad

    Joined:
    Apr 14, 2007
    Posts:
    37
    So I'm slowly working my way through lots of the manual code snippets and tinkering with them - lots of fun.

    In the bit below I took the getPixels/setPixels example to copy an entire source texture at the click point (e.g. bullet hole type thing). Works fine except I am not doing any calculations to see that the source texture will fit at the point clicked.

    What I can't figure out is how to do alphas. My source texture is RGBA.Do I need to do a separate getPixels on the Color.grayscale and apply those to the target texture?

    Also, is there any equivalent to imaging lingo (Director) to use ink effect types of blitting? (additive, etc.)

    (sorry for this barrage of questions!!)

    --Roy

    Code (csharp):
    1.  
    2.  
    3. var blobTex: Texture2D;
    4.  
    5. function Update () {
    6. // Only when we press the mouse
    7. if (!Input.GetMouseButton (0))
    8. return;
    9.  
    10. // Only if we hit something, do we continue
    11. var hit : RaycastHit;
    12. if (!Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit))
    13. return;
    14.  
    15. var renderer : Renderer = hit.collider.renderer;
    16. var meshCollider = hit.collider as MeshCollider;
    17. if (renderer == null || renderer.sharedMaterial == null ||
    18. renderer.sharedMaterial.mainTexture == null || meshCollider == null)
    19. return;
    20.  
    21.  
    22.  var px = blobTex.GetPixels(0,0, blobTex.width, blobTex.height,  0);
    23.  
    24. var tex : Texture2D = renderer.material.mainTexture;
    25. var pixelUV = hit.textureCoord;
    26. pixelUV.x *= tex.width;
    27. pixelUV.y *= tex.height;
    28.  
    29.  
    30. tex.SetPixels (pixelUV.x, pixelUV.y, blobTex.width, blobTex.height, px, 0);
    31.  
    32. tex.Apply();
    33. }
    34.  
     
  2. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    You will probably have to do it per pixel with a for() or while() statement to have blending of any kind. Multiple blend modes are easy.

    Blend: new pixel = Color.Lerp(oldpixel, brushpixel, alpha)
    Additive: new pixel = oldpixel + (brush pixel * alpha)
    Multply: new pixel = old pixel * brush pixel