public void ComputeCavityFlowMap(MicroSplatTerrain bt) { Terrain t = bt.terrain; Texture2D data = new Texture2D(t.terrainData.heightmapResolution, t.terrainData.heightmapResolution, TextureFormat.RGBA32, true, true); CurvatureMapGenerator.CreateMap(t.terrainData.GetHeights(0, 0, data.width, data.height), data); var path = MicroSplatUtilities.RelativePathFromAsset(t.terrainData); path += "/" + t.name + "_cavity.png"; var bytes = data.EncodeToPNG(); System.IO.File.WriteAllBytes(path, bytes); GameObject.DestroyImmediate(data); AssetDatabase.Refresh(); bt.cavityMap = AssetDatabase.LoadAssetAtPath <Texture2D>(path); var ai = AssetImporter.GetAtPath(path); var ti = ai as TextureImporter; if (ti.sRGBTexture != false) { ti.sRGBTexture = false; ti.SaveAndReimport(); } bt.sTerrainDirty = false; EditorUtility.SetDirty(bt); EditorUtility.SetDirty(t); MicroSplatTerrain.SyncAll(); AssetDatabase.SaveAssets(); }
public static void GenerateTerrainBlendData(MicroSplatTerrain bt) { Terrain t = bt.GetComponent <Terrain>(); int w = t.terrainData.heightmapResolution; int h = t.terrainData.heightmapResolution; Texture2D data = new Texture2D(w, h, TextureFormat.RGBAHalf, true, true); for (int x = 0; x < w; ++x) { for (int y = 0; y < h; ++y) { float height = t.terrainData.GetHeight(x, y); Vector3 normal = t.terrainData.GetInterpolatedNormal((float)x / w, (float)y / h); // When you save a texture to EXR format, either in the saving or import stage, // some type of gamma curve is applied regardless of the fact that the textures is // set to linear. So we pow it here to counteract it, whis is total BS, but works.. normal.x = (normal.x >= 0) ? Mathf.Pow(normal.x, 2.0f) : Mathf.Pow(normal.x, 2) * -1; normal.z = (normal.z >= 0) ? Mathf.Pow(normal.z, 2.0f) : Mathf.Pow(normal.z, 2) * -1; data.SetPixel(x, y, new Color(normal.x, normal.y, normal.z, height)); } } data.Apply(); var path = MicroSplatUtilities.RelativePathFromAsset(t.terrainData); path += "/" + t.name + ".exr"; var bytes = data.EncodeToEXR(Texture2D.EXRFlags.OutputAsFloat); System.IO.File.WriteAllBytes(path, bytes); GameObject.DestroyImmediate(data); AssetDatabase.Refresh(); bt.terrainDesc = AssetDatabase.LoadAssetAtPath <Texture2D>(path); var ai = AssetImporter.GetAtPath(path); var ti = ai as TextureImporter; var ps = ti.GetDefaultPlatformTextureSettings(); if (ti.isReadable == true || ti.wrapMode != TextureWrapMode.Clamp || ps.format != TextureImporterFormat.RGBAHalf || ps.textureCompression != TextureImporterCompression.Uncompressed || ps.overridden != true || ti.filterMode != FilterMode.Bilinear || ti.sRGBTexture != false) { ti.sRGBTexture = false; ti.filterMode = FilterMode.Bilinear; ti.mipmapEnabled = true; ti.wrapMode = TextureWrapMode.Clamp; ti.isReadable = false; ps.format = TextureImporterFormat.RGBAHalf; ps.textureCompression = TextureImporterCompression.Uncompressed; ps.overridden = true; ti.SetPlatformTextureSettings(ps); ti.SaveAndReimport(); } bt.sTerrainDirty = false; EditorUtility.SetDirty(bt); MicroSplatTerrain.SyncAll(); }
void Bake(MicroSplatTerrain mst) { // for each pass int pass = 1; while (pass <= (int)(BakingPasses.Emissive)) { BakingPasses p = (BakingPasses)pass; pass *= 2; if (!IsEnabled(p)) { continue; } Texture2D worldPos, worldNormal; GenerateWorldData(mst.terrain, out worldNormal, out worldPos, (int)res); var debugOutput = OutputFromPass(p); var tex = Bake(mst, p, (int)res, worldPos, worldNormal); var bytes = tex.EncodeToPNG(); DestroyImmediate(worldPos, worldNormal); string texPath = MicroSplatUtilities.RelativePathFromAsset(mst.terrain) + "/" + mst.terrain.name + "_" + debugOutput.ToString(); System.IO.File.WriteAllBytes(texPath + ".png", bytes); } AssetDatabase.Refresh(); }
public static void GenerateTerrainNormalMap(MicroSplatTerrain bt) { Terrain t = bt.GetComponent <Terrain>(); int w = t.terrainData.heightmapResolution; int h = t.terrainData.heightmapResolution; Texture2D data = new Texture2D(w, h, TextureFormat.RGBA32, true, true); for (int x = 0; x < w; ++x) { for (int y = 0; y < h; ++y) { Vector3 normal = t.terrainData.GetInterpolatedNormal((float)x / w, (float)y / h); data.SetPixel(x, y, new Color(normal.x * 0.5f + 0.5f, normal.z * 0.5f + 0.5f, normal.y)); } } data.Apply(); var path = MicroSplatUtilities.RelativePathFromAsset(t.terrainData); path += "/" + t.name + "_normal.png"; var bytes = data.EncodeToPNG(); System.IO.File.WriteAllBytes(path, bytes); GameObject.DestroyImmediate(data); AssetDatabase.Refresh(); bt.perPixelNormal = AssetDatabase.LoadAssetAtPath <Texture2D>(path); var ai = AssetImporter.GetAtPath(path); var ti = ai as TextureImporter; var ps = ti.GetDefaultPlatformTextureSettings(); if (ti.isReadable == true || ti.wrapMode != TextureWrapMode.Clamp || ps.overridden != true || ti.textureType != TextureImporterType.NormalMap) { ti.textureType = TextureImporterType.NormalMap; ti.mipmapEnabled = true; ti.wrapMode = TextureWrapMode.Clamp; ti.isReadable = false; ps.overridden = true; ti.SetPlatformTextureSettings(ps); ti.SaveAndReimport(); } bt.sTerrainDirty = false; EditorUtility.SetDirty(bt); EditorUtility.SetDirty(bt.terrain); MicroSplatTerrain.SyncAll(); AssetDatabase.SaveAssets(); }
public static Material NewShaderAndMaterial(Terrain t, string [] keywords = null) { string path = MicroSplatUtilities.RelativePathFromAsset(t.terrainData); return(NewShaderAndMaterial(path, t.name, keywords)); }
public static void GenerateTerrainBlendData(MicroSplatTerrain bt) { Terrain t = bt.GetComponent <Terrain>(); int w = t.terrainData.heightmapResolution; int h = t.terrainData.heightmapResolution; Texture2D data = null; if (SystemInfo.SupportsTextureFormat(TextureFormat.RGBAHalf)) { data = new Texture2D(w, h, TextureFormat.RGBAHalf, true, true); } #if UNITY_2018_3_OR_NEWER else if (SystemInfo.SupportsTextureFormat(TextureFormat.RGBAFloat)) { data = new Texture2D(w, h, TextureFormat.RGBAFloat, true, true); } #endif else { Debug.LogError("System does not support RGBAHalf or RGBAFloat texture formats, Terrain Descriptor cannot be generated."); return; } for (int x = 0; x < w; ++x) { for (int y = 0; y < h; ++y) { float height = t.terrainData.GetHeight(x, y); Vector3 normal = t.terrainData.GetInterpolatedNormal((float)x / w, (float)y / h); // When you save a texture to EXR format, either in the saving or import stage, // some type of gamma curve is applied regardless of the fact that the textures is // set to linear. So we pow it here to counteract it, whis is total BS, but works.. normal.x = (normal.x >= 0) ? Mathf.Pow(normal.x, 2.0f) : Mathf.Pow(normal.x, 2) * -1; normal.z = (normal.z >= 0) ? Mathf.Pow(normal.z, 2.0f) : Mathf.Pow(normal.z, 2) * -1; data.SetPixel(x, y, new Color(normal.x, normal.y, normal.z, height)); } } data.Apply(); var path = MicroSplatUtilities.RelativePathFromAsset(t.terrainData); path += "/" + t.name + ".exr"; var bytes = data.EncodeToEXR(Texture2D.EXRFlags.OutputAsFloat); System.IO.File.WriteAllBytes(path, bytes); GameObject.DestroyImmediate(data); AssetDatabase.Refresh(); bt.terrainDesc = AssetDatabase.LoadAssetAtPath <Texture2D>(path); var ai = AssetImporter.GetAtPath(path); var ti = ai as TextureImporter; // default platform no longer supports RGBA half/float in newer unity, so we override all platforms bool changed = false; var platforms = System.Enum.GetNames(typeof(BuildTarget)); for (int i = 0; i < platforms.Length; ++i) { string platform = platforms [i]; var ps = ti.GetPlatformTextureSettings(platform); if (ti.isReadable == true || ti.wrapMode != TextureWrapMode.Clamp || #if UNITY_2018_3_OR_NEWER (ps.format != TextureImporterFormat.RGBAHalf && ps.format != TextureImporterFormat.RGBAFloat) || #else ps.format != TextureImporterFormat.RGBAHalf || #endif ps.textureCompression != TextureImporterCompression.Uncompressed || ps.overridden != true || ti.filterMode != FilterMode.Bilinear || ti.sRGBTexture != false) { ti.sRGBTexture = false; ti.filterMode = FilterMode.Bilinear; ti.mipmapEnabled = true; ti.wrapMode = TextureWrapMode.Clamp; ti.isReadable = false; #if UNITY_2018_3_OR_NEWER if (SystemInfo.SupportsTextureFormat(TextureFormat.RGBAHalf)) { ps.format = TextureImporterFormat.RGBAHalf; } else { ps.format = TextureImporterFormat.RGBAFloat; } #else ps.format = TextureImporterFormat.RGBAHalf; #endif ps.textureCompression = TextureImporterCompression.Uncompressed; ps.overridden = true; try { ti.SetPlatformTextureSettings(ps); changed = true; } catch { } } } if (changed) { ti.SaveAndReimport(); } bt.sTerrainDirty = false; EditorUtility.SetDirty(bt); EditorUtility.SetDirty(bt.terrain); MicroSplatTerrain.SyncAll(); AssetDatabase.SaveAssets(); }