首页 > 作文

Unity基于ShaderLab实现光照系统(着色器代码实现小结)

更新时间:2023-04-04 10:15:29 阅读:4 评论:0

这篇主要总结unity中shaderlab的着色器代码实现总结,需要有一定和shaderlab基础;

一、着色器

1.顶点片元着色器

分顶点着色器和片元着色器,对应渲染管线的顶点变换和片元着色阶段;

最简单的顶点片元着色器:

shader "myshader/vertexfragmentshader"{    properties{        _maincolor("maincolor",color) = (1,1,1,1)    }        subshader    {        tags { "rendertype" = "opaque" }        pass        {            cgprogram            #pragma vertex vert            #pragma fragment frag            float4 _maincolor;            float4 vert(float4 v:position) :sv_position            {                return unityobjecttoclippos(v);            }            fixed4 frag () : sv_target            {                        return _maincolor;            }            endcg        }    }}

2.表面着色器

将顶点和片元着色器再进行一层封装;

通过表面函数控制反射率,光滑度,透明度等;

通过光照函数选择要使用的光照模型;

表面着色器提供了便利,但是也降低了自由度;

表面着色器能实现的,顶点片元着色器都可以实现,但顶点片元着色器的可操作性更高,性能也更好;

简单的表面着色器:

shader "myshader/surfaceshader"{    subshader    {        tags { "rendertype"="opaque" }        cgprogram        //表面着色器,使用lambert光照        #pragma surface surf lambert                    struct input {        float4 color :color;        };                void surf(input in,inout surfaceoutput o) {            o.albedo = 1;        }               endcg    }    fallback "diffu"}

3.固定函数着色器

已基本弃用不分析了;

二、光照模型

1.逐顶点光照(gourand shading)

在顶点着色器计算光照;顶点数目比片元少,计算量也少,通过线性插值得到每个像素的光照;

所以非线性光照计算时会出错——高光(后面会写);

v2f vert(a2v v) {v2f o;//顶点变换到裁剪空间o.pos = unityobjecttoclippos(v.vertex);     //环境光fixed3 ambient = unity_lightmodel_ambient.xyz;    //世界空间下法线fixed3 worldnormal = normalize(mul(v.normal,unity_worldtoobject));    //世界空间下光照方向fixed3 worldlight = normalize(_worldspacelightpos0.xyz);    //点成光照和法线得出漫反射方向,satruate取区间0-1;fixed3 diffu = _lightcolor0.rgb * _diffu.rgb * saturate(dot(worldnormal, worldlight));    //环境光+漫反射o.color = ambient + diffu;return o;}

2.逐片元光照(phong shading)

在片元着色器计算光照;根据每个片元的法线计算光照;效果好计算量大,也叫phong插值;

v2f vert(a2v v) {v2f o;//顶点变换到裁剪空间o.pos = unityobjecttoclippos(v.vertex);//传递世界坐标法线到片元着色器o.worldnormal = mul(v.normal,unity_worldtoobject);return o;}fixed4 frag(v2f v) :sv_target{//环境光fixed3 ambient = unity_lightmodel_ambient.xyz;//归一化世界法线fixed3 worldnormal = normalize(v.worldnormal);//归一化世界空间下光照方向fixed3 worldlight = normalize(_worldspacelightpos0.xyz);//求漫反射fixed3 diffu = _lightcolor0.rgb * _diffu.rgb * saturate(dot(worldnormal, worldlight));//相加环境光和漫反射fixed3 color = ambient + diffu;return fixed4(color,1.0);}

这也是lambert光照模型的算法;

3.halflambert 光照

v社做半条命使用一个标准,计算漫反射时候结果+0,5;这样对暗部有很大的优化;

//halflambertparmafixed halflambert = dot(worldnormal, worldlight) * 0.5 + 0.5;//使用halflambert计算漫反射fixed3 diffu = _lightcolor0.rgb * _diffu.rgb * halflambert;

4.逐顶点高光

上面说的逐顶点计算光照对非线性光照会有错误;

高光由反射导致,和观察方向、光线方向有关;具体关系参考;

在顶点着色器函数中添加:

//根据法线和光线方向用reflect方法计算反射方向fixed3 reflectdir = normalize(reflect(-worldlight, worldnormal));//计算观察方向,摄像机位置-顶点位置(要求同在世界坐标系下)fixed3 viewdir = normalize(_worldspacecamerapos.xyz - mul(unity_obj五年级下册复习资料ecttoworld, v.vertex).xyz);//phong光照模型中高光计算公式,_specular颜色,_gloss粗糙度,_lightcolor0系统参数光照颜色fixed3 specular = _lightcolor0.rgb * _specular.rgb * pow(saturate(dot(reflectdir, viewdir)), _gloss);o.color = ambient + diffu + specular;

5.逐像素高光

将逐顶点高光代码发放在片元着色器中执行;

6.bl梦想的故事ine-phong光照模型

上面逐顶点和逐像素高光都是使用phong光照模型;

求高光的时候使用reflect函数计算反射向量,计算比较大;

bline-phong使用(光线方向+观察方向)来替代反射向量;

//世界光线方向和观察方向中间方向;fixed3 halfdir = normalize(worldlight + viewdir);//使用halfdir来计算高光fixed3 specular = _lightcolor0.rgb * _specular.rgb * pow(max(0, dot(worldnormal, halfdir)), _gloss);fixed3 color = ambient + diffu + specular;

三、纹理贴图

1.单张纹理

使用纹理取样替代纯色,在片元着色器中对纹理贴图取样,修改像素颜色;

_maintexture_st 控制贴图的缩放和偏移(scale,translate);

v2f vert(a2v v){//uv传递给片元着色器,可以使用宏命令transform_texo.uv = v.texcoord.xy * _maintexture_st.xy + _maintexture_st.zw;//o.uv = transform_tex(v.texcoord,_maintexture);}fixed4 farg(v2f v) :sv_target{    //纹理取样,表面颜色-纹素fixed3 albedo = tex2d(_maintexture, v.uv).rgb * _color.rgb;//环境光*表面颜色fixed3 ambient = unity_lightmodel_ambient.xyz *albedo;fixed halflambert = dot(worldnormal, worldlight) * 0.5 + 0.5;//漫反射*表面颜色fixed3 diffu = _lightcolor0.rgb * albedo.rgb * halflambert;}

2.法线纹理

法线计算两种方式:

将光线和观察向量变换到切线空间计算;

将切线空间下法线变换到世界空间计算;

切线空间计算由于矩阵变换在顶点着色器,计算少效率高;

由于认知,或者有其他需求我们也会在世界空间计算法线;

– 法线纹理切线空间计算

v2f vert(a2v v) {v2f o;o.pos = unityobjecttoclippos(v.vertex);o.uv.xy = v.texcoord.xy * _maintexture_st.xy + _maintexture_st.zw;//o.uv = transform_tex(v.texcoord,_maintexture);o.uv.zw = transform_tex(v.texcoord,_bumpmap);//宏定义,求世界空间——切线空间变换矩阵rotationtangent_space_rotation;o.lightdir = mul(rotation,objspacelightdir(v.vertex)).xyz;o.viewdir = mul(rotation,objspaceviewdir(v.vertex)).xyz;return o;}fixed4 frag(v2f v) :sv_target{//切线空间-光线方向fixed3 tangentlightdir = normalize(v.lightdir);   //切线空间-观察方向    fixed3 tangentviewdir = normalize(v.viewdir);//法线贴图格式为normalmap,使用unpacknormal解压,取样得到切线空间下法线fixed3 tangentnormal = unpacknormal(tex2d(_bumpmap,v.uv.zw));//法线缩放tangentnormal.xy *= _bumpscale;//法线贴图压缩方法,z值可以计算得出,勾股定理,以下是简化后公式tangentnormal.z 史前时期= sqrt(1.0-saturate(dot(tangentnormal.xy,tangentnormal.xy)));    ...//漫反射高光计算都使用tangentnormal}

– 法线纹理世界空间计算

v2f vert(a2v v) {v2f o;o.pos = unityobjecttoclippos(v.vertex);//减少寄存器使用,xy记录主纹理uv,zw记录法线uvo.uv.xy = v.texcoord.xy * _maintexture_st.xy + _maintexture_st.zw;o.uv.zw = transform_tex(v.texcoord,_bumpmap);//求世界空间下法线、切线、副切线float3 worldpos = mul(unity_objecttoworld,v.vertex).xyz;fixed3 worldnormal = unityobjecttoworldnormal(v.normal);fixed3 worldtangent = unityobjecttoworlddir(v.tangent.xyz);fixed3 worldbinnormal = cross(worldnormal,worldtangent)*v.tangent.w;//法线、切线、副切线构成切线空间变换矩阵,w值trick利用存储世界坐标系顶点坐标o.ttow0 = float4(worldtangent.x,worldbinnormal.x,worldnormal.x,worldpos.x);o.ttow1 = float4(worldtangent.y,worldbinnormal.y,worldnormal.y,worldpos.y);o.ttow2 = float4(worldtangent.z,worldbinnormal.z,worldnormal.z,worldpos.z);return o;}fixed4 frag(v2f v) :sv_target{    ...            //法线贴图格式为normalmap,使用unpacknormal解压,取样得到切线空间法线fixed3 tangentnormal = unpacknormal( tex2d(_bumpmap,v.uv.zw));    //法线缩放tangentnormal.xy *= _bumpscale;    //法线贴图压缩方法,z值可以计算得出,勾股定理,以下是简化后公式tangentnormal.z = sqrt(1.0-saturate(dot(tangentnormal.xy,tangentnormal.xy)));    //矩阵变换求出世界空间法线tangentnormal = normalize(half3(dot(v.ttow0.xyz,tangentnormal),dot(v.ttow1.xyz,tangentnormal),dot(v.ttow2.xyz,tangentnormal)));    ...//漫反射高光计算都使用tangentnormal}

3.渐变纹理

以上漫反射颜色都是光线颜色,或者光线颜色混合表面纹素颜色;

有时候漫反射的颜色要根据反射角大小有不同的变化,比如卡通渲染;

这就需要使用渐变纹理ramptexture;

//顶点着色器转化ramptex的uv fixed4 frag (v2f i) : sv_target{    fixed halflambert = 0.5 * dot(worldnormal,worldlightdir)+0.5;         //根据halflambert反射方向取样ramptex纹素fixed3 diffucolor = tex2d(_ramptex, fixed2(halflambert, halflambert)).rgb*_col平均数问题or.rgb;fixed3 diffu = _lightcolor0.rgb * diffucolor; }

三种不同的ramp纹理:

4.遮罩纹理

有些部位高光效果太强,人为的希望有些部位暗一些等,可以用到遮罩纹理mask;

片元着色器中添加:

//反射方向fixed3 halfdir = normalize(tangentlightdir + tangentviewdir);//uv取样高光遮罩纹理*高光范围fixed3 specularmask = tex2d(_specularmask,i.uv).r *_specularscale;//高光结果混合遮罩纹理fixed3 specular = _lightcolor0.rgb * _specular.rgb * pow(max(0,dot(tangentnormal,halfdir)),_gloss) * specularmask;

效果对比:

四、透明物体

1.透明测试

alphatest只决定画不画,不做颜色混合,给定一个阈值_cutoff,透明度小于这个值都不画;

透明测试需要关闭背面裁剪,以及加上透明测试三套件;

tags { “queue”=”alphatest” “ignoreprojector”=”true” “rendertype”=”transparent”}

//渲染队列,忽略投影器,渲染类型tags { "queue"="alphatest" "ignoreprojector"="true" "rendertype"="transparent"}        //关闭裁剪cull offpass{...  fixed4 frag (v2f i) : sv_t将进酒原文带拼音arget{  ...  //alpha值小于_cutoff的都不画            clip(texcolor.a - _cutoff);  ...  }...}

修改culloff值大小的效果:

2.透明颜色混合

alphablend透明混合要关闭深度写入,否则会被剔除;

同时要选择混合模式,多种混合模式有点像ps里的透明图层叠加;

//三套件tags { "queue"="transparent" "ignoreprojector"="true" "rendertype"="transparent"}      pass{//关闭深度吸入,打开深度测试,选择颜色混合模式tags{"lightmode"="forwardba"}    zwrite off    blend srcalpha oneminussrcalpha         ...        fixed4 frag (v2f i) : sv_target{    ...     //返回着色是,加上透明度        return fixed4(ambient +diffu,texcolor.a*_alphascale);    }    }

3.复杂模型双pass颜色混合

模型复杂的时候会有自己遮挡自己的问题;用双pass解决,第一个pass提前做好深度写入且只做深度入;

pass{    zwrite on    colormask 0     //rgba任意|,选择需要写入的通道,只做深度缓冲,0不输出颜色}

4.透明混合渲染双面

同一个透明物体,我需要需要从正面看到透明物体的背面;

使用两个pass;一个cull front,一个cull back;

背面和正面分开画,先画背面,用正面和背面混合;

五、复杂光照处理

1.复杂光照

unity光源分为垂直光,点光源,锥形射光灯,面光源和探照灯都是烘焙后生效的不讨论;

unity中普通forwad前向渲染,没多一个灯光要加一个pass单独处理;

deffer延迟渲染,多个灯光也指渲染一次,有个g-buffer存储了图像,在g-buffer上处理光照;

点光源,锥形射光灯——光线方向由光源到顶点的方向;光线的衰减值也不同;

unity系统提供的点光源和锥形射光灯的光线衰减纹理图,减少了计算;

tags{"lightmode" = "forwardadd"}#pragma multi_compile_fwdadd#include "lighting.cginc"#include "autolight.cginc"fixed4 frag (v2f i) : sv_target{                fixed3 worldnormal = normalize(i.worldnormal);                      //deal with different light,get worldlightdir;    #ifdef using_directional_light        fixed3 worldlightdir = normalize(_worldspacelightpos0.xyz);        fixed atten = 1.0;    #el        fixed3 worldlightdir = normalize(_worldspacelightpos0.xyz - i.worldpos.xyz);              //get light attenuation        #if defined (point)        float3 lightcoord = mul(unity_worldtolight, float4(i.worldpos, 1)).xyz;        fixed atten = tex2d(_lighttexture0, dot(lightcoord, lightcoord).rr).unity_atten_channel;    #elif defined (spot)        float4 lightcoord = mul(unity_worldtolight, float4(i.worldpos, 1));        fixed atten = (lightcoord.z > 0) * tex2d(_lighttexture0, lightcoord.xy / lightcoord.w + 0.5).w * tex2d(_lighttextureb0, dot(lightcoord, lightcoord).rr).unity_atten_channel;    #el        fixed atten = 1.0;    #endif    #endif    ...    return fixed4((diffu+specular)*atten,1.0);}

2.阴影处理

untiy中meshrender组件上有两个选项:

castshadows——是否投射阴影,以及双面投射;

receive shadows——接受其他物体投射的阴影;

要求v2f中顶点坐标变量名必须是pos;

带阴影的shader必须fallback一个带lightmode被设置为shadowcaster的pass;

当然也可以自己实现这个pass;

tags { "lightmode"="forwardba" }cgprogram#pragma multi_compile_fwdba#include "lighting.cginc"#include "autolight.cginc"struct v2f{    float4 pos : sv_position;    shadow_coords(2)};v2f vert (appdata v){    v2f o;    o.pos = unityobjecttoclippos(v.vertex);    transfer_shadow(o);    return o;}fixed4 frag (v2f i) : sv_target{fixed atten = 1.0;fixed shadow = shadow_attenuation(i);return fixed4((ambient+ diffu + specular)*atten*shadow,1.0);}

3.透明物体阴影处理

castshadows——改成two sides即可;

life is too short for so much sorrow.

到此这篇关于unity基于shaderlab实现光照系统的文章就介绍到这了,更多相关unity光照系统内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-04 10:15:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/171492917b7f8b407cf6b0b78f748091.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Unity基于ShaderLab实现光照系统(着色器代码实现小结).doc

本文 PDF 下载地址:Unity基于ShaderLab实现光照系统(着色器代码实现小结).pdf

标签:法线   顶点   切线   纹理
相关文章
留言与评论(共有 0 条评论)
昵称:
匿名发表 登录账号
         
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图