Пример #1
0
    public void Flush()
    {
        if (m_mesh == null)
        {
            Debug.LogWarning("AlembicPointsRenderer: mesh is not assigned");
            return;
        }
        if (m_materials == null || m_materials.Length == 0 || (m_materials.Length == 1 && m_materials[0] == null))
        {
            Debug.LogWarning("AlembicPointsRenderer: material is not assigned");
            return;
        }



        var points         = GetComponent <AlembicPoints>();
        var abcData        = points.abcData;
        int max_instances  = points.abcPeakVertexCount;
        int instance_count = abcData.count;

        m_bounds.center  = mul(abcData.boundsCenter, m_trans_scale);
        m_bounds.extents = mul(abcData.boundsExtents, m_trans_scale);

        if (instance_count == 0)
        {
            return;
        }                                    // nothing to draw

        // update data texture
        if (m_texPositions == null)
        {
            int height = ceildiv(max_instances, TextureWidth);
            m_texPositions = CreateDataTexture(TextureWidth, height, RenderTextureFormat.ARGBFloat);
            m_texIDs       = CreateDataTexture(TextureWidth, height, RenderTextureFormat.RFloat);
        }
        AbcAPI.aiPointsCopyPositionsToTexture(ref abcData, m_texPositions.GetNativeTexturePtr(), m_texPositions.width, m_texPositions.height, AbcAPI.GetTextureFormat(m_texPositions));
        AbcAPI.aiPointsCopyIDsToTexture(ref abcData, m_texIDs.GetNativeTexturePtr(), m_texIDs.width, m_texIDs.height, AbcAPI.GetTextureFormat(m_texIDs));


        if (m_expanded_mesh == null)
        {
            m_expanded_mesh = CreateExpandedMesh(m_mesh, max_instances, out m_instances_par_batch);
            m_expanded_mesh.UploadMeshData(true);
            return;
        }

        if (m_actual_materials == null)
        {
            m_actual_materials = new List <List <Material> >();
            while (m_actual_materials.Count < m_materials.Length)
            {
                m_actual_materials.Add(new List <Material>());
            }
        }

        var trans = GetComponent <Transform>();

        m_expanded_mesh.bounds = m_bounds;
        m_count_rate           = Mathf.Max(m_count_rate, 0.0f);
        instance_count         = Mathf.Min((int)(instance_count * m_count_rate), (int)(max_instances * m_count_rate));
        int batch_count = ceildiv(instance_count, m_instances_par_batch);

        // clone materials if needed
        for (int i = 0; i < m_actual_materials.Count; ++i)
        {
            var a = m_actual_materials[i];
            while (a.Count < batch_count)
            {
                Material m = CloneMaterial(m_materials[i], a.Count);
                a.Add(m);
            }
        }

        // update materials
        var worldToLocalMatrix = trans.localToWorldMatrix;

        ForEachEveryMaterials((m) =>
        {
            m.SetInt("_NumInstances", instance_count);
            m.SetVector("_CountRate", new Vector4(m_count_rate, 1.0f / m_count_rate, 0.0f, 0.0f));
            m.SetVector("_ModelScale", m_model_scale);
            m.SetVector("_TransScale", m_trans_scale);
            m.SetMatrix("_Transform", worldToLocalMatrix);
        });

        // issue draw calls
        int       layer  = gameObject.layer;
        Matrix4x4 matrix = Matrix4x4.identity;

        m_actual_materials.ForEach(a =>
        {
            for (int i = 0; i < batch_count; ++i)
            {
                Graphics.DrawMesh(m_expanded_mesh, matrix, a[i], layer, null, 0, null, m_cast_shadow, m_receive_shadow);
            }
        });
    }