Unity3D的Shader基本结构

一、Unity中Shader的三种基本类型

  • 固定功能着色器(Fixed Function Shader:固定功能着色器为固定功能渲染管线的具体表现。功能较简单兼容比较老的机器
  • 表面着色器:存在于Unity3D中由U3D发扬光大的一门技术。Untiy3D为我们把Shader的复杂性包装起来,降低shader的书写门槛。
  • 顶点着色器和片段着色器:GPU上含有两个组件:可编程顶点处理器和可编程片段处理器,顶点和片段处理器被分离成可编程单元,可编程顶点处理器是一个硬件单元,可以运行顶点程序,而可编程片段处理器则是一个可以运行片段程序的单元。顶点着色器:顶点着色程序从GPU前端(寄存器)中提取图元信息(顶点位置、法向量、纹理坐标),并完成顶点坐标空间变换、法向量空间转换、光照计算等操作,最后将计算数据传送到指定寄存器中。片段着色器片段程序从上述寄存器中获取需要的数据:纹理坐标与光照信息等,并根据这些信息以及从应用程序传递的纹理信息进行每个片段的颜色计算(纹理查询),最后将处理后的数据传送光栅操作模块。

三种着色器的共同点

  • 都必须从唯一一个根Shader开始
  • Properties参数部分,作用以及语法完全相同。
  • 具体功能都在SubShader里。
  • 都可以打标签
  • 都可以Fallback
  • 都可以处理基本的功能,例如光照漫反射以及镜面反射。但如uv计算效果等高级功能,固定功能着色器无法完成。

三种着色器的不同点

  • 表面着色器没有通道pass{},加了会报错,该着色器已经把具体内容打包在光照模型中了。
  • 固定渲染管线每句代码之后都没有“;”
  • 核心结构不同:
    • 固定渲染管线的核心是:
      • Material{}以及SetTexture[_MainTex]{}
    • 顶点与片段着色器的核心是:
      • CGPROGRAM
      • #pragma vertex vert
      • #pragma fragment frag
      • #include "UnityCG.cginc"
      • ENDCG
    • 表面着色器的核心是:
      • 表面着色器使用Unity3D自带光照模型Lambert,也不做顶点处理,只需要一个表面处理函数surf即可。
      • CGPROGRAM
      • #Pragma surface surf Lambert
      • ENDCG
      • 这套表示使用的是自己写的光照模型lsyLightModel,并且使用了顶点处理函数vert
      • CGPROGRAM
      • #pragma surface surf lsyLightModel vertex:vert
      • ENDCG

二、shader整体框架

  • 从这幅图可以看到,Unity中的shader可以分为以下三个模块:Properties一般定义中着色器的起始部分。属性模块可以定义一些属性,用来指定那个这段代码有哪些输入。在使用shader 的时候可以直接中材质面板里编辑这些属性。Properties块内的语法都是单行的。属性的定义格式如下所示:

属性可以划分为以下几类:

1)、数字与滑动条
name ("display name", Range (min, max)) = number //滑动条调节
name ("display name", Float) = number//数字调节
name ("display name", Int) = number//数字调节
颜色与向量(注意是四元数)
name ("display name", Color) = (number,number,number,number)
name ("display name", Vector) = (number,number,number,number)
2)、贴图
name ("display name", 2D) = "defaulttexture" {}
name ("display name", Cube) = "defaulttexture" {}//立方体贴图
name ("display name", 3D) = "defaulttexture" {}
3)、细节
Range与float的属性只能是单精度值。
在后面的着色器程序中,属性值通过[name]来访问。而display name将显示在材质检视器中。
可以使用在属性定义加上等号为每个属性提供缺省值。
对于纹理(2D, Rect, Cube) 缺省值既可以是一个空字符串也可以是某个内置的缺省纹理:"white", "black", "gray" or"bump"
使用示例
// properties for a water shader
Properties
{
_WaveScale ("Wave scale", Range (0.02,0.15)) = 0.07 // sliders
_ReflDistort ("Reflection distort", Range (0,1.5)) = 0.5
_RefrDistort ("Refraction distort", Range (0,1.5)) = 0.4
_RefrColor ("Refraction color", Color) = (.34, .85, .92, 1) // color
_ReflectionTex ("Environment Reflection", 2D) = "" {} // textures
_RefractionTex ("Environment Refraction", 2D) = "" {}
_Fresnel ("Fresnel (A) ", 2D) = "" {}
_BumpMap ("Bumpmap (RGB) ", 2D) = "" {}
}

2、子着色器SubShader

这个我会在下一篇文章中详细说明点击打开链接

3、回滚FallBack
Shader基本框架的最后是指定一个回滚函数Fallback,用来处理所有的子着色器都不能运行时的情况(当目标设备太老时,所有的设备都有其不支持的特性时使用了Fallback),可以认为是一种defult。 

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的Unity3D着色器,可以制作冰冻效果。它使用了一个简单的反射纹理和一个噪声纹理来创建冰的外观。 ``` Shader "Custom/IceShader" { Properties { _MainTex ("Texture", 2D) = "white" {} _ReflectTex ("Reflect Texture", 2D) = "white" {} _NoiseTex ("Noise Texture", 2D) = "white" {} _BumpScale ("Bump Scale", Range(0.01, 0.1)) = 0.05 _Reflectivity ("Reflectivity", Range(0.0, 1.0)) = 0.5 _NoiseScale ("Noise Scale", Range(0.1, 10.0)) = 1.0 _IceColor ("Ice Color", Color) = (1,1,1,1) } SubShader { Tags { "Queue"="Transparent" "RenderType"="Opaque" } LOD 100 CGPROGRAM #pragma surface surf Standard sampler2D _MainTex; sampler2D _ReflectTex; sampler2D _NoiseTex; float _BumpScale; float _Reflectivity; float _NoiseScale; fixed4 _IceColor; struct Input { float2 uv_MainTex; float2 uv_ReflectTex; float2 uv_NoiseTex; float3 worldPos; float3 worldNormal; }; void surf (Input IN, inout SurfaceOutputStandard o) { // Get the base color from the main texture fixed4 baseColor = tex2D(_MainTex, IN.uv_MainTex); // Get the reflection from the reflect texture fixed4 reflectColor = tex2D(_ReflectTex, IN.uv_ReflectTex); // Combine the base color and reflection fixed4 finalColor = lerp(baseColor, reflectColor, _Reflectivity); // Apply the ice color finalColor *= _IceColor; // Get the bump from the noise texture float4 noise = tex2D(_NoiseTex, IN.uv_NoiseTex); // Convert the noise to a normal vector float3 normal = UnpackNormal(noise.rgb); // Apply the bump to the surface normal IN.worldNormal += normal * _BumpScale; // Set output parameters o.Albedo = finalColor.rgb; o.Metallic = 0.0; o.Smoothness = 1.0; o.Normal = normalize(IN.worldNormal); o.Emission = finalColor.rgb; o.Occlusion = 1.0; o.Alpha = finalColor.a; } ENDCG } FallBack "Diffuse" } ``` 要使用这个着色器,您需要将三个纹理分别指定给_MainTex,_ReflectTex和_NoiseTex属性。您还可以调整_BumpScale,_Reflectivity和_NoiseScale参数来改变效果。最后,您可以使用_IceColor来指定冰的颜色。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一零壹0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值