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

Older Clipping shader has errors with DX11

Discussion in 'Shaders' started by Josh-Armour, Mar 25, 2015.

  1. Josh-Armour

    Josh-Armour

    Joined:
    Aug 8, 2014
    Posts:
    6
    Hello everyone, I have this old "clipping" shader, which allows you to clip models based off a position variable... It works as expected with Direct3d turned off in the player settings... but with it on, it breaks the shader and the shader always hits the fall back.

    The shader says that it has errors (Shader haserrors or is not supported by your graphics card) when not in dx11, thought it does not specify which. In the errors window there is a warning that says: "Compiling shaders to OpenGL ES 2.0, Xbox 360, PS3, Flash or DX11 requires both vertex and fragment programs".... If anyone has a solution to make this work in dx11 I would really appreciate it... this shader provides a very elegant solution instead of a messy one.

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'PositionFog()' with multiply of UNITY_MATRIX_MVP by position
    2. // Upgrade NOTE: replaced 'V2F_POS_FOG' with 'float4 pos : SV_POSITION'
    3. // Upgrade NOTE: replaced 'glstate.lightmodel.ambient' with 'UNITY_LIGHTMODEL_AMBIENT'
    4. // Upgrade NOTE: replaced 'glstate.matrix.invtrans.modelview[0]' with 'UNITY_MATRIX_IT_MV'
    5.  
    6. Shader "VertexLit Clipped" {
    7. Properties {
    8.     _Color ("Main Color", Color) = (1,1,1,1)
    9.     _SpecColor ("Spec Color", Color) = (1,1,1,1)
    10.     _Emission ("Emmisive Color", Color) = (0,0,0,0)
    11.     _Shininess ("Shininess", Range (0.01, 1)) = 0.7
    12.     _MainTex ("Base (RGB)", 2D) = "white" {}
    13. }
    14. SubShader {
    15.     Pass {
    16.         Cull Off
    17.        
    18.         CGPROGRAM
    19. // Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
    20. #pragma exclude_renderers gles
    21.         #pragma vertex vert
    22.         #include "UnityCG.cginc"
    23.        
    24.         struct v2f {
    25.             float4 pos : SV_POSITION;
    26.             float4 uv[2] : TEXCOORD0;
    27.             float4 color : COLOR0;
    28.         };
    29.        
    30.         uniform float4x4 _ClipTextureMatrix;
    31.         uniform float4 _Color;
    32.        
    33.         v2f vert( appdata_base v ) {
    34.             v2f o;
    35.             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    36.             o.uv[0] = v.texcoord;
    37.            
    38.             float4 c = mul( _Object2World, v.vertex );
    39.             c = mul( _ClipTextureMatrix, c );
    40.             o.uv[1] = c;
    41.            
    42.             // lighting from single directional light + ambient
    43.             float4 color = UNITY_LIGHTMODEL_AMBIENT;
    44.             float3 lightDir = mul( glstate.light[0].position.xyz, (float3x3)UNITY_MATRIX_IT_MV );
    45.             float ndotl = max( 0, dot( lightDir, v.normal ) );
    46.             color.xyz += glstate.light[0].diffuse * _Color * ndotl;
    47.             o.color = color;
    48.             return o;
    49.         }
    50.        
    51.         ENDCG
    52.        
    53.                 AlphaTest Greater 0.1
    54.                 SetTexture [_MainTex] {
    55.                     Combine texture * primary DOUBLE, texture * primary
    56.                 }
    57.                 SetTexture [_ClipTexture] { Combine previous, texture }
    58.         }
    59.     }
    60.     Fallback Off
    61. }
     

    Attached Files: