浏览代码

Added non-directional Lightmap support

/vr_sandbox
Felipe Lira 8 年前
当前提交
7adeb563
共有 8 个文件被更改,包括 198 次插入99 次删除
  1. 66
      Assets/LowEndRenderLoop/LowEndRenderLoopScene.unity
  2. 125
      Assets/LowEndRenderLoop/LowEndRenderLoopShader.shader
  3. 17
      Assets/LowEndRenderLoop/LowEndRenderPipeline.cs
  4. 11
      Assets/ScriptableRenderLoop/RenderPasses/ShadowRenderPass.cs
  5. 9
      Assets/LowEndRenderLoop/LowEndRenderLoopScene.meta
  6. 1
      Assets/LowEndRenderLoop/LowEndRenderLoopScene/Lightmap-0_comp_light.exr
  7. 68
      Assets/LowEndRenderLoop/LowEndRenderLoopScene/Lightmap-0_comp_light.exr.meta

66
Assets/LowEndRenderLoop/LowEndRenderLoopScene.unity


m_AlbedoBoost: 1
m_TemporalCoherenceThreshold: 1
m_EnvironmentLightingMode: 0
m_EnableBakedLightmaps: 0
m_EnableBakedLightmaps: 1
m_BakeResolution: 40
m_BakeResolution: 20
m_TextureWidth: 1024
m_TextureHeight: 1024
m_AO: 0

m_Padding: 2
m_LightmapParameters: {fileID: 0}
m_LightmapsBakeMode: 1
m_LightmapsBakeMode: 0
m_TextureCompression: 1
m_FinalGather: 0
m_FinalGatherFiltering: 1

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &126291026
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &240143173
MeshRenderer:

m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 240143172}
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: 5
m_Father: {fileID: 766602296}
m_RootOrder: 17
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &261580742
GameObject:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 4
m_IsActive: 1
--- !u!23 &261580743
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &529372583
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &531853421
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &689369501
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &717059739
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!4 &766602296
Transform:

- {fileID: 1686436294}
- {fileID: 1680776009}
- {fileID: 1761282538}
- {fileID: 240143176}
- {fileID: 1459238221}
m_Father: {fileID: 0}
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_Lightmapping: 4
m_Lightmapping: 2
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_CCT: 6570

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &1280249096
MeshRenderer:

m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_Lightmapping: 4
m_Lightmapping: 2
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_CCT: 6570

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 4
m_IsActive: 1
--- !u!23 &1405931298
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &1459238218
MeshRenderer:

m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1459238217}
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: 6
m_Father: {fileID: 766602296}
m_RootOrder: 18
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1485985477
GameObject:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &1485985478
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &1499111013
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &1596459100
MeshRenderer:

m_CullingMask:
serializedVersion: 2
m_Bits: 4294967295
m_Lightmapping: 4
m_Lightmapping: 2
m_AreaSize: {x: 1, y: 1}
m_BounceIntensity: 1
m_CCT: 6570

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &1680776006
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &1686436291
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &1714697413
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!23 &1749252040
MeshRenderer:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 5
m_IsActive: 1
--- !u!4 &1761282538
Transform:

m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_StaticEditorFlags: 4
m_IsActive: 1
--- !u!23 &2031315346
MeshRenderer:

125
Assets/LowEndRenderLoop/LowEndRenderLoopShader.shader


// and objects look just like with a Standard shader.
UsePass "Standard/FORWARD"
UsePass "Standard/FORWARD_DELTA"
//UsePass "Standard/ShadowCaster"
Pass
{

#define DEBUG_CASCADES 0
#define MAX_SHADOW_CASCADES 4
#define MAX_LIGHTS 8
#define INITIALIZE_LIGHT(light, lightIndex) \
light.pos = globalLightPos[lightIndex]; \
light.color = globalLightColor[lightIndex]; \
light.atten = globalLightAtten[lightIndex]; \
light.spotDir = globalLightSpotDir[lightIndex];
#define FRESNEL_TERM(normal, viewDir) Pow4(1.0 - saturate(dot(normal, viewDir)))
// TODO: Add metallic or specular reflectivity
#define GRAZING_TERM _Glossiness
// The variables are very similar to built-in unity_LightColor, unity_LightPosition,
// unity_LightAtten, unity_SpotDirection as used by the VertexLit shaders, except here

return diffuse + specular;
}
inline half3 EvaluateOneLightAndShadow(LightInput lightInput, half3 diffuseColor, half3 specularColor, half3 normal, float4 posWorld, half3 viewDir)
inline half3 EvaluateMainLight(LightInput lightInput, half3 diffuseColor, half3 specularColor, half3 normal, float4 posWorld, half3 viewDir)
{
int cascadeIndex = ComputeCascadeIndex(posWorld.w);
float3 shadowCoord = mul(_WorldToShadow[cascadeIndex], float4(posWorld.xyz, 1.0));

half bias = 0.001;
half shadowDepth = tex2D(g_tShadowBuffer, shadowCoord.xy).r;
half shadowAttenuation = 1.0;
#if defined(UNITY_REVERSED_Z)
shadowAttenuation = step(shadowDepth - bias, shadowCoord.z);
#else

#if DEBUG_CASCADES
half4 cascadeColors[MAX_SHADOW_CASCADES] = { half4(1.0, 0.0, 0.0, 1.0), half4(0.0, 1.0, 0.0, 1.0), half4(0.0, 0.0, 1.0, 1.0), half4(1.0, 0.0, 1.0, 1.0) };
return cascadeColors[cascadeIndex] * diffuseColor * max(shadowAttenuation, 0.5);
#else
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
#endif
}
struct VertexInput

float3 texcoord : TEXCOORD0;
float2 lightmapUV : TEXCOORD1;
float2 uv : TEXCOORD0;
float4 posWSEyeZ : TEXCOORD1; // xyz: posWorld, w: eyeZ
half3 normalWS : TEXCOORD2;
half3 color : TEXCOORD3;
float2 uv0 : TEXCOORD0;
float2 uv1 : TEXCOORD1;
half4 normalWS : TEXCOORD2; // xyz: normal, w: fresnel term
float4 posWS : TEXCOORD3; // xyz: posWorld, w: eyeZ
half4 viewDir : TEXCOORD4; // xyz: viewDir, w: grazingTerm;
half3 vertexColor : TEXCOORD5;
v2f vert(appdata_base v)
v2f vert(VertexInput v)
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv0 = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv1 = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
o.posWSEyeZ.xyz = mul(unity_ObjectToWorld, v.vertex).xyz;
o.posWSEyeZ.w = -UnityObjectToViewPos(v.vertex).z;
o.posWS.xyz = mul(unity_ObjectToWorld, v.vertex).xyz;
o.posWS.w = -UnityObjectToViewPos(v.vertex).z;
o.viewDir.xyz = normalize(_WorldSpaceCameraPos - o.posWS.xyz);
#if !GLOSSMAP
o.viewDir.w = GRAZING_TERM;
#endif
o.normalWS = UnityObjectToWorldNormal(v.normal);
o.normalWS.xyz = UnityObjectToWorldNormal(v.normal);
o.normalWS.w = FRESNEL_TERM(o.normalWS.xyz, o.viewDir.xyz);
half3 viewDir = normalize(_WorldSpaceCameraPos - o.posWSEyeZ);
lightInput.pos = globalLightPos[lightIndex];
lightInput.color = globalLightColor[lightIndex];
lightInput.atten = globalLightAtten[lightIndex];
lightInput.spotDir = globalLightSpotDir[lightIndex];
o.color += EvaluateOneLight(lightInput, diffuseAndSpecularColor, diffuseAndSpecularColor, o.normalWS, o.posWSEyeZ.xyz, viewDir);
INITIALIZE_LIGHT(lightInput, lightIndex);
o.vertexColor += EvaluateOneLight(lightInput, diffuseAndSpecularColor, diffuseAndSpecularColor, o.normalWS, o.posWS.xyz, o.viewDir.xyz);
}
return o;

{
i.normalWS = normalize(i.normalWS);
float3 posWorld = i.posWSEyeZ.xyz;
half3 viewDir = normalize(_WorldSpaceCameraPos - posWorld);
half3 normalWS = normalize(i.normalWS.xyz);
float3 posWorld = i.posWS.xyz;
half3 viewDir = i.viewDir.xyz;
half4 diffuseAlbedo = tex2D(_MainTex, i.uv);
half4 diffuseAlbedo = tex2D(_MainTex, i.uv0);
metalSmooth = tex2D(_MetallicGlossMap, i.uv).ra;
metalSmooth = tex2D(_MetallicGlossMap, i.uv0).ra;
half3 specColor;
half3 specular;
half3 diffuse = DiffuseAndSpecularFromMetallic(diffuseAlbedo.rgb, metalSmooth.x, specColor, oneMinuReflectivity);
half3 diffuse = DiffuseAndSpecularFromMetallic(diffuseAlbedo.rgb, metalSmooth.x, specular, oneMinuReflectivity);
// Indirect Light Contribution
UnityIndirect giIndirect;
giIndirect.diffuse = DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1));
giIndirect.specular = half3(0, 0, 0);
half3 indirectColor = BRDF3_Indirect(diffuse, specular, giIndirect, i.posWS.w, i.normalWS.w);
half3 directColor = i.vertexColor * diffuseAlbedo.rgb;
half3 color = i.color * diffuseAlbedo.rgb;
// Compute direct contribution from main directional light.
// Only a single directional shadow caster is supported.
LightInput mainLight;
INITIALIZE_LIGHT(mainLight, 0)
LightInput lightInput;
lightInput.pos = globalLightPos[0];
lightInput.color = globalLightColor[0];
lightInput.atten = globalLightAtten[0];
lightInput.spotDir = globalLightSpotDir[0];
color = EvaluateOneLightAndShadow(lightInput, diffuse, specColor, i.normalWS, i.posWSEyeZ, viewDir);
#else
for (int lightIndex = 0; lightIndex < globalLightCount.x; ++lightIndex)
return half4(EvaluateMainLight(mainLight, diffuse, specular, normalWS, i.posWS, viewDir), 1.0);
#endif
directColor += EvaluateMainLight(mainLight, diffuse, specular, normalWS, i.posWS, viewDir);
// Compute direct contribution from additional lights.
for (int lightIndex = 1; lightIndex < globalLightCount.x; ++lightIndex)
LightInput lightInput;
lightInput.pos = globalLightPos[lightIndex];
lightInput.color = globalLightColor[lightIndex];
lightInput.atten = globalLightAtten[lightIndex];
lightInput.spotDir = globalLightSpotDir[lightIndex];
LightInput additionalLight;
INITIALIZE_LIGHT(additionalLight, lightIndex);
directColor += EvaluateOneLight(additionalLight, diffuse, specular, normalWS, posWorld, viewDir);
}
if (lightIndex == 0)
color += EvaluateOneLightAndShadow(lightInput, diffuse, specColor, i.normalWS, i.posWSEyeZ, viewDir);
else
color += EvaluateOneLight(lightInput, diffuse, specColor, i.normalWS, posWorld, viewDir);
}
#endif
return half4(color, diffuseAlbedo.a);
half3 finalColor = directColor + indirectColor;
return half4(finalColor, diffuseAlbedo.a);
}
ENDCG
}

ZWrite On ZTest LEqual Cull Front
CGPROGRAM
#pragma enable_d3d11_debug_symbols
#pragma target 3.0
#pragma target 2.0
#pragma vertex vert
#pragma fragment frag

17
Assets/LowEndRenderLoop/LowEndRenderPipeline.cs


using UnityEngine;
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Experimental.ScriptableRenderLoop;
using UnityEngine.Rendering;

context.ExecuteCommandBuffer(cmd);
cmd.Dispose();
VisibleLight[] lights = cull.visibleLights;
ShadowOutput shadowOutput;
m_ShadowPass.Render(context, cull, out shadowOutput);
SetupShadowShaderVariables(shadowOutput, context, camera.nearClipPlane, cullingParameters.shadowDistance);

var settings = new DrawRendererSettings(cull, camera, new ShaderPassName("LowEndForwardBase"));
settings.sorting.flags = SortFlags.CommonOpaque;
settings.inputFilter.SetQueuesOpaque();
if (Lightmapping.bakedGI)
settings.rendererConfiguration = settings.rendererConfiguration | RendererConfiguration.PerObjectLightmaps;
context.DrawSkybox(camera);
settings.sorting.flags = SortFlags.CommonTransparent;

Vector4[] lightColors = new Vector4[kMaxLights];
Vector4[] lightAttenuations = new Vector4[kMaxLights];
Vector4[] lightSpotDirections = new Vector4[kMaxLights];
Vector4[] lightIntensity = new Vector4[kMaxLights];
// TODO: Sort Lighting Importance
int pixelLightCount = Mathf.Min(lights.Length, QualitySettings.pixelLightCount);
int vertexLightCount = (m_Asset.SupportsVertexLight) ? Mathf.Min(lights.Length - pixelLightCount, kMaxLights) : 0;
int totalLightCount = pixelLightCount + vertexLightCount;

VisibleLight currLight = lights[i];
if (currLight.lightType == LightType.Directional)
{
Vector4 dir = currLight.localToWorld.GetColumn(2);
lightPositions[i] = new Vector4(-dir.x, -dir.y, -dir.z, 0.0f);
Vector4 dir = -currLight.localToWorld.GetColumn(2);
lightPositions[i] = new Vector4(dir.x, dir.y, dir.z, 0.0f);
}
else
{

lightColors[i] = currLight.finalColor;
lightIntensity[i] = new Vector4(currLight.light.intensity, 0.0f, 0.0f, 0.0f);
float rangeSq = currLight.range * currLight.range;
float quadAtten = (currLight.lightType == LightType.Directional) ? 0.0f : 25.0f / rangeSq;

11
Assets/ScriptableRenderLoop/RenderPasses/ShadowRenderPass.cs


// Start filling lights into texture
var requestedPages = new List<AtlasEntry>();
packedShadows.shadowLights = new ShadowLight[lights.Length];
packedShadows.shadowLights = new ShadowLight[shadowLights.Count];
for (int i = 0; i != shadowIndices.Count; i++)
{
var numShadowSplits = CalculateNumShadowSplits(shadowIndices[i], lights);

var lights = cullResults.visibleLights;
int directionalLightCount = 0;
for (int i = 0; i < lights.Length; i++)
int lightsCount = (settings.maxShadowLightsSupported > 0) ? Mathf.Min(lights.Length, settings.maxShadowLightsSupported) : lights.Length;
for (int i = 0; i < lightsCount; i++)
{
//@TODO: ignore baked. move this logic to c++...
if (lights[i].light.shadows == LightShadows.None)

VisibleLight[] visibleLights = cullResults.visibleLights;
var shadowSlices = packedShadows.shadowSlices;
int shadowLightsCount = (m_Settings.maxShadowLightsSupported > 0)
? Mathf.Min(packedShadows.shadowLights.Length)
: packedShadows.shadowLights.Length;
for (int lightIndex = 0; lightIndex < shadowLightsCount; lightIndex++)
for (int lightIndex = 0; lightIndex < packedShadows.shadowLights.Length; lightIndex++)
{
int shadowSliceCount = packedShadows.shadowLights[lightIndex].shadowSliceCount;
if (shadowSliceCount == 0)

9
Assets/LowEndRenderLoop/LowEndRenderLoopScene.meta


fileFormatVersion: 2
guid: 36930a06efb38b44c86d80533989a6de
folderAsset: yes
timeCreated: 1483973859
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

1
Assets/LowEndRenderLoop/LowEndRenderLoopScene/Lightmap-0_comp_light.exr


v/1channelschlist7BGRcompressioncompressiondataWindowbox2idisplayWindowbox2ilineOrderlineOrderpixelAspectRatiofloat�?screenWindowCenterv2fscreenWindowWidthfloat�?A<<<

68
Assets/LowEndRenderLoop/LowEndRenderLoopScene/Lightmap-0_comp_light.exr.meta


fileFormatVersion: 2
guid: c3725aec356fa894fa33910a5551f44f
timeCreated: 1483973859
licenseType: Pro
TextureImporter:
fileIDToRecycleName: {}
serializedVersion: 4
mipmaps:
mipMapMode: 0
enableMipMap: 1
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
filterMode: 1
aniso: 3
mipBias: 0
wrapMode: 1
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaUsage: 0
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 6
textureShape: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存