Initial Upload;
This commit is contained in:
28
Runtime/Shader/Includes/VolumeData.cs
Normal file
28
Runtime/Shader/Includes/VolumeData.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
|
||||
namespace Misaki.AoVolume
|
||||
{
|
||||
[GenerateHLSL]
|
||||
internal enum VolumeType
|
||||
{
|
||||
Capsule,
|
||||
Box
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
[GenerateHLSL(needAccessors = false)]
|
||||
internal struct VolumeData
|
||||
{
|
||||
public VolumeType volumeType;
|
||||
|
||||
public Matrix4x4 worldMatrix;
|
||||
public Matrix4x4 inverseWorldMatrix;
|
||||
public Vector3 size;
|
||||
|
||||
public float intensity;
|
||||
public float falloff;
|
||||
public float normalFalloff;
|
||||
}
|
||||
}
|
||||
27
Runtime/Shader/Includes/VolumeData.cs.hlsl
Normal file
27
Runtime/Shader/Includes/VolumeData.cs.hlsl
Normal file
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// This file was automatically generated. Please don't edit by hand. Execute Editor command [ Edit > Rendering > Generate Shader Includes ] instead
|
||||
//
|
||||
|
||||
#ifndef VOLUMEDATA_CS_HLSL
|
||||
#define VOLUMEDATA_CS_HLSL
|
||||
//
|
||||
// Misaki.AoVolume.VolumeType: static fields
|
||||
//
|
||||
#define VOLUMETYPE_CAPSULE (0)
|
||||
#define VOLUMETYPE_BOX (1)
|
||||
|
||||
// Generated from Misaki.AoVolume.VolumeData
|
||||
// PackingRules = Exact
|
||||
struct VolumeData
|
||||
{
|
||||
int volumeType;
|
||||
float4x4 worldMatrix;
|
||||
float4x4 inverseWorldMatrix;
|
||||
float3 size;
|
||||
float intensity;
|
||||
float falloff;
|
||||
float normalFalloff;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
7
Runtime/Shader/Includes/VolumeData.cs.hlsl.meta
Normal file
7
Runtime/Shader/Includes/VolumeData.cs.hlsl.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 09a8e9400b2c54c42a5b447eecb8a1fe
|
||||
ShaderIncludeImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
2
Runtime/Shader/Includes/VolumeData.cs.meta
Normal file
2
Runtime/Shader/Includes/VolumeData.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c7f876ff60a035f43a75279d1731ac41
|
||||
60
Runtime/Shader/Includes/VolumeSDF.hlsl
Normal file
60
Runtime/Shader/Includes/VolumeSDF.hlsl
Normal file
@@ -0,0 +1,60 @@
|
||||
#ifndef VOLUME_SDF
|
||||
#define VOLUME_SDF
|
||||
|
||||
#include "Packages/com.misaki.ao-volume/Runtime/Shader/Includes/VolumeData.cs.hlsl"
|
||||
|
||||
float3 GetPositionLS(float4x4 inverseWorldMatrix, float3 positionWS)
|
||||
{
|
||||
return mul(inverseWorldMatrix, float4(positionWS, 1.0)).xyz;
|
||||
}
|
||||
|
||||
float BoxSDF(float3 positionWS, VolumeData data)
|
||||
{
|
||||
float3 halfDim = data.size * 0.5;
|
||||
|
||||
float3 positionLS = GetPositionLS(data.inverseWorldMatrix, positionWS);
|
||||
|
||||
float3 d = halfDim - abs(positionLS);
|
||||
float distToFace = min(d.x, min(d.y, d.z));
|
||||
float maxDist = min(halfDim.x, min(halfDim.y, halfDim.z));
|
||||
|
||||
return saturate(distToFace / maxDist);
|
||||
}
|
||||
|
||||
float CapsuleSDF(float3 positionWS, VolumeData data)
|
||||
{
|
||||
float3 positionLS = GetPositionLS(data.inverseWorldMatrix, positionWS);
|
||||
|
||||
float radius = data.size.x;
|
||||
float length = data.size.y;
|
||||
|
||||
// Define capsule along Y-axis in local space
|
||||
float3 capsuleStart = float3(0, -length * 0.5, 0);
|
||||
float3 capsuleEnd = float3(0, length * 0.5, 0);
|
||||
|
||||
// Compute SDF in local space
|
||||
float3 dir = capsuleEnd - capsuleStart;
|
||||
float lengthSq = dot(dir, dir);
|
||||
float3 toPoint = positionLS - capsuleStart;
|
||||
float projection = saturate(dot(toPoint, dir) / lengthSq);
|
||||
float3 closestPoint = capsuleStart + projection * dir;
|
||||
return distance(positionLS, closestPoint) - radius;
|
||||
}
|
||||
|
||||
float EvaluateAOVolumeMask(VolumeData data, float3 positionWS, float3 normalWS)
|
||||
{
|
||||
float volumeSDF = 1.0;
|
||||
switch (data.volumeType)
|
||||
{
|
||||
case VOLUMETYPE_CAPSULE:
|
||||
volumeSDF = CapsuleSDF(positionWS, data);
|
||||
break;
|
||||
case VOLUMETYPE_BOX:
|
||||
volumeSDF = BoxSDF(positionWS, data);
|
||||
break;
|
||||
}
|
||||
|
||||
return volumeSDF;
|
||||
}
|
||||
|
||||
#endif
|
||||
7
Runtime/Shader/Includes/VolumeSDF.hlsl.meta
Normal file
7
Runtime/Shader/Includes/VolumeSDF.hlsl.meta
Normal file
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 415aa9e4981653147803fb5036c79f4c
|
||||
ShaderIncludeImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user