浏览代码

Add light support to the minimal forward loop (WIP, not fully working)

Add support of point and spot without shadow with structured buffer in
forward.
/main
sebastienlagarde 8 年前
当前提交
636e2983
共有 5 个文件被更改,包括 148 次插入45 次删除
  1. 4
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/DisneyGGX.shader
  2. 63
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/LightDefinition.cs
  3. 8
      Assets/ScriptableRenderLoop/UnityStandard/Shaders/Lighting/Lighting.hlsl
  4. 86
      Assets/ScriptableRenderLoop/UnityStandard/UnityStandardRenderLoop.cs
  5. 32
      Assets/TestScenes/ForwardRenderLoop/Materials/gray.mat

4
Assets/ScriptableRenderLoop/UnityStandard/Shaders/DisneyGGX.shader


float3 positionWS = input.positionWS;
SurfaceData surfaceData = GetSurfaceData(input);
BSDFData BSDFData = ConvertSurfaceDataToBSDFData(surfaceData);
BSDFData bsdfData = ConvertSurfaceDataToBSDFData(surfaceData);
ForwardLighting(V, positionWS, BSDFData, diffuseLighting, specularLighting);
ForwardLighting(V, positionWS, bsdfData, diffuseLighting, specularLighting);
return float4(diffuseLighting.rgb + specularLighting.rgb, 1.0);
}

63
Assets/ScriptableRenderLoop/UnityStandard/Shaders/LightDefinition.cs


// structure definition
//-----------------------------------------------------------------------------
struct PunctualLightData
#if !__HLSL
namespace UnityEngine.ScriptableRenderLoop
public Vec3 positionWS;
public float invSqrAttenuationRadius;
public Vec3 color;
public float unused;
#endif
public Vec3 forward;
public float diffuseScale;
struct PunctualLightData
{
public Vec3 positionWS;
public float invSqrAttenuationRadius;
public Vec3 color;
public float unused;
public Vec3 up;
public float specularScale;
public Vec3 forward;
public float diffuseScale;
public Vec3 right;
public float shadowDimmer;
public Vec3 up;
public float specularScale;
public Vec3 right;
public float shadowDimmer;
public float angleScale;
public float angleOffset;
public Vec2 unused2;
};
public float angleScale;
public float angleOffset;
public Vec2 unused2;
};
struct AreaLightData
{
public Vec3 positionWS;
};
struct AreaLightData
{
public Vec3 positionWS;
};
struct EnvLightData
{
public Vec3 positionWS;
};
struct EnvLightData
{
public Vec3 positionWS;
};
struct PlanarLightData
{
public Vec3 positionWS;
};
struct PlanarLightData
{
public Vec3 positionWS;
};
#if !__HLSL
} // namespace UnityEngine.ScriptableRenderLoop
#endif

8
Assets/ScriptableRenderLoop/UnityStandard/Shaders/Lighting/Lighting.hlsl


// Simple forward loop architecture
//-----------------------------------------------------------------------------
StructuredBuffer<PunctualLightData> g_punctualLightData;
float g_lightCount;
StructuredBuffer<PunctualLightData> g_punctualLightList;
int g_punctualLightCount;
// TODO: Think about how to apply Disney diffuse preconvolve on indirect diffuse => must be done during GBuffer layout! Else emissive will be fucked...
// That's mean we need to read DFG texture during Gbuffer...

diffuseLighting = float4(0.0, 0.0, 0.0, 0.0);
specularLighting = float4(0.0, 0.0, 0.0, 0.0);
for (uint i = 0; i < (uint)g_lightCount; ++i)
for (int i = 0; i < g_punctualLightCount; ++i)
EvaluateBSDF_Punctual(V, positionWS, g_punctualLightData[i], material, localDiffuseLighting, localSpecularLighting);
EvaluateBSDF_Punctual(V, positionWS, g_punctualLightList[i], material, localDiffuseLighting, localSpecularLighting);
diffuseLighting += localDiffuseLighting;
specularLighting += localSpecularLighting;
}

86
Assets/ScriptableRenderLoop/UnityStandard/UnityStandardRenderLoop.cs


AssetDatabase.CreateAsset(instance, "Assets/UnityStandardRenderLoop.asset");
}
[SerializeField]
//[SerializeField]
const int MAX_LIGHTS = 10;
const int MAX_SHADOWMAP_PER_LIGHTS = 6;
const int MAX_DIRECTIONAL_SPLIT = 4;
// Directional lights become spotlights at a far distance. This is the distance we pull back to set the spotlight origin.
const float DIRECTIONAL_LIGHT_PULLBACK_DISTANCE = 10000.0f;
[NonSerialized] private int m_nWarnedTooManyLights = 0;
static private ComputeBuffer m_punctualLightList;
public const int MAX_LIGHTS = 1024;
void OnEnable()

Rebuild ();
}
void ClearComputeBuffers()
{
if (m_punctualLightList != null)
m_punctualLightList.Release();
}
ClearComputeBuffers();
m_punctualLightList = new ComputeBuffer(MAX_LIGHTS, System.Runtime.InteropServices.Marshal.SizeOf(typeof(PunctualLightData)));
void OnDisable()
{
m_punctualLightList.Release();
}
void UpdatePunctualLights(ActiveLight[] activeLights)
{
int punctualLightCount = 0;
List<PunctualLightData> lights = new List<PunctualLightData>();
for (int lightIndex = 0; lightIndex < Math.Min(activeLights.Length, MAX_LIGHTS); lightIndex++)
{
ActiveLight light = activeLights[lightIndex];
if (light.lightType == LightType.Spot || light.lightType == LightType.Point)
{
Matrix4x4 lightToWorld = light.localToWorld;
PunctualLightData l = new PunctualLightData();
l.positionWS = lightToWorld.GetColumn(3);
l.invSqrAttenuationRadius = 1.0f / (light.range * light.range);
l.color = new Vec3(light.finalColor.r, light.finalColor.g, light.finalColor.b);
l.forward = lightToWorld.GetColumn(0);
l.up = lightToWorld.GetColumn(1);
l.right = lightToWorld.GetColumn(2);
l.diffuseScale = 1.0f;
l.specularScale = 1.0f;
l.shadowDimmer = 1.0f;
if (light.lightType == LightType.Spot)
{
// TODO: Add support of cosine inner and outer for spot light to get nicer spotlight...
float cosSpotOuterHalfAngle = 1.0f / light.invCosHalfSpotAngle;
float cosSpotInnerHalfAngle = cosSpotOuterHalfAngle;
l.angleScale = 1.0f / Math.Max(0.001f, (cosSpotInnerHalfAngle - cosSpotOuterHalfAngle));
l.angleOffset = -cosSpotOuterHalfAngle * l.angleScale;
}
else
{
// 1.0f, 2.0f are neutral value allowing GetAngleAnttenuation in shader code to return 1.0
l.angleScale = 1.0f;
l.angleOffset = 2.0f;
}
lights.Add(l);
punctualLightCount++;
}
}
m_punctualLightList.SetData(lights.ToArray());
Shader.SetGlobalBuffer("g_punctualLightList", m_punctualLightList);
Shader.SetGlobalInt("g_punctualLightCount", punctualLightCount);
}
void UpdateLightConstants(ActiveLight[] activeLights /*, ref ShadowOutput shadow */)
{
/*

Vector4[] g_vLightDirection = new Vector4[ MAX_LIGHTS ];
Vector4[] g_vLightShadowIndex_vLightParams = new Vector4[ MAX_LIGHTS ];
Vector4[] g_vLightFalloffParams = new Vector4[ MAX_LIGHTS ];
Vector4[] g_vSpotLightInnerOuterConeCosines = new Vector4[ MAX_LIGHTS ];
Vector4[] g_vSpotLightInnersuterConeCosines = new Vector4[ MAX_LIGHTS ];
Matrix4x4[] g_matWorldToShadow = new Matrix4x4[ MAX_LIGHTS * MAX_SHADOWMAP_PER_LIGHTS ];
Vector4[] g_vDirShadowSplitSpheres = new Vector4[ MAX_DIRECTIONAL_SPLIT ];

public override void Render(Camera[] cameras, RenderLoop renderLoop)
{
// Set Frame constant buffer
// TODO...
// TODO...
CullResults cullResults;
CullingParameters cullingParams;

renderLoop.SetupCameraProperties (camera);
UpdateLightConstants(cullResults.culledLights /*, ref shadows */);
//UpdateLightConstants(cullResults.culledLights /*, ref shadows */);
UpdatePunctualLights(cullResults.culledLights);
DrawRendererSettings settings = new DrawRendererSettings (cullResults, camera, new ShaderPassName("Forward"));
settings.rendererConfiguration = RendererConfiguration.ConfigureOneLightProbePerRenderer | RendererConfiguration.ConfigureReflectionProbesProbePerRenderer;

32
Assets/TestScenes/ForwardRenderLoop/Materials/gray.mat


m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_Name: gray
m_Shader: {fileID: 4800000, guid: 908b379d4818cd64184b95a21123d909, type: 3}
m_Shader: {fileID: 4800000, guid: c846f5368b257ca49aa135748a1700e2, type: 3}
m_ShaderKeywords: S_RECEIVE_SHADOWS S_SPECULAR_METALLIC
m_LightmapFlags: 5
m_CustomRenderQueue: -1

name: _DetailNormalMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _DiffuseMap
second:
m_Texture: {fileID: 2800000, guid: 32c4b9c8ddc1c5d49ac7d10122540447, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:

m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _NormalMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _OcclusionMap
second:
m_Texture: {fileID: 0}

m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _SmoothnessMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: _SpecGlossMap
second:
m_Texture: {fileID: 0}

name: _SpecMap
second:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- first:
name: g_tOverrideLightmap
second:
m_Texture: {fileID: 0}

name: _Parallax
second: 0.02
- first:
name: _Smoothness
second: 0.5
- first:
name: _SmoothnessTextureChannel
second: 0
- first:

- first:
name: _Color
second: {r: 1, g: 1, b: 1, a: 1}
- first:
name: _DiffuseColor
second: {r: 0.71323526, g: 0.19404197, b: 0.19404197, a: 1}
- first:
name: _EmissionColor
second: {r: 0, g: 0, b: 0, a: 1}

正在加载...
取消
保存