Forward rendering path

shader如下:

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Custom/Forward Rendering"
{
Properties
{
_MainTex("Main Texture", 2D) = "white" {}
_Specular("Specular", Color) = (,,,)
_Gloss("Gloss", Range(,)) =
} SubShader
{
Pass
{
Tags
{
"LightMode" = "ForwardBase"
} CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase #include "UnityCg.cginc"
#include "Lighting.cginc" sampler2D _MainTex;
fixed4 _Specular;
float _Gloss; struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
}; struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
float4 worldPos: TEXCOORD2;
}; v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
} fixed4 frag(v2f i) : SV_TARGET
{
fixed4 albedo = tex2D(_MainTex, i.uv);
fixed4 ambient = albedo * UNITY_LIGHTMODEL_AMBIENT; float3 worldLight = normalize(UnityWorldSpaceLightDir(i.worldPos.xyz));
float3 worldView = normalize(UnityWorldSpaceViewDir(i.worldPos.xyz));
fixed4 diff = albedo * _LightColor0 * max(, dot(i.worldNormal, worldLight)); float3 halfDir = normalize(worldView + worldLight);
fixed4 spec = albedo * _Specular * pow(max(, dot(halfDir, i.worldNormal)), _Gloss); fixed4 col = ambient + diff + spec;
return col;
} ENDCG
} Pass
{
Tags
{
"LightMode" = "ForwardAdd"
}
Blend One One CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdadd #include "UnityCg.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc" sampler2D _MainTex;
fixed4 _Specular;
float _Gloss; struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
}; struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 worldNormal : TEXCOORD1;
float4 worldPos: TEXCOORD2;
}; v2f vert(appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
} fixed4 frag(v2f i) : SV_TARGET
{
fixed4 albedo = tex2D(_MainTex, i.uv); float3 worldLight = normalize(UnityWorldSpaceLightDir(i.worldPos.xyz));
float3 worldView = normalize(UnityWorldSpaceViewDir(i.worldPos.xyz));
fixed4 diff = albedo * _LightColor0 * max(, dot(i.worldNormal, worldLight)); float3 halfDir = normalize(worldView + worldLight);
fixed4 spec = albedo * _Specular * pow(max(, dot(halfDir, i.worldNormal)), _Gloss); // 参考 AutoLight.cginc
float atten;
#ifdef USING_DIRECTIONAL_LIGHT
atten = ;
#else
float4 lightCoord = mul(unity_WorldToLight, i.worldPos);
#ifdef POINT
atten = tex2D(_LightTexture0, dot(lightCoord.xyz,lightCoord.xyz).xx).UNITY_ATTEN_CHANNEL;
#elif SPOT
atten = (lightCoord.z > ) * tex2D(_LightTexture0, lightCoord.xy / lightCoord.w + 0.5).w * tex2D(_LightTextureB0, dot(lightCoord, lightCoord).xx).UNITY_ATTEN_CHANNEL;
#endif
#endif fixed4 col = (diff + spec) * atten;
return col;
} ENDCG
}
} Fallback "VertexLit"
}

计算衰减因子的代码:

float atten;
#ifdef USING_DIRECTIONAL_LIGHT
atten = ;
#else
float4 lightCoord = mul(unity_WorldToLight, i.worldPos);
#ifdef POINT
atten = tex2D(_LightTexture0, dot(lightCoord.xyz,lightCoord.xyz).xx).UNITY_ATTEN_CHANNEL;
#elif SPOT
atten = (lightCoord.z > ) * tex2D(_LightTexture0, lightCoord.xy / lightCoord.w + 0.5).w * tex2D(_LightTextureB0, dot(lightCoord, lightCoord).xx).UNITY_ATTEN_CHANNEL;
#endif
#endif

Unity shader学习之Forward Rendering Path的更多相关文章

  1. [转]Unity Shader 学习总结

    1.先来一段单张纹理贴图的shader示例代码: // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClip ...

  2. 前向渲染路径细节 Forward Rendering Path Details

    正向渲染路径细节 Forward Rendering Path Details Forward Rendering path renders each object in one or more pa ...

  3. 正向渲染路径细节 Forward Rendering Path Details

    http://www.ceeger.com/Components/RenderTech-ForwardRendering.html This page describes details of For ...

  4. Unity Shader 学习之旅

    Unity Shader 学习之旅 unityshader图形图像 纸上学来终觉浅,绝知此事要躬行 美丽的梦和美丽的诗一样 都是可遇而不可求的——席慕蓉 一.渲染流水线 示例图 Tips:什么是 GP ...

  5. Unity Shader 学习之旅之SurfaceShader

    Unity Shader 学习之旅之SurfaceShader unity shader 图形图像  如果大地的每个角落都充满了光明 谁还需要星星,谁还会 在夜里凝望 寻找遥远的安慰——江河 官方文档 ...

  6. Unity shader学习之阴影,衰减统一处理

    使用unity AutoLight.cginc文件里的内置函数 UNITY_LIGHT_ATTENUATION shader如下: // Upgrade NOTE: replaced 'mul(UNI ...

  7. Unity shader学习之阴影

    Unity阴影采用的是 shadow map 的技术,即把摄像机放到光源位置上,看不到的地方就有阴影. 前向渲染中,若一光源开启了阴影,Unity会计算它的阴影映射纹理(shadow map),它其实 ...

  8. 第四章 开始Unity Shader学习之旅(2)

    目录 1. 强大的援手:Unity提供的内置文件和变量 1.1 内置的包含文件 1.2 内置的变量 2. Unity提供的Cg/HLSL语义 2.1 什么是语义 2.2 Unity支持的语义 2.3 ...

  9. 第四章 开始Unity Shader学习之旅(1)

    1. 一个最简单的顶点/片元着色器 现在,我们正式开始学习如何编写Unity Shader,更准确的说是,学习如何编写顶点/片元着色器 2.顶点/片元着色器的基本结构 我们在以前已经讲过了Unity ...

随机推荐

  1. [development][libhtp] libhtp 启用debug模式

    可以使用 ./configure --help 查看帮助. 可以通过参数, 修改配置. 即对应的Makefile内容. 也可以修改 configure.ac 里的内容, help中的部分内容, 也依赖 ...

  2. [dev][https] 非PFS协商的https的流量的解码

    经过基础调研之后,目前准备确认实现方案,完成对https的解码. 之前的调研,传送门: http://www.cnblogs.com/hugetong/p/6670083.html 1. 需求: 以旁 ...

  3. Python开发【笔记】:PEP 8 编码规范

    PEP 8 编码规范     https://bk.tencent.com/document/bkrule/040101.pdf                                     ...

  4. js一次控制 多个style样式

    ]; m.style.cssText='color:green;background:cyan;' 控制多个过渡效果 m.style.transition='opacity 1s ease-in,ba ...

  5. Activiti(生成25张表)

    有两种方式,第一种是使用默认的配置文件,第二种是指定配置文件: package com.ouyan.activiti.table; import org.activiti.engine.Process ...

  6. Linux 抓包工具:tcpdump

    tcpdump 是一个抓包工具,通常用来分析网络 安装tcpdump命令 [root@mysql test]# yum install -y tcpdump -i 指定网卡 捉取网卡数据包 抓取指定网 ...

  7. sql server 跟踪日志

    1,当SQL Server错误日志很大时,手工运行:  exec sp_cycle_errorlog 系统存储过程,即可使用新的日志文件 sqlserver系统自动调用 exec sp_cycle_e ...

  8. select默认选择后台转过来的option选项

    <select> <option value="#" >--请选择--</option> <option value="1&qu ...

  9. SLAM最近的工作

  10. Scala之隐式转换

    概述 简单说,隐式转换就是:当Scala编译器进行类型匹配时,如果找不到合适的候选,那么隐式转化提供了另外一种途径来告诉编译器如何将当前的类型转换成预期类型. 隐式转换有四种常见的使用场景: 将某一类 ...