namespace UnityEngine.Experimental.Rendering.HDPipeline
{
[GenerateHLSL]
public struct VolumeProperties
public struct Density VolumeProperties
public static VolumeProperties GetNeutralVolume Properties ( )
public static Density VolumeProperties GetNeutralProperties ( )
VolumeProperties properties = new VolumeProperties ( ) ;
Density VolumeProperties properties = new Density VolumeProperties( ) ;
properties . scattering = Vector3 . zero ;
properties . extinction = 0 ;
} // struct VolumeProperties
[Serializable]
public class VolumeParameters
public class Density VolumeParameters
public bool isLocal ; // Enables voxelization
public Color albedo ; // Single scattering albedo [0, 1]
public float meanFreePath ; // In meters [1, inf]. Should be chromatic - this is an optimization!
public float asymmetry ; // Single global parameter for all volumes. TODO: UX
public bool isLocal ; // Enables voxelization
public Color albedo ; // Single scattering albedo [0, 1]
public float meanFreePath ; // In meters [1, inf]. Should be chromatic - this is an optimization!
public float asymmetry ; // Only used if (isLocal == false)
public VolumeParameters ( )
public Density VolumeParameters( )
{
isLocal = true ;
albedo = new Color ( 0.5f , 0.5f , 0.5f ) ;
asymmetry = Mathf . Clamp ( asymmetry , - 1.0f , 1.0f ) ;
}
public VolumeProperties GetProperties ( )
public Density VolumeProperties GetProperties ( )
VolumeProperties properties = new VolumeProperties ( ) ;
Density VolumeProperties properties = new Density VolumeProperties( ) ;
properties . scattering = GetScatteringCoefficient ( ) ;
properties . extinction = GetExtinctionCoefficient ( ) ;
} // class VolumeParameters
public struct DensityVolumeList
{
public List < OrientedBBox > bounds ;
public List < DensityVolumeProperties > properties ;
}
public class VolumetricLightingModule
{
ComputeShader m_VolumetricLightingCS = null ;
List < VBuffer > m_VBuffers = null ;
List < OrientedBBox > m_VisibleVolumes = null ;
List < VolumeProperties > m_VisibleVolumeProperties = null ;
public const int k_MaxVisibleVolumeCount = 5 1 2 ;
List < VBuffer > m_VBuffers = null ;
List < OrientedBBox > m_VisibleVolumeBound s = null ;
List < Density VolumeProperties> m_VisibleVolumeProperties = null ;
public const int k_MaxVisibleVolumeCount = 5 1 2 ;
static ComputeBuffer s_VisibleVolumesBuffer = null ;
static ComputeBuffer s_VisibleVolumeBound sBuffer = null ;
static ComputeBuffer s_VisibleVolumePropertiesBuffer = null ;
float m_VBufferNearPlane = 0.5f ; // Distance in meters; dynamic modifications not handled by reprojection
m_VolumetricLightingCS = asset . renderPipelineResources . volumetricLightingCS ;
m_VBuffers = new List < VBuffer > ( ) ;
m_VisibleVolumes = new List < OrientedBBox > ( ) ;
m_VisibleVolumeProperties = new List < VolumeProperties > ( ) ;
s_VisibleVolumesBuffer = new ComputeBuffer ( k_MaxVisibleVolumeCount , System . Runtime . InteropServices . Marshal . SizeOf ( typeof ( OrientedBBox ) ) ) ;
s_VisibleVolumePropertiesBuffer = new ComputeBuffer ( k_MaxVisibleVolumeCount , System . Runtime . InteropServices . Marshal . SizeOf ( typeof ( VolumeProperties ) ) ) ;
m_VisibleVolumeBound s = new List < OrientedBBox > ( ) ;
m_VisibleVolumeProperties = new List < Density VolumeProperties> ( ) ;
s_VisibleVolumeBound sBuffer = new ComputeBuffer ( k_MaxVisibleVolumeCount , System . Runtime . InteropServices . Marshal . SizeOf ( typeof ( OrientedBBox ) ) ) ;
s_VisibleVolumePropertiesBuffer = new ComputeBuffer ( k_MaxVisibleVolumeCount , System . Runtime . InteropServices . Marshal . SizeOf ( typeof ( Density VolumeProperties) ) ) ;
}
public void Cleanup ( )
}
m_VBuffers = null ;
m_VisibleVolumes = null ;
m_VisibleVolumeBound s = null ;
CoreUtils . SafeRelease ( s_VisibleVolumesBuffer ) ;
CoreUtils . SafeRelease ( s_VisibleVolumeBound sBuffer ) ;
CoreUtils . SafeRelease ( s_VisibleVolumePropertiesBuffer ) ;
}
HomogeneousDensityVolume globalVolume = HomogeneousDensityVolume . GetGlobalHomogeneousDensityVolume ( ) ;
// TODO: may want to cache these results somewhere.
VolumeProperties globalVolumeProperties = ( globalVolume ! = null ) ? globalVolume . volumeP arameters. GetProperties ( )
: VolumeProperties . GetNeutralVolume Properties ( ) ;
Density VolumeProperties globalVolumeProperties = ( globalVolume ! = null ) ? globalVolume . p arameters. GetProperties ( )
: Density VolumeProperties. GetNeutralProperties ( ) ;
float asymmetry = globalVolume ! = null ? globalVolume . volumeP arameters. asymmetry : 0 ;
float asymmetry = globalVolume ! = null ? globalVolume . p arameters. asymmetry : 0 ;
cmd . SetGlobalVector ( HDShaderIDs . _Global_Scattering , globalVolumeProperties . scattering ) ;
cmd . SetGlobalFloat ( HDShaderIDs . _Global_Extinction , globalVolumeProperties . extinction ) ;
cmd . SetGlobalFloat ( HDShaderIDs . _Global_Asymmetry , asymmetry ) ;
cmd . SetGlobalTexture ( HDShaderIDs . _VBufferLighting , vBuffer . GetLightingIntegralBuffer ( ) ) ;
}
public void VoxelizeDensityVolumes ( HDCamera camera , CommandBuffer cmd )
public DensityVolumeList PrepareVisibleDensityVolumeList ( HDCamera camera , CommandBuffer cmd )
if ( preset = = VolumetricLightingPreset . Off ) return ;
DensityVolumeList densityVolumes = new DensityVolumeList ( ) ;
if ( preset = = VolumetricLightingPreset . Off ) return densityVolumes ;
Vector3 camPosition = camera . camera . transform . position ;
Vector3 camOffset = Vector3 . zero ; // World-origin-relative
camOffset = - camPosition ; // Camera-relative
}
m_VisibleVolumes . Clear ( ) ;
m_VisibleVolumeBound s . Clear ( ) ;
// Collect all the visible volume data, and upload it to the GPU.
// Collect all visible finit e volume data, and upload it to the GPU.
if ( volume . enabled & & volume . volumeP arameters. IsLocalVolume ( ) )
if ( volume . enabled & & volume . p arameters. IsLocalVolume ( ) )
{
// TODO: cache these?
var obb = OrientedBBox . Create ( volume . transform ) ;
{
// TODO: cache these?
var properties = volume . volumeParameters . GetProperties ( ) ;
var properties = volume . parameters . GetProperties ( ) ;
m_VisibleVolumes . Add ( obb ) ;
m_VisibleVolumeBounds . Add ( obb ) ;
s_VisibleVolumesBuffer . SetData ( m_VisibleVolumes ) ;
s_VisibleVolumeBoundsBuffer . SetData ( m_VisibleVolumeBounds ) ;
// Fill the struct with pointers in order to share the data with the light loop.
densityVolumes . bounds = m_VisibleVolumeBounds ;
densityVolumes . properties = m_VisibleVolumeProperties ;
return densityVolumes ;
}
// Ref: https://en.wikipedia.org/wiki/Close-packing_of_equal_spheres
Debug . Assert ( vBuffer ! = null ) ;
HomogeneousDensityVolume globalVolume = HomogeneousDensityVolume . GetGlobalHomogeneousDensityVolume ( ) ;
float asymmetry = globalVolume ! = null ? globalVolume . volumeP arameters. asymmetry : 0 ;
float asymmetry = globalVolume ! = null ? globalVolume . p arameters. asymmetry : 0 ;
if ( globalVolume = = null )
{