CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}"
Assistant
This code defines a custom shader within Unity, leveraging the ShaderLab syntax. It's designed to handle surface rendering with physically based shading (PBR). Let's break down each section:
1. Shader Declaration
csharp
Shader "Custom/NewSurfaceShader"
This line specifies the name of the shader. Here, it's labeled as "Custom/NewSurfaceShader".
This section contains the actual shader code written in Cg/HLSL:
#pragma surface surf Standard fullforwardshadows: Specifies a surface shader with the Standard lighting model and full forward shadows support.
#pragma target 3.0: Sets shader model 3.0 as the target, providing more advanced features and better performance.
Variables in the CGProgram:
sampler2D _MainTex;: A texture sampler for the albedo texture.
half _Glossiness;, half _Metallic;, fixed4 _Color;: Variables corresponding to the properties defined earlier.
The surf function calculates the surface properties:
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;: Fetches albedo color from the texture and tints it with the _Color property.
o.Albedo = c.rgb;: Sets the albedo of the output.
o.Metallic = _Metallic;: Sets the metallic value.
o.Smoothness = _Glossiness;: Sets the smoothness.
o.Alpha = c.a;: Sets the alpha (transparency).
5. FallBack
csharp
FallBack "Diffuse"
This line indicates that if the current hardware can't handle the specified shader, Unity should fall back to using a simpler "Diffuse" shader.
Instancing Support (Commented Out)
csharp
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
The commented lines show support for GPU instancing, allowing the shader to render many objects efficiently by sharing the same material, with slight differences per instance.
This ShaderLab script thus defines a relatively simple PBR shader adjustable in the Unity editor, rendering opaque objects with a standard lighting model.