浏览代码

Fixed a shader compiler error due to glsl optimiser not understand interpolator arrays. Added shader keywords for cascades and vertex lighting.

/vr_sandbox
Felipe Lira 8 年前
当前提交
52030767
共有 6 个文件被更改,包括 82 次插入77 次删除
  1. 18
      Assets/LowEndMobilePipeline/LowEndMobilePipeline.cs
  2. 36
      Assets/LowEndMobilePipeline/LowEndMobilePipeline.shader
  3. 64
      Assets/LowEndMobilePipeline/LowEndMobilePipelineCore.cginc
  4. 12
      ProjectSettings/GraphicsSettings.asset
  5. 27
      ProjectSettings/ProjectSettings.asset
  6. 2
      ProjectSettings/ProjectVersion.txt

18
Assets/LowEndMobilePipeline/LowEndMobilePipeline.cs


using System;
using System;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;

cmd.SetGlobalVectorArray("globalLightAtten", m_LightAttenuations);
cmd.SetGlobalVectorArray("globalLightSpotDir", m_LightSpotDirections);
cmd.SetGlobalVector("globalLightCount", new Vector4(pixelLightCount, totalLightCount, 0.0f, 0.0f));
SetShadowKeywords(cmd);
SetShaderKeywords(cmd);
context.ExecuteCommandBuffer(cmd);
cmd.Dispose();
}

};
var setupShadow = new CommandBuffer() {name = "SetupShadowShaderConstants"};
SetShadowKeywords(setupShadow);
SetShadowKeywords(setupShadow);
void SetShadowKeywords(CommandBuffer cmd)
void SetShaderKeywords(CommandBuffer cmd)
if (m_Asset.SupportsVertexLight)
cmd.EnableShaderKeyword("_VERTEX_LIGHTS");
else
cmd.DisableShaderKeyword("_VERTEX_LIGHTS");
if (m_Asset.CascadeCount == 1)
cmd.DisableShaderKeyword("_SHADOW_CASCADES");
else
cmd.EnableShaderKeyword("_SHADOW_CASCADES");
switch (m_Asset.CurrShadowType)
{
case ShadowType.NO_SHADOW:

36
Assets/LowEndMobilePipeline/LowEndMobilePipeline.shader


#pragma multi_compile _ LIGHTMAP_ON
#pragma multi_compile _ HARD_SHADOWS SOFT_SHADOWS
#pragma multi_compile _ _SHADOW_CASCADES
#pragma multi_compile _ _VERTEX_LIGHTS
#pragma multi_compile_fog
#pragma only_renderers d3d9 d3d11 d3d11_9x glcore gles gles3 metal
#pragma enable_d3d11_debug_symbols

o.uv01.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
o.hpos = UnityObjectToClipPos(v.vertex);
o.posWS.xyz = mul(unity_ObjectToWorld, v.vertex).xyz;
o.posWS = mul(unity_ObjectToWorld, v.vertex).xyz;
o.viewDir.xyz = normalize(_WorldSpaceCameraPos - o.posWS.xyz);
o.viewDir.xyz = normalize(_WorldSpaceCameraPos - o.posWS);
half3 normal = normalize(UnityObjectToWorldNormal(v.normal));
#if _NORMALMAP

// Initialize tangetToWorld in column-major to benefit from better glsl matrix multiplication code
o.tangentToWorld[0] = half3(tangent.x, binormal.x, normal.x);
o.tangentToWorld[1] = half3(tangent.y, binormal.y, normal.y);
o.tangentToWorld[2] = half3(tangent.z, binormal.z, normal.z);
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);
#if _VERTEX_LIGHTS
o.fogCoord.yzw += EvaluateOneLight(lightInput, diffuseAndSpecular.rgb, diffuseAndSpecular, normal, o.posWS.xyz, o.viewDir.xyz);
o.fogCoord.yzw += EvaluateOneLight(lightInput, diffuseAndSpecular.rgb, diffuseAndSpecular, normal, o.posWS, o.viewDir.xyz);
#endif
#ifndef _SHADOW_CASCADES
o.shadowCoord = mul(_WorldToShadow[0], float4(o.posWS, 1.0)).xyz;
#endif
#ifndef LIGHTMAP_ON
o.fogCoord.yzw += max(half3(0, 0, 0), ShadeSH9(half4(normal, 1)));

LightInput mainLight;
INITIALIZE_LIGHT(mainLight, 0);
float3 posWorld = i.posWS.xyz;
half3 viewDir = i.viewDir.xyz;
#if DEBUG_CASCADES
return half4(EvaluateMainLight(mainLight, diffuse, specularGloss, normal, i.posWS, viewDir), 1.0);
#endif
half3 directColor = EvaluateMainLight(mainLight, diffuse, specularGloss, normal, i.posWS, viewDir);
half3 viewDir = i.viewDir.xyz;
half3 directColor = EvaluateOneLight(mainLight, diffuse, specularGloss, normal, i.posWS, viewDir);
#if defined(HARD_SHADOWS) || defined(SOFT_SHADOWS)
directColor *= ComputeShadowAttenuation(i);
#endif
directColor += EvaluateOneLight(additionalLight, diffuse, specularGloss, normal, posWorld, viewDir);
directColor += EvaluateOneLight(additionalLight, diffuse, specularGloss, normal, i.posWS, viewDir);
}
half3 color = directColor + indirectDiffuse + _EmissionColor;

64
Assets/LowEndMobilePipeline/LowEndMobilePipelineCore.cginc


struct v2f
{
float4 uv01 : TEXCOORD0; // uv01.xy: uv0, uv01.zw: uv1
float4 posWS : TEXCOORD1; // xyz: posWorld
float3 posWS : TEXCOORD1;
half3 tangentToWorld[3] : TEXCOORD2; // tangentToWorld matrix
half3 tangentToWorld0 : TEXCOORD2; // tangentToWorld matrix
half3 tangentToWorld1 : TEXCOORD3; // tangentToWorld matrix
half3 tangentToWorld2 : TEXCOORD4; // tangentToWorld matrix
float4 hpos : SV_POSITION;
#ifndef _SHADOW_CASCADES
float3 shadowCoord : TEXCOORD7;
#endif
float4 hpos : SV_POSITION;
};
// The variables are very similar to built-in unity_LightColor, unity_LightPosition,

// glsl compiler will generate underperforming code by using a row-major pre multiplication matrix: mul(normalmap, i.tangentToWorld)
// i.tangetToWorld was initialized as column-major in vs and here dot'ing individual for better performance.
// The code below is similar to post multiply: mul(i.tangentToWorld, normalmap)
normal = half3(dot(normalmap, i.tangentToWorld[0]), dot(normalmap, i.tangentToWorld[1]), dot(normalmap, i.tangentToWorld[2]));
normal = half3(dot(normalmap, i.tangentToWorld0), dot(normalmap, i.tangentToWorld1), dot(normalmap, i.tangentToWorld2));
#else
normal = normalize(i.normal);
#endif

vDirShadowSplitSphereSqRadii.y = _DirShadowSplitSpheres[1].w;
vDirShadowSplitSphereSqRadii.z = _DirShadowSplitSpheres[2].w;
vDirShadowSplitSphereSqRadii.w = _DirShadowSplitSpheres[3].w;
fixed4 weights = float4(distances2 < vDirShadowSplitSphereSqRadii);
fixed4 weights = fixed4(distances2 < vDirShadowSplitSphereSqRadii);
return 4 - dot(weights, float4(4, 3, 2, 1));
return 4 - dot(weights, fixed4(4, 3, 2, 1));
}
inline half ShadowAttenuation(half2 shadowCoord, half shadowCoordDepth)

#endif
}
inline half ShadowPCF(half4 shadowCoord)
inline half ShadowPCF(half3 shadowCoord)
{
// TODO: simulate textureGatherOffset not available, simulate it
half2 offset = half2(0, 0);

float distanceSqr = max(dot(posToLight, posToLight), 0.001);
float lightAtten = 1.0 / (1.0 + distanceSqr * lightInput.atten.z);
float3 lightDir = posToLight * rsqrt(distanceSqr);
float SdotL = saturate(dot(lightInput.spotDir.xyz, lightDir));
half3 lightDir = posToLight * rsqrt(distanceSqr);
half SdotL = saturate(dot(lightInput.spotDir.xyz, lightDir));
float cutoff = step(distanceSqr, lightInput.atten.w);
half cutoff = step(distanceSqr, lightInput.atten.w);
float NdotL = saturate(dot(normal, lightDir));
half NdotL = saturate(dot(normal, lightDir));
half3 halfVec = normalize(lightDir + viewDir);
half NdotH = saturate(dot(normal, halfVec));

#endif
}
inline half3 EvaluateMainLight(LightInput lightInput, half3 diffuseColor, half4 specularGloss, half3 normal, float4 posWorld, half3 viewDir)
inline half ComputeShadowAttenuation(v2f i)
half3 color = EvaluateOneLight(lightInput, diffuseColor, specularGloss, normal, posWorld, viewDir);
#if defined(HARD_SHADOWS) || defined(SOFT_SHADOWS)
int cascadeIndex = ComputeCascadeIndex(posWorld);
half shadowAttenuation = 1.0;
if (cascadeIndex < 4)
{
float4 shadowCoord = mul(_WorldToShadow[cascadeIndex], float4(posWorld.xyz, 1.0));
shadowCoord.z = saturate(shadowCoord.z);
#ifdef SOFT_SHADOWS
shadowAttenuation = ShadowPCF(shadowCoord);
#else
shadowAttenuation = ShadowAttenuation(shadowCoord.xy, shadowCoord.z);
half3 shadowCoord;
#ifndef _SHADOW_CASCADES
shadowCoord = i.shadowCoord;
#else
int cascadeIndex = ComputeCascadeIndex(i.posWS);
if (cascadeIndex < 4)
shadowCoord = mul(_WorldToShadow[cascadeIndex], half4(i.posWS, 1.0)).xyz;
else
return 1.0;
}
#if DEBUG_CASCADES
half3 cascadeColors[MAX_SHADOW_CASCADES] = {half3(1.0, 0.0, 0.0), half3(0.0, 1.0, 0.0), half3(0.0, 0.0, 1.0), half3(1.0, 0.0, 1.0)};
return cascadeColors[cascadeIndex] * diffuseColor * max(shadowAttenuation, 0.5);
#endif
shadowCoord.z = saturate(shadowCoord.z);
return color * shadowAttenuation;
#ifdef SOFT_SHADOWS
return ShadowPCF(shadowCoord);
return color;
return ShadowAttenuation(shadowCoord.xy, shadowCoord.z);
#endif
}

12
ProjectSettings/GraphicsSettings.asset


- {fileID: 10782, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 4800000, guid: 595434cc3b6405246b6cd3086d0b6f7d, type: 3}
- {fileID: 16000, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 16001, guid: 0000000000000000f000000000000000, type: 0}
m_CustomRenderPipeline: {fileID: 11400000, guid: e185fecca3c73cd47a09f1092663ef32,
m_CustomRenderPipeline: {fileID: 11400000, guid: 877878ed40e8ad94095582ff91179016,
type: 2}
m_TransparencySortMode: 0
m_TransparencySortAxis: {x: 0, y: 0, z: 1}

- serializedVersion: 4
- serializedVersion: 5
m_BuildTarget: 1
m_Tier: 0
m_Settings:

useHDR: 1
useDetailNormalMap: 1
useCascadedShadowMaps: 1
prefer32BitShadowMaps: 0
- serializedVersion: 4
- serializedVersion: 5
m_BuildTarget: 1
m_Tier: 1
m_Settings:

useHDR: 1
useDetailNormalMap: 1
useCascadedShadowMaps: 1
prefer32BitShadowMaps: 0
- serializedVersion: 4
- serializedVersion: 5
m_BuildTarget: 1
m_Tier: 2
m_Settings:

useHDR: 1
useDetailNormalMap: 1
useCascadedShadowMaps: 1
prefer32BitShadowMaps: 0
useDitherMaskForAlphaBlendedShadows: 1
m_Automatic: 0
m_LightmapStripping: 0

27
ProjectSettings/ProjectSettings.asset


serializedVersion: 11
productGUID: 900e9e40d20cd3e44ad40e5b00239dcc
AndroidProfiler: 0
defaultScreenOrientation: 4
defaultScreenOrientation: 3
companyName: DefaultCompany
productName: dev
companyName: Unity
productName: srp
defaultCursor: {fileID: 0}
cursorHotspot: {x: 0, y: 0}
m_SplashScreenBackgroundColor: {r: 0.13333334, g: 0.17254902, b: 0.21176471, a: 1}

defaultScreenWidthWeb: 960
defaultScreenHeightWeb: 600
m_StereoRenderingPath: 0
m_ActiveColorSpace: 1
m_ActiveColorSpace: 0
m_MTRendering: 1
m_MobileMTRendering: 0
m_StackTraceTypes: 010000000100000001000000010000000100000001000000

xboxEnableFitness: 0
visibleInBackground: 1
allowFullscreenSwitch: 1
graphicsJobMode: 0
macFullscreenMode: 2
d3d9FullscreenMode: 1
d3d11FullscreenMode: 1

depthFormat: 1
protectGraphicsMemory: 0
useHDRDisplay: 0
targetPixelDensity: 0
resolutionScalingMode: 0
iOS: com.Company.ProductName
iOS: com.unity.srp
tvOS: com.Company.ProductName
buildNumber:
iOS: 0

m_APIs: 1000000011000000
m_Automatic: 1
- m_BuildTarget: iOSSupport
m_APIs: 10000000
m_APIs: 08000000
m_Automatic: 0
m_BuildTargetVRSettings:
- m_BuildTarget: Android

switchParentalControl: 0
switchAllowsScreenshot: 1
switchDataLossConfirmation: 0
XboxTitleId:
XboxImageXexPath:
XboxSpaPath:
XboxGenerateSpa: 0
XboxDeployKinectResources: 0
XboxSplashScreen: {fileID: 0}
xboxEnableSpeech: 0
xboxAdditionalTitleMemorySize: 0
xboxDeployKinectHeadOrientation: 0
xboxDeployKinectHeadPosition: 0
ps4NPAgeRating: 12
ps4NPTitleSecret:
ps4NPTrophyPackPath:

webGLCompressionFormat: 1
scriptingDefineSymbols: {}
platformArchitecture:
iOS: 2
iOS: 0
scriptingBackend:
Android: 0
Metro: 2

2
ProjectSettings/ProjectVersion.txt


m_EditorVersion: 5.6.0b6
m_EditorVersion: 2017.1.0a4
正在加载...
取消
保存