예제 #1
0
        public bool Initialize(GameObject targetObject)
        {
            if (Target == null)
            {
                Target = new MeshTarget();
            }
            if (!Target.Initialize(targetObject))
            {
                return(false);
            }

            // Store the original mesh and make a copy (stored in dynamicMesh).
            // Assign the copy back to the filter so that this object has a unique mesh.
            if (!initialized)
            {
                OriginalMesh = Target.GetMesh();
                if (OriginalMesh == null)
                {
                    return(false);
                }
                DynamicMesh = GameObject.Instantiate(Target.GetMesh());
            }
            // Since this has already been initialized, make a new mesh for the dynamic mesh to reference
            // so that two Deformables aren't displaying and modifying the same mesh.
            else if (OriginalMesh != null)
            {
                DynamicMesh = GameObject.Instantiate(OriginalMesh);
            }
            else
            {
                return(false);
            }

            // Tell the mesh filter to display the dynamic mesh.
            Target.SetMesh(DynamicMesh);
            // Mark the dynamic mesh as dynamic for a hypothetical performance boost.
            // (I've heard this method doesn't do anything)
            DynamicMesh.MarkDynamic();

            Length = DynamicMesh.vertexCount;

            // Store mesh information in managed data.
            originalManaged = new ManagedMeshData(DynamicMesh);
            dynamicManaged  = new ManagedMeshData(DynamicMesh);
            // Copy the managed data into native data.
            OriginalNative = new NativeMeshData(originalManaged);
            DynamicNative  = new NativeMeshData(dynamicManaged);

            initialized = true;

            return(true);
        }
예제 #2
0
파일: MeshData.cs 프로젝트: AStox/Plantasia
        public bool Initialize(GameObject targetObject)
        {
            if (Target == null)
            {
                Target = new MeshTarget();
            }
            if (!Target.Initialize(targetObject))
            {
                return(false);
            }

            // Store the original mesh and make a copy (stored in DynamicMesh).
            // Assign the copy back to the filter so that this object has a unique mesh.
            if (!initialized)
            {
                OriginalMesh = Target.GetMesh();

                if (OriginalMesh == null)
                {
                    return(false);
                }

                if (!OriginalMesh.isReadable)
                {
                    Debug.LogError($"The mesh '{OriginalMesh.name}' must have read/write permissions enabled.", OriginalMesh);
                    return(false);
                }

                DynamicMesh = GameObject.Instantiate(Target.GetMesh());
            }
            // Since this has already been initialized, make a new mesh for the dynamic mesh to reference
            // so that two Deformables aren't displaying and modifying the same mesh.
            else if (OriginalMesh != null)
            {
                DynamicMesh = GameObject.Instantiate(OriginalMesh);
            }
            else if (DynamicMesh != null)
            {
                Debug.Log($"Original mesh is missing. Attempting to create one from dynamic mesh ({DynamicMesh.name}) and original managed mesh data.", targetObject);
                OriginalMesh = GameObject.Instantiate(DynamicMesh);
                try
                {
                    OriginalMesh.vertices  = originalManaged.Vertices;
                    OriginalMesh.normals   = originalManaged.Normals;
                    OriginalMesh.tangents  = originalManaged.Tangents;
                    OriginalMesh.uv        = originalManaged.UVs;
                    OriginalMesh.colors    = originalManaged.Colors;
                    OriginalMesh.triangles = originalManaged.Triangles;
                    OriginalMesh.bounds    = originalManaged.Bounds;
                }
                catch (NullReferenceException)
                {
                    Debug.LogError($"Attempted to recreate original mesh (from {DynamicMesh.name}), but the data was not valid. Please assign a new mesh.", targetObject);
                    return(false);
                }

                Debug.Log($"Original mesh was recreated from {DynamicMesh.name}. This is not ideal, but prevents stuff from breaking when an original mesh is deleted. The best solution is to find and reassign the original mesh.", targetObject);
            }
            else
            {
                return(false);
            }

            // Tell the mesh filter to display the dynamic mesh.
            Target.SetMesh(DynamicMesh);
            // Mark the dynamic mesh as dynamic for a hypothetical performance boost.
            // (I've heard this method doesn't do anything)
            DynamicMesh.MarkDynamic();

            Length = DynamicMesh.vertexCount;

            // Store mesh information in managed data.
            originalManaged = new ManagedMeshData(DynamicMesh);
            dynamicManaged  = new ManagedMeshData(DynamicMesh);
            // Copy the managed data into native data.
            OriginalNative = new NativeMeshData(originalManaged);
            DynamicNative  = new NativeMeshData(dynamicManaged);

            initialized = true;

            return(true);
        }
예제 #3
0
        public bool Initialize(GameObject targetObject)
        {
            if (Target == null)
            {
                Target = new MeshTarget();
            }
            if (!Target.Initialize(targetObject))
            {
                return(false);
            }

            // Store the original mesh and make a copy (stored in DynamicMesh).
            // Assign the copy back to the filter so that this object has a unique mesh.
            if (!initialized)
            {
                OriginalMesh = Target.GetMesh();

                if (OriginalMesh == null)
                {
                    return(false);
                }

                if (!OriginalMesh.isReadable)
                {
                    Debug.LogError($"The mesh '{OriginalMesh.name}' must have read/write permissions enabled.", OriginalMesh);
                    return(false);
                }

                DynamicMesh = GameObject.Instantiate(Target.GetMesh());
            }
            // Since this has already been initialized, make a new mesh for the dynamic mesh to reference
            // so that two Deformables aren't displaying and modifying the same mesh.
            else if (OriginalMesh != null)
            {
                DynamicMesh = GameObject.Instantiate(OriginalMesh);
            }
            else if (DynamicMesh != null)
            {
                Debug.Log($"Original mesh is missing. Recreating one from dynamic mesh (\"{DynamicMesh.name}\"). This is not ideal, but prevents stuff from breaking when an original mesh is deleted. The best solution is to find and reassign the original mesh.", targetObject);
                OriginalMesh = GameObject.Instantiate(DynamicMesh);
                return(false);
            }
            else
            {
                return(false);
            }

            // Tell the mesh filter to display the dynamic mesh.
            Target.SetMesh(DynamicMesh);
            // Mark the dynamic mesh as dynamic for a hypothetical performance boost.
            // (I've heard this method doesn't do anything)
            DynamicMesh.MarkDynamic();

            Length = DynamicMesh.vertexCount;

            // Store the native data.
            OriginalNative = new NativeMeshData(DynamicMesh);
            DynamicNative  = new NativeMeshData(DynamicMesh);

#if !UNITY_2019_3_OR_NEWER
            OriginalManaged = new ManagedMeshData(DynamicMesh);
            DynamicManaged  = new ManagedMeshData(DynamicMesh);
#endif

            initialized = true;

            return(true);
        }