148 lines
8.1 KiB
C#

//pipelinedefine
#define H_URP
using HTraceWSGI.Scripts.Data.Private;
using UnityEngine;
using UnityEngine.Rendering;
namespace HTraceWSGI.Scripts.Passes.Shared
{
public class VoxelizationFunctionsShared
{
//globals
internal static readonly int g_OffsetAxisIndex = Shader.PropertyToID("_OffsetAxisIndex");
internal static readonly int g_AxisOffset = Shader.PropertyToID("_AxisOffset");
internal static readonly int g_CullingTrim = Shader.PropertyToID("_CullingTrim");
internal static readonly int g_OctantOffset = Shader.PropertyToID("_OctantOffset");
internal static readonly int g_CullingTrimAxis = Shader.PropertyToID("_CullingTrimAxis");
// Calculates offset data for voxelization
internal static void CalculateOctantOffsets(CommandBuffer cmd)
{
Vector2 cullingTrim = Vector2.zero;
Vector2 cullingTrimAxis = Vector2.zero;
Vector3 octantOffset = Vector3.zero;
Vector3 axisOffset = Vector3.zero;
Vector3 voxelResolutionSwizzled = new Vector3(HSettings.VoxelizationSettings.ExactData.Resolution.x, HSettings.VoxelizationSettings.ExactData.Resolution.z, HSettings.VoxelizationSettings.ExactData.Resolution.y);
Vector3 axisOffsetSign = VoxelizationRuntimeData.VoxelOctantCamera.transform.position - VoxelizationRuntimeData.VoxelCamera.transform.position;
int offsetAxisIndex = (int)VoxelizationRuntimeData.OffsetAxisIndex > 2 ? (int)VoxelizationRuntimeData.OffsetAxisIndex - 3 : (int)VoxelizationRuntimeData.OffsetAxisIndex;
int octantIndex = (int)VoxelizationRuntimeData.OctantIndex;
if (offsetAxisIndex == 0) // X axis
{
axisOffset = axisOffsetSign.x > 0 ? new Vector3(voxelResolutionSwizzled.x / 2, 0, 0) : new Vector3(0, 0, 0);
int cullingFarPlane = axisOffsetSign.x > 0
? Mathf.RoundToInt(VoxelizationRuntimeData.CullingCamera.Camera.farClipPlane * HSettings.VoxelizationSettings.ExactData.VoxelsPerMeter)
: Mathf.FloorToInt(VoxelizationRuntimeData.CullingCamera.Camera.farClipPlane * HSettings.VoxelizationSettings.ExactData.VoxelsPerMeter);
cullingTrim = axisOffsetSign.x > 0 ? new Vector2((int)voxelResolutionSwizzled.x - cullingFarPlane, (int)voxelResolutionSwizzled.x) : new Vector2(0, cullingFarPlane);
cullingTrimAxis = axisOffsetSign.x > 0 ? new Vector2(1, 0) : new Vector2(0, 1);
if (octantIndex == 1) octantOffset = new Vector3(0, voxelResolutionSwizzled.y / 2, 0);
if (octantIndex == 2) octantOffset = new Vector3(0, voxelResolutionSwizzled.y / 2, voxelResolutionSwizzled.z / 2);
if (octantIndex == 3) octantOffset = new Vector3(0, 0, 0);
if (octantIndex == 4) octantOffset = new Vector3(0, 0, voxelResolutionSwizzled.z / 2);
}
if (offsetAxisIndex == 1) // Y axis
{
axisOffset = axisOffsetSign.y > 0 ? new Vector3(0, voxelResolutionSwizzled.y / 2, 0) : new Vector3(0, 0, 0);
int cullingFarPlane = axisOffsetSign.y > 0
? Mathf.RoundToInt(VoxelizationRuntimeData.CullingCamera.Camera.farClipPlane * HSettings.VoxelizationSettings.ExactData.VoxelsPerMeter)
: Mathf.FloorToInt(VoxelizationRuntimeData.CullingCamera.Camera.farClipPlane * HSettings.VoxelizationSettings.ExactData.VoxelsPerMeter);
cullingTrim = axisOffsetSign.y > 0 ? new Vector2((int)voxelResolutionSwizzled.y - cullingFarPlane, (int)voxelResolutionSwizzled.y) : new Vector2(0, cullingFarPlane);
cullingTrimAxis = axisOffsetSign.y > 0 ? new Vector2(1, 0) : new Vector2(0, 1);
if (octantIndex == 1) octantOffset = new Vector3(0, 0, 0);
if (octantIndex == 2) octantOffset = new Vector3(0, 0, voxelResolutionSwizzled.z / 2);
if (octantIndex == 3) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, 0);
if (octantIndex == 4) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, voxelResolutionSwizzled.z / 2);
}
if (offsetAxisIndex == 2) // Z axis
{
axisOffset = axisOffsetSign.z > 0 ? new Vector3(0, 0, voxelResolutionSwizzled.z / 2) : new Vector3(0, 0, 0);
int cullingFarPlane = axisOffsetSign.z > 0
? Mathf.RoundToInt(VoxelizationRuntimeData.CullingCamera.Camera.farClipPlane * HSettings.VoxelizationSettings.ExactData.VoxelsPerMeter)
: Mathf.FloorToInt(VoxelizationRuntimeData.CullingCamera.Camera.farClipPlane * HSettings.VoxelizationSettings.ExactData.VoxelsPerMeter);
cullingTrim = axisOffsetSign.z > 0 ? new Vector2((int)voxelResolutionSwizzled.z - cullingFarPlane, (int)voxelResolutionSwizzled.z) : new Vector2(0, cullingFarPlane);
cullingTrimAxis = axisOffsetSign.z > 0 ? new Vector2(1, 0) : new Vector2(0, 1);
if (octantIndex == 1) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, voxelResolutionSwizzled.y / 2, 0);
if (octantIndex == 2) octantOffset = new Vector3(0, voxelResolutionSwizzled.y / 2, 0);
if (octantIndex == 3) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, 0);
if (octantIndex == 4) octantOffset = new Vector3(0, 0, 0);
}
cmd.SetGlobalInt(g_OffsetAxisIndex, offsetAxisIndex);
cmd.SetGlobalVector(g_AxisOffset, axisOffset);
cmd.SetGlobalVector(g_CullingTrim, cullingTrim);
cmd.SetGlobalVector(g_OctantOffset, octantOffset);
cmd.SetGlobalVector(g_CullingTrimAxis, cullingTrimAxis);
}
// Calculates offset data for copying compute shader
internal static Vector3 CalculateOctantOffsetsForCopyShader()
{
Vector3 octantOffset = Vector3.zero;
Vector3 voxelResolutionSwizzled = new Vector3(HSettings.VoxelizationSettings.ExactData.Resolution.x, HSettings.VoxelizationSettings.ExactData.Resolution.z, HSettings.VoxelizationSettings.ExactData.Resolution.y);
Vector3 axisOffsetSign = VoxelizationRuntimeData.VoxelOctantCamera.transform.position - VoxelizationRuntimeData.VoxelCamera.transform.position;
int offsetAxisIndex = (int)VoxelizationRuntimeData.OffsetAxisIndex > 2 ? (int)VoxelizationRuntimeData.OffsetAxisIndex - 3 : (int)VoxelizationRuntimeData.OffsetAxisIndex;
int octantIndex = (int)VoxelizationRuntimeData.OctantIndex;
if (offsetAxisIndex == 0) // X axis
{
if (axisOffsetSign.x < 0)
{
if (octantIndex == 1) octantOffset = new Vector3(0, 0, 0);
if (octantIndex == 2) octantOffset = new Vector3(0, 0, voxelResolutionSwizzled.z / 2);
if (octantIndex == 3) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, voxelResolutionSwizzled.z / 2);
if (octantIndex == 4) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, 0);
}
else
{
if (octantIndex == 1) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, 0);
if (octantIndex == 2) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, voxelResolutionSwizzled.z / 2);
if (octantIndex == 3) octantOffset = new Vector3(0, 0, 0);
if (octantIndex == 4) octantOffset = new Vector3(0, 0, voxelResolutionSwizzled.z / 2);
}
}
if (offsetAxisIndex == 1) // Y axis
{
if (octantIndex == 1) octantOffset = new Vector3(0, 0, 0);
if (octantIndex == 2) octantOffset = new Vector3(0, 0, voxelResolutionSwizzled.z / 2);
if (octantIndex == 3) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, 0);
if (octantIndex == 4) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, voxelResolutionSwizzled.z / 2);
}
if (offsetAxisIndex == 2) // Z axis
{
if (axisOffsetSign.z < 0)
{
if (octantIndex == 1) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, 0);
if (octantIndex == 2) octantOffset = new Vector3(0, 0, 0);
if (octantIndex == 3) octantOffset = new Vector3(0, 0, voxelResolutionSwizzled.z / 2);
if (octantIndex == 4) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, voxelResolutionSwizzled.z / 2);
}
else
{
if (octantIndex == 1) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, voxelResolutionSwizzled.z / 2);
if (octantIndex == 2) octantOffset = new Vector3(0, 0, voxelResolutionSwizzled.z / 2);
if (octantIndex == 3) octantOffset = new Vector3(0, 0, 0);
if (octantIndex == 4) octantOffset = new Vector3(voxelResolutionSwizzled.x / 2, 0, 0);
}
}
return octantOffset;
}
}
}