private void OnApplicationQuit() { ChunkGenerator.DestroyBuffers(); DensityFunction.DestroyBuffer(); }
// Update is called once per frame void Update() { bool valuesChanged = false; Ray mouseRay = cam.ScreenPointToRay(Input.mousePosition); ChangeBrushSize(); float distance; if (bounds.IntersectRay(mouseRay, out distance)) { if (!brush.activeSelf) { brush.SetActive(true); } ChangeBrushDistance(); MoveBrush(mouseRay, distance); if (Input.GetMouseButton(0)) { Vector3 center = mouseRay.GetPoint(rayDistance); //left click for (int i = 0; i < values.Length; i++) { Vector3 diff = new Vector3(values[i].x, values[i].y, values[i].z) - center; if (diff.x * diff.x + diff.y * diff.y + diff.z * diff.z < brushSize * brushSize) { values[i].w += 0.01f; valuesChanged = true; values[i].w = Utility.Constrain(0, 1, values[i].w); } } } else if (Input.GetMouseButton(1)) { Vector3 center = mouseRay.GetPoint(rayDistance); //right click for (int i = 0; i < values.Length; i++) { Vector3 diff = new Vector3(values[i].x, values[i].y, values[i].z) - center; if (diff.x * diff.x + diff.y * diff.y + diff.z * diff.z < brushSize * brushSize) { values[i].w -= 0.01f; valuesChanged = true; values[i].w = Utility.Constrain(0, 1, values[i].w); } } } } else { if (brush.activeSelf) { brush.SetActive(false); } } if (valuesChanged) { terrainChunkData = ChunkGenerator.Generate(values, terrainSize, surfaceLevel); CreateChunks(); } else if (valuesChangedLastFrame) { ChunkGenerator.DestroyBuffers(); } valuesChangedLastFrame = valuesChanged; }