Compare commits
3 Commits
5f24e4c27d
...
d0fc79923c
| Author | SHA1 | Date | |
|---|---|---|---|
| d0fc79923c | |||
| 45a3e81dfc | |||
| e5f963e2e5 |
@@ -2,7 +2,8 @@
|
||||
"name": "Misaki.AoVolume.Editor",
|
||||
"rootNamespace": "Misaki.AoVolume.Editor",
|
||||
"references": [
|
||||
"GUID:33624ad552e4d7342a53d46280a5a916"
|
||||
"GUID:33624ad552e4d7342a53d46280a5a916",
|
||||
"GUID:d8b63aba1907145bea998dd612889d6b"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
|
||||
@@ -2,7 +2,9 @@ using Misaki.AoVolume;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Misaki.AoVolume.Shader;
|
||||
using Unity.Collections;
|
||||
using Unity.Jobs;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Experimental.Rendering;
|
||||
using UnityEngine.Rendering;
|
||||
@@ -11,6 +13,7 @@ using UnityEngine.Rendering.HighDefinition;
|
||||
internal class AoVolumePass : CustomPass
|
||||
{
|
||||
private NativeArray<OrientedBoundingBox> _volumeBounds;
|
||||
private NativeList<VolumeData> _culledVolumeDatas;
|
||||
private ComputeBuffer _volumeBoundsBuffer;
|
||||
private ComputeBuffer _visibleIndicesBuffer;
|
||||
private ComputeBuffer _visibleVolumeCountBuffer;
|
||||
@@ -42,6 +45,7 @@ internal class AoVolumePass : CustomPass
|
||||
protected override void Setup(ScriptableRenderContext renderContext, CommandBuffer cmd)
|
||||
{
|
||||
_volumeBounds = new NativeArray<OrientedBoundingBox>(maxVolumeOnScreen, Allocator.Persistent);
|
||||
_culledVolumeDatas = new NativeList<VolumeData>(maxVolumeOnScreen, Allocator.Persistent);
|
||||
_volumeBoundsBuffer = new ComputeBuffer(maxVolumeOnScreen, Marshal.SizeOf<OrientedBoundingBox>());
|
||||
_visibleIndicesBuffer = new ComputeBuffer(maxVolumeOnScreen, Marshal.SizeOf<uint>(), ComputeBufferType.Raw);
|
||||
_visibleVolumeCountBuffer = new ComputeBuffer(1, Marshal.SizeOf<uint>(), ComputeBufferType.Counter);
|
||||
@@ -58,13 +62,48 @@ internal class AoVolumePass : CustomPass
|
||||
{
|
||||
return Mathf.FloorToInt(Mathf.Log(Mathf.Min(hDCamera.actualWidth, hDCamera.actualHeight), 2)) - 1;
|
||||
}
|
||||
private void FrustumCulling(CustomPassContext ctx, ref int volumeCount)
|
||||
{
|
||||
Frustum cameraFrustum = ctx.hdCamera.frustum;
|
||||
|
||||
GPUFrustum frustum = new();
|
||||
frustum.normal0 = cameraFrustum.planes[0].normal;
|
||||
frustum.dist0 = cameraFrustum.planes[0].distance;
|
||||
frustum.normal1 = cameraFrustum.planes[1].normal;
|
||||
frustum.dist1 = cameraFrustum.planes[1].distance;
|
||||
frustum.normal2 = cameraFrustum.planes[2].normal;
|
||||
frustum.dist2 = cameraFrustum.planes[2].distance;
|
||||
frustum.normal3 = cameraFrustum.planes[3].normal;
|
||||
frustum.dist3 = cameraFrustum.planes[3].distance;
|
||||
frustum.normal4 = cameraFrustum.planes[4].normal;
|
||||
frustum.dist4 = cameraFrustum.planes[4].distance;
|
||||
frustum.normal5 = cameraFrustum.planes[5].normal;
|
||||
frustum.dist5 = cameraFrustum.planes[5].distance;
|
||||
frustum.corner0.xyz = cameraFrustum.corners[0];
|
||||
frustum.corner1.xyz = cameraFrustum.corners[1];
|
||||
frustum.corner2.xyz = cameraFrustum.corners[2];
|
||||
frustum.corner3.xyz = cameraFrustum.corners[3];
|
||||
frustum.corner4.xyz = cameraFrustum.corners[4];
|
||||
frustum.corner5.xyz = cameraFrustum.corners[5];
|
||||
frustum.corner6.xyz = cameraFrustum.corners[6];
|
||||
frustum.corner7.xyz = cameraFrustum.corners[7];
|
||||
|
||||
_culledVolumeDatas.Clear();
|
||||
FrustumCullingJob cullingJob = new()
|
||||
{
|
||||
GPUFrustum = frustum,
|
||||
InputVolumeDatas = VolumeDatabase.Instance.VolumeDatas,
|
||||
InputVolumeBounds = _volumeBounds,
|
||||
OutputVolumeDataWriter = _culledVolumeDatas.AsParallelWriter()
|
||||
};
|
||||
|
||||
JobHandle cullingJobHandle = cullingJob.Schedule(volumeCount, 64);
|
||||
cullingJobHandle.Complete();
|
||||
volumeCount = _culledVolumeDatas.Length;
|
||||
}
|
||||
|
||||
private void HierarchicalZCulling(CustomPassContext ctx, int volumeCount)
|
||||
{
|
||||
for (var i = 0; i < volumeCount; i++)
|
||||
{
|
||||
_volumeBounds[i] = new OrientedBoundingBox(VolumeDatabase.Instance.VolumeDatas[i].worldMatrix);
|
||||
}
|
||||
_volumeBoundsBuffer.SetData(_volumeBounds);
|
||||
|
||||
ctx.cmd.SetComputeBufferParam(cullingShader, 0, "_VolumeBounds", _volumeBoundsBuffer);
|
||||
@@ -81,7 +120,7 @@ internal class AoVolumePass : CustomPass
|
||||
|
||||
private void RenderVisibleVolumes(CustomPassContext ctx)
|
||||
{
|
||||
_volumeDataBuffer.SetData(VolumeDatabase.Instance.VolumeDatas);
|
||||
_volumeDataBuffer.SetData(_culledVolumeDatas.AsArray());
|
||||
|
||||
ctx.cmd.SetComputeBufferParam(renderingShader, 0, "_VolumeDatas", _volumeDataBuffer);
|
||||
ctx.cmd.SetComputeBufferParam(renderingShader, 0, "_VisibleVolumeCount", _visibleVolumeCountBuffer);
|
||||
@@ -140,6 +179,7 @@ internal class AoVolumePass : CustomPass
|
||||
return;
|
||||
}
|
||||
|
||||
FrustumCulling(ctx, ref volumeCount);
|
||||
HierarchicalZCulling(ctx, volumeCount);
|
||||
RenderVisibleVolumes(ctx);
|
||||
//DebugVisibleVolumes();
|
||||
@@ -149,6 +189,7 @@ internal class AoVolumePass : CustomPass
|
||||
protected override void Cleanup()
|
||||
{
|
||||
_volumeBounds.Dispose();
|
||||
_culledVolumeDatas.Dispose();
|
||||
_volumeBoundsBuffer?.Dispose();
|
||||
_visibleIndicesBuffer?.Dispose();
|
||||
_visibleVolumeCountBuffer?.Dispose();
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
"GUID:457756d89b35d2941b3e7b37b4ece6f1",
|
||||
"GUID:df380645f10b7bc4b97d4f5eb6303d95",
|
||||
"GUID:e0cd26848372d4e5c891c569017e11f1",
|
||||
"GUID:d8b63aba1907145bea998dd612889d6b"
|
||||
"GUID:d8b63aba1907145bea998dd612889d6b",
|
||||
"GUID:2665a8d13d1b3f18800f46e256720795"
|
||||
],
|
||||
"includePlatforms": [],
|
||||
"excludePlatforms": [],
|
||||
|
||||
119
Runtime/Shader/FrustumCullingJob.cs
Normal file
119
Runtime/Shader/FrustumCullingJob.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Jobs;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Misaki.AoVolume.Shader
|
||||
{
|
||||
[BurstCompile]
|
||||
internal struct FrustumCullingJob : IJobParallelFor
|
||||
{
|
||||
[ReadOnly] internal GPUFrustum GPUFrustum;
|
||||
[ReadOnly] internal NativeArray<VolumeData> InputVolumeDatas;
|
||||
internal NativeArray<OrientedBoundingBox> InputVolumeBounds;
|
||||
internal NativeList<VolumeData>.ParallelWriter OutputVolumeDataWriter;
|
||||
|
||||
public void Execute(int index)
|
||||
{
|
||||
OrientedBoundingBox obb = new(InputVolumeDatas[index].worldMatrix);
|
||||
if (FrustumObbIntersection(obb))
|
||||
{
|
||||
OutputVolumeDataWriter.AddNoResize(InputVolumeDatas[index]);
|
||||
InputVolumeBounds[index] = obb;
|
||||
}
|
||||
}
|
||||
|
||||
private struct FrustumPlane
|
||||
{
|
||||
internal float3 Normal;
|
||||
internal float Distance;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool CheckOverlap(OrientedBoundingBox obb, float3 planeNormal, float planeDist)
|
||||
{
|
||||
float maxHalfDiagProj = obb.extent.x * math.abs(math.dot(obb.right, planeNormal)) +
|
||||
obb.extent.y * math.abs(math.dot(obb.up, planeNormal)) +
|
||||
obb.extent.z * math.abs(math.dot(GetForward(obb), planeNormal));
|
||||
float centerProj = math.dot(obb.center, planeNormal) + planeDist;
|
||||
return maxHalfDiagProj + centerProj >= 0;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static Vector3 GetForward(OrientedBoundingBox obb)
|
||||
{
|
||||
return Vector3.Cross(obb.up, obb.right);
|
||||
}
|
||||
|
||||
private bool FrustumObbIntersection(OrientedBoundingBox obb)
|
||||
{
|
||||
bool overlap = CheckOverlap(obb, GPUFrustum.normal0, GPUFrustum.dist0);
|
||||
overlap &= CheckOverlap(obb, GPUFrustum.normal1, GPUFrustum.dist1);
|
||||
overlap &= CheckOverlap(obb, GPUFrustum.normal2, GPUFrustum.dist2);
|
||||
overlap &= CheckOverlap(obb, GPUFrustum.normal3, GPUFrustum.dist3);
|
||||
overlap &= CheckOverlap(obb, GPUFrustum.normal4, GPUFrustum.dist4);
|
||||
overlap &= CheckOverlap(obb, GPUFrustum.normal5, GPUFrustum.dist5);
|
||||
|
||||
var planes = new NativeArray<FrustumPlane>(3, Allocator.Temp);
|
||||
planes[0] = new FrustumPlane
|
||||
{
|
||||
Normal = obb.right,
|
||||
Distance = obb.extent.x
|
||||
};
|
||||
planes[1] = new FrustumPlane
|
||||
{
|
||||
Normal = obb.up,
|
||||
Distance = obb.extent.y
|
||||
};
|
||||
planes[2] = new FrustumPlane
|
||||
{
|
||||
Normal = GetForward(obb),
|
||||
Distance = obb.extent.z
|
||||
};
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
bool outsidePos = true;
|
||||
bool outsideNeg = true;
|
||||
|
||||
float proj = math.dot(planes[i].Normal, GPUFrustum.corner0.xyz - obb.center);
|
||||
outsidePos &= proj > planes[i].Distance;
|
||||
outsideNeg &= -proj > planes[i].Distance;
|
||||
|
||||
proj = math.dot(planes[i].Normal, GPUFrustum.corner1.xyz - obb.center);
|
||||
outsidePos &= proj > planes[i].Distance;
|
||||
outsideNeg &= -proj > planes[i].Distance;
|
||||
|
||||
proj = math.dot(planes[i].Normal, GPUFrustum.corner2.xyz - obb.center);
|
||||
outsidePos &= proj > planes[i].Distance;
|
||||
outsideNeg &= -proj > planes[i].Distance;
|
||||
|
||||
proj = math.dot(planes[i].Normal, GPUFrustum.corner3.xyz - obb.center);
|
||||
outsidePos &= proj > planes[i].Distance;
|
||||
outsideNeg &= -proj > planes[i].Distance;
|
||||
|
||||
proj = math.dot(planes[i].Normal, GPUFrustum.corner4.xyz - obb.center);
|
||||
outsideNeg &= -proj > planes[i].Distance;
|
||||
outsideNeg &= -proj > planes[i].Distance;
|
||||
|
||||
proj = math.dot(planes[i].Normal, GPUFrustum.corner5.xyz - obb.center);
|
||||
outsideNeg &= -proj > planes[i].Distance;
|
||||
outsideNeg &= -proj > planes[i].Distance;
|
||||
|
||||
proj = math.dot(planes[i].Normal, GPUFrustum.corner6.xyz - obb.center);
|
||||
outsideNeg &= -proj > planes[i].Distance;
|
||||
outsideNeg &= -proj > planes[i].Distance;
|
||||
|
||||
proj = math.dot(planes[i].Normal, GPUFrustum.corner7.xyz - obb.center);
|
||||
outsideNeg &= -proj > planes[i].Distance;
|
||||
outsideNeg &= -proj > planes[i].Distance;
|
||||
|
||||
overlap &= !(outsidePos || outsideNeg);
|
||||
}
|
||||
|
||||
planes.Dispose();
|
||||
return overlap;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Runtime/Shader/FrustumCullingJob.cs.meta
Normal file
3
Runtime/Shader/FrustumCullingJob.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5403ee0538c94d9abc5aa44a761092d0
|
||||
timeCreated: 1740495091
|
||||
@@ -1,4 +1,4 @@
|
||||
using UnityEngine;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
namespace Misaki.AoVolume
|
||||
@@ -7,28 +7,28 @@ namespace Misaki.AoVolume
|
||||
internal struct GPUFrustum
|
||||
{
|
||||
// The data of the 6 planes of the frustum
|
||||
public Vector3 normal0;
|
||||
public float3 normal0;
|
||||
public float dist0;
|
||||
public Vector3 normal1;
|
||||
public float3 normal1;
|
||||
public float dist1;
|
||||
public Vector3 normal2;
|
||||
public float3 normal2;
|
||||
public float dist2;
|
||||
public Vector3 normal3;
|
||||
public float3 normal3;
|
||||
public float dist3;
|
||||
public Vector3 normal4;
|
||||
public float3 normal4;
|
||||
public float dist4;
|
||||
public Vector3 normal5;
|
||||
public float3 normal5;
|
||||
public float dist5;
|
||||
|
||||
// The data of the 8 corners of the frustum
|
||||
public Vector4 corner0;
|
||||
public Vector4 corner1;
|
||||
public Vector4 corner2;
|
||||
public Vector4 corner3;
|
||||
public Vector4 corner4;
|
||||
public Vector4 corner5;
|
||||
public Vector4 corner6;
|
||||
public Vector4 corner7;
|
||||
public float4 corner0;
|
||||
public float4 corner1;
|
||||
public float4 corner2;
|
||||
public float4 corner3;
|
||||
public float4 corner4;
|
||||
public float4 corner5;
|
||||
public float4 corner6;
|
||||
public float4 corner7;
|
||||
}
|
||||
|
||||
[GenerateHLSL(PackingRules.Exact, false)]
|
||||
@@ -36,32 +36,32 @@ namespace Misaki.AoVolume
|
||||
{
|
||||
// 4 x float3 = 48 bytes.
|
||||
// TODO: pack the axes into 16-bit UNORM per channel, and consider a quaternionic representation.
|
||||
public Vector3 center;
|
||||
public Vector3 right;
|
||||
public Vector3 up;
|
||||
public float3 center;
|
||||
public float3 right;
|
||||
public float3 up;
|
||||
|
||||
public Vector3 extent;
|
||||
public float3 extent;
|
||||
|
||||
//public ushort quatX;
|
||||
//public ushort quatY;
|
||||
//public ushort quatZ;
|
||||
//public ushort quatW;
|
||||
|
||||
public readonly Vector3 Forward => Vector3.Cross(up, right);
|
||||
public readonly float3 Forward => math.cross(up, right);
|
||||
|
||||
public OrientedBoundingBox(Matrix4x4 trs)
|
||||
public OrientedBoundingBox(float4x4 trs)
|
||||
{
|
||||
var vecX = (Vector3)trs.GetColumn(0);
|
||||
var vecY = (Vector3)trs.GetColumn(1);
|
||||
var vecZ = (Vector3)trs.GetColumn(2);
|
||||
float3 vecX = trs.c0.xyz;
|
||||
float3 vecY = trs.c1.xyz;
|
||||
float3 vecZ = trs.c2.xyz;
|
||||
|
||||
center = trs.GetColumn(3);
|
||||
right = vecX * (1.0f / vecX.magnitude);
|
||||
up = vecY * (1.0f / vecY.magnitude);
|
||||
center = trs.c3.xyz;
|
||||
right = vecX * (1.0f / math.length(vecX));
|
||||
up = vecY * (1.0f / math.length(vecY));
|
||||
|
||||
extent.x = 0.5f * vecX.magnitude;
|
||||
extent.y = 0.5f * vecY.magnitude;
|
||||
extent.z = 0.5f * vecZ.magnitude;
|
||||
extent.x = 0.5f * math.length(vecX);
|
||||
extent.y = 0.5f * math.length(vecY);
|
||||
extent.z = 0.5f * math.length(vecZ);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -16,17 +16,17 @@ struct CullingData
|
||||
// PackingRules = Exact
|
||||
struct GPUFrustum
|
||||
{
|
||||
float3 normal0;
|
||||
float4 normal0;
|
||||
float dist0;
|
||||
float3 normal1;
|
||||
float4 normal1;
|
||||
float dist1;
|
||||
float3 normal2;
|
||||
float4 normal2;
|
||||
float dist2;
|
||||
float3 normal3;
|
||||
float4 normal3;
|
||||
float dist3;
|
||||
float3 normal4;
|
||||
float4 normal4;
|
||||
float dist4;
|
||||
float3 normal5;
|
||||
float4 normal5;
|
||||
float dist5;
|
||||
float4 corner0;
|
||||
float4 corner1;
|
||||
@@ -42,10 +42,10 @@ struct GPUFrustum
|
||||
// PackingRules = Exact
|
||||
struct OrientedBoundingBox
|
||||
{
|
||||
float3 center;
|
||||
float3 right;
|
||||
float3 up;
|
||||
float3 extent;
|
||||
float4 center;
|
||||
float4 right;
|
||||
float4 up;
|
||||
float4 extent;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
@@ -10,12 +11,12 @@ namespace Misaki.AoVolume
|
||||
{
|
||||
[HideInInspector]
|
||||
[NonSerialized]
|
||||
public Matrix4x4 worldMatrix;
|
||||
public float4x4 worldMatrix;
|
||||
[HideInInspector]
|
||||
[NonSerialized]
|
||||
public Matrix4x4 inverseWorldMatrix;
|
||||
public float4x4 inverseWorldMatrix;
|
||||
|
||||
public Vector3 size;
|
||||
public float3 size;
|
||||
|
||||
[Range(0.0f, 1.0f)]
|
||||
public float intensity;
|
||||
@@ -26,7 +27,7 @@ namespace Misaki.AoVolume
|
||||
|
||||
public static VolumeData Default => new()
|
||||
{
|
||||
size = Vector3.one,
|
||||
size = new float3(1f),
|
||||
intensity = 1.0f,
|
||||
falloff = 0.25f,
|
||||
normalFalloff = 0.0f
|
||||
|
||||
Reference in New Issue
Block a user