当前位置: 代码迷 >> 综合 >> Shader学习Day02 固定管线着色器、ShaderLab
  详细解决方案

Shader学习Day02 固定管线着色器、ShaderLab

热度:17   发布时间:2023-12-05 15:45:03.0

固定管线着色器

Shader基本语法

LearnShaderLab

//创建Shader,标注Shader所在路径
Shader "MyShader/LearnShaderLab"
{//设置Shader外部属性Properties{}//子着色器  【高质量】//真正去渲染的,每个都是独立的单元,不是为了叠加,而是为了区分设备SubShader{//渲染正面//固定管线着色器至少要有一个pass通道Pass   //路径{}//渲染背面Pass{}}//子着色器  【中质量】SubShader{Pass   //路径{}}//子着色器  【低质量】SubShader{Pass   //路径{}}//备胎  最简单的Shader,只有一个漫反射Fallback "Diffuse"
}

1.Properties 外部属性

常用外部属性

Shader "MyShader/ShaderLabProperties"
{Properties{//属性名  面板名称  数据类型 默认值  【后面不加;】_MyIntValue("一个整数属性",Int) = 20   _MyFloatValue("一个小数类型",Float) =  3.14 _MyRangeValue("一个范围小数属性",Range(0,10)) = 3_MyColorValue("一个颜色属性",Color) = (1,0,0,1)_MyVectorValue("四维数属性",Vector) = (1,2,3,4)//图片_My2DValue("2阶图片",2D) = "white"{}_MyRectValue("非2阶图片",Rect) = ""{}_My3DValue("3阶图片",cube) = ""{}}SubShader{Pass{}}
}

 纯色处理

Shader "MyShader/Fixed/Fixed001"
{Properties{_MainColor("纯色",Color) = (1,0,0,1)}SubShader{Pass {//设置纯色渲染Color(0,1,0,1)//设置可变纯色渲染Color[_MainColor]}}
}

镜面反射和漫反射

Shader "MyShader/Fixed/Fixed002"
{Properties{_DiffuseColor("漫反射颜色",Color) = (1,0,0,1)_AmbientColor("环境光颜色",Color) = (0,0,1,1)_SpecularColor("高光颜色",Color) =(1,1,1,1)_Shininess("光泽度",Range(0.1,2)) = 2_EmissionColor("自发光",Color) = (1,0,0,1)}SubShader{Pass {//开启顶点光照命令Lighting on//开启镜面反射命令SeparateSpecular on//材质命令Material{//漫反射效果命令//Diffuse(1,0,0,1)Diffuse[_DiffuseColor]//环境光颜色Ambient[_AmbientColor]//高光颜色Specular[_SpecularColor]//光泽度Shininess[_Shininess]//自发光Emission[_EmissionColor]}}}
}

 

2.Tags标签

子着色器标签:

Pass通道标签 :

渲染队列:Queue

3.常用渲染

标签  、正面和背面剔除

Shader "MyShader/Fixed/Fixed003"
{Properties{_MainColor("主颜色",Color) = (1,0,0,1)}SubShader{Pass {//剔出背面Cull backLighting onMaterial{Diffuse[_MainColor]}}Pass {//剔出正面Cull FrontColor(1,0,1,1)}}
}

4.深度测试和深度缓存

渲染队列

Shader "MyShader/Fixed/Fixed004"
{Properties{_MainColor("主颜色",Color) = (1,0,0,1)}SubShader{//关闭深度测试ZTest  off//SubShader标签//默认是Geometry(几何体)-2000Tags{"Queue" = "Transparent+15"}Pass {Lighting onMaterial{Diffuse[_MainColor]}}Pass {//剔出正面Cull FrontColor(1,0,1,1)}}
} 
Shader "MyShader/Fixed/Fixed005"
{Properties{_MainColor("主颜色",Color) = (1,0,0,1)}SubShader{//关闭深度测试ZTest  offTags{"Queue" = "OverLay"}Pass {Lighting onMaterial{Diffuse[_MainColor]}}}
}
Shader "MyShader/Fixed/Fixed006"
{Properties{_MainColor("主颜色",Color) = (1,0,0,1)}SubShader{Pass {//Greater后面不能接数//深度测试大于【被其他对象挡住】ZTest  Greater Color(0,1,0,1)}Pass {//Greater后面不能接数//深度测试大于【被其他对象挡住】ZTest  LEqual //关闭深度缓存ZWrite OffLighting onMaterial{Diffuse[_MainColor]Ambient(1,1,1,1)}}}
}

5.混合

图片

Shader "MyShader/Fixed/Fixed007"
{Properties{_MainTexture("主纹理",2D) = ""{}}SubShader{Pass {SetTexture[_MainTexture]{}}}
}

颜色和图片(混合)

Shader "MyShader/Fixed/Fixed008"
{Properties{_MainColor("主颜色",Color)= (1,0,0,1)_MainTexture("主纹理",2D) = ""{}}SubShader{Pass {/*Lighting OnMaterial{Diffuse[_MainColor]}*/Color[_MainColor]SetTexture[_MainTexture]{/*//图片和颜色混合//顶点光照下的颜色结果 + 当前设置的纹理图片combine primary * texture //DOUBLE*//**/ConstantColor[_MainColor]combine constant + texture}}}
}

图片和图片(混合)

Shader "MyShader/Fixed/Fixed009"
{Properties{_MainTexture("主纹理",2D) = ""{}_DetailTexture("细节纹理",2D) = ""{}}SubShader{Pass {SetTexture[_MainTexture]SetTexture[_DetailTexture]{//当前纹理和上一次Setture的结果混合combine texture * previous DOUBLE}}}
}

混合lerp

Shader "MyShader/Fixed/Fixed010"
{Properties{_FirstTexture("图片1",2D) = ""{}_SecondTexture("图片2",2D) = ""{}_BlendScale("混合比例",Range(0,1)) = 0}SubShader{Pass {SetTexture[_FirstTexture]{constantcolor(1,0,0,0.1)combine texture * constant}SetTexture[_SecondTexture]{constantcolor(0,0,0,[_BlendScale])//       源1           源2        源3combine previous lerp (constant) texture}}}
}

半透明混合

Shader "MyShader/Fixed/Fixed011"
{//半透明的混合效果Properties{_MainCol("主颜色",Color) = (1,0,0,0.5)_MainTex("主纹理",2D) = ""{}}SubShader{ Tags{"Queue" = "Transparent"}Blend SrcAlpha OneMinusSrcAlphaPass {Lighting OnBlend SrcAlpha OneMinusSrcAlphaMaterial{Diffuse[_MainCol]}SetTexture[_MainTex]{combine texture + primary}}}
}

6.AlphaTest

Shader "MyShader/Fixed/Fixed012"
{//半透明的混合效果Properties{_MainTex("主纹理",2D) = ""{}_AlphaTestValue("测试标准",Range(0,1)) = 0}SubShader{ AlphaTest Greater [_AlphaTestValue]Pass {SetTexture[_MainTex]}}
}