浏览代码

Use PBS Standard BRDF in the example shader

/main
Aras Pranckevicius 8 年前
当前提交
d49289a8
共有 3 个文件被更改,包括 129 次插入56 次删除
  1. 3
      Assets/BasicRenderLoopTutorial/BasicRenderLoop.cs
  2. 65
      Assets/BasicRenderLoopTutorial/BasicRenderLoopScene.unity
  3. 117
      Assets/BasicRenderLoopTutorial/BasicRenderLoopShader.shader

3
Assets/BasicRenderLoopTutorial/BasicRenderLoop.cs


// Very basic scriptable rendering loop example:
// - Use with BasicRenderLoopShader.shader (the loop expects "BasicPass" pass type to exist)
// - Supports up to 8 enabled lights in the scene (directional, point or spot)
// - Does the same physically based BRDF as the Standard shader
// - This loop also does not setup lightmaps, light probes or reflection probes
// - This loop also does not setup lightmaps, light probes, reflection probes or light cookies
[ExecuteInEditMode]
public class BasicRenderLoop : MonoBehaviour

65
Assets/BasicRenderLoopTutorial/BasicRenderLoopScene.unity


m_ReflectionIntensity: 0.5
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.09067552, g: 0.11147809, b: 0.14205924, a: 0.5}
m_IndirectSpecularColor: {r: 0.090675384, g: 0.11147798, b: 0.14205901, a: 0.5}
--- !u!157 &3
LightmapSettings:
m_ObjectHideFlags: 0

m_LocalScale: {x: 2, y: 2, z: 2}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 11
m_RootOrder: 8
m_LocalEulerAnglesHint: {x: -0.33200002, y: -365.245, z: 189.77199}
--- !u!1 &531853420
GameObject:

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 4
m_RootOrder: 3
--- !u!1 &722899210
GameObject:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
serializedVersion: 5
m_Component:
- component: {fileID: 722899211}
m_Layer: 0
m_Name: Lighting
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &722899211
Transform:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 722899210}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 2011495864}
- {fileID: 1900527475}
- {fileID: 1677622458}
m_Father: {fileID: 0}
m_RootOrder: 10
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1280249095
GameObject:
m_ObjectHideFlags: 0

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 2
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1405931297
GameObject:

m_LocalScale: {x: 2, y: 2, z: 2}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 12
m_RootOrder: 9
m_LocalEulerAnglesHint: {x: 30.332, y: -620.687, z: 71.02901}
--- !u!1 &1447851825
GameObject:

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 64.917, y: -28.537, z: 220.46599}
--- !u!1 &1596459099
GameObject:

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 6
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1677622456
GameObject:

m_LocalPosition: {x: -5.03, y: 1.37, z: 0.97}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 9
m_Father: {fileID: 722899211}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 18.036001, y: 49.154003, z: -0.014}
--- !u!1 &1714697412
GameObject:

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 3
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1749252039
GameObject:

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 7
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: -24.621, y: 19.384, z: 34.179}
--- !u!1 &1900527473
GameObject:

m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1900527473}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_Father: {fileID: 0}
m_RootOrder: 8
m_Father: {fileID: 722899211}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &2011495862
GameObject:

m_LocalPosition: {x: -9.080719, y: 3.299361, z: 1.6153903}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_Father: {fileID: 722899211}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1 &2031315345
GameObject:

m_LocalScale: {x: 2, y: 2, z: 2}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 10
m_RootOrder: 7
m_LocalEulerAnglesHint: {x: -6.8960004, y: -33.794003, z: -20.829}

117
Assets/BasicRenderLoopTutorial/BasicRenderLoopShader.shader


// Example shader for a scriptable render loop that calculates multiple lights
// in a single forward-rendered shading pass.
// in a single forward-rendered shading pass. Uses same PBR shading model as the
// Standard shader.
//
// The parameters and inspector of the shader are the same as Standard shader,
// for easier experimentation.
Shader "RenderLoop/Basic/Standard"

ZWrite[_ZWrite]
CGPROGRAM
#pragma shader_feature _METALLICGLOSSMAP
#include "UnityStandardBRDF.cginc"
#include "UnityStandardUtils.cginc"
// Global lighting data (setup from C# code once per frame).
CBUFFER_START(GlobalLightData)

CBUFFER_END
struct v2f
// Surface inputs for evaluating Standard BRDF
struct SurfaceInputData
float2 uv : TEXCOORD0;
float3 positionWS : TEXCOORD1;
float3 normalWS : TEXCOORD2;
float4 hpos : SV_POSITION;
half3 diffColor, specColor;
half oneMinusReflectivity, smoothness;
float4 _MainTex_ST;
v2f vert(appdata_base v)
{
v2f o;
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
o.hpos = UnityObjectToClipPos(v.vertex);
o.positionWS = mul(unity_ObjectToWorld, v.vertex).xyz;
o.normalWS = UnityObjectToWorldNormal(v.normal);
return o;
}
// Compute illumination from one light, given attenuation
half3 ComputeLighting(int idx, half3 dirToLight, half3 normalWS, half4 diffuseAlbedo, float atten)
{
half NdotL = max(dot(normalWS, dirToLight), 0.0);
// diffuse
half3 color = NdotL * diffuseAlbedo.rgb * globalLightColor[idx].rgb;
return color * atten;
}
half3 ComputeOneLight(int idx, float3 positionWS, half3 normalWS, half4 diffuseAlbedo)
half3 EvaluateOneLight(int idx, float3 positionWS, half3 normalWS, half3 eyeVec, SurfaceInputData s)
// direction to light
float3 dirToLight = globalLightPos[idx].xyz;
dirToLight -= positionWS * globalLightPos[idx].w;
// distance attenuation

if (globalLightPos[idx].w != 0 && distSqr > globalLightAtten[idx].w) att = 0.0; // set to 0 if outside of range
distSqr = max(distSqr, 0.000001); // don't produce NaNs if some vertex position overlaps with the light
dirToLight *= rsqrt(distSqr);
// spot angle attenuation
// spotlight angular attenuation
return min(ComputeLighting(idx, dirToLight, normalWS, diffuseAlbedo, att), 1.0);
// Super simple diffuse lighting instead of PBR would be this:
//half ndotl = max(dot(normalWS, dirToLight), 0.0);
//half3 color = ndotl * s.diffColor * globalLightColor[idx].rgb;
//return color * att;
// Fill in light & indirect structures, and evaluate Standard BRDF
UnityLight light;
light.color = globalLightColor[idx].rgb * att;
light.dir = dirToLight;
UnityIndirect indirect;
indirect.diffuse = 0;
indirect.specular = 0;
half4 c = BRDF1_Unity_PBS(s.diffColor, s.specColor, s.oneMinusReflectivity, s.smoothness, normalWS, -eyeVec, light, indirect);
return c.rgb;
}

{
half3 res;
half4 normal = half4(n, 1);
return res;
}
// Vertex shader
struct v2f
{
float2 uv : TEXCOORD0;
float3 positionWS : TEXCOORD1;
float3 normalWS : TEXCOORD2;
float4 hpos : SV_POSITION;
};
return res;
float4 _MainTex_ST;
v2f vert(appdata_base v)
{
v2f o;
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
o.hpos = UnityObjectToClipPos(v.vertex);
o.positionWS = mul(unity_ObjectToWorld, v.vertex).xyz;
o.normalWS = UnityObjectToWorldNormal(v.normal);
return o;
sampler2D _MetallicGlossMap;
float _Metallic;
float _Glossiness;
// Main pixel shader
// Fragment shader
i.normalWS = normalize(i.normalWS);
half3 eyeVec = normalize(i.positionWS - _WorldSpaceCameraPos);
// Sample textures
half2 metalSmooth;
#ifdef _METALLICGLOSSMAP
metalSmooth = tex2D(_MetallicGlossMap, i.uv).ra;
#else
metalSmooth.r = _Metallic;
metalSmooth.g = _Glossiness;
#endif
// Fill in surface input structure
SurfaceInputData s;
s.diffColor = DiffuseAndSpecularFromMetallic(diffuseAlbedo.rgb, metalSmooth.x, s.specColor, s.oneMinusReflectivity);
s.smoothness = metalSmooth.y;
color.rgb += EvaluateSH(i.normalWS) * diffuseAlbedo.rgb;
UnityLight light;
light.color = 0;
light.dir = 0;
UnityIndirect indirect;
indirect.diffuse = EvaluateSH(i.normalWS);
indirect.specular = 0;
color.rgb += BRDF1_Unity_PBS(s.diffColor, s.specColor, s.oneMinusReflectivity, s.smoothness, i.normalWS, -eyeVec, light, indirect);
color.rgb += ComputeOneLight(il, i.positionWS, i.normalWS, diffuseAlbedo);
color.rgb += EvaluateOneLight(il, i.positionWS, i.normalWS, eyeVec, s);
}
return color;
}

正在加载...
取消
保存