So I have a cutaway shader that is based on the legacy shaders and would like to port it over to the standard shader to get that delicious pbr, but looking at the source for the standard it is way different from what I am used to, seems they are doing a lot of fancy stuff that is not editable directly in the .shader file? What would be the best way to accomplish this? Here is the source I am trying to port: Code (CSharp): Shader "Cutaway/Diffuse" { Properties { _MainTex ("Texture", 2D) = "white" {} _CutColor ("Cut Color", color) = (1,0,0,1) } SubShader { Tags { "RenderType" = "Opaque" } Cull Off CGPROGRAM #pragma surface surf Lambert struct Input { float2 uv_MainTex; float3 worldPos; float3 worldNormal; float3 viewDir; }; sampler2D _MainTex; float _d; float enableClip = 1; float4 clippingVector, _CutColor; void surf (Input IN, inout SurfaceOutput o) { if(enableClip == 1) { float dist = clippingVector.x * IN.worldPos.x + clippingVector.y * IN.worldPos.y + clippingVector.z * IN.worldPos.z + clippingVector.w; clip(-dist); float fd = dot( IN.viewDir, IN.worldNormal); if (fd.x > 0) { fixed4 c = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = c.rgb; o.Alpha = c.a; return; } o.Emission = _CutColor; }else{ fixed4 c = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = c.rgb; o.Alpha = c.a; } } ENDCG } Fallback "Diffuse" } Thanks
Hey Mike, here's a version with the standard lighting. In testing I wasn't super sure if I'd broken anything, because I have no idea of the use case or standard parameters are for this shader. Code (CSharp): Shader "Cutaway/Diffuse" { Properties { _MainTex ("Texture", 2D) = "white" {} _CutColor ("Cut Color", color) = (1,0,0,1) _Glossiness ("Smoothness", Range(0,1)) = 0.5 _Metallic ("Metallic", Range(0,1)) = 0.0 } SubShader { Tags { "RenderType" = "Opaque" } Cull Off CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows struct Input { float2 uv_MainTex; float3 worldPos; float3 worldNormal; float3 viewDir; }; sampler2D _MainTex; float _d; float enableClip = 1; float4 clippingVector, _CutColor; half _Glossiness; half _Metallic; void surf (Input IN, inout SurfaceOutputStandard o) { if(enableClip == 1) { float dist = clippingVector.x * IN.worldPos.x + clippingVector.y * IN.worldPos.y + clippingVector.z * IN.worldPos.z + clippingVector.w; clip(-dist); float fd = dot( IN.viewDir, IN.worldNormal); if (fd.x > 0) { fixed4 c = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = c.rgb * _CutColor; o.Alpha = c.a; return; } }else{ fixed4 c = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = c.rgb; o.Alpha = c.a; } o.Metallic = _Metallic; o.Smoothness = _Glossiness; } ENDCG } Fallback "Diffuse" } As a side note, it would be fairly trivial to remove the if(){}else{} statements in this. Have you considered doing that? It would probably boost performance.
Awesome thanks! This seems to almost work, only thing is that the _CutColor seems to need to be applied as a solid color after the return, with how it is currently it colors the visible part of the model and not the inside cut, how I did this before is using the Emission, but I can not seem to get the emission to have any effect, any ideas? It would be great to speed up the performance if possible, how would you remove the conditionals while keeping the functionality? Here is what I changed it to : Code (CSharp): Shader "Cutaway/Diffuse" { Properties { _MainTex ("Texture", 2D) = "white" {} _CutColor ("Cut Color", color) = (1,0,0,1) _Glossiness ("Smoothness", Range(0,1)) = 0.5 _Metallic ("Metallic", Range(0,1)) = 0.0 } SubShader { Tags { "RenderType" = "Opaque" } Cull Off CGPROGRAM // Physically based Standard lighting model, and enable shadows on all light types #pragma surface surf Standard fullforwardshadows struct Input { float2 uv_MainTex; float3 worldPos; float3 worldNormal; float3 viewDir; }; sampler2D _MainTex; float _d; float enableClip = 1; float4 clippingVector, _CutColor; half _Glossiness; half _Metallic; void surf (Input IN, inout SurfaceOutputStandard o) { if(enableClip == 1) { float dist = clippingVector.x * IN.worldPos.x + clippingVector.y * IN.worldPos.y + clippingVector.z * IN.worldPos.z + clippingVector.w; clip(-dist); float fd = dot( IN.viewDir, IN.worldNormal); if (fd.x > 0) { fixed4 c = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = c.rgb; o.Alpha = c.a; return; } o.Albedo = _CutColor; o.Emission = _CutColor; }else{ fixed4 c = tex2D(_MainTex, IN.uv_MainTex); o.Albedo = c.rgb; o.Alpha = c.a; } o.Metallic = _Metallic; o.Smoothness = _Glossiness; } ENDCG } Fallback "Diffuse" } Thanks again!