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

Hi!

I’m using a plugin which has this

    if(!safeMaterial){
                safeMaterial = new Material (
                    "Shader \"Hidden/Invert\" {" +
                    "SubShader {" +
                    " Pass {" +
                    " ZTest Always Cull Off ZWrite Off" +
                    " SetTexture [_RenderTex] { combine texture }" +
                    " }" +
                    "}" +
                    "}"
                    );
                safeMaterial.hideFlags = HideFlags.HideAndDontSave;
                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

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:

safeMaterial = newMaterial(Shader.Find("Hidden/Invert"));

Then made a new shader:

Shader "Hidden/Invert" {

    SubShader {
        Pass {
            ZTest Always Cull Off ZWrite Off
            SetTexture [_RenderTex] {
                combine texture
            }
        }
    }
}

I know that’s no help to you at all, but maybe someone can see something in the shader that’s missing.

Yes VTP! :slight_smile:

Fingers crossed someone comes to our aid.

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.)

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?

Same here, I just get a black screen where the video should be.

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

Shader "Hidden/Invert" {
//  Properties {
//        _RenderTex ("Base (RGB)", 2D) = "white" {}
//    }
    SubShader {
        Pass {
            ZTest Always Cull Off ZWrite Off
           
            CGPROGRAM
            #pragma vertex vert_img
            #pragma fragment frag

            #include "UnityCG.cginc"
            uniform sampler2D _RenderTex;

            float4 frag(v2f_img i) : SV_Target {
                return tex2D(_RenderTex, i.uv);
            }
            ENDCG
        }
    }
}

Cheers,
Felix

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.

Brian has replied in the VTP thread with a suggestion of how to fix – will look into this myself, but check out here:
http://forum.unity3d.com/threads/osx-video-texture-pro-improved-qtkit-quicktime-video-player-for-macosx.117947/page-6

Awesome, that seems to work for me in an initial test :slight_smile:

Just upgraded to Unity 5.3… and looks like videos are broken again. Are you seeing this too @Ajr_1 ? :confused:

Fix for this on 5.3: Change the Graphics Api for Mac to Open GL2 instead of OpenGLCore.