Search Unity

Bug EditorGUI.DrawPreviewTexture in scrollview not working correctly

Discussion in 'Editor & General Support' started by arkano22, Nov 22, 2022.

  1. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,929
    EditorGUI.DrawPreviewTexture isn't correctly clipped by scrollviews (Unity 2021.3.3f1)



    All other UI elements in the EditorGUI and GUI namespaces are. Am I missing something? this looks like a bug to me...
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,929
    For anyone stumbling upon this, I found the culprit: passing a custom material to
    EditorGUI.DrawPreviewTexture breaks clipping, probably because Unity relies on the shader to perform it. Passing null as the material and allowing Unity to use the built-in blit material allows clipping to work.

    Will try to work out what needs to be done in a custom shader to get this working, but would be nice to mention this pitfall in the documentation.
     
  3. Sunday14

    Sunday14

    Joined:
    Jun 7, 2018
    Posts:
    4
    Hi,@INedelcu @aleksandrk @bgolus, can you help me with this

    I have met the same problem writing custom editor window , I just want to know how to make my custom shader work properly with the editor clipping, don't know which shader is used when passing null as the material to Editor.
    DrawPreviewTexture
     
  4. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,929
    If you open up Unity's built-in UI shader, you can just copy-paste the relevant code over to yours. It's very simple, you'll need 2 things: a texture to read alpha values from, and a matrix to convert from eye space to that texture's UV space. You can just declare them in your shader and Unity will populate them for you:

    Code (CSharp):
    1. uniform float4x4 unity_GUIClipTextureMatrix;
    2. sampler2D _GUIClipTexture;

    Then declare clip UVs in your shader's vertex-to-fragment struct, this will be used to calculate uvs per-vertex and pass them over to the fragment stage:
    Code (CSharp):
    1.   float2 clipUV : TEXCOORD1;
    In the vertex shader, calculate and store clipUVs to pass to the fragment shader:
    Code (CSharp):
    1. float3 eyePos = UnityObjectToViewPos(v.vertex);
    2. o.clipUV = mul(unity_GUIClipTextureMatrix, float4(eyePos.xy, 0, 1.0));
    In the fragment shader, multiply your output's alpha by the clip alpha value read from the clip texture using the uvs you just calculated:
    Code (CSharp):
    1. color.a *= tex2D(_GUIClipTexture, i.clipUV).a;
    For reference, the shader that this is copied from is Internal_GUITextureClip, which you can find by downloading Unity's built-in shaders from the archive.
     
    Last edited: Feb 9, 2023
  5. Sunday14

    Sunday14

    Joined:
    Jun 7, 2018
    Posts:
    4
    Thank you so much ,it works,!!
    I tried the UI/Default and some other frequently-used unlit shaders which don't work