protected virtual void OnDestroy() { if (meshes != null) { for (var i = meshes.Count - 1; i >= 0; i--) { var mesh = meshes[i]; if (mesh != null) { mesh.Clear(false); SgtObjectPool <Mesh> .Add(meshes[i]); } } } if (models != null) { for (var i = models.Count - 1; i >= 0; i--) { SgtAuroraModel.MarkForDestruction(models[i]); } } SgtHelper.Destroy(material); }
protected virtual void OnDestroy() { if (mesh != null) { mesh.Clear(false); SgtObjectPool <Mesh> .Add(mesh); } }
public static Mesh CreateTempMesh(string meshName) { var mesh = SgtObjectPool <Mesh> .Pop() ?? new Mesh(); mesh.name = meshName; #if UNITY_EDITOR mesh.hideFlags = HideFlags.DontSave; #endif return(mesh); }
protected virtual void OnDestroy() { if (mesh != null) { mesh.Clear(false); SgtObjectPool <Mesh> .Add(mesh); } SgtHelper.Destroy(material); }
protected virtual void OnDestroy() { if (quitting == false) { if (generatedMesh != null) { generatedMesh.Clear(false); SgtObjectPool <Mesh> .Add(generatedMesh); } } }
public void PoolMeshNow() { if (mesh != null) { if (meshFilter == null) { meshFilter = gameObject.GetComponent <MeshFilter>(); } mesh.Clear(false); mesh = meshFilter.sharedMesh = SgtObjectPool <Mesh> .Add(mesh); } }
private void Pool() { if (meshCollider != null) { SgtHelper.Destroy(meshCollider.gameObject); meshCollider = null; } if (mesh != null) { mesh.Clear(false); mesh = SgtObjectPool <Mesh> .Add(mesh); } }
private Mesh GetMesh() { if (mesh == null) { mesh = SgtObjectPool <Mesh> .Pop() ?? new Mesh(); #if UNITY_EDITOR mesh.hideFlags = HideFlags.DontSave; #endif mesh.name = "Terrain"; } if (cachedMeshFilter == null) { cachedMeshFilter = GetComponent <MeshFilter>(); } cachedMeshFilter.sharedMesh = mesh; return(mesh); }
public void UpdateMeshesAndModels() { if (meshes == null) { meshes = new List <Mesh>(); } if (PathDetail > 0 && PathLengthMin > 0.0f && PathLengthMax > 0.0f) { var meshCount = 1; var mesh = GetMesh(0); var vertexCount = 0; SgtHelper.BeginRandomSeed(Seed); { for (var i = 0; i < PathCount; i++) { AddPath(ref mesh, ref meshCount, ref vertexCount); } } SgtHelper.EndRandomSeed(); BakeMesh(mesh); for (var i = meshes.Count - 1; i >= meshCount; i--) { var extraMesh = meshes[i]; if (extraMesh != null) { extraMesh.Clear(false); SgtObjectPool <Mesh> .Add(extraMesh); } meshes.RemoveAt(i); } } for (var i = 0; i < meshes.Count; i++) { var model = SgtHelper.GetIndex(ref models, i); if (model == null) { model = models[i] = SgtAuroraModel.Create(this); } model.SetMesh(meshes[i]); model.SetMaterial(material); } // Remove any excess if (models != null) { var min = Mathf.Max(0, meshes.Count); for (var i = models.Count - 1; i >= min; i--) { SgtAuroraModel.Pool(models[i]); models.RemoveAt(i); } } }
private void Build(SgtTerrain terrain, Vector3 bestPoint) { if (meshCollider == null) { var gameObject = new GameObject("Plane"); #if UNITY_EDITOR gameObject.hideFlags = HideFlags.DontSave; #endif meshCollider = gameObject.AddComponent <MeshCollider>(); } if (mesh == null) { mesh = SgtObjectPool <Mesh> .Pop() ?? new Mesh(); #if UNITY_EDITOR mesh.hideFlags = HideFlags.DontSave; #endif mesh.name = "Plane"; } var sideE = Detail; var sideP = Detail + 1; var vertexCount = sideP * sideP; var indexCount = sideE * sideE * 6; var rotation = Quaternion.Inverse(terrain.transform.rotation) * Quaternion.LookRotation(bestPoint); var distance = bestPoint.magnitude; var uniformScale = SgtHelper.UniformScale(terrain.transform.lossyScale); var size = Size * SgtHelper.Reciprocal(uniformScale); var step = (size * 2.0f) / Detail; if (positions == null || positions.Length != vertexCount) { positions = new Vector3[vertexCount]; } for (var y = 0; y <= Detail; y++) { for (var x = 0; x <= Detail; x++) { var index = x + y * sideP; var point = rotation * new Vector3(x * step - size, y * step - size, distance); positions[index] = (Vector3)terrain.GetLocalPoint(new SgtVector3D(point)); } } // Regen indices? if (indices == null || indices.Length != indexCount) { indices = new int[indexCount]; for (var y = 0; y < sideE; y++) { for (var x = 0; x < sideE; x++) { var index = (x + y * sideE) * 6; var vertex = x + y * sideP; indices[index + 0] = vertex; indices[index + 1] = vertex + 1; indices[index + 2] = vertex + sideP; indices[index + 3] = vertex + sideP + 1; indices[index + 4] = vertex + sideP; indices[index + 5] = vertex + 1; } } mesh.Clear(); } mesh.vertices = positions; mesh.triangles = indices; meshCollider.sharedMesh = mesh; meshCollider.transform.SetParent(terrain.transform, false); }