Exemplo n.º 1
0
            public void CreateDrawData()
            {
                if (m_Material == null)
                {
                    return;
                }
                if (m_NumResults == 0)
                {
                    return;
                }
                // only add if anything in this decal set is visible.
                AddToTextureList(ref instance.m_TextureList);
                int instanceCount = 0;
                int batchCount    = 0;

                Matrix4x4[] decalToWorldBatch  = null;
                Matrix4x4[] normalToWorldBatch = null;

                AssignCurrentBatches(ref decalToWorldBatch, ref normalToWorldBatch, batchCount);

                Vector3   cameraPos   = instance.CurrentCamera.transform.position;
                Matrix4x4 worldToView = LightLoop.WorldToCamera(instance.CurrentCamera);

                for (int resultIndex = 0; resultIndex < m_NumResults; resultIndex++)
                {
                    int decalIndex = m_ResultIndices[resultIndex];
                    // do additional culling based on individual decal draw distances
                    float distanceToDecal = (cameraPos - m_BoundingSpheres[decalIndex].position).magnitude;
                    float cullDistance    = m_CachedDrawDistances[decalIndex].x + m_BoundingSpheres[decalIndex].radius;
                    if (distanceToDecal < cullDistance)
                    {
                        // d-buffer data
                        decalToWorldBatch[instanceCount]  = m_CachedDecalToWorld[decalIndex];
                        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].SetRow(3, m_CachedUVScaleBias[decalIndex]);

                        // clustered forward data
                        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++;

                        instanceCount++;
                        if (instanceCount == kDrawIndexedBatchSize)
                        {
                            instanceCount = 0;
                            batchCount++;
                            AssignCurrentBatches(ref decalToWorldBatch, ref normalToWorldBatch, batchCount);
                        }
                    }
                }
            }
Exemplo n.º 2
0
            public void CreateDrawData()
            {
                int instanceCount = 0;
                int batchCount    = 0;

                Matrix4x4[] decalToWorldBatch     = null;
                Matrix4x4[] normalToWorldBatch    = null;
                bool        anyAffectTransparency = false;

                AssignCurrentBatches(ref decalToWorldBatch, ref normalToWorldBatch, batchCount);

                Vector3   cameraPos      = instance.CurrentCamera.transform.position;
                Matrix4x4 worldToView    = LightLoop.WorldToCamera(instance.CurrentCamera);
                bool      perChannelMask = instance.perChannelMask;

                for (int resultIndex = 0; resultIndex < m_NumResults; resultIndex++)
                {
                    int decalIndex = m_ResultIndices[resultIndex];
                    // do additional culling based on individual decal draw distances
                    float distanceToDecal = (cameraPos - m_BoundingSpheres[decalIndex].position).magnitude;
                    float cullDistance    = m_CachedDrawDistances[decalIndex].x + m_BoundingSpheres[decalIndex].radius;
                    if (distanceToDecal < cullDistance)
                    {
                        // d-buffer data
                        decalToWorldBatch[instanceCount]  = m_CachedDecalToWorld[decalIndex];
                        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;
                        normalToWorldBatch[instanceCount].SetRow(3, m_CachedUVScaleBias[decalIndex]);

                        // clustered forward data
                        if (m_CachedAffectsTransparency[decalIndex])
                        {
                            m_DecalDatas[m_DecalDatasCount].worldToDecal  = decalToWorldBatch[instanceCount].inverse;
                            m_DecalDatas[m_DecalDatasCount].normalToWorld = normalToWorldBatch[instanceCount];
                            m_DecalDatas[m_DecalDatasCount].baseColor     = m_BaseColor;
                            m_DecalDatas[m_DecalDatasCount].blendParams   = m_BlendParams;
                            if (!perChannelMask)
                            {
                                m_DecalDatas[m_DecalDatasCount].blendParams.z = (float)Decal.MaskBlendFlags.Smoothness;
                            }

                            // 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)
                        {
                            instanceCount = 0;
                            batchCount++;
                            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);
                }
            }