Stardust draws at BeforeImageEffectsOpaque, before the nebula composite; scene verifier proves dust under fog is attenuated
The nebula is an [ImageEffectOpaque] OnRenderImage; a command buffer at BeforeForwardAlpha runs after its composite, so the dust landed on top of the fog (B49). BeforeImageEffectsOpaque runs before it on the deferred camera. FlowFieldSceneVerify now requires one Stardust buffer at the new event and none at BeforeForwardAlpha, and scene-stardust-under-fog compares the dust's contribution with the cloud renderer off and on in an HDR target, with a fog control. All commits build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
6b6e7c7c26
commit
ebce853aba
2 changed files with 127 additions and 16 deletions
|
|
@ -142,6 +142,7 @@ namespace Aetheria.EditorTools
|
|||
if (Shader.GetGlobalFloat("_NebulaNoiseTime") == noiseTime) Fail("scene-noise-clock", "_NebulaNoiseTime did not move while the zone ran");
|
||||
|
||||
CheckStardustUncullable(dust, bufferField);
|
||||
CheckStardustUnderFog(dust, bufferField);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
|
@ -156,24 +157,13 @@ namespace Aetheria.EditorTools
|
|||
{
|
||||
var camera = dust.TargetCamera;
|
||||
if (camera == null) { Fail("scene-stardust-uncullable", "Stardust has no TargetCamera"); return; }
|
||||
var attached = camera.GetCommandBuffers(CameraEvent.BeforeForwardAlpha).Count(buffer => buffer.name == "Stardust");
|
||||
var attached = camera.GetCommandBuffers(CameraEvent.BeforeImageEffectsOpaque).Count(buffer => buffer.name == "Stardust");
|
||||
if (attached != 1) Fail("scene-stardust-uncullable", $"{attached} Stardust command buffers on the target camera, expected 1");
|
||||
var late = camera.GetCommandBuffers(CameraEvent.BeforeForwardAlpha).Count(buffer => buffer.name == "Stardust");
|
||||
if (late != 0) Fail("scene-stardust-uncullable", $"{late} Stardust command buffers at BeforeForwardAlpha, expected 0");
|
||||
|
||||
var particles = (ComputeBuffer) bufferField.GetValue(dust);
|
||||
var side = Mathf.RoundToInt(Mathf.Sqrt(particles.count));
|
||||
float[] Layout(float size, Vector3 centre, float below)
|
||||
{
|
||||
var data = new float[particles.count * 7];
|
||||
for (var i = 0; i < particles.count; i++)
|
||||
{
|
||||
data[i * 7 + 0] = centre.x + ((i % side) - side / 2f) * 4f;
|
||||
data[i * 7 + 1] = centre.y - below;
|
||||
data[i * 7 + 2] = centre.z + ((i / side) - side / 2f) * 4f;
|
||||
data[i * 7 + 3] = data[i * 7 + 4] = data[i * 7 + 5] = 1f;
|
||||
data[i * 7 + 6] = size;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
float[] Layout(float size, Vector3 centre, float below) => DustLayout(particles.count, size, centre, below);
|
||||
|
||||
var transform = camera.transform;
|
||||
var (position, rotation) = (transform.position, transform.rotation);
|
||||
|
|
@ -231,6 +221,126 @@ namespace Aetheria.EditorTools
|
|||
}
|
||||
}
|
||||
|
||||
// The nebula is an [ImageEffectOpaque] that fogs the whole ray to a depth the dust never writes (B49): dust drawn
|
||||
// before it is attenuated by the fog, dust drawn after it sits on top at full strength. The dust's contribution to a
|
||||
// pixel (render with dust minus render without) is read with the cloud renderer off and on, in an HDR target.
|
||||
static void CheckStardustUnderFog(Stardust dust, FieldInfo bufferField)
|
||||
{
|
||||
const int Size = 128;
|
||||
var camera = dust.TargetCamera;
|
||||
var cloud = camera == null ? null : camera.GetComponent<VolumeCloudRenderer>();
|
||||
if (cloud == null) { Fail("scene-stardust-under-fog", "the target camera has no VolumeCloudRenderer"); return; }
|
||||
var particles = (ComputeBuffer) bufferField.GetValue(dust);
|
||||
|
||||
var transform = camera.transform;
|
||||
var (position, rotation) = (transform.position, transform.rotation);
|
||||
var (far, flags, background, mask, target, hdr) = (camera.farClipPlane, camera.clearFlags, camera.backgroundColor, camera.cullingMask, camera.targetTexture, camera.allowHDR);
|
||||
var texture = RenderTexture.GetTemporary(Size, Size, 24, RenderTextureFormat.ARGBHalf);
|
||||
var readback = new Texture2D(Size, Size, TextureFormat.RGBAHalf, false);
|
||||
var previous = RenderTexture.active;
|
||||
var effects = camera.GetComponents<MonoBehaviour>().Where(behaviour => behaviour.enabled).ToArray();
|
||||
var fields = new System.Collections.Generic.List<Texture2D>();
|
||||
float[] Render(bool fog, float size)
|
||||
{
|
||||
cloud.enabled = fog;
|
||||
for (var frame = 0; frame < 2; frame++) // the cloud's first frame forces ULTRA and fills its history; the second is the settled one
|
||||
{
|
||||
Call(dust, "Update");
|
||||
particles.SetData(DustLayout(particles.count, size, position, 100f));
|
||||
camera.Render();
|
||||
}
|
||||
RenderTexture.active = texture;
|
||||
readback.ReadPixels(new Rect(0, 0, Size, Size), 0, 0);
|
||||
readback.Apply();
|
||||
return readback.GetPixels().Select(pixel => pixel.r * .2126f + pixel.g * .7152f + pixel.b * .0722f).ToArray();
|
||||
}
|
||||
try
|
||||
{
|
||||
foreach (var effect in effects) effect.enabled = false;
|
||||
camera.allowHDR = true;
|
||||
camera.clearFlags = CameraClearFlags.SolidColor;
|
||||
camera.backgroundColor = Color.black;
|
||||
camera.cullingMask = 1;
|
||||
camera.targetTexture = texture;
|
||||
camera.farClipPlane = 1000f;
|
||||
transform.SetPositionAndRotation(position, Quaternion.Euler(90f, 0f, 0f));
|
||||
|
||||
// The scene's own nebula fields are textures the game renders; here a uniform fill of white fog stands in for them.
|
||||
foreach (var (name, format) in new[] { ("_NebulaSurfaceHeight", TextureFormat.RFloat), ("_NebulaPatchHeight", TextureFormat.RHalf), ("_NebulaPatch", TextureFormat.RHalf) })
|
||||
Shader.SetGlobalTexture(name, Uniform(format, Color.black, fields));
|
||||
Shader.SetGlobalTexture("_NebulaTint", Uniform(TextureFormat.RGBA32, Color.white, fields));
|
||||
Shader.DisableKeyword("NOISE_SLOPE");
|
||||
Shader.SetGlobalFloat("_NebulaFillDensity", .01f);
|
||||
Shader.SetGlobalFloat("_NebulaFillDistance", 120f);
|
||||
Shader.SetGlobalFloat("_NebulaFillExponent", 0f);
|
||||
Shader.SetGlobalFloat("_NebulaFillOffset", 0f);
|
||||
Shader.SetGlobalFloat("_NebulaPatchDensity", 0f);
|
||||
Shader.SetGlobalFloat("_NebulaLuminance", 1f);
|
||||
Shader.SetGlobalFloat("_ExtinctionCoefficient", .5f);
|
||||
Shader.SetGlobalFloat("_SafetyDistance", 30f);
|
||||
Shader.SetGlobalFloat("_TintLodExponent", 0f);
|
||||
|
||||
var clearBare = Render(false, 0f);
|
||||
var clearDust = Render(false, 5f);
|
||||
var fogBare = Render(true, 0f);
|
||||
var fogDust = Render(true, 5f);
|
||||
|
||||
var fogShown = 0f;
|
||||
var (clearSum, fogSum, drawn) = (0f, 0f, 0);
|
||||
for (var i = 0; i < clearBare.Length; i++)
|
||||
{
|
||||
fogShown += Mathf.Abs(fogBare[i] - clearBare[i]) / clearBare.Length;
|
||||
if (clearDust[i] == clearBare[i]) continue;
|
||||
drawn++; clearSum += clearDust[i] - clearBare[i]; fogSum += fogDust[i] - fogBare[i];
|
||||
}
|
||||
var (clearMean, fogMean) = (drawn == 0 ? 0f : clearSum / drawn, drawn == 0 ? 0f : fogSum / drawn);
|
||||
Debug.Log($"{Tag} stardust under fog: fog control mean |difference|={fogShown}, dust pixels={drawn}, dust contribution cloud off={clearMean}, cloud on={fogMean}, ratio={(clearMean == 0f ? 0f : fogMean / clearMean)}");
|
||||
if (fogShown < 1e-3f) Fail("scene-stardust-under-fog", "control: the cloud renderer changed no pixels, so no fog is in view");
|
||||
else if (drawn == 0 || clearMean <= 0f) Fail("scene-stardust-under-fog", "the dust drew no pixels with the cloud renderer off");
|
||||
else if (fogMean > .9f * clearMean) Fail("scene-stardust-under-fog", "the dust is not attenuated by the fog in front of it: it was drawn after the nebula composites");
|
||||
}
|
||||
finally
|
||||
{
|
||||
RenderTexture.active = previous;
|
||||
foreach (var effect in effects) effect.enabled = true;
|
||||
camera.allowHDR = hdr;
|
||||
camera.targetTexture = target;
|
||||
camera.cullingMask = mask;
|
||||
camera.backgroundColor = background;
|
||||
camera.clearFlags = flags;
|
||||
camera.farClipPlane = far;
|
||||
transform.SetPositionAndRotation(position, rotation);
|
||||
RenderTexture.ReleaseTemporary(texture);
|
||||
UnityEngine.Object.DestroyImmediate(readback);
|
||||
foreach (var field in fields) UnityEngine.Object.DestroyImmediate(field);
|
||||
}
|
||||
}
|
||||
|
||||
static Texture2D Uniform(TextureFormat format, Color colour, System.Collections.Generic.List<Texture2D> owner)
|
||||
{
|
||||
var texture = new Texture2D(1, 1, format, false) { wrapMode = TextureWrapMode.Clamp };
|
||||
texture.SetPixel(0, 0, colour);
|
||||
texture.Apply();
|
||||
owner.Add(texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
// A square patch of particles 4 apart, `below` under `centre`.
|
||||
static float[] DustLayout(int count, float size, Vector3 centre, float below)
|
||||
{
|
||||
var side = Mathf.RoundToInt(Mathf.Sqrt(count));
|
||||
var data = new float[count * 7];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
data[i * 7 + 0] = centre.x + ((i % side) - side / 2f) * 4f;
|
||||
data[i * 7 + 1] = centre.y - below;
|
||||
data[i * 7 + 2] = centre.z + ((i / side) - side / 2f) * 4f;
|
||||
data[i * 7 + 3] = data[i * 7 + 4] = data[i * 7 + 5] = 1f;
|
||||
data[i * 7 + 6] = size;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
// CloudShader's pass 0 compiles with the flow keywords (it must #include_with_pragmas the shared include).
|
||||
static void CheckCloudKeywords()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -59,7 +59,8 @@ public class Stardust : MonoBehaviour
|
|||
// The dust is drawn by a command buffer on TargetCamera: a camera replays its buffers at their event and never culls them,
|
||||
// so the draw needs no bounds and no extent can fall short of how deep the field is.
|
||||
private CommandBuffer _draw;
|
||||
private const CameraEvent DrawEvent = CameraEvent.BeforeForwardAlpha;
|
||||
// Before the [ImageEffectOpaque] nebula, so its march fogs the dust; a deferred camera skips the forward-opaque events when nothing forward-opaque is drawn (B46, B49).
|
||||
private const CameraEvent DrawEvent = CameraEvent.BeforeImageEffectsOpaque;
|
||||
|
||||
#endregion
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue