关注forwardbase下的

standard.shader

#pragma vertex vertBase
#pragma fragment fragBase
#include "UnityStandardCoreForward.cginc"

跟踪到UnityStandardCoreForward.cginc

#if UNITY_STANDARD_SIMPLE
#include "UnityStandardCoreForwardSimple.cginc"
VertexOutputBaseSimple vertBase (VertexInput v) { return vertForwardBaseSimple(v); }
VertexOutputForwardAddSimple vertAdd (VertexInput v) { return vertForwardAddSimple(v); }
half4 fragBase (VertexOutputBaseSimple i) : SV_Target { return fragForwardBaseSimpleInternal(i); }
half4 fragAdd (VertexOutputForwardAddSimple i) : SV_Target { return fragForwardAddSimpleInternal(i); }
#else
#include "UnityStandardCore.cginc"
VertexOutputForwardBase vertBase (VertexInput v) { return vertForwardBase(v); }
VertexOutputForwardAdd vertAdd (VertexInput v) { return vertForwardAdd(v); }
half4 fragBase (VertexOutputForwardBase i) : SV_Target { return fragForwardBaseInternal(i); }
half4 fragAdd (VertexOutputForwardAdd i) : SV_Target { return fragForwardAddInternal(i); }
#endif

UNITY_STANDARD_SIMPLE开关是简化版意思,进下边的分支

顶点着色器 vertForwardBase

先看输出

struct VertexOutputForwardBase
{
float4 pos : SV_POSITION;
float4 tex : TEXCOORD0;
half3 eyeVec : TEXCOORD1;
half4 tangentToWorldAndParallax[] : TEXCOORD2; // [3x3:tangentToWorld | 1x3:viewDirForParallax]
half4 ambientOrLightmapUV : TEXCOORD5; // SH or Lightmap UV
SHADOW_COORDS()
UNITY_FOG_COORDS() // next ones would not fit into SM2.0 limits, but they are always for SM3.0+
#if UNITY_REQUIRE_FRAG_WORLDPOS
float3 posWorld : TEXCOORD8;
#endif #if UNITY_OPTIMIZE_TEXCUBELOD
#if UNITY_REQUIRE_FRAG_WORLDPOS
half3 reflUVW : TEXCOORD9;
#else
half3 reflUVW : TEXCOORD8;
#endif
#endif UNITY_VERTEX_OUTPUT_STEREO
};
UNITY_REQUIRE_FRAG_WORLDPOS要不要worldpos,要
reflUVW是用来做反射的,要
UNITY_VERTEX_OUTPUT_STEREO 是用来做实例化的,不要
简化成
struct VertexOutputForwardBase
{
float4 pos : SV_POSITION;
float4 tex : TEXCOORD0;
half3 eyeVec : TEXCOORD1;
half4 tangentToWorldAndParallax[] : TEXCOORD2; // [3x3:tangentToWorld | 1x3:viewDirForParallax]
half4 ambientOrLightmapUV : TEXCOORD5; // SH or Lightmap UV
SHADOW_COORDS()
UNITY_FOG_COORDS()
float3 posWorld : TEXCOORD8;
half3 reflUVW : TEXCOORD9;
};
再来看顶点着色器
VertexOutputForwardBase vertForwardBase (VertexInput v)
{
VertexOutputForwardBase o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_OUTPUT(VertexOutputForwardBase, o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); float4 posWorld = mul(unity_ObjectToWorld, v.vertex);
#if UNITY_REQUIRE_FRAG_WORLDPOS
o.posWorld = posWorld.xyz;
#endif
o.pos = UnityObjectToClipPos(v.vertex); o.tex = TexCoords(v);
o.eyeVec = NormalizePerVertexNormal(posWorld.xyz - _WorldSpaceCameraPos);
float3 normalWorld = UnityObjectToWorldNormal(v.normal);
#ifdef _TANGENT_TO_WORLD
float4 tangentWorld = float4(UnityObjectToWorldDir(v.tangent.xyz), v.tangent.w); float3x3 tangentToWorld = CreateTangentToWorldPerVertex(normalWorld, tangentWorld.xyz, tangentWorld.w);
o.tangentToWorldAndParallax[].xyz = tangentToWorld[];
o.tangentToWorldAndParallax[].xyz = tangentToWorld[];
o.tangentToWorldAndParallax[].xyz = tangentToWorld[];
#else
o.tangentToWorldAndParallax[].xyz = ;
o.tangentToWorldAndParallax[].xyz = ;
o.tangentToWorldAndParallax[].xyz = normalWorld;
#endif
//We need this for shadow receving
TRANSFER_SHADOW(o); o.ambientOrLightmapUV = VertexGIForward(v, posWorld, normalWorld); #ifdef _PARALLAXMAP
TANGENT_SPACE_ROTATION;
half3 viewDirForParallax = mul (rotation, ObjSpaceViewDir(v.vertex));
o.tangentToWorldAndParallax[].w = viewDirForParallax.x;
o.tangentToWorldAndParallax[].w = viewDirForParallax.y;
o.tangentToWorldAndParallax[].w = viewDirForParallax.z;
#endif #if UNITY_OPTIMIZE_TEXCUBELOD
o.reflUVW = reflect(o.eyeVec, normalWorld);
#endif UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
UNITY_SETUP_INSTANCE_ID是instance用的,不要
UNITY_INITIALIZE_OUTPUT啥也没干,不要
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO不要
NormalizePerVertexNormal相等于什么都没做
_TANGENT_TO_WORLD是切线空间和世界空间转换矩阵 保留
o.ambientOrLightmapUV = VertexGIForward(v, posWorld, normalWorld);
因为打算用pbr来做角色,没有lightmap,所以这儿等于
o.ambientOrLightmapUV = 0 ;
_PARALLAXMAP视差贴图,不用
去掉分支后代码如下
VertexOutputForwardBase vertForwardBase (VertexInput v)
{
VertexOutputForwardBase o;
float4 posWorld = mul(unity_ObjectToWorld, v.vertex);
o.posWorld = posWorld.xyz;
o.pos = UnityObjectToClipPos(v.vertex);
o.tex = TexCoords(v);
o.eyeVec = NormalizePerVertexNormal(posWorld.xyz - _WorldSpaceCameraPos);
float3 normalWorld = UnityObjectToWorldNormal(v.normal);
float4 tangentWorld = float4(UnityObjectToWorldDir(v.tangent.xyz), v.tangent.w); float3x3 tangentToWorld = CreateTangentToWorldPerVertex(normalWorld, tangentWorld.xyz, tangentWorld.w);
o.tangentToWorldAndParallax[0].xyz = tangentToWorld[0];
o.tangentToWorldAndParallax[1].xyz = tangentToWorld[1];
o.tangentToWorldAndParallax[2].xyz = tangentToWorld[2];
//We need this for shadow receving
TRANSFER_SHADOW(o);
o.ambientOrLightmapUV = 0;
o.reflUVW = reflect(o.eyeVec, normalWorld);
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}

 至此顶点着色器完事,下篇开始像素着色器

 
 
 

unity里standard pbr(一)的更多相关文章

  1. 关于unity里pbr技术和材质 unity5默认shader和传统的对比

    刚开始也不知道什么是pbr (Physically Based Rendering)后来才发现这是一种新的渲染方式 与之对应的是材质是pbs(Physically Based Shader) unit ...

  2. 【Unity Shaders】Unity里的雾效模拟

    写在前面 熟悉Unity的都知道,Unity可以进行基本的雾效模拟.所谓雾效,就是在远离我们视角的方向上,物体看起来像被蒙上了某种颜色(通常是灰色).这种技术的实现实际上非常简单,就是根据物体距离摄像 ...

  3. Unity里的人物驱动/换装备/换武器/换衣服/卡通重定位(转)

    Unity里的人物驱动/换装备/换武器/换衣服/动画重定位 刚学的过程被这个问题困扰最多. 首先,基本的,大家都知道驱动人物需要骨架.绑骨的Mesh和动画(这三个要是不知道的话就得考虑看看计算机图形学 ...

  4. Unity里的Mesh属性

    ----------------------------------------------------------------------------------------------- Mesh ...

  5. 用C# 模拟实现unity里的协程

    注:需要了解C#的迭代器,不然很难理解. 之前面试有被问到unity协程的原理,以及撇开unity用纯C#去实现协程的方法.后来了解一下,确实可以的.趁这会有空,稍微总结一下. 还是结合代码说事吧: ...

  6. unity, access standard shared emission by script

    unity 5.1.1f1 personal 用下面方法在脚本中设置standard shader的emssion: gameObject.GetComponent<MeshRenderer&g ...

  7. standard pbr(二)

    下一步看像素着色器代码 half4 fragBase (VertexOutputForwardBase i) : SV_Target { return fragForwardBaseInternal( ...

  8. Unity里包裹Debug,且不影响Debug的重定向

    Debug.Log, Debug.LogWarning, Debug.LogError在project中常常须要再包裹一层.做些定制.也方便开关Log.但有一个问题时.当用一个类将Debug包裹起来后 ...

  9. FairyGUI编辑器的和unity里的Obj对应关系

    1.在FairyGUI官网上下载好unity的工程,用FairyGUI编辑器打开它的官方案例 2.在FairyGUI编辑器和Unity中,从一个最简单的示例"Bag"着手.     ...

随机推荐

  1. PHP Warning: 的解决方法

    在后台管理,用header("location:");做返回时,总是不能正常返回, Warning: Cannot modify header information - head ...

  2. PS 如何制作Vista的毛玻璃效果

    1 对一个图像的任意一部分新建一个选区   2 对选中区域进行高斯模糊,大小为5像素   3 再次新建一个图层,填充为深蓝色(#E9E7E3),填充为10%-15%.高斯模糊0.5像素.   4 再对 ...

  3. 【经典数据结构】B树与B+树(转)

    本文转载自:http://www.cnblogs.com/yangecnu/p/Introduce-B-Tree-and-B-Plus-Tree.html 维基百科对B树的定义为“在计算机科学中,B树 ...

  4. js 根据所输内容生成助记码

      js生成与中文字符串相对映的拼音首字母串 CreateTime--2017年7月14日16:35:31Author:Marydon 需要工具js文件letter.js,代码如下: /** 作者:梅 ...

  5. mongoDB Java SDK CRUD操作例子

    Example:   package com.sdfwerwer.test; import java.net.UnknownHostException; import com.mongodb.Basi ...

  6. Flask--请求进来后流程

    启动先执行manage.py 中的    app.run() class Flask(_PackageBoundObject): def run(self, host=None, port=None, ...

  7. Eclipse Plugin Installation and Windows User Access Control

    I make Eclipse Plugins and I sell them to developers using Eclipse. Most of the visitors to my web s ...

  8. hdu 4454 Stealing a Cake(计算几何:最短距离、枚举/三分)

    题意:已知起点.圆.矩形,要求计算从起点开始,经过圆(和圆上任一点接触即可),到达矩形的路径的最短距离.(可以穿过园). 分析:没什么好的方法,凭感觉圆上的每个点对应最短距离,应该是一个凸函数,用三分 ...

  9. 转载了个js代码

    document.selection.createRange方法 document.selection.createRange() 根据当前文字选择返回 TextRange 对象,或根据控件选择返回 ...

  10. NGINX不允许向静态文件提交POST方式的请求,否则报405错误(apache中没有出现)

    telnet *.*.*.* 80POST /map/navigation/2011winter/jsn/jsn_20120723_pack/pvf.jsnHTTP/1.1Host:*.*.*.* ( ...