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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Fixing script for Unity 5.2: Fixed Function Shader using Material (String) Constructor

Discussion in 'Scripting' started by mr-sambarlow, Sep 11, 2015.

  1. mr-sambarlow

    mr-sambarlow

    Joined:
    Nov 11, 2014
    Posts:
    36
    Hi!

    I'm using a plugin which has this

    Code (CSharp):
    1.     if(!safeMaterial){
    2.                 safeMaterial = new Material (
    3.                     "Shader \"Hidden/Invert\" {" +
    4.                     "SubShader {" +
    5.                     " Pass {" +
    6.                     " ZTest Always Cull Off ZWrite Off" +
    7.                     " SetTexture [_RenderTex] { combine texture }" +
    8.                     " }" +
    9.                     "}" +
    10.                     "}"
    11.                     );
    12.                 safeMaterial.hideFlags = HideFlags.HideAndDontSave;
    13.                 safeMaterial.shader.hideFlags = HideFlags.HideAndDontSave;        
    Which doesn't work in 5.2 because of the new Material (string) usage.

    I know nothing about shaders, so how do I fix this? I'm assuming I create a shader asset and use this for the constructor instead, but could someone point me to how to define this shader?

    Cheers,

    --Sam
     
  2. Ajr_1

    Ajr_1

    Joined:
    May 22, 2013
    Posts:
    20
    VTP? I was just about to post exactly the same question. I don't know anything about shaders either, so it's the blind leading the blind here - I tried the following but it didn't work:

    Code (CSharp):
    1. safeMaterial = newMaterial(Shader.Find("Hidden/Invert"));
    Then made a new shader:

    Code (CSharp):
    1. Shader "Hidden/Invert" {
    2.  
    3.     SubShader {
    4.         Pass {
    5.             ZTest Always Cull Off ZWrite Off
    6.             SetTexture [_RenderTex] {
    7.                 combine texture
    8.             }
    9.         }
    10.     }
    11. }
    I know that's no help to you at all, but maybe someone can see something in the shader that's missing.
     
  3. mr-sambarlow

    mr-sambarlow

    Joined:
    Nov 11, 2014
    Posts:
    36
    Yes VTP! :)

    Fingers crossed someone comes to our aid.
     
  4. wtrebella

    wtrebella

    Joined:
    Mar 18, 2013
    Posts:
    35
    What Ajr_1 said should almost work, but Unity isn't finding the shader because it's not compiling it with the build. Try adding the shader under "Always Included Shaders" list in ProjectSettings/Graphics (found this instruction in the Unity docs.)
     
  5. mr-sambarlow

    mr-sambarlow

    Joined:
    Nov 11, 2014
    Posts:
    36
    The error went away, but I'm not getting any movie visuals (the plugin is a movie player for OSX/iOS). I can hear the movie playing but get no visuals, despite the texture having been created. Getting crash about 1 in 4 times after I stop the movie playing too. Guessing that there might be more needed to get the plugin working in 5.2 -- unless I've done something silly. Any luck, Ajr_1?
     
  6. Ajr_1

    Ajr_1

    Joined:
    May 22, 2013
    Posts:
    20
    Same here, I just get a black screen where the video should be.
     
  7. mml

    mml

    Joined:
    Mar 29, 2015
    Posts:
    11
    Hi Sam and Ajr_1,
    I'm also no shader pro, but maybe it helps if you rewrite the shader to a fragment and vertex shader without using fixed function.
    It seems to me the shader you have is just rendering the texture. The last shader in the Unity Doc Writing vertex and fragment shaders called Texture is doing exactly that. Mayb this one helps

    Code (CSharp):
    1. Shader "Hidden/Invert" {
    2. //  Properties {
    3. //        _RenderTex ("Base (RGB)", 2D) = "white" {}
    4. //    }
    5.     SubShader {
    6.         Pass {
    7.             ZTest Always Cull Off ZWrite Off
    8.            
    9.             CGPROGRAM
    10.             #pragma vertex vert_img
    11.             #pragma fragment frag
    12.  
    13.             #include "UnityCG.cginc"
    14.             uniform sampler2D _RenderTex;
    15.  
    16.             float4 frag(v2f_img i) : SV_Target {
    17.                 return tex2D(_RenderTex, i.uv);
    18.             }
    19.             ENDCG
    20.         }
    21.     }
    22. }
    23.  
    Cheers,
    Felix
     
  8. mr-sambarlow

    mr-sambarlow

    Joined:
    Nov 11, 2014
    Posts:
    36
    Went back to 5.1.3f and the plugin works (including the change above, which I hadn't undo'd).

    So assuming some other Unity 5.3 change/fix has broken VTP.
     
  9. mr-sambarlow

    mr-sambarlow

    Joined:
    Nov 11, 2014
    Posts:
    36
  10. Ajr_1

    Ajr_1

    Joined:
    May 22, 2013
    Posts:
    20
    Awesome, that seems to work for me in an initial test :)
     
  11. mr-sambarlow

    mr-sambarlow

    Joined:
    Nov 11, 2014
    Posts:
    36
    Just upgraded to Unity 5.3... and looks like videos are broken again. Are you seeing this too @Ajr_1 ? :/
     
  12. mr-sambarlow

    mr-sambarlow

    Joined:
    Nov 11, 2014
    Posts:
    36
    Fix for this on 5.3: Change the Graphics Api for Mac to Open GL2 instead of OpenGLCore.