public void UpdateMaterialIndex() { int count = 0; RenderTexture cacheRT = new RenderTexture(MTerrain.MASK_RESOLUTION, MTerrain.MASK_RESOLUTION, 0, GraphicsFormat.R32_SFloat, 0); cacheRT.dimension = TextureDimension.Tex2DArray; cacheRT.volumeDepth = 1; cacheRT.enableRandomWrite = true; cacheRT.Create(); int largestChunkCount = (int)(pow(2.0, targetData.GetLodOffset()) + 0.1); VirtualTextureLoader loader = new VirtualTextureLoader( targetData.maskmapPath, terrainEditShader, largestChunkCount, MTerrain.MASK_RESOLUTION, false, null); Dictionary <int2, List <Pair <SplatSettings, int> > > commandDict = new Dictionary <int2, List <Pair <SplatSettings, int> > >(256); for (int i = 0; i < allMaterials.Count; ++i) { for (int j = 0; j < allMaterials[i].blendWeights.Count; ++j) { var mat = allMaterials[i].blendWeights[j]; foreach (var a in mat.splatSettings) { Texture splat = a.splatMap; if (splat) { int2 border = largestChunkCount - a.splatStartPos; border = min(border, splat.width / MTerrain.MASK_RESOLUTION); for (int x = 0; x < border.x; ++x) { for (int y = 0; y < border.y; ++y) { if (!commandDict.ContainsKey(int2(x, y) + a.splatStartPos)) { commandDict.Add(int2(x, y) + a.splatStartPos, new List <Pair <SplatSettings, int> >()); } commandDict[int2(x, y) + a.splatStartPos].Add(new Pair <SplatSettings, int>(a, count)); } } } } count++; } } foreach (var i in commandDict) { foreach (var j in i.Value) { int2 offsetValue = i.Key - j.key.splatStartPos; SaveToMask(cacheRT, j.key.splatMap, j.key.chan, offsetValue, j.value / (count - 1f)); } loader.WriteToDisk(cacheRT, 0, i.Key); } loader.Dispose(); DestroyImmediate(cacheRT); }
private void OnGUI() { if (!terrainEdit) { terrainEdit = Resources.Load <ComputeShader>("TerrainEdit"); } terrainData = (MTerrainData)EditorGUILayout.ObjectField("Terrain Data", terrainData, typeof(MTerrainData), false); if (!terrainData) { return; } chunkPosition = EditorGUILayout.Vector2IntField("Chunk Position", new Vector2Int(chunkPosition.x, chunkPosition.y)); heightTexture = EditorGUILayout.ObjectField("Height Texture", heightTexture, typeof(Texture), false) as Texture; targetChunkCount = EditorGUILayout.IntField("Chunk Count", targetChunkCount); targetChunkCount = max(0, targetChunkCount); int largestChunkCount = (int)(pow(2.0, terrainData.GetLodOffset()) + 0.1); if (GUILayout.Button("Update Height Texture")) { VirtualTextureLoader loader = new VirtualTextureLoader( terrainData.heightmapPath, terrainEdit, largestChunkCount, MTerrain.MASK_RESOLUTION, true, null); RenderTexture cacheRt = new RenderTexture(new RenderTextureDescriptor { width = MTerrain.MASK_RESOLUTION, height = MTerrain.MASK_RESOLUTION, volumeDepth = 1, dimension = UnityEngine.Rendering.TextureDimension.Tex2DArray, msaaSamples = 1, graphicsFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.R32_SFloat, enableRandomWrite = true }); cacheRt.Create(); int mipLevel = 0; int meshResolution = terrainData.GetMeshResolution(); int saveMipLevel = 0; ComputeBuffer cb = new ComputeBuffer(meshResolution * meshResolution, sizeof(float2)); float2[] resultArr = new float2[meshResolution * meshResolution]; RenderTexture mipRT = new RenderTexture(MTerrain.MASK_RESOLUTION, MTerrain.MASK_RESOLUTION, 0, UnityEngine.Experimental.Rendering.GraphicsFormat.R32G32_SFloat, mipLevel); mipRT.enableRandomWrite = true; mipRT.useMipMap = true; mipRT.autoGenerateMips = false; mipRT.Create(); System.IO.FileStream fsm = new System.IO.FileStream(terrainData.boundPath, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite); for (int i = MTerrain.MASK_RESOLUTION; i > 0; i /= 2) { mipLevel++; if (i <= meshResolution) { saveMipLevel++; } } MTerrainBoundingTree btree = new MTerrainBoundingTree(saveMipLevel); for (int x = 0; x < targetChunkCount; ++x) { for (int y = 0; y < targetChunkCount; ++y) { int2 pos = int2(x, y) + int2(chunkPosition.x, chunkPosition.y); if (pos.x >= largestChunkCount || pos.y >= largestChunkCount) { continue; } terrainEdit.SetTexture(6, ShaderIDs._SourceTex, heightTexture); terrainEdit.SetTexture(6, ShaderIDs._DestTex, cacheRt); terrainEdit.SetInt(ShaderIDs._Count, MTerrain.MASK_RESOLUTION); terrainEdit.SetInt(ShaderIDs._OffsetIndex, 0); terrainEdit.SetVector("_ScaleOffset", float4(float2(1.0 / targetChunkCount), float2(x, y) / targetChunkCount)); const int disp = MTerrain.MASK_RESOLUTION / 16; terrainEdit.Dispatch(6, disp, disp, 1); loader.WriteToDisk(cacheRt, 0, pos); for (int i = 0, res = MTerrain.MASK_RESOLUTION; i < mipLevel; ++i, res /= 2) { int pass; if (i == 0) { pass = 7; terrainEdit.SetTexture(pass, "_SourceArray", cacheRt); terrainEdit.SetTexture(pass, "_Mip1", mipRT); } else { if (res <= meshResolution) { pass = 9; terrainEdit.SetBuffer(pass, "_DataBuffer", cb); } else { pass = 8; } terrainEdit.SetTexture(pass, "_Mip0", mipRT, i - 1); terrainEdit.SetTexture(pass, "_Mip1", mipRT, i); } terrainEdit.SetInt(ShaderIDs._Count, res); int mipdisp = Mathf.CeilToInt(res / 16f); terrainEdit.Dispatch(pass, mipdisp, mipdisp, 1); if (pass == 9) { cb.GetData(resultArr, 0, 0, res * res); int targetMipLevel = mipLevel - 1 - i; for (int xx = 0; xx < res * res; ++xx) { btree[xx, targetMipLevel] = resultArr[xx]; } } } btree.WriteToDisk(fsm, x + y * largestChunkCount); } } btree.Dispose(); cacheRt.Release(); cb.Dispose(); mipRT.Release(); loader.Dispose(); fsm.Dispose(); Debug.Log("Finish!"); } maskTexture = EditorGUILayout.ObjectField("Mask Texture", maskTexture, typeof(Texture), false) as Texture; void SaveToMask(RenderTexture cacheRt) { VirtualTextureLoader loader = new VirtualTextureLoader( terrainData.maskmapPath, terrainEdit, largestChunkCount, MTerrain.MASK_RESOLUTION, false, null); for (int x = 0; x < targetChunkCount; ++x) { for (int y = 0; y < targetChunkCount; ++y) { int2 pos = int2(x, y) + int2(chunkPosition.x, chunkPosition.y); if (pos.x >= largestChunkCount || pos.y >= largestChunkCount) { continue; } terrainEdit.SetTexture(6, ShaderIDs._SourceTex, maskTexture); terrainEdit.SetTexture(6, ShaderIDs._DestTex, cacheRt); terrainEdit.SetInt(ShaderIDs._Count, MTerrain.MASK_RESOLUTION); terrainEdit.SetInt(ShaderIDs._OffsetIndex, 0); terrainEdit.SetVector("_ScaleOffset", float4(float2(1.0 / targetChunkCount), float2(x, y) / targetChunkCount)); const int disp = MTerrain.MASK_RESOLUTION / 16; terrainEdit.Dispatch(6, disp, disp, 1); loader.WriteToDisk(cacheRt, 0, pos); } } loader.Dispose(); } if (GUILayout.Button("Update Mask Texture")) { RenderTexture cacheRt = new RenderTexture(new RenderTextureDescriptor { width = MTerrain.MASK_RESOLUTION, height = MTerrain.MASK_RESOLUTION, volumeDepth = 1, dimension = UnityEngine.Rendering.TextureDimension.Tex2DArray, msaaSamples = 1, graphicsFormat = UnityEngine.Experimental.Rendering.GraphicsFormat.R32_SFloat, enableRandomWrite = true }); cacheRt.Create(); SaveToMask(cacheRt); cacheRt.Release(); } }