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. Uniform Resource Name Server

    HTTP The Definitive Guide 按址标识 identify by address 按名标识 identify by name Domain Name Server Uniform ...

  2. cocos2dx 常用的构建工具

    理编辑工具Physics Editing ToolsMekanimo 网址:http://www.mekanimo.net/PhysicsBench 网址:http://www.cocos2d-iph ...

  3. oracle 监听报错the information provided for this listener is currently in use by other software on this computer

    use another port number: the information provided for this listener is currently in use by other sof ...

  4. activemq安装使用教程

    一.下载安装 下载地址:http://activemq.apache.org/activemq-5158-release.html 然后解压即可,apache的东西解压后就可以使用了. 二.启动 在安 ...

  5. 【Python基础】random 的高级玩法

    random 模块的高级玩法 1.python 随机产生姓名 方式一: import random xing = [ '赵', '钱', '孙', '李', '周', '吴', '郑', '王', ' ...

  6. Redis分布式锁服务(转)

    原文:http://www.cnblogs.com/mushroom/p/4752499.html 概述 在多线程环境下,通常会使用锁来保证有且只有一个线程来操作共享资源.比如: object obj ...

  7. PostgreSQL 自动输入密码

    在 Shell 命令行中,使用 postgresql-client 连接 PostgreSQL 数据库时,每次都要输入密码.如果要写 Shell Script,做一些类似于备份的自动化管理工作,每次都 ...

  8. ConcurrentHashMap详解

    public class ConcurrentHashMap<K,V>extends AbstractMap<K,V>implements ConcurrentMap<K ...

  9. Performance Tuning MySQL

    通常来说,MySQL性能调优是非常复杂的一件事,不是简单的修改参数就可以完成的.需要综合考虑.而且找出性能瓶颈也非易事.但是通常我们有以下的几种方法找到蛛丝马迹.通过下面的几种方法发现瓶颈以后,我们才 ...

  10. MySQL忘记root密码--不重启mysqd重置root密码

    先提个问题:如何不重启mysqld,且没有权限修改用户账号和权限的情况下,如何重新设置root密码?不知道没关系,在此之前我也是不知道如何操作的,先看看下面的几种重置root密码的方法. 1.skip ...