|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private static readonly int m_NormalToWorldID = Shader.PropertyToID("normalToWorld"); |
|
|
|
private static MaterialPropertyBlock m_PropertyBlock = new MaterialPropertyBlock(); |
|
|
|
|
|
|
|
private const int kDecalBlockSize = 128; |
|
|
|
|
|
|
|
// to work on Vulkan Mobile?
|
|
|
|
|
|
|
|
|
|
|
static public Mesh m_DecalMesh = null; |
|
|
|
static public Matrix4x4[] m_InstanceMatrices = new Matrix4x4[kDrawIndexedBatchSize]; |
|
|
|
static public Matrix4x4[] m_InstanceNormalToWorld = new Matrix4x4[kDrawIndexedBatchSize]; |
|
|
|
|
|
|
|
private Dictionary<int, DecalSet> m_DecalSets = new Dictionary<int, DecalSet>(); |
|
|
|
|
|
|
|
|
|
|
public void UpdateBoundingSphere(DecalProjectorComponent decal) |
|
|
|
{ |
|
|
|
m_CachedTransforms[decal.CullIndex] = decal.transform.localToWorldMatrix; |
|
|
|
|
|
|
|
Matrix4x4 decalRotation = Matrix4x4.Rotate(decal.transform.rotation); |
|
|
|
// z/y axis swap for normal to decal space
|
|
|
|
Vector4 row2 = decalRotation.GetRow(2); |
|
|
|
decalRotation.SetRow(2, decalRotation.GetRow(1)); |
|
|
|
decalRotation.SetRow(1, row2); |
|
|
|
m_CachedNormalToWorld[decal.CullIndex] = decalRotation; |
|
|
|
|
|
|
|
m_BoundingSpheres[decal.CullIndex] = GetDecalProjectBoundingSphere(m_CachedTransforms[decal.CullIndex]); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
DecalProjectorComponent[] newDecals = new DecalProjectorComponent[m_DecalsCount + kDecalBlockSize]; |
|
|
|
BoundingSphere[] newSpheres = new BoundingSphere[m_DecalsCount + kDecalBlockSize]; |
|
|
|
Matrix4x4[] newCachedTransforms = new Matrix4x4[m_DecalsCount + kDecalBlockSize]; |
|
|
|
Matrix4x4[] newCachedNormalToWorld = new Matrix4x4[m_DecalsCount + kDecalBlockSize]; |
|
|
|
m_CachedNormalToWorld.CopyTo(newCachedNormalToWorld, 0); |
|
|
|
m_CachedNormalToWorld = newCachedNormalToWorld; |
|
|
|
} |
|
|
|
|
|
|
|
m_Decals[m_DecalsCount] = decal; |
|
|
|
|
|
|
// update the bounding spheres array
|
|
|
|
m_BoundingSpheres[removeAtIndex] = m_BoundingSpheres[m_DecalsCount - 1]; |
|
|
|
m_CachedTransforms[removeAtIndex] = m_CachedTransforms[m_DecalsCount - 1]; |
|
|
|
m_CachedNormalToWorld[removeAtIndex] = m_CachedNormalToWorld[m_DecalsCount - 1]; |
|
|
|
m_DecalsCount--; |
|
|
|
decal.CullIndex = DecalProjectorComponent.kInvalidIndex; |
|
|
|
} |
|
|
|
|
|
|
int decalIndex = m_ResultIndices[resultIndex]; |
|
|
|
|
|
|
|
m_InstanceMatrices[instanceCount] = m_CachedTransforms[decalIndex]; |
|
|
|
m_InstanceNormalToWorld[instanceCount] = m_CachedNormalToWorld[decalIndex]; |
|
|
|
cmd.DrawMeshInstanced(m_DecalMesh, 0, m_Decals[0].m_Material, 0, m_InstanceMatrices, kDrawIndexedBatchSize); |
|
|
|
m_PropertyBlock.SetMatrixArray(m_NormalToWorldID, m_InstanceNormalToWorld); |
|
|
|
// cmd.SetGlobalMatrixArray(m_NormalToWorldID, m_InstanceNormalToWorld);
|
|
|
|
cmd.DrawMeshInstanced(m_DecalMesh, 0, m_Decals[0].m_Material, 0, m_InstanceMatrices, kDrawIndexedBatchSize, m_PropertyBlock); |
|
|
|
cmd.DrawMeshInstanced(m_DecalMesh, 0, m_Decals[0].m_Material, 0, m_InstanceMatrices, instanceCount); |
|
|
|
m_PropertyBlock.SetMatrixArray(m_NormalToWorldID, m_InstanceNormalToWorld); |
|
|
|
cmd.DrawMeshInstanced(m_DecalMesh, 0, m_Decals[0].m_Material, 0, m_InstanceMatrices, instanceCount, m_PropertyBlock); |
|
|
|
cmd.SetGlobalMatrixArray(m_NormalToWorldID, m_InstanceNormalToWorld); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
private int m_NumResults = 0; |
|
|
|
private int m_DecalsCount = 0; |
|
|
|
private Matrix4x4[] m_CachedTransforms = new Matrix4x4[kDecalBlockSize]; |
|
|
|
private Matrix4x4[] m_CachedNormalToWorld = new Matrix4x4[kDecalBlockSize]; |
|
|
|
private Material m_Material; |
|
|
|
} |
|
|
|
|
|
|
|