Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Render To RenderTexture Bug?

Discussion in 'Editor & General Support' started by ev3d, May 20, 2014.

  1. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    $Screenshot 2014-05-19 at 09.23.05 PM.png

    Why does this object (a car grille in this case) seem to "drill a hole" in the resulting render when i render the camera to a render texture then save it to a png?

    Code used is...

    RenderTexture.active = RT;
    Texture2D virtualPhoto = new Texture2D(sqr, sqr, TextureFormat.ARGB32, false);
    // false, meaning no need for mipmaps
    virtualPhoto.ReadPixels(new Rect(0, 0, sqr,sqr), 0, 0);

    RenderTexture.active = null; //can help avoid errors
    camera.targetTexture = null;
    // consider ... Destroy(tempRT);

    byte[] bytes;
    bytes = virtualPhoto.EncodeToPNG();

    System.IO.File.WriteAllBytes(Path, bytes);
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    It will depend on the shader that is used on that part of the model. Try alternate shaders or adjusting/one that will do what you want. There were some notes, I thought, in the docs about how the rendertexture handles alpha, but I can't find them. I ran into the same issue a while back and I ended up modifying the shader to get the proper alpha.
     
  3. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    It uses the diffuse texture. Tried the mobile - diffuse texture,no change.
     
  4. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    i think it may have something to do with the specific mesh cause my other cars don't do that.. but i'm at a loss as to what it might be.. i mean, it renders fine unless it's a render texture.. I even look at the render texture itself in the editor and it looks fine.. but when i display the texture on screen or save it to a png it does that..
     
  5. smd863

    smd863

    Joined:
    Jan 26, 2014
    Posts:
    292
    They default diffuse shader just outputs the content of your texture so I would guess your grille texture has an alpha of 0 (or close to 0), and that value is simply getting written through to your render texture.

    When you render straight to the screen it doesn't matter since the screen buffer doesn't care what's in the alpha channel. But when you save it as PNG or draw it to the screen, it treats that area as transparent.

    You could modify the diffuse shader (i.e. download the built-in shaders and do a quick change in the standard diffuse one):
    Code (csharp):
    1.  
    2. ...
    3. void surf (Input IN, inout SurfaceOutput o) {
    4.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    5.     o.Albedo = c.rgb;
    6.     o.Alpha = 1;
    7. }
    8. ...
    9.  
    A better solution may be to alter your texture to make sure its alpha is 1 for the entire texture. Also check the "Main Color" (i.e. _Color) property of your shader to make sure alpha isn't 0 there either. It is also possible that your mesh data has 0 for the alpha of the vertex color, and that is getting multiplied in somewhere (most Unity shaders don't use vertex colour at all, but it's within the realm of possibility that the shader you're using does).
     
  6. ev3d

    ev3d

    Joined:
    Apr 19, 2013
    Posts:
    327
    i checked that, it is a photoshop texture (psd) and the texture has no alpha or transparency in it..