Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Garbled texture from Camera....

Discussion in 'Scripting' started by acme3D, Dec 1, 2010.

  1. acme3D

    acme3D

    Joined:
    Feb 4, 2009
    Posts:
    203
    I'm using this code to get a texture out of a camera view.


    Code (csharp):
    1. var myCam : Camera;
    2. var theTexture : Texture;
    3. var targetCube : GameObject;
    4.  
    5.  
    6. function Start(){
    7. }
    8.  
    9. function OnGUI () {
    10.     myCam.targetTexture = new RenderTexture(256,256,24);
    11.     myCam.targetTexture.isPowerOfTwo = true;
    12.     myCam.targetTexture.Create();
    13.     targetCube.renderer.material.mainTexture = myCam.targetTexture;
    14.     GUI.DrawTexture(Rect(40,200,400,100),myCam.targetTexture);
    15. }

    The result is a garbled image (that keeps changing over time); I include a screenshot....

    How to get a proper texture from my camera ?
     

    Attached Files:

  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    2 things:

    1. is the camera set to not overdraw the backbuffer with some fill? in that case such stuff can happen cause the previous content will remain in there.

    2. The code there is meant to go into Start not Ongui (aside of the drawtexture), cause you can skip the dream that you can generate a rendertexture 2-4 times per frame and get away without problems ;)
     
  3. acme3D

    acme3D

    Joined:
    Feb 4, 2009
    Posts:
    203
    2 infos:

    1. don't understand what you mean by "overdrawing the back buffer"... Clear Flags is set to "don't clear" and Culling Mask is on "Everything" (also tried with a layer containing the object I wanted to render). I don't know is this is the info you needed...

    2. was thinking it.... :eek:
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    1. Thats exactly what I meant with overdrwaing the backbuffer. Because you don't clear, you basically leave in whatever there was in the backbuffer before, the only place that stuff (garbage) in there will be overdrawn is where you draw something new which is the model and potentially the thing top right.
    But the stuff on the left there is garbage present in the backbuffer from some previous rendering or data storage, its undefine whats in the backbuffer unless you fill it with content though

    You should (must?) never use "don't clear" unless this is one of multiple cameras where another camera is clearing the whole backdrop and filling it with a color or something else
     
  5. acme3D

    acme3D

    Joined:
    Feb 4, 2009
    Posts:
    203
    I've tried to set Clear Flags to Solid Color and Skybox (I've put one) but the result is still garbled.....
     
  6. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Can you post the script in its current state?