static public DecalData [ ] m_DecalDatas = new DecalData [ kDecalBlockSize ] ;
static public SFiniteLightBound [ ] m_Bounds = new SFiniteLightBound [ kDecalBlockSize ] ;
static public LightVolumeData [ ] m_LightVolumes = new LightVolumeData [ kDecalBlockSize ] ;
static public TextureScaleBias [ ] m_DiffuseTextureScaleBias = new TextureScaleBias [ kDecalBlockSize ] ;
static public TextureScaleBias [ ] m_NormalTextureScaleBias = new TextureScaleBias [ kDecalBlockSize ] ;
static public TextureScaleBias [ ] m_MaskTextureScaleBias = new TextureScaleBias [ kDecalBlockSize ] ;
static public int m_DecalDatasCount = 0 ;
static public float [ ] m_BoundingDistances = new float [ 1 ] ;
m_Normal . m_Texture = m_Material . GetTexture ( "_NormalMap" ) ;
m_Mask . m_Texture = m_Material . GetTexture ( "_MaskMap" ) ;
m_Blend = m_Material . GetFloat ( "_DecalBlend" ) ;
m_AlbedoContribution = m_Material . GetFloat ( "_AlbedoMode" ) ;
}
public DecalSet ( Material material )
return res ;
}
public void UpdateCachedData ( Transform transform , float drawDistance , float fadeScale , Vector4 uvScaleBias , DecalHandle handle )
public void UpdateCachedData ( Transform transform , float drawDistance , float fadeScale , Vector4 uvScaleBias , bool affectsTransparency , DecalHandle handle )
{
if ( m_Material = = null )
return ;
: instance . DrawDistance ;
m_CachedDrawDistances [ index ] . y = fadeScale ;
m_CachedUVScaleBias [ index ] = uvScaleBias ;
m_CachedAffectsTransparency [ index ] = affectsTransparency ;
public DecalHandle AddDecal ( Transform transform , float drawDistance , float fadeScale , Vector4 uvScaleBias , int materialID )
public DecalHandle AddDecal ( Transform transform , float drawDistance , float fadeScale , Vector4 uvScaleBias , bool affectsTransparency , int materialID )
{
// increase array size if no space left
if ( m_DecalsCount = = m_Handles . Length )
Matrix4x4 [ ] newCachedNormalToWorld = new Matrix4x4 [ m_DecalsCount + kDecalBlockSize ] ;
Vector2 [ ] newCachedDrawDistances = new Vector2 [ m_DecalsCount + kDecalBlockSize ] ;
Vector4 [ ] newCachedUVScaleBias = new Vector4 [ m_DecalsCount + kDecalBlockSize ] ;
bool [ ] newCachedAffectsTransparency = new bool [ m_DecalsCount + kDecalBlockSize ] ;
m_ResultIndices = new int [ m_DecalsCount + kDecalBlockSize ] ;
m_Handles . CopyTo ( newHandles , 0 ) ;
m_CachedDrawDistances . CopyTo ( newCachedDrawDistances , 0 ) ;
m_CachedUVScaleBias . CopyTo ( newCachedUVScaleBias , 0 ) ;
m_CachedAffectsTransparency . CopyTo ( newCachedAffectsTransparency , 0 ) ;
m_Handles = newHandles ;
m_BoundingSpheres = newSpheres ;
m_CachedUVScaleBias = newCachedUVScaleBias ;
m_CachedAffectsTransparency = newCachedAffectsTransparency ;
UpdateCachedData ( transform , drawDistance , fadeScale , uvScaleBias , decalHandle ) ;
UpdateCachedData ( transform , drawDistance , fadeScale , uvScaleBias , affectsTransparency , decalHandle ) ;
m_DecalsCount + + ;
return decalHandle ;
}
m_CachedNormalToWorld [ removeAtIndex ] = m_CachedNormalToWorld [ m_DecalsCount - 1 ] ;
m_CachedDrawDistances [ removeAtIndex ] = m_CachedDrawDistances [ m_DecalsCount - 1 ] ;
m_CachedUVScaleBias [ removeAtIndex ] = m_CachedUVScaleBias [ m_DecalsCount - 1 ] ;
m_CachedAffectsTransparency [ removeAtIndex ] = m_CachedAffectsTransparency [ m_DecalsCount - 1 ] ;
m_DecalsCount - - ;
handle . m_Index = kInvalidIndex ;
}
return ;
if ( m_NumResults = = 0 )
return ;
// only add if anything in this decal set is visible.
AddToTextureList ( ref instance . m_TextureList ) ;
bool anyAffectTransparency = false ;
AssignCurrentBatches ( ref decalToWorldBatch , ref normalToWorldBatch , batchCount ) ;
normalToWorldBatch [ instanceCount ] = m_CachedNormalToWorld [ decalIndex ] ;
float fadeFactor = Mathf . Clamp ( ( cullDistance - distanceToDecal ) / ( cullDistance * ( 1.0f - m_CachedDrawDistances [ decalIndex ] . y ) ) , 0.0f , 1.0f ) ;
normalToWorldBatch [ instanceCount ] . m03 = fadeFactor * m_Blend ; // vector3 rotation matrix so bottom row and last column can be used for other data to save space
normalToWorldBatch [ instanceCount ] . m13 = m_AlbedoContribution ;
m_DecalDatas [ m_DecalDatasCount ] . worldToDecal = decalToWorldBatch [ instanceCount ] . inverse ;
m_DecalDatas [ m_DecalDatasCount ] . normalToWorld = normalToWorldBatch [ instanceCount ] ;
m_DecalDatas [ m_DecalDatasCount ] . diffuseScaleBias = m_Diffuse . m_ScaleBias ;
m_DecalDatas [ m_DecalDatasCount ] . normalScaleBias = m_Normal . m_ScaleBias ;
m_DecalDatas [ m_DecalDatasCount ] . maskScaleBias = m_Mask . m_ScaleBias ;
GetDecalVolumeDataAndBound ( decalToWorldBatch [ instanceCount ] , worldToView ) ;
m_DecalDatasCount + + ;
if ( m_CachedAffectsTransparency [ decalIndex ] )
{
m_DecalDatas [ m_DecalDatasCount ] . worldToDecal = decalToWorldBatch [ instanceCount ] . inverse ;
m_DecalDatas [ m_DecalDatasCount ] . normalToWorld = normalToWorldBatch [ instanceCount ] ;
// we have not allocated the textures in atlas yet, so only store references to them
m_DiffuseTextureScaleBias [ m_DecalDatasCount ] = m_Diffuse ;
m_NormalTextureScaleBias [ m_DecalDatasCount ] = m_Normal ;
m_MaskTextureScaleBias [ m_DecalDatasCount ] = m_Mask ;
GetDecalVolumeDataAndBound ( decalToWorldBatch [ instanceCount ] , worldToView ) ;
m_DecalDatasCount + + ;
anyAffectTransparency = true ;
}
instanceCount + + ;
if ( instanceCount = = kDrawIndexedBatchSize )
AssignCurrentBatches ( ref decalToWorldBatch , ref normalToWorldBatch , batchCount ) ;
}
}
}
// only add if any projectors in this decal set affect transparency, doesn't actually allocate textures in the atlas yet, this is because we want all the textures in the list so we can optimize the packing
if ( anyAffectTransparency )
{
AddToTextureList ( ref instance . m_TextureList ) ;
}
}
private Matrix4x4 [ ] m_CachedNormalToWorld = new Matrix4x4 [ kDecalBlockSize ] ;
private Vector2 [ ] m_CachedDrawDistances = new Vector2 [ kDecalBlockSize ] ; // x - draw distance, y - fade scale
private Vector4 [ ] m_CachedUVScaleBias = new Vector4 [ kDecalBlockSize ] ; // xy - scale, zw bias
private bool [ ] m_CachedAffectsTransparency = new bool [ kDecalBlockSize ] ;
private float m_AlbedoContribution = 0 ;
TextureScaleBias m_Diffuse = new TextureScaleBias ( ) ;
TextureScaleBias m_Normal = new TextureScaleBias ( ) ;
public DecalHandle AddDecal ( Transform transform , float drawDistance , float fadeScale , Vector4 uvScaleBias , Material material )
public DecalHandle AddDecal ( Transform transform , float drawDistance , float fadeScale , Vector4 uvScaleBias , bool affectsTransparency , Material material )
{
DecalSet decalSet = null ;
int key = material ! = null ? material . GetInstanceID ( ) : kNullMaterialIndex ;
m_DecalSets . Add ( key , decalSet ) ;
}
return decalSet . AddDecal ( transform , drawDistance , fadeScale , uvScaleBias , key ) ;
return decalSet . AddDecal ( transform , drawDistance , fadeScale , uvScaleBias , affectsTransparency , key ) ;
}
public void RemoveDecal ( DecalHandle handle )
}
}
public void UpdateCachedData ( Transform transform , float drawDistance , float fadeScale , Vector4 uvScaleBias , DecalHandle handle )
public void UpdateCachedData ( Transform transform , float drawDistance , float fadeScale , Vector4 uvScaleBias , bool affectsTransparency , DecalHandle handle )
{
if ( ! DecalHandle . IsValid ( handle ) )
return ;
if ( m_DecalSets . TryGetValue ( key , out decalSet ) )
{
decalSet . UpdateCachedData ( transform , drawDistance , fadeScale , uvScaleBias , handle ) ;
decalSet . UpdateCachedData ( transform , drawDistance , fadeScale , uvScaleBias , affectsTransparency , handle ) ;
}
}
}
}
private void UpdateDecalDatasWithAtlasInfo ( )
{
for ( int decalDataIndex = 0 ; decalDataIndex < m_DecalDatasCount ; decalDataIndex + + )
{
m_DecalDatas [ decalDataIndex ] . diffuseScaleBias = m_DiffuseTextureScaleBias [ decalDataIndex ] . m_ScaleBias ;
m_DecalDatas [ decalDataIndex ] . normalScaleBias = m_NormalTextureScaleBias [ decalDataIndex ] . m_ScaleBias ;
m_DecalDatas [ decalDataIndex ] . maskScaleBias = m_MaskTextureScaleBias [ decalDataIndex ] . m_ScaleBias ;
}
}
public void UpdateTextureAtlas ( CommandBuffer cmd )
{
m_AllocationSuccess = true ;
}
}
m_PrevAllocationSuccess = m_AllocationSuccess ;
// now that textures have been stored in the atlas we can update their location info in decal data
UpdateDecalDatasWithAtlasInfo ( ) ;
}
public void CreateDrawData ( )
m_DecalDatas = new DecalData [ newDecalDatasSize ] ;
m_Bounds = new SFiniteLightBound [ newDecalDatasSize ] ;
m_LightVolumes = new LightVolumeData [ newDecalDatasSize ] ;
m_DiffuseTextureScaleBias = new TextureScaleBias [ newDecalDatasSize ] ;
m_NormalTextureScaleBias = new TextureScaleBias [ newDecalDatasSize ] ;
m_MaskTextureScaleBias = new TextureScaleBias [ newDecalDatasSize ] ;
}
foreach ( var pair in m_DecalSets )
{