private void UpdateTextureCamera(Terrain t) { int mLayer = 1 << prefs.meshLayer; float raycastDistance = maxBounds.y - minBounds.y + 10; Vector3 vScale = t.terrainData.size; float tsx = prefs.textureResolution + 1; float tsy = prefs.textureResolution + 1; vScale.x = vScale.x / tsx; vScale.z = vScale.z / tsy; Vector3 beginPoint = t.transform.position; if (prefs.direction == MeshToTerrainDirection.normal) { beginPoint.y += raycastDistance; } else { beginPoint.y = maxBounds.y - raycastDistance; } Vector3 curPoint = beginPoint + new Vector3(prefs.textureResolution / 2 * vScale.x, 0, prefs.textureResolution / 2 * vScale.z); GameObject cameraGO = new GameObject("__Mesh to Terrain Camera__"); Camera camera = cameraGO.AddComponent <Camera>(); cameraGO.transform.position = curPoint; cameraGO.transform.rotation = Quaternion.Euler(prefs.direction == MeshToTerrainDirection.normal? 90: -90, 0, 0); camera.orthographic = true; camera.orthographicSize = boundsRange.x / 2 / prefs.newTerrainCountX; camera.clearFlags = CameraClearFlags.Color; camera.backgroundColor = prefs.textureEmptyColor; camera.cullingMask = mLayer; camera.targetTexture = new RenderTexture(prefs.textureResolution, prefs.textureResolution, 16); RenderTexture currentRT = RenderTexture.active; RenderTexture.active = camera.targetTexture; camera.Render(); Texture2D texture = new Texture2D(prefs.textureResolution, prefs.textureResolution); texture.ReadPixels(new Rect(0, 0, prefs.textureResolution, prefs.textureResolution), 0, 0); texture.Apply(); RenderTexture.active = currentRT; string textureFilename = Path.Combine(resultFolder, t.name + ".png"); File.WriteAllBytes(textureFilename, texture.EncodeToPNG()); AssetDatabase.Refresh(); TextureImporter importer = AssetImporter.GetAtPath(textureFilename) as TextureImporter; if (importer != null) { importer.maxTextureSize = Mathf.Max(prefs.textureWidth, prefs.textureHeight); importer.wrapMode = TextureWrapMode.Clamp; AssetDatabase.ImportAsset(textureFilename, ImportAssetOptions.ForceUpdate); } texture = (Texture2D)AssetDatabase.LoadAssetAtPath(textureFilename, typeof(Texture2D)); DestroyImmediate(cameraGO); #if !RTP Vector2 tileSize = new Vector2( t.terrainData.size.x / prefs.textureResolution * (prefs.textureResolution + 2), t.terrainData.size.z / prefs.textureResolution * (prefs.textureResolution + 2)); Vector2 tileOffset = new Vector2( t.terrainData.size.x / prefs.textureResolution / 2, t.terrainData.size.z / prefs.textureResolution / 2); #if UNITY_2018_3_OR_NEWER TerrainLayer l = new TerrainLayer { tileSize = tileSize, tileOffset = tileOffset, diffuseTexture = texture }; string layerPath = Path.Combine(resultFolder, t.name + ".terrainlayer"); AssetDatabase.CreateAsset(l, layerPath); AssetDatabase.Refresh(); t.terrainData.terrainLayers = new[] { AssetDatabase.LoadAssetAtPath <TerrainLayer>(layerPath) }; #else SplatPrototype sp = new SplatPrototype { tileSize = tileSize, tileOffset = tileOffset, texture = texture }; t.terrainData.splatPrototypes = new[] { sp }; #endif #else LoadRTPTextures(); #if UNITY_2018_3_OR_NEWER TerrainLayer[] ls = new TerrainLayer[4]; for (int i = 0; i < 4; i++) { TerrainLayer l = ls[i] = new TerrainLayer { diffuseTexture = rtpTextures[i * 3] }; string layerPath = Path.Combine(resultFolder, t.name + " " + i + ".terrainlayer"); AssetDatabase.CreateAsset(l, layerPath); AssetDatabase.Refresh(); ls[i] = AssetDatabase.LoadAssetAtPath <TerrainLayer>(layerPath); } t.terrainData.terrainLayers = ls; #else SplatPrototype[] sps = new SplatPrototype[4]; for (int i = 0; i < 4; i++) { sps[i] = new SplatPrototype { texture = rtpTextures[i * 3] }; } t.terrainData.splatPrototypes = sps; #endif ReliefTerrain reliefTerrain = t.gameObject.GetComponent <ReliefTerrain>() ?? t.gameObject.AddComponent <ReliefTerrain>(); reliefTerrain.InitArrays(); reliefTerrain.ColorGlobal = texture; #endif activeIndex++; progress = activeIndex / (float)prefs.terrains.Count; if (activeIndex >= prefs.terrains.Count) { activeIndex = 0; phase = MeshToTerrainPhase.finish; } }
private void SetTexturesToTerrain(Terrain t) { #if UNITY_2018_3_OR_NEWER #if !RTP Texture2D texture = GetTexture(t); float tsx = prefs.textureWidth - 4; float tsy = prefs.textureHeight - 4; Vector2 tileSize = new Vector2(t.terrainData.size.x + t.terrainData.size.x / tsx * 4, t.terrainData.size.z + t.terrainData.size.z / tsy * 4); Vector2 tileOffset = new Vector2(t.terrainData.size.x / prefs.textureWidth / 2, t.terrainData.size.z / prefs.textureHeight / 2); TerrainLayer tl = new TerrainLayer { tileSize = tileSize, tileOffset = tileOffset, diffuseTexture = texture }; t.terrainData.terrainLayers = new[] { tl }; #else LoadRTPTextures(); TerrainLayer[] tls = new TerrainLayer[4]; for (int i = 0; i < 4; i++) { tls[i] = new TerrainLayer { diffuseTexture = rtpTextures[i * 3] }; } t.terrainData.terrainLayers = tls; ReliefTerrain reliefTerrain = t.gameObject.GetComponent <ReliefTerrain>() ?? t.gameObject.AddComponent <ReliefTerrain>(); reliefTerrain.InitArrays(); reliefTerrain.ColorGlobal = GetTexture(t); #endif #else #if !RTP Texture2D texture = GetTexture(t); float tsx = prefs.textureWidth - 4; float tsy = prefs.textureHeight - 4; Vector2 tileSize = new Vector2(t.terrainData.size.x + t.terrainData.size.x / tsx * 4, t.terrainData.size.z + t.terrainData.size.z / tsy * 4); Vector2 tileOffset = new Vector2(t.terrainData.size.x / prefs.textureWidth / 2, t.terrainData.size.z / prefs.textureHeight / 2); SplatPrototype sp = new SplatPrototype { tileSize = tileSize, tileOffset = tileOffset, texture = texture }; t.terrainData.splatPrototypes = new [] { sp }; #else LoadRTPTextures(); SplatPrototype[] sps = new SplatPrototype[4]; for (int i = 0; i < 4; i++) { sps[i] = new SplatPrototype { texture = rtpTextures[i * 3] }; } t.terrainData.splatPrototypes = sps; ReliefTerrain reliefTerrain = t.gameObject.GetComponent <ReliefTerrain>() ?? t.gameObject.AddComponent <ReliefTerrain>(); reliefTerrain.InitArrays(); reliefTerrain.ColorGlobal = GetTexture(t); #endif #endif }
private void SetTexturesToTerrain(Terrain t, Texture2D texture) { #if UNITY_2018_3_OR_NEWER #if !RTP float tsx = prefs.textureWidth - 4; float tsy = prefs.textureHeight - 4; Vector2 tileSize = new Vector2(t.terrainData.size.x + t.terrainData.size.x / tsx * 4, t.terrainData.size.z + t.terrainData.size.z / tsy * 4); Vector2 tileOffset = new Vector2(t.terrainData.size.x / prefs.textureWidth / 2, t.terrainData.size.z / prefs.textureHeight / 2); TerrainLayer tl = new TerrainLayer { tileSize = tileSize, tileOffset = tileOffset, diffuseTexture = texture }; string filename = Path.Combine(resultFolder, t.name + ".terrainlayer"); AssetDatabase.CreateAsset(tl, filename); AssetDatabase.Refresh(); t.terrainData.terrainLayers = new[] { AssetDatabase.LoadAssetAtPath <TerrainLayer>(filename) }; SetAlphaMaps(t); #else LoadRTPTextures(); TerrainLayer[] tls = new TerrainLayer[4]; for (int i = 0; i < 4; i++) { tls[i] = new TerrainLayer { diffuseTexture = rtpTextures[i * 3] }; } t.terrainData.terrainLayers = tls; SetAlphaMaps(t); ReliefTerrain reliefTerrain = t.gameObject.GetComponent <ReliefTerrain>() ?? t.gameObject.AddComponent <ReliefTerrain>(); reliefTerrain.InitArrays(); reliefTerrain.ColorGlobal = texture; #endif #else #if !RTP float tsx = prefs.textureWidth - 4; float tsy = prefs.textureHeight - 4; Vector2 tileSize = new Vector2(t.terrainData.size.x + t.terrainData.size.x / tsx * 4, t.terrainData.size.z + t.terrainData.size.z / tsy * 4); Vector2 tileOffset = new Vector2(t.terrainData.size.x / prefs.textureWidth / 2, t.terrainData.size.z / prefs.textureHeight / 2); SplatPrototype sp = new SplatPrototype { tileSize = tileSize, tileOffset = tileOffset, texture = texture }; t.terrainData.splatPrototypes = new [] { sp }; #else LoadRTPTextures(); SplatPrototype[] sps = new SplatPrototype[4]; for (int i = 0; i < 4; i++) { sps[i] = new SplatPrototype { texture = rtpTextures[i * 3] }; } t.terrainData.splatPrototypes = sps; ReliefTerrain reliefTerrain = t.gameObject.GetComponent <ReliefTerrain>() ?? t.gameObject.AddComponent <ReliefTerrain>(); reliefTerrain.InitArrays(); reliefTerrain.ColorGlobal = texture; #endif #endif }