Ejemplo n.º 1
0
        private static void CreateSDFMesh(MenuCommand menuCommand)
        {
            GameObject selection = Selection.activeGameObject;

            GameObject child = new GameObject("Mesh");

            child.transform.SetParent(selection.transform);
            child.transform.Reset();

            SDFMesh newMesh = child.AddComponent <SDFMesh>();

            Selection.activeGameObject = child;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Some mesh data is shared across all instances, such as the sample and UV information as well as the start indices in those static buffers
        /// for all meshes. Returns true if the static buffers have been changed and need to be resent to the groups.
        /// </summary>
        /// <param name="locals">List of SDFMesh objects to ensure are in the global list.</param>
        /// <param name="onlySendBufferOnChange">Whether to invoke the components and inform them the buffer has changed. This is only really necessary when the size changes.</param>
        private static bool RebuildGlobalMeshData(IList <SDFObject> locals, bool onlySendBufferOnChange = true)
        {
            int previousMeshSamplesCount = m_meshSamples.Count;
            int previousMeshUVsCount     = m_meshPackedUVs.Count;

            m_meshSamples.Clear();
            m_meshPackedUVs.Clear();

            m_meshSdfSampleStartIndices.Clear();
            m_meshSdfUVStartIndices.Clear();

            // remove null refs
            for (int i = m_globalSDFMeshes.Count - 1; i >= 0; --i)
            {
                if (m_globalSDFMeshes[i] == null || m_globalSDFMeshes[i].Asset == null)
                {
                    m_globalSDFMeshes.RemoveAt(i);
                }
            }

            for (int i = 0; i < locals.Count; i++)
            {
                if (locals[i] is SDFMesh mesh && !m_globalSDFMeshes.Contains(mesh))
                {
                    m_globalSDFMeshes.Add(mesh);
                }
            }

            // loop over each mesh, adding its samples/uvs to the sample buffer
            // and taking note of where each meshes samples start in the buffer.
            // check for repeats so we don't add the same mesh to the samples buffer twice
            for (int i = 0; i < m_globalSDFMeshes.Count; i++)
            {
                SDFMesh mesh = m_globalSDFMeshes[i];

                // ignore meshes which are in the list but not present in any group
                if (m_meshCounts.TryGetValue(mesh.ID, out int count) && count <= 0)
                {
                    continue;
                }

                mesh.Asset.GetDataArrays(out float[] samples, out float[] packedUVs);

                if (!m_meshSdfSampleStartIndices.ContainsKey(mesh.ID))
                {
                    int startIndex = m_meshSamples.Count;
                    m_meshSamples.AddRange(samples);
                    m_meshSdfSampleStartIndices.Add(mesh.ID, startIndex);
                }

                if (mesh.Asset.HasUVs && !m_meshSdfUVStartIndices.ContainsKey(mesh.ID))
                {
                    int startIndex = m_meshPackedUVs.Count;
                    m_meshPackedUVs.AddRange(packedUVs);
                    m_meshSdfUVStartIndices.Add(mesh.ID, startIndex);
                }
            }

            bool newBuffers = false;

            if (m_meshSamplesBuffer == null || !m_meshSamplesBuffer.IsValid() || previousMeshSamplesCount != m_meshSamples.Count)
            {
                m_meshSamplesBuffer?.Dispose();
                m_meshSamplesBuffer = new ComputeBuffer(Mathf.Max(1, m_meshSamples.Count), sizeof(float), ComputeBufferType.Structured);
                newBuffers          = true;
            }

            if (m_meshSamples.Count > 0)
            {
                m_meshSamplesBuffer.SetData(m_meshSamples);
            }

            if (m_meshPackedUVsBuffer == null || !m_meshPackedUVsBuffer.IsValid() || previousMeshUVsCount != m_meshPackedUVs.Count)
            {
                m_meshPackedUVsBuffer?.Dispose();
                m_meshPackedUVsBuffer = new ComputeBuffer(Mathf.Max(1, m_meshPackedUVs.Count), sizeof(float), ComputeBufferType.Structured);
                newBuffers            = true;
            }

            if (m_meshPackedUVs.Count > 0)
            {
                m_meshPackedUVsBuffer.SetData(m_meshPackedUVs);
            }

            m_isGlobalMeshDataDirty = false;

            return(newBuffers);
        }