Search Unity

RenderTexture colors looking washed out.

Discussion in 'Scripting' started by Marc, Dec 19, 2007.

  1. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Greetings

    I get this error:

    Code (csharp):
    1.  
    2. Error assigning 2D rectangle texture to 2D texture property '_MainTex': Dimensions must match
    3.  
    when running this code:

    Code (csharp):
    1.  
    2. public int width;
    3. public int height;
    4.    
    5. public Camera sceneCam; // The cam that render what we wanna show on the texture.
    6.    
    7. private RenderTexture texRender;
    8.  
    9. void Awake() {
    10.        
    11.         // Create render texture in the right proportions.
    12.         texRender = new RenderTexture(width, height, 0);
    13.        
    14.         sceneCam.targetTexture = texRender;
    15.         sceneCam.Render(); // Render one frame for static texture.
    16.        
    17.         MeshRenderer meshRenderer = (MeshRenderer)GetComponent(typeof(MeshRenderer));
    18.         meshRenderer.material.SetTexture("_MainTex", texRender);   
    19. }
    20.  
    And i have no clue to what excactly this means. What dimensions is it excactly that must match?

    When i play the scene in the editor it looks like that cam is rendered to the texture for a split second after that it looks like the texture does not exist anymore and it displays random garbarge texture.

    Anyone had this problem before?

    Regards,
    Marc
     
  2. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Ok turns out that the wrong shader used on the object generated this error. Solution was to use the Transparent/Diffuse shader. How ever now i ran into a different problem. I'll explain in detail.

    Setup

    1. Mesh model consisting of 2 objects with 2 different materials. (a) A frame using solid color black and alpha set to 30% (b) Inner texture display using Transparent/Diffuse shader color set to pure white alpha set to 100%

    2. A Camera rendering an animation somewhere in the scene to a render texture of non power of two proportinos (created in code). The camera has Aras' excellent shader script added for all white alpha channel (See snippet below, i converted it to C#). The Camera has Clear flags set to Solid color and the color is RGB(255, 0, 0) Alpha = 100%

    Code (csharp):
    1.  
    2.     private Material mat;
    3.  
    4.     void OnPostRender() {
    5.         if (!mat) {
    6.             mat = new Material ("Shader \"Hidden/Alpha\" {" +
    7.                                 "SubShader {" +
    8.                                 "    Pass {" +
    9.                                 "        ZTest Always Cull Off ZWrite Off" +
    10.                                 "        ColorMask A" +
    11.                                 "        Color (1,1,1,1)" +
    12.                                 "    }" +
    13.                                 "}" +
    14.                                 "}"
    15.                                 );
    16.             mat.shader.hideFlags = HideFlags.HideAndDontSave;
    17.             mat.hideFlags = HideFlags.HideAndDontSave;
    18.         }
    19.         GL.PushMatrix ();
    20.         GL.LoadOrtho ();
    21.         for (int i = 0; i < mat.passCount; ++i) {
    22.             mat.SetPass (i);
    23.             GL.Begin (GL.QUADS);
    24.             GL.Vertex3(0f, 0f, 0.1f);
    25.             GL.Vertex3 (1f, 0f, 0.1f);
    26.             GL.Vertex3 (1f, 1f, 0.1f);
    27.             GL.Vertex3 (0f, 1f, 0.1f);
    28.             GL.End ();
    29.         }
    30.         GL.PopMatrix ();
    31.     }
    32.  
    The problem
    Now the render texture does contain what the camera renders, so that works nice. The problem is the colors of the texture. They look all milky and washed out and seem that the texture is still blended with the background (red) to give a purple look. (See image attached)

    I want the texture to render excactly as the camera with out blending with the background.

    Help will be greatly apreciated. So the posted image should display the frame like it is now fade to background, but the texture inside the frame should have a pure blue background instead of the purple washed out one.

    EDIT: I can tell the camera renders correctly by not assigning it a render texture. It is pure blue with the objects animated that should be.

    Regards,
    Marc
     

    Attached Files:

  3. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    I tried replacing the render texture i create in code with one created from assets and put it on the camera inside Unity by linking it directly.

    Then the milky overlay is not pressent.

    Im at a loss here, anyone have any ideas what is going on?

    Regards
    Marc
     
  4. Doug

    Doug

    Joined:
    Oct 19, 2007
    Posts:
    36
  5. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Turns out it had to do with the lighting on the texture. Forced the light to be vertex lit and it renders like i want now.
     
    joshuacwilde likes this.