title: 第一个shader代码
date: 2019-06-22 22:49:16
tags:
学习shader第一天!
刚刚开始学习unity着色器写个博客记录一下。
人生中写的第一个shader:
Shader "Skin/test2"
{
Properties{
_MainTex ("Albedo",2D) = "white" {}
_Color ("MainColor",Color) = (1,0,0,1)
}
SubShader {
CGPROGRAM
#pragma surface surf Lambert
struct Input {
float2 uv_MainTex;
};
sampler2D _MainTex;
float4 _Color;
void surf(Input IN,inout SurfaceOutput o){
o.Albedo = tex2D (_MainTex,IN.uv_MainTex).rgb* _Color.rgb;
}
ENDCG
}
}
写完和官方基本便面着色器Standard Surface Shader
对比了一下。。发现这就是个阉割版的基础shader。。
官方基础着色器代码
Shader "Custom/NewSurfaceShader"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
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"
}
基本的使用的是Standard fullforwardshadows
光照模型。有金属度和光泽度属性。和一个表示不透明物体渲染类型的标签Tag
.
LOD
则表示着色器细节等级,用于确定不同画质下shader是否调用。
最后一句FallBack "Diffuse"
用于在由于硬件或者其他问题,shader没有被正常执行的时候执行回滚操作。