u3d的u3d shaderr有tex2D的个数限制吗

22:04 提问
shader顶点程序中,UV信息不来自纹理坐标集,而来自输出pos的换算,求分析原因!
v2f vert(appdata_base v)
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);//投影空间坐标
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
float scale = 1.0;
o.uv.xy = (float2(o.pos.x, o.pos.y*scale) + o.pos.w) * 0.5;
o.uv.zw = o.pos.
float4 frag (v2f i) : COLOR
// 对_GrabTexture纹理进行取样,进行2D纹理映射查找,后面传入的一定要四元纹理坐标。
// UNITY_PROJ_COORD传入四元纹理坐标用于给tex2Dproj读取,但是多数平台上,返回一样的值。
// 【自动生成的纹理UV】类型是float4,使用如下方式进行2D纹理映射查找
//half4 texCol = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uv));
// 也可以使用tex2D进行采样,但是【自动生成的纹理UV】时必须要除以w转为齐次坐标
float last_x = i.uv.x / i.uv.w;
float last_y = i.uv.y / i.uv.w;
// 好比将_GrabTexture平铺到屏幕,模型出现的地方才显示
half4 texCol = tex2D(_GrabTexture, float2(last_x, last_y));
// 颜色反相,便于观察效果
return 1 - texC
上面是一段shader的顶点程序,通常情况o.uv等于纹理坐标集,但是这里是来自顶点的换算。
o.pos输出的是剪裁坐标,最后的效果好像是主材质直接贴到屏幕上,而显示模型的地方才显示纹理。我不是很清楚其中的原理。求大神们解释一下?
按赞数排序
只发个vert要分析太勉强吧,一般都是根据整个的上下文分析的。如果是使用pos的话可能这张纹理和屏幕大小是一样的
其他相关推荐【Unity-Shader】Unity60个内置Shader代码浅析(一) - CSDN博客
【Unity-Shader】Unity60个内置Shader代码浅析(一)
Sunset虽然还没有正式开始工作,但已经有一点小忙了,总是感觉有好多好多不得不做的事,即使身心都有点疲倦。这次带来的是Unity内置的60个Shader代码的浅析的第一部分。往后会像制作ARPG那样陆续更新。
今天,在蛮牛群里,和几位同为学习者的群友进行了一番讨论,问题是,在游戏公司实习进行学习好还是自学好。个人认为,如果想要从事游戏开发相关方面的职业,首先提升自身能力以达到某一标准水平肯定是必需的。我相信,现今的许多怀有游戏梦想的大学生都会在不同时间以不同方式埋头苦干想方设法为了这个目标而努力。且不论哪一种方式更快,我认为:自学是人类自身能力的一种体现。从结果论出发,自学并不是为了讨论能够学的有多快,而是为了学的有多扎实,脑袋里面拥有多少的知识量。在进公司实习前,尽可能做到知识的完备,具有一定的项目经验,达到这个条件后进公司只需要进行团队之间的磨合和注意一些平时个人进行开发学习过程中可能没注意到的更加专业化的规定或者细节,远比进了公司之后不仅需要对自身能力进行提升,还需要跟团队磨合来得轻松。当然,每个人有每个人的想法,求同存异嘛。毕竟人与人之间所面对的环境与条件是不太一样,在某些地区,可能游戏公司较多,人才的需求度比较大,只需要拥有基础的能力就能正式入职或者进入实习,但对于sunset而言,由于游戏公司量小,规模也并非很大,所以往往精中求精,所以面试要求也比其他地区苛刻许多。sunset个人的想法是,完善自身能力后在进入公司实习,不仅是对自己负责,也是对团队负责,更是对能够给予自己机会进入这个行业的公司负责。所以,不管怎么样,先提升自身能力总是不错的选择。好了,废话有点多,接下来开始正题:
10个shader的名称:
(1)Alpha-Bumped Diffuse
(2)Alpha-Bumped Specular
(3)Alpha-Diffuse
(4)Alpha-Glossy
(5)Alpha-Parallax
(6)Alpha-ParallaxSpec
(7)AlphaTest-Bumped
(8)AlphaTest-BumpSpec
(9)AlphaTest-Diffuse
(10)AlphaTest-GLossy
Shader "Custom/Shader1"
Properties
_Color("Main Color",Color) = (1,1,1,1)
_MainTex("MainTexure Base(RGB) Trans(A)",2D) = "white"{}
_BumpMap("Normal Map",2D) = "bump"{}
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
#pragma surface surf Lambert alpha
sampler2D _MainT
sampler2D _BumpM
struct Input
float2 uv_MainT
float2 uv_BumpM
void surf(Input IN, inout SurfaceOutput o)
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _C
o.Albedo = c.
o.Alpha = c.a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
FallBack "Transparent/Diffuse"
Shader "Custom/Shader2"
//着色器主属性变量的声明,包含:主颜色,高光颜色,光泽度,主纹理,法线贴图
Properties
_Color("Main Color", Color) = (1,1,1,1)
_SpecColor("Specular Color", Color) = (1,1,1,1)
_Shininess("Shininess", Range(0, 1)) = 0.078125
_MainTex("Base(RGB Trans(A))", 2D) = "white"{}
_BumpMap("Normal Map", 2D) = "bump"{}
//子着色器,是表面着色器
//子着色器标签:透明渲染队列, 忽视阴影为真, 渲染类型为透明
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
//LEVEL OF DETAIL 为400
//CG程序段开始得标志,表面着色器采用Blinn-Phong光照模型, 采用Alpha混合方式
#pragma surface surf BlinnPhong alpha
sampler2D _MainTex
sampler2D _BumpMap
fixed4 _Color
half _Shininess
struct Input
float2 uv_MainTex
float2 uv_BumpMap
//表面着色器方法,混合纹理,计算RGB值,计算Alpha值,其中输出结构的光泽(Gloss)为Alpha值
//计算镜面反射值,计算法线
void surf(Input IN, inout SurfaceOutput o)
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex)
o.Albedo = tex.rgb * _Color.rgb
o.Gloss = tex.a
o.Alpha = tex.a * _Color.a
o.Specular = _Shininess
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap))
//CG程序段结束标志
Shader "Custom/Shader3"
Properties
_Color("Main Color", Color) = (1,1,1,1)
_MainTex("Base(RGB) Trans(A)", 2D) = "white"{}
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
#pragma surface surf Lambert alpha
sampler2D _MainT
struct Input
float2 uv_MainT
void surf(Input IN, inout SurfaceOutput o)
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _C
o.Albedo = c.
o.Alpha = c.a;
FallBack "Transparent/VertexLix"
Shader "Custom/Shader4"
//主属性变量声明,包含:主颜色,高光颜色,光泽度,主纹理
Properties
_Color("Main Color", Color) = (1,1,1,1)
_SpecColor("Specular Color", Color) = (0.5,0.5,0.5,0)
_Shininess("Shininess", Range(0.01, 1)) = 0.078125
_MainTex("Main Texture", 2D) = "white"{}
//子着色器是一个表面着色器
//子着色器标签为透明渲染队列,忽视阴影为真, 渲染类型为透明
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
//CG程序段开始标志,表面着色器采用Blinn-Phong光照模型,采用透明混合模式
#pragma surface surf BlinnPhong alpha
sampler2D _MainTex
fixed4 _Color
half _Shininess
//输入结构,包含:主纹理
struct Input
float2 uv_MainTex
//表面着色器函数,混合纹理,计算反射率,计算光泽,计算透明值, 计算镜面光泽度
void surf(Input IN, inout SurfaceOutput o)
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex)
o.Albedo = tex.rgb * _Color.rgb
o.Gloss = tex.a
o.Alpha = tex.a
o.Specular = _Shininess
//备用为"顶点光照着色器"
FallBack "Transparent/VertexLit"
Shader "Custom/Shader5"
//着色器的输入属性变量声明,包含:主颜色、虚拟位移量、主纹理、法线贴图、虚拟位移贴图
Properties
_Color("Main Color", Color) = (1,1,1,1)
_Parallax("Height", Range(0.005, 0.08)) = 0.02
_MainTex("Main Texture(RGB + A)", 2D) = "white"{}
_BumpMap("Normal Map", 2D) = "Bump"{}
_ParallaxMap("Height Map (A)", 2D) = "black"{}
//这个子着色器是表面着色器
//着色器标签包含:透明渲染队列,忽视阴影为真、渲染类型为透明
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
//LEVEL OF DETAIL为500
//CG程序段的开始标志,表面着色器采用Lambert光照模型、采用透明混合模式
#pragma surface surf Lambert alpha
sampler2D _MainTex
sampler2D _BumpMap
sampler2D _ParallaxMap
fixed4 _Color
float _Parallax
//输入结构,包含:主纹理、法线贴图、视图方向
struct Input
float2 uv_MainTex
float2 uv_BumpMap
float3 viewDir
//表面着色器函数,声明一个half类型值h,其值为采用2D非投影纹理查询的w量,
//然后根据虚拟位移量和视角方向计算偏移,然后主纹理和法线贴图分别加上偏移,形成视觉偏差效果
void surf(Input IN, inout SurfaceOutput o)
half h = tex2D(_ParallaxMap, IN.uv_BumpMap).w
float2 offset = ParallaxOffset(h, _Parallax, IN.viewDir)
IN.uv_MainTex += offset
IN.uv_BumpMap += offset
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color
o.Albedo = c.rgb
o.Alpha = c.a
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap))
//CG程序段结束的标志
//备用:“使用法线贴图的漫反射着色器”
Fallback "Transparent/Bumped Diffuse"
Shader "Custom/Shader6"
//主属性输入为:主颜色,高光颜色,光泽度,主纹理,法线贴图,虚拟位移贴图,虚拟位移量
Properties
_Color("Main Color", Color) = (1,1,1,1)
_SpecColor("Specular Color", Color) = (0.5,0.5,0.5,0)
_Shininess("Shininess", Range(0.01, 1)) = 0.078125
_MainTex("Main Texture", 2D) = "white"{}
_BumpMap("Bump Map", 2D)= "bump"{}
_ParallaxMap("HeightMap(A)",2D) = "black"{}
_Parallax("Parallax", Range(0.005, 0.08)) = 0.02
//子着色器是表面着色器
//子着色器的的标签为:透明渲染队列,忽视阴影为真, 渲染类型为透明
"Queue" = "Transparemt"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
//CG程序段开始标志,表面着色器采用BlinnPhong光照模型,采用透明混合模式
#pragma surface surf BlinnPhong alpha
sampler2D _MainTex
sampler2D _BumpMap
sampler2D _ParallaxMap
fixed4 _Color
half _Shininess
float _Parallax
//输入结构,包含:主纹理,法线贴图,视角方向
struct Input
float2 uv_MainTex
float2 uv_BumpMap
float3 viewDir
//表面着色器函数,总之,根据视图方向,虚拟位移视图,法线贴图计算偏移,然后分别加以偏移
void surf(Input IN, inout SurfaceOutput o)
half h = tex2D(_ParallaxMap, IN.uv_BumpMap).w
float2 offset = ParallaxOffset(h, _Parallax, IN.viewDir)
IN.uv_MainTex += offset
IN.uv_BumpMap += offset
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex)
o.Albedo = tex.rgb * _Color.rgb
o.Alpha = tex.a * _Color.a
o.Gloss = tex.a
o.Specular = _Shininess
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap))
//CG程序段结束标志
//备用:采用法线贴图的高光着色器。
FallBack "Transparent/Bumped Specular"
Shader "Custom/Shader7"
Properties
_Color("Main Color",Color) = (1,1,1,1)
_MainTex("Base (RGB) Trans(A)", 2D) = "white"{}
_BumpMap("Bump Map", 2D) = "bump"{}
_Cutoff("Alpha CutOff", Range(0, 1)) = 0.5
"Queue" = "Transparent"
"IgnnoreProjector" = "True"
"RenderType" = "TransparentCutout"
#pragma surface surf Lambert alphatest:_Cutoff
sampler2D _MainT
sampler2D _BumpM
struct Input
float2 uv_MainT
float2 uv_BumpM
void surf(Input IN , inout SurfaceOutput o)
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _C
o.Albedo = c.
o.Alpha = c.a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
FallBack "Transparent/Cutout/Diffuse"
Shader "Custom/Shader8"
//主属性输入,包含:主颜色,镜面高光颜色,光泽度。主纹理,法线贴图,透明测试模式阈值
Properties
_Color("Main Color", Color) = (1,1,1,1)
_SpecColor("Specular Color", Color) = (0.5,0.5,0.5,0)
_Shininess("Shininess", Range(0.01, 1)) = 0.02
_MainTex("Base(RGB) Trans(A)", 2D) = "white"{}
_BumpMap("Normal Map", 2D) = "bump"{}
_Cutoff("Alpha cutoff", Range(0, 1)) = 0.5
//表面着色器
//子着色器标签,透明渲染队列,忽视阴影,渲染类型为透明
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"Rendertype" = "TransparentCutout"
//LEVEL OF DETAIL 为400
//CG程序段开始标志,表面着色器,采用BlinnPhong光照模型,采用透明测试模式,阈值为_Cutoff
#pragma surface surf BlinnPhong alphatest:_Cutoff
sampler2D _MainTex
sampler2D _BumpMap
fixed4 _Color
half _Shininess
//输入结构,包含:主纹理, 法线贴图
struct Input
float2 uv_MainTex
float2 uv_BumpMap
//表面着色器函数,纹理查询赋值tex,计算输出结构的反射率,计算透明值,计算光泽,计算镜面反射率,计算法线
void surf(Input IN, inout SurfaceOutput o)
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex)
o.Albedo = tex.rgb * _Color.rgb
o.Alpha = tex.a * _Color.a
o.Gloss = tex.a
o.Specular = _Shininess
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap))
//CG程序段结束标志
//备用:采用透明测试模式的顶点光照着色器
Fallback "Transparent/Cutoff/VertexLit"
Shader "Custom/Shader9"
Properties
_Color("Main Color", Color) = (1,1,1,1)
_MainTex("Base(RGB) Teans(A)",2D) = "white"{}
_Cutoff("Alpha cutoff", Range(0, 1)) = 0.5
"Queue"="AlphaTest"
"IgnoreProjector"="True"
"RenderType"="TransparentCutout"
#pragma surface surf Lambert alphatest:_Cutoff
sampler2D _MainT
struct Input
float2 uv_MainT
void surf(Input IN, inout SurfaceOutput o)
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _C
o.Albedo = c.
o.Alpha = c.a;
FallBack "Transparent/Cutout/VertexLit"
Shader "Custom/Shader10"
//主属性输入,包含:主颜色,镜面高光颜色,光泽度,主纹理,透明测试模式阈值
Properties
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
//子着色器,这是一个表面着色器
"Queue"="AlphaTest"
"IgnoreProjector"="True"
"RenderType"="TransparentCutout"
//CG程序段的开始标志,表面着色器采用BlinnPhong光照模型,采用透明测试模式,阈值为_Cutout
#pragma surface surf BlinnPhong alphatest:_Cutoff
sampler2D _MainTex
fixed4 _Color
half _Shininess
//输入结构,包含:主纹理
struct Input
float2 uv_MainTex
//表面着色器函数
void surf (Input IN, inout SurfaceOutput o)
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex)
o.Albedo = tex.rgb * _Color.rgb
o.Gloss = tex.a
o.Alpha = tex.a * _Color.a
o.Specular = _Shininess
//CG程序段结束标志
//备用:”镂空顶点光照透明着色器“
Fallback "Transparent/Cutout/VertexLit"
上面这10个着色器都是sunset手敲的,也算是一种练习吧,还有这10个着色器基本上都是表面着色器,不同着色器区别的同学先去看看官方的Shader Totual,或者其他一些资料。嗯,今天就先这样。
本文已收录于以下专栏:
相关文章推荐
unity 内置 shader 源码
在上一篇博客中,已经编写了人物属性编辑器的大部分,但是光有一些属性的定义并没有什么卵用,我们还需要添加在角色受到伤害时,反映在人物属性上的变化,或者状态的变化。其实说到底,这部分也就是编写两个方法来接...
【摘要】往后的几篇博客,sunset会将以前自己学习过程中独立开发的一些小游戏或者小demo陆续做成几篇小教程公布出来,其中有十分简单但有趣的小游戏,也不乏比较深奥的一些demo代码。sunset会在...
今天考完了大学最后一门考试,是瞬间感觉轻松了一点,花了一天多的时间预习一本书,马上就去考试了,就结果而言还是不错的。但是不知道是一种解脱,还是马上要戴上的一种新的枷锁。总归还是不错的。好了,闲话少扯,...
前面写了这么多东西,场景和人物弄到一起风格渐渐统一了,但环境效果差了些,游戏demo显得很简陋,如何让游戏的效果变得绚丽,这个就要考虑对场景环境uv做烘培,开始写shader了,shader这个东西我...
这几天在看小宝更新的bolg,小宝的通信底层写的真不错,为什么前面写了kbe,firefly的用法,这里又说scut,其实就服务器方面用这些或者网易的柚子,甚至云峰的那个3000行代码的工具功能都差不...
最近在某培训机构教C#基础,算是赶鸭子上架了,
回头看了些简单的C#习题,自己做了下,可能有些low,不过贴出来大家看看吧
using System.Collectio...
任务系统是对于仙剑游戏来说
首先,本人个人尤其喜欢ARPG游戏,原因在于ARPG游戏不仅可以通过修改游戏难度考验玩家的个人操作能力,还可以通过或感人,或令人发省的游戏剧情引导玩家一步一步的思考,最终明白制作者或者制作团队想要倾诉...
他的最新文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)Unity Shader 效果分析
Unity上对于图像的处理,如果单纯使用代码,那么很遗憾,程序基本会跑死,毕竟是直接对像素的操作,读取写入都是比较耗费CPU和内存的。
所以,这次因为项目需要想实现类似哈哈镜的效果,想来想去,还是觉得用unity的Shader比较好,毕竟不需要CPU做什么,只用GPU就可以了。
话说GPU也是很强大的。
下面简单说一下Shader(其实我也是新手,学习的话,参考https://blog.csdn.net/poem_qianmo/article/details/
)这是位大牛,我的一些基础知识是在里面学习的。
这次我是来分享几个Shader的,对于具体的内容原理什么的,我就不讲了,也讲不来,只能大致说一下,我也在学习。
1、哈哈镜效果(放大)
Shader &Custom/MaxFace& {
Properties {
_MainTex(&texture&,2D)=&white&{}
_Radius(&radius&,float)=0.2
_CenterX(&centerX&,float)=0.5
_CenterY(&centerY&,float)=0.5
SubShader {
tags{&Queue&=&Transparent& &RenderType&=&Transparent&}
#pragma vertex vert
#pragma fragment frag
#include &UnityCG.cginc&
sampler2D _MainT
float _CenterX;
float _CenterY;
struct v2f{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float4 _MainTex_ST;
v2f vert(appdata_base v){
o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
o.uv=TRANSFORM_TEX(v.texcoord,_MainTex);
float newX=0;
float newY=0;
half4 frag(v2f i):COLOR
float tx=i.uv.x-_CenterX;
float ty=i.uv.y-_CenterY;
float distan=tx*tx+ty*
float real_radius=_Radius/2;
if(distan&_Radius*_Radius){
newX=tx/2;
newY=ty/2;
newX=newX*(sqrt(distan)/real_radius);
newY=newY*(sqrt(distan)/real_radius);
newX=newX+_CenterX;
newY=newY+_CenterY;
newX=i.uv.x;
newY=i.uv.y;
float u_x=newX;
float u_y=newY;
float2 uv_earth=float2(u_x,u_y);
half4 texcolor_earth=tex2D(_MainTex,uv_earth);
return texcolor_
FallBack &Diffuse&
下面是解释,将就着看一下
编译无误的话,我们看一下在Unity中的效果
新建一个Plane,导入一张图片,然后拖到Plane上面,在选择我们刚才创建的Shader
可以看到,下面的三个参数就是我们创建的在Propertes里面,随便挑以下参数,下面是效果
好吧,糟蹋了美女。。。
算法是我从里面改过来的,细节就不要问我了,我是代码搬运工。。
2、哈哈镜缩小模式
我直接上代码咯,因为基本没有变什么
Shader &Custom/MinFace& {
Properties {
_MainTex(&texture&,2D)=&white&{}
_CenterX(&centerX&,float)=0.5
_CenterY(&centerY&,float)=0.5
SubShader {
tags{&Queue&=&Transparent& &RenderType&=&Transparent&}
#pragma vertex vert
#pragma fragment frag
#include &UnityCG.cginc&
sampler2D _MainT
float _CenterX;
float _CenterY;
struct v2f{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float4 _MainTex_ST;
v2f vert(appdata_base v){
o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
o.uv=TRANSFORM_TEX(v.texcoord,_MainTex);
float newX=0;
float newY=0;
float theta=0;
half4 frag(v2f i):COLOR
float tx=i.uv.x-_CenterX;
float ty=i.uv.y-_CenterY;
theta = atan2(ty, tx);
float radius=sqrt(tx * tx+ ty * ty);
float newR=sqrt(radius)*0.5 ;
newX=_CenterX+newR*cos(theta);
newY=_CenterY+newR*sin(theta);
if (newX&0)
if (newX&1)
if (newY&1)
if (newY&0)
float2 uv_earth=float2(newX,newY);
half4 texcolor_earth=tex2D(_MainTex,uv_earth);
return texcolor_
FallBack &Diffuse&
看看效果(其实第二个还蛮可爱的)
3、再来个对称的
Shader &Custom/SymmertyFace& {
//对称特效
Properties {
_MainTex(&texture&,2D)=&white&{}
SubShader {
tags{&Queue&=&Transparent& &RenderType&=&Transparent&}
#pragma vertex vert
#pragma fragment frag
#include &UnityCG.cginc&
sampler2D _MainT
float _CenterX;
float _CenterY;
struct v2f{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float4 _MainTex_ST;
v2f vert(appdata_base v){
o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
o.uv=TRANSFORM_TEX(v.texcoord,_MainTex);
half4 frag(v2f i):COLOR
float uv_x;
if(i.uv.x&0.5){
uv_x=1-i.uv.x;
uv_x=i.uv.x;
float2 uv_earth=float2(uv_x,i.uv.y);
half4 texcolor_earth=tex2D(_MainTex,uv_earth);
return texcolor_
FallBack &Diffuse&
额,这妹子即使对称了,看着还是挺好看(花痴ing)附上原图
暂时就这么多吧,还有其他特效,以后慢慢更新下次自动登录
现在的位置:
& 综合 & 正文
Unity shader 官网文档全方位学习(一)————Surface Shaders基础及Examples详尽解析
/ICkengine/archives/85229.shtml
What?? Shader,看起来好高级的样子,是的,这是Unity中高级进阶的必备。因此,兄弟我就在此记下我学习官网的一些心得。
此为一。主要介绍些Surface Shaders的知识。具体的大家也可去官网(如下)学习。
/Documentation/Components/SL-SurfaceShaders.html
一、概念篇
1.基准:unity里的shader并不是一门独特的语言,而是一种代码生成方式,且可将低层次且复杂的shader编程进行简化。但同时你也还是得使用Cg/HLSL来写的。
2.原理:写一个函数,以UVs或者一些数据为入口,然后以SurfaceOutput为输出。同时在SurfaceOutput这个结构体里还有不同的属性。这样对于这个函数来说,他的执行过程会生成vertex和pixel的Shader,并且传递一些渲染的路径。
3.结构:输出结构:
struct SurfaceOutput {
二、编程规则
1.要写在CGPROGRAM..ENDCG的SubShader的块里。不可写在Pass里。
2.shader的名字是可以重复的,重复后,以后来的shader为主。
3.指令详细:
#pragma surface surfaceFunction lightModel [optionalparams]
=&surfaceFunction,没什么好说,肯定是函数名了。
=&lightModel是所采用的光照模型。可以自己写也可使用内置如Lambert和BlinnPhong.
=&optionalparams:可选参数,一堆可选包括透明度,顶点与颜色函数,投射贴花shader等等。具体用到可以细选。
另外这里有一个功能。在Surface shader的CGPROGRAM里添加 #pragma debug [内容]。可在编译结果的文件中看到。写多少都行。但尝试在其他种shader下不行。
三、实例学习:
Shader "Example/Diffuse Simple" {
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert
struct Input {
float4 color : COLOR;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = 1;
Fallback "Diffuse" }
第一个。行行来:
第一行:写个名字。这也有讲究的。斜线左边为其父类的组,无则新增,有则累加,右边才是真正的名字。注意,这些shader名不像C#脚本,无需文件名与shader名相同。
第二、三行:接下来就在SubShader里添加内容,SubShader是可以有多个的。然后上一个Tags,此处只用到RenderType这种,另外的还有Rendering order, ForceNoShadowCasting..等。这些本阶段暂不研究。
第四行:上一条指令,里面指定响应方法为surf且采用Lambert的光照模型。这个必须有的。
第五行:这个结构体,记得名字不能改,只能为Input。里面一个四元素的颜色值(RGBA)。
第七到第九行:第一个参数,纯输入的上述结构体参数。第二个参数,inout标识,意思是可为输入参数也可为输出参数。函数体内将其Albedo值为1,意为全反射。
最后Fallback,如果所有subshader在当前显卡都不支持,则默认返回自带的Diffuse。
2.Texture:
Shader "Example/Diffuse Texture"
Properties { _MainTex ("Texture", 2D) = "white" {} }
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert
struct Input { float2 uv_MainT };
sampler2D _MainT
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
Fallback "Diffuse"
这个例子呢。其实只是第一个的基础上添加了一个2D属性显示名为Texture。以下解析:
第一个黑体:添加一个名叫_MainTex的属性,指定其为2D类型且显示为Texture。”white”那块可不是乱写的,是unity的build-in的一些textures的名称,而不是单纯颜色名字。意思是当默认时显示为名叫white的材质。如改成red(即使用名叫red的材质,如果有其他也可叫其名字),则效果如下:
第二个黑体:uv_MainTex。这其中大有玄机,uv开头指代后方材质的uv值,因此uv不变,后面的可以根据开头起的名字动态换。还有哦,这种类似于_MainTex的命名方式是CG推荐的,其实不用下划线也OK的。
第三个黑体:这个Sampler2D,可以理解为引用一个2D Texture。因为下面的Tex2D函数需要这种类型。所以说这个后面的名字要与Properties里的对应一样才行。
第四个黑体:Tex2D,这玩意就是根据对应材质上所有的点找指定 2DSample上的Texture信息,此处需要其RGB信息,就打出来赋给了其反射值。所以对有材质图的情况下,要显示出图,还是要相应的反射其原图的rgb值。
3.Normal mapping
Shader "Example/Diffuse Bump" {
Properties {
_MainTex (
"Texture", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float2 uv_BumpM
sampler2D _MainT
sampler2D _BumpM
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
Fallback "Diffuse"
这个例子里加了个凹凸贴图,可实现类似一些很漂亮的凹凸效果。
第一个黑体:加一个2D类型的材质,默认为bump。(即带有凹凸效果的)。
第二个黑体:上一个采集器。采集下来上面的材质。
第三个黑体:有讲究,这个UnpackNormal是unity自带的标准解压法线用的,所谓解压,我暂时学习到的只是将法线的区间进行变换。由于tex2D(_BumpMap, IN.uv_BumpMap)取出的是带压缩的[0,1]之间,需要转成[-1,1]。这个函数会针对移动平台或OPENGL ES平台采用 RGB法线贴图,其他采用DXT5nm贴图。为此也可自己写。也在网上找到了一些资料,如下参考:
Shader: 带法线贴图的Surface Shader
Author: 风宇冲
Shader "Custom/3_NormalMap" {
Properties
_MainTex ("Texture", 2D) = "white" {}
_NormalMap ("NormalMap", 2D) = "white" {}
#pragma surface surf BlinnPhong
struct Input
float2 uv_MainT
//法线范围转换:单位法线 float3(x,y,z),x,y,z的取值范围是 [-1,1]。在法线贴图中被压缩在颜色的范围[0,1]中,所以需要转换
//(1)RGB法线贴图
float3 expand(float3 v) { return (v - 0.5) * 2; }
//(2)DXT5nm法线贴图
float3 expand2(float4 v)
normal.xy = v.wy * 2 - 1;
normal.z = sqrt(1 - normal.x*normal.x - normal.y * normal.y);
sampler2D _MainT
sampler2D _NormalM
void surf(Input IN,inout SurfaceOutput o)
half4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = c.
o.Alpha = c.a;
//对法线贴图进行采样,取得压缩在颜色空间里的法线([0,1])
float4 packedNormal = tex2D(_NormalMap, IN.uv_MainTex);
//要将颜色空间里的法线[0,1],转换至真正3D空间里的法线范围[-1,1]
//注意:范围基本都是从[0,1]转换至[-1,1].主要是图的通道与法线xyz的对应关系要根据法线贴图格式而定
//UnpackNormal, UnityCG.cginc里的函数
//o.Normal = UnpackNormal(packedNormal);
//expand,标准法线解压函数
o.Normal = expand(packedNormal.xyz);
[转载自/thread--1.html]
4.Rim Lighting
Shader "Example/Rim" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_RimColor ("Rim Color", Color) = (0.26,0.19,0.16,0.0)//1
_RimPower ("Rim Power", Range(0.5,8.0)) = 3.0 //2
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float2 uv_BumpM float3 viewD //3
sampler2D _MainT
sampler2D _BumpM
float4 _RimC//4
float _RimP//5 void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));//6
o.Emission = _RimColor.rgb * pow (rim, _RimPower);//7 }
Fallback "Diffuse"
新增的一些东西,我都用数字标注了。以下进行详细解读:
第一处(//1):上一个Color类型的显示为Rim Color的变量。颜色值RGBA对应0.26,0.19,0.16,0.0
第二处(//2):这个Range类型的变量,结果还是一个float。只是这个float是在这个range之内。为什么这么定义呢。如果超多,或过小,则使用range内指明的值代替。
第三处(//3):viewDir 意为World Space View Direction。就是当前坐标的视角方向。这里有个从相关网上找的图:链接:/forum/read.php?tid=11367
第四、五处(//4,//5):定义两个变量对应properties里的值,取出使用。
第六、七处:最里层是Normalize函数,用于获取到的viewDir坐标转成一个单位向量且方向不变,外面再与点的法线做点积。最外层再用saturate算出一[0,1]之间的最靠近(最小值但大于所指的值)的值。这样算出一个rim边界。为什么这么做。原理以下解释:
=&这里o.Normal就是单位向量。外加Normalize了viewDir。因此求得的点积就是夹角的cos值。
=&因为cos值越大,夹角越小,所以,这时取反来。这样,夹角越大,所反射上的颜色就越多。于是就得到的两边发光的效果。哈哈这样明了吧。
这里介绍一下这个half。CG里还有类似的float和fixed。half是一种低精度的float,但有时也会被选择成与float一样的精度。fragment是一定会支持fixed类型,同时也会有可能将其精度设成与float一样,这个比较复杂,后面篇章学到fragment时再深入探讨。
以下为与3的对比,大家一下就知道谁是用了rim color的吧。对!下面那个盒子就是用些shader的效果。
5.Detail Texture
Shader "Example/Detail" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_Detail ("Detail", 2D) = "gray" {}
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float2 uv_BumpM
float2 uv_D
sampler2D _MainT
sampler2D _BumpM
sampler2D _D
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
o.Albedo *= tex2D (_Detail, IN.uv_Detail).rgb * 2;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
Fallback "Diffuse"
这个最好理解了。
前面三个一样。上一个2D Texture。
最后一个黑体:在原先的反射基础上,在加一层,Texture的反射。
就是这样啦。最后上几个截图,大家一定就明白。
6.Detail Texture in Screen Space
Shader "Example/ScreenPos" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_Detail ("Detail", 2D) = "gray" {}
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT float4 screenP };
sampler2D _MainT
sampler2D _D
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
float2 screenUV = IN.screenPos.xy / IN.screenPos.w; //2
screenUV *= float2(8,6);
o.Albedo *= tex2D (_Detail, screenUV).rgb * 2;
Fallback "Diffuse"
这个比较有趣,是从上个例子的基础上将第二层叠加上的2D Texture根据当前屏幕的UV进行叠加,而不是根据自身的UV。这样带有含此shader材质的物体的贴图就会跟着移动到的位置而变换图片。
这里只需要说三点:
1.关于screenPos:screenPos是一个三维点,但是用齐次坐标的形式表示出来就是(x,y,z,w),根据齐次坐标的性质。(x,y,z,w)的齐次坐标对应三维点(x/w,y/w,z/w)。因此把w值除掉可以看来是一种Normalize的作法,这样就取出了实际的屏幕xy的UV值。
2.对screenUV进行倍剩:此处剩float2(8,6)意为将原获取到屏幕尺寸进行拉大的倍数。即x轴拉大8倍,y轴拉大6倍。
3.如何就平铺了刚好一行8个,一列6个了呢? 原因我觉得是在于2d Texture自己是按Normalize后进行铺的,因此在//2(刚转完标准的)screenPos后,将其剩多少即便将原图铺多少张。
OK。明了。其实这个东西可以拿来做放大镜的应用。上图:
7. Cubemap reflection
Shader "Example/WorldRefl" {
Properties {
_MainTex (
"Texture", 2D) = "white" {}
_Cube ("Cubemap", CUBE) = "" {}
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float3 worldR
sampler2D _MainT
samplerCUBE _C
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.5;
o.Emission = texCUBE (_Cube, IN.worldRefl).
Fallback "Diffuse"
Shader "Example/WorldRefl Normalmap" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
_Cube ("Cubemap", CUBE) = "" {}
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float2 uv_BumpM
float3 worldR INTERNAL_DATA };
sampler2D _MainT
sampler2D _BumpM
samplerCUBE _C
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.5;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
o.Emission = texCUBE (_Cube, WorldReflectionVector (IN, o.Normal)).
Fallback "Diffuse"
这两段都是加一个cubemap的反射。第二段相比之下是在有normal反射的基础上加的。Cubemap这东西,可设置几种面的不能渲染图,这方面可用于做天空盒。因为这样可以从各个角度看过去以显示不同的渲染效果。
以下说明:
1. worldRefl:即为世界空间的反射向量。
2. texCUBE:将反射向量一个个的往_Cube反射盒上找出然后做为Emission反射出来。
3. 第二个例子只是将其用在Normal反射后,这样一定要多添加一个INTERNAL_DATA的属性,另外也需用到WorldReflectionVectore方法取其利用Normal后的反射向量值。
类似于的效果,可见官网中的。我这也有一个,有点像打了光的样子。
8.Slices via World Space Position
Shader "Example/Slices" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_BumpMap ("Bumpmap", 2D) = "bump" {}
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float2 uv_BumpM
float3 worldP
sampler2D _MainT
sampler2D _BumpM
void surf (Input IN, inout SurfaceOutput o) {
clip (frac((IN.worldPos.y+IN.worldPos.z*0.1) * 5) - 0.5);
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
Fallback "Diffuse"
在看完这段后,我自己另外又加一段,以作对比:
float3 _tWorldP
void surf (Input IN, inout SurfaceOutput o) {
_tWorldPos = IN.screenPos.xyz / IN.screenPos.w;
//clip (frac((IN.worldPos.y+IN.worldPos.z*0.1) * 5) - 0.5);
clip (frac((_tWorldPos.y+_tWorldPos.z*0.1) * 3) - 0.5);
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
第二个黑体:frac是取小数的函数,如1.23 取出来是 0.23。clip函数用于清Pixel的,负值情况下才进行清pixel。且越小,即绝对值越大则清越多。这里注意那个* 5,仔细一想,如果frac出来的值越大,-0.5值就越大,绝对值就越小,因此这样清掉的pixel越少,所以就可以间接的增加分段的次数。那为什么要+IN.worldPos.z*0.1呢,主要原因就是空开的断添加一个倾斜角度,可以用空间思想想下。
我的那段,就是将要clip的坐标换掉,换成屏幕的。这样你移动物体时,clip掉的部分会变化。
最后,上下效果图:
9.Normal Extrusion with Vertex Modifier
Shader "Example/Normal Extrusion" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_Amount ("Extrusion Amount", Range(-1,1)) = 0.5
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert vertex:vert //1
struct Input {
float2 uv_MainT
float _A //2
void vert (inout appdata_full v) { //3
v.vertex.xyz += v.normal * _A
sampler2D _MainT
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
Fallback "Diffuse"
这是个自定义vertex的例子,效果可以实现点坐标的放大缩小,以形成肥仔与瘦棍的效果,哈哈。
第一个黑体(//1):添加一个可选参数为vertex,主要是为了给其添加一个函数vert。
第二个黑体(//2):这个_Amount对应开头的那个属性_Amount。具体是个Range值,可在shader界面外通过滑动条改变这个值。默认为0.5。
第三个黑体(//3):这里除了之前学过的东西外,多了个appdata_full的结构体。这里面的结构(载自UNITY官方论坛)如下:
struct appdata_full{
float4 vertex : POSITION;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
float4 texcoord1 : TEXCOORD1;
fixed4 color : COLOR;
#if defined(SHADER_API_XBOX360)
half4 texcoord2 : TEXCOORD2;
half4 texcoord3 : TEXCOORD3;
half4 texcoord4 : TEXCOORD4;
half4 texcoord5 : TEXCOORD5;
第四个黑体(//4):就是像为个点,换当前法线向量的指定倍数进行扩展。
10.Custom data computed per-vertex
Shader "Example/Custom Vertex Data" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert vertex:vert
struct Input {
float2 uv_MainT
float3 customC
void vert (inout appdata_full v, out Input o) {//2
UNITY_INITIALIZE_OUTPUT(Input,o);
o.customColor = abs(v.normal);
sampler2D _MainT
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
o.Albedo *= IN.customC
Fallback "Diffuse"
这个例子是用来渲染颜色的。我的分析如下:
第一处(//1):取一个颜色值,float3,对应RGB。
第二处(//2):较前个例子,多一个Input类型的参数,只为输出使用。
第三处(//3):UNITY_INITIALIZE_OUTPUT(type,name)这个函数大有用处,主要是将叫[name]的变量请空改成type类型。以下是从HLSLSupport.cginc里找到的定义:
#if defined(UNITY_COMPILER_HLSL)
#define UNITY_INITIALIZE_OUTPUT(type,name) name = (type)0;
#define UNITY_INITIALIZE_OUTPUT(type,name)
第四处(//4):RGB颜色值当然只能为正值,所以使用绝对值去取normal的值。
第五处(//5):在原先已经渲染上texture颜色值的基础上,加上这层自定义的颜色值。
11.Final Color Modifier
Shader "Example/Tint Final Color" {
Properties {
_MainTex (
"Texture", 2D) = "white" {}
_ColorTint ("Tint", Color) = (1.0, 0.6, 0.6, 1.0)
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert finalcolor:mycolor
struct Input {
float2 uv_MainT
fixed4 _ColorT
void mycolor (Input IN, SurfaceOutput o, inout fixed4 color) {
color *= _ColorT
sampler2D _MainT
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
Fallback "Diffuse"
这个例子是跟上面例子的对比,前种使用普通反射进行叠加上颜色,此处则是直接使用finalcolor对其颜色进行处理,这种可以处理整个模型的固定颜色值的渲染。以下做简要的分析:
1.finalcolor:mycolor :这个是另一种可选参数,就是用户自定义的颜色处理函数。函数名为mycolor.
2.mycolor函数:注意到函数除了有surf的两个参数外,还多了个颜色参数,这个颜色参数就是当前模型上颜色对象,对他的更改将直接影响全部来自于lightmap,light probe和一些相关资源的颜色值。
12.Custom Fog with Final Color Modifier
Shader "Example/Fog via Final Color" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_FogColor ("Fog Color", Color) = (0.3, 0.4, 0.7, 1.0)
SubShader {
Tags { "RenderType" = "Opaque" }
#pragma surface surf Lambert finalcolor:mycolor vertex:myvert
struct Input {
float2 uv_MainT
void myvert (inout appdata_full v, out Input data)
UNITY_INITIALIZE_OUTPUT(Input,data);
float4 hpos = mul (UNITY_MATRIX_MVP, v.vertex);//1
data.fog = min (1, dot (hpos.xy, hpos.xy) * 0.1); //2
fixed4 _FogC
void mycolor (Input IN, SurfaceOutput o, inout fixed4 color)
fixed3 fogColor = _FogColor.
#ifdef UNITY_PASS_FORWARDADD
fogColor = 0;
color.rgb = lerp (color.rgb, fogColor, IN.fog);
sampler2D _MainT
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
Fallback "Diffuse"
这个很高难度,里面还有些之前没用到过的函数,以下是我的理解:
第一处(//1):mul是矩阵相乘的函数。UNITY_MATRIX_MVP是model、view、projection三个矩阵相乘出来的4×4的矩阵。v.vertex是一个float4的变量,可理解成4×1的矩阵,两者相乘,则得出一个float4,这个值就是视角窗口的坐标值,这个坐标就跟camera的关联了。
第二处(//2):这个fog的浮点值就是其强度,范围一般在-1到1之间,说一般,只是我个人建议的值,设成其他也行,只是没多大意义。越负就越黑。再看后面这个点积,这个仔细一想,不难理解,其实就是为了达到一种扩散的效果,因此两个一样的向量相乘,其实就是直接对坐标做平方扩展,这样fog就更有雾的感觉。
第三处(//3):这个宏不好找,就看官方对这个例子的解释为正向渲染时的额外通道。字面不好理解,多多尝试过可以有所发现,其实就是在雾气渐渐消失处那块额外的渲染区。可以将fogColor = 0; 改成fogColor = fixed3(1,0,0)。外面雾气颜色再选成白色,效果则如下:
雾气改成绿色后:效果如下:
第四处(//4):lerp函数是个有趣的函数。第一个参数是左边界,第二个参数是右边界,第三个相当于一个值介于0到1之间的游标。游标为0,则为左边界,为1为右边界,取中间值则是以此类推,取插值。其实也可以把它看成百分比。这里的fog则可以看来那个游标,值越大,则越接近fogColor,越小越接近原色。
原shader所出来的效果再来张:
13.Linear Fog
Shader "Example/Linear Fog"{
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
SubShader {
Tags { "RenderType"="Opaque" }
#pragma surface surf Lambert finalcolor:mycolor vertex:myvert
sampler2D _MainT
uniform half4 unity_FogC
uniform half4 unity_FogS
uniform half4 unity_FogE
struct Input {
float2 uv_MainT
void myvert (inout appdata_full v, out Input data) {
UNITY_INITIALIZE_OUTPUT(Input,data);
float pos = length(mul (UNITY_MATRIX_MV, v.vertex).xyz); //3
float diff = unity_FogEnd.x - unity_FogStart.x; //4
float invDiff = 1.0f /
data.fog = clamp ((unity_FogEnd.x - pos) * invDiff, 0.0, 1.0); //6
void mycolor (Input IN, SurfaceOutput o, inout fixed4 color) {
fixed3 fogColor = unity_FogColor.
#ifdef UNITY_PASS_FORWARDADD
fogColor = 0;
color.rgb = lerp (fogColor, color.rgb, IN.fog);
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.
o.Alpha = c.a;
FallBack "Diffuse"
这个官方只贴出了代码,无任何解释。网上也未曾看到有人解答,在此为大家分析下。其实这个与上面那个例子相比之下,采用的fog的源头变了,这里是获取rendersettings里的fog来设置Fog的颜色、强度与起点终点等。以下进行解析:
第一处(//1):LOD 200,200是个代号,设成此的目的就是限制shader级别只到200为止,高过200的不采用,即使显卡支持,也不会使用高过200的shader级别的渲染方式。官方的解释:/Documentation/Components/SL-ShaderLOD.html
第二处(//2):此处标记uniform的意图就是让Cg可以使用此变量。因此这三个uniform变量均来自于RenderSetting中。你可以预先设置好三个值。
第三处(//3):length函数用于取一个向量的长度,如果是float3则采取如下形式:
float length(float3 v)
return sqrt(dot(v,v));
就是点积取平方根。
第四处(//4):计算fog起终点间的反差。
第五处(//5):将4中算得的diff置反过来。
第六处(//6):则将算出来的离视角的距离与0到1之间进行比对,小于0则为0,大于1则为1,范围之内就是其原值,总的来说,利用clamp函数防止其出界。
分析下原理:咱们先将rendersetting里的颜色设成红色,fog start 设成0, fog end设成50。
这时算出的diff = 50, invdiff = 1/50。将原fog的计算稍做简化,得出如下结果:
fog = clamp((1 – pos/50) , 0 , 1);这个式子很是明了,pos是距离,即距离越远,clamp里值越小,根据后面这句:
color.rgb = lerp (fogColor, color.rgb, IN.fog);
我们就可以判断出其越靠近fogColor,雾气就会越重。
最后上个效果图:这里选的是Linear的fog。
到此,所有的surface shader的官方例子都详细的介绍完了。哎,开源中国对cg无什么代码显示支持,大家要代码看不清,可以直接去官网上去面看。/Documentation/Components/SL-SurfaceShaderExamples.html
这里送上本文的项目工程:/s/1xindN
四、学习技巧
这里是我个人的一些观点:
1.遇问题先找官网,找官网论坛,找官网文档。
2.学会从软件根目录下的CGIncludes文件夹下找相关的函数宏定义。
3.积累相关线性代数与计算机图形学的知识,学习会更轻松些。
文中要有不对的地方,欢迎大家留言指正,其实与爱好Unity的人互相学习讨论!
下文将为大家送上对于官网的surface shader的其他方面的学习手记。敬请期待。。积极更新中。。
#本文系原创,全部解析内容来自自我学习的记录,请在转载时注明出处,谢谢#
&&&&推荐文章:
【上篇】【下篇】}

我要回帖

更多关于 u3d shader 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信