Skip to content

Instantly share code, notes, and snippets.

@stramit
Created May 20, 2020 12:51
Show Gist options
  • Save stramit/ddcd1e4b3c12a5d82b13448ba8eaa12a to your computer and use it in GitHub Desktop.
Save stramit/ddcd1e4b3c12a5d82b13448ba8eaa12a to your computer and use it in GitHub Desktop.
#if ENABLE_HYBRID_RENDERER_V2 && URP_9_0_0_OR_NEWER
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using static Unity.Mathematics.math;
using UnityEngine;
using UnityEngine.Rendering;
namespace Unity.Rendering
{
struct SHProperties
{
public float4 SHAr;
public float4 SHAg;
public float4 SHAb;
public float4 SHBr;
public float4 SHBg;
public float4 SHBb;
public float4 SHC;
public SHProperties(SphericalHarmonicsL2 sh)
{
SHAr = GetSHA(sh, 0);
SHAg = GetSHA(sh, 1);
SHAb = GetSHA(sh, 2);
SHBr = GetSHB(sh, 0);
SHBg = GetSHB(sh, 1);
SHBb = GetSHB(sh, 2);
SHC = GetSHC(sh);
}
static float4 GetSHA(SphericalHarmonicsL2 sh, int i)
{
return float4(sh[i, 3], sh[i, 1], sh[i, 2], sh[i, 0] - sh[i, 6]);
}
static float4 GetSHB(SphericalHarmonicsL2 sh, int i)
{
return float4(sh[i, 4], sh[i, 6], sh[i, 5] * 3, sh[i, 7]);
}
static float4 GetSHC(SphericalHarmonicsL2 sh)
{
return float4(sh[0, 8], sh[2, 8], sh[1, 8], 1);
}
}
[UpdateInGroup(typeof(UpdatePresentationSystemGroup))]
[ExecuteAlways]
class AmbientProbeUpdateSystem : SystemBase
{
private EntityQuery m_Query;
private SphericalHarmonicsL2 m_LastAmbientProbe;
protected override void OnCreate()
{
m_Query = GetEntityQuery(
ComponentType.ReadWrite<BuiltinMaterialPropertyUnity_SHAr>(),
ComponentType.ReadWrite<BuiltinMaterialPropertyUnity_SHAg>(),
ComponentType.ReadWrite<BuiltinMaterialPropertyUnity_SHAb>(),
ComponentType.ReadWrite<BuiltinMaterialPropertyUnity_SHBr>(),
ComponentType.ReadWrite<BuiltinMaterialPropertyUnity_SHBg>(),
ComponentType.ReadWrite<BuiltinMaterialPropertyUnity_SHBb>(),
ComponentType.ReadWrite<BuiltinMaterialPropertyUnity_SHC>());
m_Query.SetChangedVersionFilter(ComponentType.ReadWrite<BuiltinMaterialPropertyUnity_SHAr>());
}
protected override void OnUpdate()
{
var ambientProbe = RenderSettings.ambientProbe;
var updateAll = ambientProbe != m_LastAmbientProbe;
if (updateAll)
{
m_Query.ResetFilter();
}
m_LastAmbientProbe = ambientProbe;
var job = new UpdateSHValuesJob
{
Properties = new SHProperties(ambientProbe),
SHArType = GetArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHAr>(),
SHAgType = GetArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHAg>(),
SHAbType = GetArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHAb>(),
SHBrType = GetArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHBr>(),
SHBgType = GetArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHBg>(),
SHBbType = GetArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHBb>(),
SHCType = GetArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHC>(),
};
Dependency = job.ScheduleParallel(m_Query, Dependency);
if (updateAll)
{
m_Query.SetChangedVersionFilter(ComponentType.ReadWrite<BuiltinMaterialPropertyUnity_SHAr>());
}
}
[BurstCompile]
struct UpdateSHValuesJob : IJobChunk
{
public SHProperties Properties;
public ArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHAr> SHArType;
public ArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHAg> SHAgType;
public ArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHAb> SHAbType;
public ArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHBr> SHBrType;
public ArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHBg> SHBgType;
public ArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHBb> SHBbType;
public ArchetypeChunkComponentType<BuiltinMaterialPropertyUnity_SHC> SHCType;
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
{
var chunkSHAr = chunk.GetNativeArray(SHArType);
var chunkSHAg = chunk.GetNativeArray(SHAgType);
var chunkSHAb = chunk.GetNativeArray(SHAbType);
var chunkSHBr = chunk.GetNativeArray(SHBrType);
var chunkSHBg = chunk.GetNativeArray(SHBgType);
var chunkSHBb = chunk.GetNativeArray(SHBbType);
var chunkSHC = chunk.GetNativeArray(SHCType);
for (var i = 0; i < chunkSHAr.Length; i++)
{
chunkSHAr[i] = new BuiltinMaterialPropertyUnity_SHAr {Value = Properties.SHAr};
chunkSHAg[i] = new BuiltinMaterialPropertyUnity_SHAg {Value = Properties.SHAg};
chunkSHAb[i] = new BuiltinMaterialPropertyUnity_SHAb {Value = Properties.SHAb};
chunkSHBr[i] = new BuiltinMaterialPropertyUnity_SHBr {Value = Properties.SHBr};
chunkSHBg[i] = new BuiltinMaterialPropertyUnity_SHBg {Value = Properties.SHBg};
chunkSHBb[i] = new BuiltinMaterialPropertyUnity_SHBb {Value = Properties.SHBb};
chunkSHC[i] = new BuiltinMaterialPropertyUnity_SHC {Value = Properties.SHC};
}
}
}
}
[UpdateInGroup(typeof(StructuralChangePresentationSystemGroup))]
[ExecuteAlways]
class AddBuiltInSHProperties : SystemBase
{
private EntityQuery[] m_MissingSHQueries;
private EntityQuery m_MissingRenderMesh;
private ComponentType[] m_ComponentTypes;
protected override void OnCreate()
{
m_ComponentTypes = new[]
{
ComponentType.ReadOnly<BuiltinMaterialPropertyUnity_SHAr>(),
ComponentType.ReadOnly<BuiltinMaterialPropertyUnity_SHAg>(),
ComponentType.ReadOnly<BuiltinMaterialPropertyUnity_SHAb>(),
ComponentType.ReadOnly<BuiltinMaterialPropertyUnity_SHBr>(),
ComponentType.ReadOnly<BuiltinMaterialPropertyUnity_SHBg>(),
ComponentType.ReadOnly<BuiltinMaterialPropertyUnity_SHBb>(),
ComponentType.ReadOnly<BuiltinMaterialPropertyUnity_SHC>()
};
m_MissingSHQueries = new EntityQuery[m_ComponentTypes.Length];
for (var i = 0; i < m_ComponentTypes.Length; i++)
{
m_MissingSHQueries[i] = GetEntityQuery(new EntityQueryDesc
{
All = new[] {ComponentType.ReadOnly<RenderMesh>()},
None = new[] {m_ComponentTypes[i]}
});
}
m_MissingRenderMesh = GetEntityQuery(new EntityQueryDesc
{
Any = m_ComponentTypes,
None = new[] {ComponentType.ReadOnly<RenderMesh>()}
});
}
protected override void OnUpdate()
{
for (var i = 0; i < m_ComponentTypes.Length; i++)
{
EntityManager.AddComponent(m_MissingSHQueries[i], m_ComponentTypes[i]);
EntityManager.RemoveComponent(m_MissingRenderMesh, m_ComponentTypes[i]);
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment