浏览代码

Bring handwritten and graphmade vertex structs in line

/main
Matt Dean 7 年前
当前提交
02a01bed
共有 10 个文件被更改,包括 113 次插入119 次删除
  1. 87
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightCore.cginc
  2. 43
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightFastBlinn.cginc
  3. 24
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightInput.cginc
  4. 33
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightPBR.cginc
  5. 6
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightFastBlinn.shader
  6. 6
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPBR.shader
  7. 2
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/MetallicTest.ShaderGraph.meta
  8. 2
      MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/SpecularTest.ShaderGraph.meta
  9. 14
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderPBR.template
  10. 15
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/AbstractLightweightPBRMasterNode.cs

87
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightCore.cginc


#endif
}
inline void CalculateNormal(half3 normalMap, LightweightVertexOutput i, out half3 normal)
{
#if _NORMALMAP
normal = normalize(half3(dot(normalMap, i.tangentToWorld0), dot(normalMap, i.tangentToWorld1), dot(normalMap, i.tangentToWorld2)));
#else
normal = normalize(i.normal);
#endif
}
half4 OutputColor(half3 color, half alpha)
{
#if defined(_ALPHABLEND_ON) || defined(_ALPHAPREMULTIPLY_ON)

#endif
}
void Vertex(inout VertexInput v);
LightweightVertexOutput o = (LightweightVertexOutput)0;
LightweightVertexOutput o = (LightweightVertexOutput)0;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.normal = normalize(UnityObjectToWorldNormal(v.normal));
o.uv01.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
o.meshUV0.xy = TRANSFORM_TEX(v.texcoord0, _MainTex);
o.uv01.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
o.meshUV0.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
o.hpos = UnityObjectToClipPos(v.vertex);
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.posWS.xyz = worldPos;
o.viewDir.xyz = normalize(_WorldSpaceCameraPos - worldPos);
half3 normal = normalize(UnityObjectToWorldNormal(v.normal));
//${ VertexShaderBody }
o.tangent = normalize(UnityObjectToWorldDir(v.tangent));
o.binormal = cross(o.normal, o.tangent) * v.tangent.w;
//End VertexShaderBody
#if _NORMALMAP
half sign = v.tangent.w * unity_WorldTransformParams.w;
half3 tangent = normalize(UnityObjectToWorldDir(v.tangent));
half3 binormal = cross(normal, tangent) * v.tangent.w;
o.hpos = UnityObjectToClipPos(v.vertex);
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
o.posWS.xyz = worldPos;
o.viewDir.xyz = normalize(_WorldSpaceCameraPos - worldPos);
// Initialize tangetToWorld in column-major to benefit from better glsl matrix multiplication code
o.tangentToWorld0 = half3(tangent.x, binormal.x, normal.x);
o.tangentToWorld1 = half3(tangent.y, binormal.y, normal.y);
o.tangentToWorld2 = half3(tangent.z, binormal.z, normal.z);
#else
o.normal = normal;
#endif
// TODO: change to only support point lights per vertex. This will greatly simplify shader ALU
// TODO: change to only support point lights per vertex. This will greatly simplify shader ALU
half3 diffuse = half3(1.0, 1.0, 1.0);
// pixel lights shaded = min(pixelLights, perObjectLights)
// vertex lights shaded = min(vertexLights, perObjectLights) - pixel lights shaded
// Therefore vertexStartIndex = pixelLightCount; vertexEndIndex = min(vertexLights, perObjectLights)
int vertexLightStart = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
int vertexLightEnd = min(globalLightCount.y, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = vertexLightStart; lightIter < vertexLightEnd; ++lightIter)
{
int lightIndex = unity_4LightIndices0[lightIter];
LightInput lightInput;
INITIALIZE_LIGHT(lightInput, lightIndex);
half3 diffuse = half3(1.0, 1.0, 1.0);
// pixel lights shaded = min(pixelLights, perObjectLights)
// vertex lights shaded = min(vertexLights, perObjectLights) - pixel lights shaded
// Therefore vertexStartIndex = pixelLightCount; vertexEndIndex = min(vertexLights, perObjectLights)
int vertexLightStart = min(globalLightCount.x, unity_LightIndicesOffsetAndCount.y);
int vertexLightEnd = min(globalLightCount.y, unity_LightIndicesOffsetAndCount.y);
for (int lightIter = vertexLightStart; lightIter < vertexLightEnd; ++lightIter)
{
int lightIndex = unity_4LightIndices0[lightIter];
LightInput lightInput;
INITIALIZE_LIGHT(lightInput, lightIndex);
half3 lightDirection;
half atten = ComputeLightAttenuationVertex(lightInput, normal, worldPos, lightDirection);
o.fogCoord.yzw += LightingLambert(diffuse, lightDirection, normal, atten);
}
half3 lightDirection;
half atten = ComputeLightAttenuationVertex(lightInput, o.normal, worldPos, lightDirection);
o.fogCoord.yzw += LightingLambert(diffuse, lightDirection, normal, atten);
}
o.fogCoord.yzw += max(half3(0, 0, 0), ShadeSH9(half4(normal, 1)));
o.fogCoord.yzw += max(half3(0, 0, 0), ShadeSH9(half4(o.normal, 1)));
UNITY_TRANSFER_FOG(o, o.hpos);
return o;
UNITY_TRANSFER_FOG(o, o.hpos);
return o;
}
#endif

43
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightFastBlinn.cginc


return s;
}
void DefineSurface(LightweightVertexOutput i, inout SurfaceFastBlinn s);
void DefineSurface(LightweightVertexOutput i, inout SurfaceFastBlinn o);
SurfaceFastBlinn s = InitializeSurfaceFastBlinn();
DefineSurface(i, s);
SurfaceFastBlinn o = InitializeSurfaceFastBlinn();
DefineSurface(i, o);
clip(s.Alpha - _Cutoff);
clip(o.Alpha - _Cutoff);
half3 normal;
CalculateNormal(s.Normal, i, normal);
half3 normal;
#if _NORMALMAP
half3 tangentToWorld0 = half3(i.tangent.x, i.binormal.x, i.normal.x);
half3 tangentToWorld1 = half3(i.tangent.y, i.binormal.y, i.normal.y);
half3 tangentToWorld2 = half3(i.tangent.z, i.binormal.z, i.normal.z);
normal = normalize(half3(dot(o.Normal, tangentToWorld0), dot(o.Normal, tangentToWorld1), dot(o.Normal, tangentToWorld2)));
#else
normal = normalize(i.normal);
#endif
half3 viewDir = i.viewDir.xyz;
float3 worldPos = i.posWS.xyz;

half4 specularGloss = half4(s.Specular, s.Glossiness);
half4 specularGloss = half4(o.Specular, o.Glossiness);
half3 vertexNormal = half3(i.tangentToWorld0.z, i.tangentToWorld1.z, i.tangentToWorld2.z); // Fix this
half3 vertexNormal = half3(tangentToWorld0.z, tangentToWorld1.z, tangentToWorld2.z); // Fix this
#else
half3 vertexNormal = i.normal;
#endif

#endif
#ifdef LIGHTWEIGHT_SPECULAR_HIGHLIGHTS
half3 color = LightingBlinnPhong(s.Diffuse, specularGloss, lightDirection, normal, viewDir, lightAtten) * lightInput.color;
half3 color = LightingBlinnPhong(o.Diffuse, specularGloss, lightDirection, normal, viewDir, lightAtten) * lightInput.color;
half3 color = LightingLambert(s.Diffuse, lightDirection, normal, lightAtten) * lightInput.color;
half3 color = LightingLambert(o.Diffuse, lightDirection, normal, lightAtten) * lightInput.color;
#endif
#else

#endif
#ifdef LIGHTWEIGHT_SPECULAR_HIGHLIGHTS
color += LightingBlinnPhong(s.Diffuse, specularGloss, lightDirection, normal, viewDir, lightAtten) * lightData.color;
color += LightingBlinnPhong(o.Diffuse, specularGloss, lightDirection, normal, viewDir, lightAtten) * lightData.color;
color += LightingLambert(s.Diffuse, lightDirection, normal, lightAtten) * lightData.color;
color += LightingLambert(o.Diffuse, lightDirection, normal, lightAtten) * lightData.color;
color += s.Emission;
color += o.Emission;
color += (DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv01.zw)) + i.fogCoord.yzw) * s.Diffuse;
color += (DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv01.zw)) + i.fogCoord.yzw) * o.Diffuse;
color += i.fogCoord.yzw * s.Diffuse;
color += i.fogCoord.yzw * o.Diffuse;
color += texCUBE(_Cube, reflectVec).rgb * s.Specular;
color += texCUBE(_Cube, reflectVec).rgb * o.Specular;
color += reflectionProbe.rgb * (reflectionProbe.a * unity_SpecCube0_HDR.x) * s.Specular;
color += reflectionProbe.rgb * (reflectionProbe.a * unity_SpecCube0_HDR.x) * o.Specular;
return OutputColor(color, s.Alpha);
return OutputColor(color, o.Alpha);
};
#endif

24
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightInput.cginc


struct LightweightVertexInput
{
float4 vertex : POSITION;
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
float4 texcoord0 : TEXCOORD0;
float2 lightmapUV : TEXCOORD1;
UNITY_VERTEX_INPUT_INSTANCE_ID
};

float4 uv01 : TEXCOORD0; // uv01.xy: uv0, uv01.zw: uv1 // uv
float4 posWS : TEXCOORD1;
#if _NORMALMAP
half3 tangentToWorld0 : TEXCOORD2; // tangentToWorld matrix
half3 tangentToWorld1 : TEXCOORD3; // tangentToWorld matrix
half3 tangentToWorld2 : TEXCOORD4; // tangentToWorld matrix
#else
half3 normal : TEXCOORD2;
#endif
half4 viewDir : TEXCOORD5; // xyz: viewDir
half4 fogCoord : TEXCOORD6; // x: fogCoord, yzw: vertexColor
float4 hpos : SV_POSITION;
float4 posWS : TEXCOORD0;
float4 viewDir : TEXCOORD1; // xyz: viewDir
half4 fogCoord : TEXCOORD2; // x: fogCoord, yzw: vertexColor
half3 normal : TEXCOORD3;
half4 meshUV0 : TEXCOORD4; // uv01.xy: uv0, uv01.zw: uv1 // uv
half3 tangent : TEXCOORD5;
half3 binormal : TEXCOORD6;
float4 hpos : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};

33
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/CGIncludes/LightweightPBR.cginc


half4 LightweightFragmentPBR(LightweightVertexOutput i) : SV_Target
{
SurfacePBR s = InitializeSurfacePBR();
DefineSurface(i, s);
SurfacePBR o = InitializeSurfacePBR();
DefineSurface(i, o);
float2 uv = i.uv01.xy;
float2 lightmapUV = i.uv01.zw;
//float2 uv = i.mesUV0.xy;
float2 lightmapUV = i.meshUV0.zw;
half3 diffColor = MetallicSetup(uv, s, specColor, smoothness, oneMinusReflectivity);
half3 diffColor = MetallicSetup(i.meshUV0.xy, o, specColor, smoothness, oneMinusReflectivity);
half3 diffColor = SpecularSetup(uv, s, specColor, smoothness, oneMinusReflectivity);
half3 diffColor = SpecularSetup(i.meshUV0.xy, o, specColor, smoothness, oneMinusReflectivity);
diffColor = PreMultiplyAlpha(diffColor, s.Alpha, oneMinusReflectivity, /*out*/ s.Alpha);
diffColor = PreMultiplyAlpha(diffColor, o.Alpha, oneMinusReflectivity, /*out*/ o.Alpha);
half3 normal;
CalculateNormal(s.Normal, i, normal);
half3 normal;
#if _NORMALMAP
half3 tangentToWorld0 = half3(i.tangent.x, i.binormal.x, i.normal.x);
half3 tangentToWorld1 = half3(i.tangent.y, i.binormal.y, i.normal.y);
half3 tangentToWorld2 = half3(i.tangent.z, i.binormal.z, i.normal.z);
normal = normalize(half3(dot(o.Normal, tangentToWorld0), dot(o.Normal, tangentToWorld1), dot(o.Normal, tangentToWorld2)));
#else
normal = normalize(i.normal);
#endif
UnityIndirect indirectLight = LightweightGI(lightmapUV, i.fogCoord.yzw, reflectVec, s.Occlusion, perceptualRoughness);
UnityIndirect indirectLight = LightweightGI(lightmapUV, i.fogCoord.yzw, reflectVec, o.Occlusion, perceptualRoughness);
// PBS
// grazingTerm = F90

#ifdef _SHADOWS
#if _NORMALMAP
half3 vertexNormal = half3(i.tangentToWorld0.z, i.tangentToWorld1.z, i.tangentToWorld2.z); // Fix this
half3 vertexNormal = half3(tangentToWorld0.z, tangentToWorld1.z, tangentToWorld2.z); // Fix this
#else
half3 vertexNormal = i.normal;
#endif

}
#endif
color += s.Emission;
color += o.Emission;
return OutputColor(color, s.Alpha);
return OutputColor(color, o.Alpha);
}
#endif

6
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightFastBlinn.shader


void DefineSurface(LightweightVertexOutput i, inout SurfaceFastBlinn s)
{
// Albedo
float4 c = tex2D(_MainTex, i.uv01.xy);
float4 c = tex2D(_MainTex, i.meshUV0.xy);
s.Diffuse = LIGHTWEIGHT_GAMMA_TO_LINEAR(c.rgb) * _Color.rgb;
// Specular
#ifdef _SPECGLOSSMAP

#endif
#elif defined(_SPECGLOSSMAP_BASE_ALPHA)
s.Specular.rgb = LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_SpecGlossMap, i.uv01.xy).rgb) * _SpecColor.rgb;
s.Specular.rgb = LIGHTWEIGHT_GAMMA_TO_LINEAR(tex2D(_SpecGlossMap, i.meshUV0.xy).rgb) * _SpecColor.rgb;
s.Glossiness = s.Alpha;
#else
s.Specular = _SpecColor.rgb;

#if _NORMALMAP
s.Normal = UnpackNormal(tex2D(_BumpMap, i.uv01.xy));
s.Normal = UnpackNormal(tex2D(_BumpMap, i.meshUV0.xy));
#endif
// Emission
#ifndef _EMISSION

6
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/LightweightPBR.shader


void DefineSurface(LightweightVertexOutput i, inout SurfacePBR s)
{
// Albedo
float4 c = tex2D(_MainTex, i.uv01.xy);
float4 c = tex2D(_MainTex, i.meshUV0.xy);
s.Albedo = LIGHTWEIGHT_GAMMA_TO_LINEAR(c.rgb) * _Color.rgb;
// Metallic
#ifdef _METALLICSPECGLOSSMAP

#endif
// Normal
#if _NORMALMAP
s.Normal = UnpackNormal(tex2D(_BumpMap, i.uv01.xy));
s.Normal = UnpackNormal(tex2D(_BumpMap, i.meshUV0.xy));
s.Occlusion = Occlusion(i.uv01.xy);
s.Occlusion = Occlusion(i.meshUV0.xy);
// Emission
#ifndef _EMISSION
s.Emission = 0;

2
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/MetallicTest.ShaderGraph.meta


fileFormatVersion: 2
guid: 02a3ca69ec995e94aa47105621a17e4c
timeCreated: 1505501440
timeCreated: 1505507124
licenseType: Pro
ShaderImporter:
externalObjects: {}

2
MaterialGraphProject/Assets/SRP/ScriptableRenderPipeline/LightweightPipeline/Shaders/SpecularTest.ShaderGraph.meta


fileFormatVersion: 2
guid: a96bafc6e4c6e7c4ca1fb43c905d7948
timeCreated: 1505499391
timeCreated: 1505507152
licenseType: Pro
ShaderImporter:
externalObjects: {}

14
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightSubshaderPBR.template


UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
${VertexShaderBody}
// TODO - Handle lightmaps
//o.uv01.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
o.meshUV0.xy = v.texcoord0.xy;//TRANSFORM_TEX(v.texcoord0, _MainTex);
//o.uv01.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
o.meshUV0.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
o.hpos = UnityObjectToClipPos(v.vertex);
${VertexShaderBody}
o.hpos = UnityObjectToClipPos(v.vertex);
o.viewDir.xyz = normalize(_WorldSpaceCameraPos - worldPos);
// TODO: change to only support point lights per vertex. This will greatly simplify shader ALU

15
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/LightweightPipeline/AbstractLightweightPBRMasterNode.cs


node.GeneratePropertyUsages(propertyUsages, mode);
}
int vertInputIndex = 3;
int vertOutputIndex = 4;
int vertInputIndex = 2;
int vertOutputIndex = 5;
shaderInputVisitor.AddShaderChunk("half4 tangent : TANGENT;", true);
shaderInputVisitor.AddShaderChunk("half4 color : COLOR;", true);
shaderInputVisitor.AddShaderChunk("half4 tangent : TEXCOORD2;", true);
shaderInputVisitor.AddShaderChunk("half4 color : COLOR;", true);
// Need these for lighting
shaderOutputVisitor.AddShaderChunk("float4 posWS : TEXCOORD0;", true);

shaderOutputVisitor.AddShaderChunk("half4 meshUV0 : TEXCOORD4;", true);
shaderOutputVisitor.AddShaderChunk("float4 hpos : SV_POSITION;", true);
bool requiresBitangent = activeNodeList.OfType<IMayRequireBitangent>().Any(x => x.RequiresBitangent());

if(uvIndex != 0)
{
shaderInputVisitor.AddShaderChunk(string.Format("half4 texcoord{0} : TEXCOORD{1};", uvIndex, vertInputIndex), true);
shaderOutputVisitor.AddShaderChunk(string.Format("half4 meshUV{0} : TEXCOORD{1};", uvIndex, vertOutputIndex), true);
vertexShaderBlock.AddShaderChunk(string.Format("o.meshUV{0} = v.texcoord{1};", uvIndex, uvIndex), true);
vertOutputIndex++;
shaderOutputVisitor.AddShaderChunk(string.Format("half4 meshUV{0} : TEXCOORD{1};", uvIndex, vertOutputIndex), true);
vertexShaderBlock.AddShaderChunk(string.Format("o.meshUV{0} = v.texcoord{1};", uvIndex, uvIndex), true);
vertOutputIndex++;
}
}

正在加载...
取消
保存