Search Unity

Build in Image effects applied to Texture2D

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

  1. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Is it possible to apply one of the build in image effects to a Texture2D created from a script?

    The thing is i want the grey scale effect to affect parts of my gui, but it seems that GUI elements are not effected by image effects.

    Regards,
    Marc
     
  2. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    I figure no answer means no and that the UT people are very busy, so i tried to come up with a solution for my problem.

    I made a new ortogonal camera with the image effect attached and a Plane aligned infront of it to view. Then i do a screenshot of the part of the gui i want the effect applied to and make the plane excactly the size of the screenshot and pixel perfect align the camera to view only the excact screenshot size (when i figure out how to do that?). I put this screenshot in a texture and apply it to the plane the camera views.

    Then i create a render texture excactly the size of the screenshot part of the gui and i add this render texture as the target of the camera.

    then i use the render texture as the texture for the gui box and voila in theory this should give me gui affected by image effect but with no interactivity which is fine for what i need.

    How ever i get this error when i run the code where the render texture is assigned to the gui.

    What i cant figure out is which dimension must match?

    Code (csharp):
    1.  
    2. public Camera fakeUICamera;
    3. public GameObject fakeUITexturePlane;
    4.  
    5. private Texture2D fakeAnimationControllerUI = null;
    6. private RenderTexture renderTexture = null;
    7.  
    8. // This is filled by the gui code some where else
    9. private Rect fakeUI;
    10.  
    11. // Create screenshot of gui to apply effect to.
    12. fakeAnimationControllerUI = new Texture2D((int)fakeUI.width, (int)fakeUI.height, TextureFormat.RGB24, false);      
    13.  
    14. // (0,0) is lower left cornor as this is pixel space.
    15. fakeAnimationControllerUI.ReadPixels(new Rect((int)fakeUI.xMin + 200, 0, (int)fakeUI.width, (int)fakeUI.height), 0, 0);
    16. fakeAnimationControllerUI.Apply();
    17.        
    18. fakeUITexturePlane.renderer.material.mainTexture = fakeAnimationControllerUI;
    19.        
    20. // Create render texture to fit fake gui.
    21. renderTexture = new RenderTexture((int)fakeUI.width, (int)fakeUI.height, 0);
    22. renderTexture.Create();
    23.        
    24. fakeUICamera.targetTexture = renderTexture;
    25.  
    26.  
    Above is the code used to create the screenshot and the render texture. Below the code where the render texture is used.

    Code (csharp):
    1.  
    2. GUI.BeginGroup(fakeUI);
    3.  
    4. /** THIS IS THE LINE WHERE THE ERROR POINTS TO **/
    5. GUI.Box(new Rect(0, 0, renderTexture.width, renderTexture.height), renderTexture, "fakeUI");
    6.  
    7. GUI.EndGroup();
    8.  
    Any help will be much apreciated.

    Edit: If it was possible to get the code that is used for image effects the effect could probably be done directly to the texture screenshot. The effects i use are Color correction and Grayscale. Is there anywhere to find the code for these effects or create similar ones?

    Regards,
    Marc
     
  3. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Still don't know what is wrong with the error i get, but i found a way to do what i want without using camera and render textures.

    I simply do the grayscale effect directly on the texture screenshot using the grayscale attribute of each pixel.

    Code (csharp):
    1.  
    2. Color[] screenshotColors = fakeAnimationControllerUI.GetPixels(0);
    3.        
    4.         // Do grayscale effect.
    5.         // Todo: use ramp texture.
    6.         for(int i = 0; i < screenshotColors.Length; i++) {
    7.             screenshotColors[i].r = screenshotColors[i].grayscale;
    8.             screenshotColors[i].g = screenshotColors[i].grayscale;
    9.             screenshotColors[i].b = screenshotColors[i].grayscale;
    10.             screenshotColors[i].a = 1f;
    11.         }
    12.        
    13.         fakeAnimationControllerUI.SetPixels(screenshotColors, 0);
    14.  
    Next on the todo list is to make it use a ramp texture. :D

    Regards,
    Marc