Beispiel #1
0
        // does this actually work? I feel like it doesn't
        public void OnDestroy()
        {
            if (Application.isEditor)
            {
                if ((IvyBehavior)target == null)
                {
                    // clean up meshes as well
                    if (wasPartOfPrefab && !IvyCore.ConfirmDestroyMeshes())
                    {
                        return;
                    }

                    if (lastDataAsset != null)
                    {
                        foreach (var meshID in lastMeshIDs)
                        {
                            IvyCore.TryDestroyMesh(meshID, lastDataAsset, false);
                        }
                        IvyCore.needToSaveAssets = true;
                    }
                }
            }
        }
Beispiel #2
0
        public static void GenerateMesh(IvyGraph ivyGraph, IvyProfile ivyProfile, bool doUV2s = false, bool forceGeneration = false)
        {
            // avoid GC allocations by reusing static lists
            verticesAll.Clear();
            texCoordsAll.Clear();
            trianglesAll.Clear();
            leafVerticesAll.Clear();
            leafUVsAll.Clear();
            leafTrianglesAll.Clear();
            leafColorsAll.Clear();

            // the main mesh generation actually happens here; if it can't generate a mesh, then stop
            if (!GenerateMeshData(ivyGraph, ivyProfile, forceGeneration))
            {
                return;
            }
            ivyGraph.dirtyUV2s = !doUV2s;

            InitOrRefreshRoot(ivyGraph, ivyProfile);
            var myAsset = IvyCore.GetDataAsset(ivyGraph.rootGO);

            // Branch mesh debug
            // Debug.Log( "branchVertices: " + verticesAll.Count );
            // Debug.Log( "branchTris: " + string.Join(", ", trianglesAll.Select( tri => tri.ToString() ).ToArray()) );
            // foreach ( var vert in verticesAll ) {
            //     Debug.DrawRay( vert + ivyGraph.seedPos, Vector3.up, Color.cyan, 1f, false );
            // }

            if (ivyProfile.ivyBranchSize < 0.0001f)
            {
                if (ivyGraph.branchMF != null)
                {
                    IvyCore.DestroyObject(ivyGraph.branchMF.gameObject);
                }
                IvyCore.TryDestroyMesh(ivyGraph.branchMeshID, myAsset, true);
            }
            else
            {
                CheckMeshDataAsset(ref ivyGraph.branchMeshID, myAsset, ivyProfile.meshCompress);
                branchMesh = myAsset.meshList[ivyGraph.branchMeshID];

                if (ivyGraph.branchMF == null || ivyGraph.branchR == null)
                {
                    CreateIvyMeshObject(ivyGraph, ivyProfile, branchMesh, false);
                }
                RefreshMeshObject(ivyGraph.branchMF, ivyProfile);

                branchMesh.Clear();
                ivyGraph.branchMF.name             = ivyGraph.rootGO.name + "_Branches";
                ivyGraph.branchR.shadowCastingMode = ivyProfile.castShadows;
                ivyGraph.branchR.receiveShadows    = ivyProfile.receiveShadows;
                branchMesh.name = ivyGraph.branchMF.name;
                branchMesh.SetVertices(verticesAll);
                branchMesh.SetUVs(0, texCoordsAll);
                if (ivyProfile.useLightmapping && doUV2s)
                {
                    PackBranchUV2s(ivyGraph);
                }
                branchMesh.SetTriangles(trianglesAll, 0);
                branchMesh.RecalculateBounds();
                branchMesh.RecalculateNormals();
                branchMesh.RecalculateTangents();
                #if UNITY_2017_1_OR_NEWER
                MeshUtility.Optimize(branchMesh);
                #endif
                ivyGraph.branchMF.sharedMesh    = branchMesh;
                ivyGraph.branchR.sharedMaterial = ivyProfile.branchMaterial != null ? ivyProfile.branchMaterial : AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            }

            // Leaves mesh debug
            // Debug.Log( "leafVertices: " + ivyGraph.leafVertices.Count );
            // Debug.Log( "leafTris: " + string.Join(", ", ivyGraph.leafTriangles.Select( tri => tri.ToString() ).ToArray()) );

            // don't do leaf mesh if it's unnecessary
            if (ivyProfile.leafProbability < 0.001f)
            {
                if (ivyGraph.leafMF != null)
                {
                    IvyCore.DestroyObject(ivyGraph.leafMF.gameObject);
                }
                IvyCore.TryDestroyMesh(ivyGraph.leafMeshID, myAsset, true);
            }
            else
            {
                CheckMeshDataAsset(ref ivyGraph.leafMeshID, myAsset, ivyProfile.meshCompress);
                leafMesh = myAsset.meshList[ivyGraph.leafMeshID];

                if (ivyGraph.leafMF == null || ivyGraph.leafR == null)
                {
                    CreateIvyMeshObject(ivyGraph, ivyProfile, leafMesh, true);
                }
                RefreshMeshObject(ivyGraph.leafMF, ivyProfile);

                leafMesh.Clear();
                ivyGraph.leafMF.name             = ivyGraph.rootGO.name + "_Leaves";
                ivyGraph.leafR.shadowCastingMode = ivyProfile.castShadows;
                ivyGraph.leafR.receiveShadows    = ivyProfile.receiveShadows;
                leafMesh.name = ivyGraph.leafMF.name;
                leafMesh.SetVertices(leafVerticesAll);
                leafMesh.SetUVs(0, leafUVsAll);
                if (ivyProfile.useLightmapping && doUV2s)
                {
                    PackLeafUV2s(ivyGraph);
                }
                leafMesh.SetTriangles(leafTrianglesAll, 0);
                if (ivyProfile.useVertexColors)
                {
                    leafMesh.SetColors(leafColorsAll);
                }
                leafMesh.RecalculateBounds();
                leafMesh.RecalculateNormals();
                leafMesh.RecalculateTangents();
                #if UNITY_2017_1_OR_NEWER
                MeshUtility.Optimize(leafMesh);
                #endif
                ivyGraph.leafMF.sharedMesh    = leafMesh;
                ivyGraph.leafR.sharedMaterial = ivyProfile.leafMaterial != null ? ivyProfile.leafMaterial : AssetDatabase.GetBuiltinExtraResource <Material>("Default-Diffuse.mat");
            }
            // EditorUtility.SetDirty( myAsset );
            // AssetDatabase.SaveAssets();
            // AssetDatabase.ImportAsset( AssetDatabase.GetAssetPath(myAsset) );
        }