void InitResources() { // Volume InitVolume(ref m_VolumeInject); InitVolume(ref m_VolumeScatter); // Compute buffers int pointLightCount = 0, tubeLightCount = 0, areaLightCount = 0; HashSet <FogLight> fogLights = LightManagerFogLights.Get(); for (var x = fogLights.GetEnumerator(); x.MoveNext();) { var fl = x.Current; if (fl == null) { continue; } bool isOn = fl.isOn; switch (fl.type) { case FogLight.Type.Point: if (isOn) { pointLightCount++; } break; case FogLight.Type.Tube: if (isOn) { tubeLightCount++; } break; case FogLight.Type.Area: if (isOn) { areaLightCount++; } break; } } // PointLightParams {float3 float float3 float} -> 32 bytes CreateBuffer(ref m_PointLightParamsCB, pointLightCount, 32); // TubeLightParams {float3 float float3 float float3 float} -> 48 bytes CreateBuffer(ref m_TubeLightParamsCB, tubeLightCount, 48); // TubeLightShadowPlaneParams {float4 float4 float float float float} -> 48 bytes CreateBuffer(ref m_TubeLightShadowPlaneParamsCB, tubeLightCount, 48); // TubeLightParams {float4x4 float4 float3 float} -> 96 bytes CreateBuffer(ref m_AreaLightParamsCB, areaLightCount, 96); // FogEllipsoidParams {float3 float float3 9xfloat} -> 64 bytes HashSet <FogEllipsoid> fogEllipsoids = LightManagerFogEllipsoids.Get(); CreateBuffer(ref m_FogEllipsoidParamsCB, fogEllipsoids == null ? 0 : fogEllipsoids.Count, 64); }
void SetUpTubeLightBuffers(int kernel) { int count = m_TubeLightParamsCB == null ? 0 : m_TubeLightParamsCB.count; m_InjectLightingAndDensity.SetFloat("_TubeLightsCount", count); if (count == 0) { return; } if (m_TubeLightParams == null || m_TubeLightParams.Length != count) { m_TubeLightParams = new TubeLightParams[count]; } if (m_TubeLightShadowPlaneParams == null || m_TubeLightShadowPlaneParams.Length != count) { m_TubeLightShadowPlaneParams = new TubeLightShadowPlaneParams[count]; } HashSet <FogLight> fogLights = LightManagerFogLights.Get(); int j = 0; for (var x = fogLights.GetEnumerator(); x.MoveNext();) { var fl = x.Current; if (fl == null || fl.type != FogLight.Type.Tube || !fl.isOn) { continue; } TubeLight light = fl.tubeLight; Transform t = light.transform; Vector3 pos = t.position; Vector3 halfLength = 0.5f * t.up * light.m_Length; // Tube lights m_TubeLightParams[j].start = pos + halfLength; m_TubeLightParams[j].end = pos - halfLength; float range = light.m_Range * fl.m_RangeMult; m_TubeLightParams[j].range = 1.0f / (range * range); //m_TubeLightParams[j].color = new Vector3(light.m_Color.r, light.m_Color.g, light.m_Color.b) * light.m_Intensity * fl.m_IntensityMult; m_TubeLightParams[j].radius = light.m_Radius; // Tube light shadow planes var p = light.GetShadowPlaneParams(ref sppArr); m_TubeLightShadowPlaneParams[j].plane0 = p[0].plane; m_TubeLightShadowPlaneParams[j].plane1 = p[1].plane; m_TubeLightShadowPlaneParams[j].feather0 = p[0].feather; m_TubeLightShadowPlaneParams[j].feather1 = p[1].feather; j++; } m_TubeLightParamsCB.SetData(m_TubeLightParams); m_InjectLightingAndDensity.SetBuffer(kernel, "_TubeLights", m_TubeLightParamsCB); m_TubeLightShadowPlaneParamsCB.SetData(m_TubeLightShadowPlaneParams); m_InjectLightingAndDensity.SetBuffer(kernel, "_TubeLightShadowPlanes", m_TubeLightShadowPlaneParamsCB); }
void AddToLightManager() { if (!m_AddedToLightManager) { m_AddedToLightManager = LightManagerFogLights.Add(this); } }
void SetUpAreaLightBuffers(int kernel) { int count = m_AreaLightParamsCB == null ? 0 : m_AreaLightParamsCB.count; m_InjectLightingAndDensity.SetFloat("_AreaLightsCount", count); if (count == 0) { // Can't not set the buffers/textures m_InjectLightingAndDensity.SetBuffer(kernel, "_AreaLights", m_DummyCB); m_InjectLightingAndDensity.SetTexture(kernel, "_AreaLightShadowmap", Texture2D.whiteTexture); return; } if (m_AreaLightParams == null || m_AreaLightParams.Length != count) { m_AreaLightParams = new AreaLightParams[count]; } HashSet <FogLight> fogLights = LightManagerFogLights.Get(); int shadowedAreaLightIndex = -1; int j = 0; for (var x = fogLights.GetEnumerator(); x.MoveNext();) { var fl = x.Current; if (fl == null || fl.type != FogLight.Type.Area || !fl.isOn) { continue; } AreaLight light = fl.areaLight; m_AreaLightParams[j].mat = light.GetProjectionMatrix(true); m_AreaLightParams[j].pos = light.GetPosition(); m_AreaLightParams[j].color = new Vector3(light.m_Color.r, light.m_Color.g, light.m_Color.b) * light.m_Intensity * fl.m_IntensityMult; m_AreaLightParams[j].bounded = fl.m_Bounded ? 1 : 0; if (fl.m_Shadows) { RenderTexture shadowmap = light.GetBlurredShadowmap(); if (shadowmap != null) { m_InjectLightingAndDensity.SetTexture(kernel, "_AreaLightShadowmap", shadowmap); m_InjectLightingAndDensity.SetFloat("_ESMExponentAreaLight", fl.m_ESMExponent); shadowedAreaLightIndex = j; } } j++; } m_AreaLightParamsCB.SetData(m_AreaLightParams); m_InjectLightingAndDensity.SetBuffer(kernel, "_AreaLights", m_AreaLightParamsCB); m_InjectLightingAndDensity.SetFloat("_ShadowedAreaLightIndex", shadowedAreaLightIndex < 0 ? fogLights.Count : shadowedAreaLightIndex); if (shadowedAreaLightIndex < 0) { m_InjectLightingAndDensity.SetTexture(kernel, "_AreaLightShadowmap", Texture2D.whiteTexture); } }
void InitResources() { // Volume InitVolume(ref m_VolumeInject); InitVolume(ref m_VolumeScatter); // Compute buffers int pointLightCount = 0, tubeLightCount = 0, areaLightCount = 0; HashSet <FogLight> fogLights = LightManagerFogLights.Get(); for (var x = fogLights.GetEnumerator(); x.MoveNext();) { var fl = x.Current; if (fl == null) { continue; } bool isOn = fl.isOn; switch (fl.type) { case FogLight.Type.Point: if (isOn) { pointLightCount++; } break; case FogLight.Type.Tube: if (isOn) { tubeLightCount++; } break; case FogLight.Type.Area: if (isOn) { areaLightCount++; } break; } } CreateBuffer(ref m_PointLightParamsCB, pointLightCount, Marshal.SizeOf(typeof(PointLightParams))); CreateBuffer(ref m_TubeLightParamsCB, tubeLightCount, Marshal.SizeOf(typeof(TubeLightParams))); CreateBuffer(ref m_TubeLightShadowPlaneParamsCB, tubeLightCount, Marshal.SizeOf(typeof(TubeLightShadowPlaneParams))); CreateBuffer(ref m_AreaLightParamsCB, areaLightCount, Marshal.SizeOf(typeof(AreaLightParams))); HashSet <FogEllipsoid> fogEllipsoids = LightManagerFogEllipsoids.Get(); CreateBuffer(ref m_FogEllipsoidParamsCB, fogEllipsoids == null ? 0 : fogEllipsoids.Count, Marshal.SizeOf(typeof(FogEllipsoidParams))); CreateBuffer(ref m_DummyCB, 1, 4); }
FogLight GetDirectionalLight() { HashSet <FogLight> fogLights = LightManagerFogLights.Get(); FogLight fogLight = null; for (var x = fogLights.GetEnumerator(); x.MoveNext();) { var fl = x.Current; if (fl == null || fl.type != FogLight.Type.Directional || !fl.isOn) { continue; } fogLight = fl; break; } return(fogLight); }
void SetUpPointLightBuffers(int kernel) { int count = m_PointLightParamsCB == null ? 0 : m_PointLightParamsCB.count; m_InjectLightingAndDensity.SetFloat("_PointLightsCount", count); if (count == 0) { // Can't not set the buffer m_InjectLightingAndDensity.SetBuffer(kernel, "_PointLights", m_DummyCB); return; } if (m_PointLightParams == null || m_PointLightParams.Length != count) { m_PointLightParams = new PointLightParams[count]; } HashSet <FogLight> fogLights = LightManagerFogLights.Get(); int j = 0; for (var x = fogLights.GetEnumerator(); x.MoveNext();) { var fl = x.Current; if (fl == null || fl.type != FogLight.Type.Point || !fl.isOn) { continue; } Light light = fl.light; m_PointLightParams[j].pos = light.transform.position; float range = light.range * fl.m_RangeMult; m_PointLightParams[j].range = 1.0f / (range * range); m_PointLightParams[j].color = new Vector3(light.color.r, light.color.g, light.color.b) * light.intensity * fl.m_IntensityMult; j++; } // TODO: try a constant buffer with setfloats instead for perf m_PointLightParamsCB.SetData(m_PointLightParams); m_InjectLightingAndDensity.SetBuffer(kernel, "_PointLights", m_PointLightParamsCB); }
void OnDisable() { LightManagerFogLights.Remove(this); m_AddedToLightManager = false; CleanupDirectionalShadowmap(); }