作为一名准游戏开发程序猿来说 shader是很重要的
于是乎我也开始了shader的学习了
之后我会在这里持续更新我的学习过程
今天这篇博客简单介绍一下shader的入门级知识
理论知识我就不多说了 知乎上好多好多大佬写的好文章
我在这里简单操作一下 实现一个我们自己写的shader
首先我们先创建一个shader(standed Surface Shader)
然后通过vs就可编译shader
我们用vs打开它 发现好多没见过的语句
Shader "Custom/etst"
{
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"
}
其实shader中也有面向编辑器的开放的
Shader "Custom/MyShader"
//此处用于指定你的着色器的名称以及在Shader的下栏菜单中的位置
//注意不要和其他的着色器同名。
{
Properties{
//着色器的输入,说白了,这里缩写的内容都会显示到材质面板上
//如果有一些参数要留给用户去调整,都要写在这里,比如材质的纹理等
}
SubShader{
/*子着色器,每个子着色器都是一个完整的功能模块,
只不过他们应对不同的显卡,有任何一个子着色器可用
该着色器就是可用的,如果所有的着色器都不可用,
则Unity会使用Fallback指定的系统着色器来进行着色器
如果你不指定任何Fallback且所有的子着色器都失效了
那么该物体的渲染就会失效*/
pass{
/*每个子着色器中的pass块都要执行一遍
拿锻造一柄剑来说,一个pass就像是一道加工工艺
加工工艺越多,该着色器要处理的任务也就越多
当所有的pass都完成了,我们就可以得到一柄剑了。
*/
CGPROGRAM
//这里是真正的着色器代码所需要编写的地方,
//ShaderLab支持CG语法和GLSL语法,当然,仅仅是“语法”
//其本质还是ShaderLab,如果你要使用GLSL来编写着色器
//则使用GLSLPROGRAM和ENDGLSL语法块来定义
ENDCG
}
}
SubShader{
//针对显卡B的着色器
}
Fallback"Diffuse"
//如果上述所有的显卡都不管用,那我们就
//使用UnityShader自带的Diffuse来进行渲染
}
下面这个概述可以说是很详细了
之后如果在Properties里边添加语句 在编译器中会发现同步的面板
Properties{
//着色器的输入,说白了,这里缩写的内容都会显示到材质面板上
//如果有一些参数要留给用户去调整,都要写在这里,比如材质的纹理等
}
Properties{
_BaseColor("MyColor",Color) = (1.0,1.0,1.0,1.0) //后面不能加分号
_Int("testInt",Int)=1
_Float("testfloat",Float)=2.2
_Range("testRange",Range(0.0,5.0))=1.0
_Vector("testVector",Vector)=(1,1,1,1)
_2D("test2D",2D) = ""{}
_3D("test3D",3D) = ""{}
}
在unity里边对应出来的就是
所以是不是感觉shader也不是很难呢
接下来我会持续更新我的shader学习过程
如果大家感兴趣可以关注我