IEnumerator DrawRoads() { yield return(new WaitForSeconds(3f)); UnityEngine.Terrain t = GetComponent <UnityEngine.Terrain>(); Texture2D texture = t.terrainData.splatPrototypes[0].texture; while (true) { for (int x = 0; x < GameManager.WIDTH; x++) { for (int y = 0; y < GameManager.LENGTH; y++) { // texture.SetPixel(y, x, texture.GetPixel(y, x) + (Color.black * Roads[x, y])); if (Roads[x, y] > 0) { float h, s, v; Color.RGBToHSV(texture.GetPixel(x, y), out h, out s, out v); Color c = Color.HSVToRGB(h, s, 1.0f - Roads[x, y]); texture.SetPixel(x, y, c); } } } SplatPrototype splat = new SplatPrototype(); splat.texture = texture; splat.tileOffset = new Vector2(0, 0); splat.tileSize = new Vector2(GameManager.WIDTH, GameManager.LENGTH); splat.texture.Apply(true); t.terrainData.splatPrototypes = new SplatPrototype[] { splat }; yield return(new WaitForSeconds(1f)); } }
private void SetNormals() { #if UNITY_2017 || UNITY_2018_1 || UNITY_2018_2 var splatProtos = new List <SplatPrototype>(); for (var i = 0; i < albedos.Count; i++) { var prototype = new SplatPrototype(); prototype.texture = albedos[i]; prototype.normalMap = normals[normalCount[i]]; splatProtos.Insert(i, prototype); } Undo.RegisterCompleteObjectUndo(terrain.gameObject, "AddNormalsToSplats"); terrain.terrainData.splatPrototypes = splatProtos.ToArray(); #endif #if UNITY_2018_3_OR_NEWER var splatProtos = new List <TerrainLayer>(); for (var i = 0; i < albedos.Count; i++) { var prototype = new TerrainLayer(); prototype.diffuseTexture = albedos[i]; prototype.normalMapTexture = normals[normalCount[i]]; splatProtos.Insert(i, prototype); } Undo.RegisterCompleteObjectUndo(terrain.gameObject, "AddNormalsToSplats"); terrain.terrainData.terrainLayers = splatProtos.ToArray(); #endif terrain.Flush(); terrain.terrainData.RefreshPrototypes(); Close(); }
public void exportSplatTexture_NotRun() { string path = Application.streamingAssetsPath; int idx = 0; SplatPrototype splatLayer = null; Texture2D writeTex = null; Color color; for (idx = 0; idx < terrainData.splatPrototypes.Length; ++idx) { splatLayer = terrainData.splatPrototypes[idx]; writeTex = new Texture2D(splatLayer.texture.width, splatLayer.texture.height, TextureFormat.RGB24, false); for (int imageY = 0; imageY < splatLayer.texture.height; ++imageY) { for (int imageX = 0; imageX < splatLayer.texture.width; ++imageX) { // 这个纹理是不能读写的,需要使用 AssetDatabase.GetAssetPath 读取纹理目录 color = splatLayer.texture.GetPixel(imageX, imageY); writeTex.SetPixel(imageX, imageY, color); } } UtilPath.saveTex2File(splatLayer.texture, path + "/SplatTextures" + idx + ".png"); } }
//Creating various prototypes (needed) void Prototypes() { //Ground textures splats = new SplatPrototype[2]; splats[0] = new SplatPrototype(); splats[0].texture = splat0; splats[0].tileSize = new Vector2(splat0Size, splat0Size); splats[1] = new SplatPrototype(); splats[1].texture = splat1; splats[1].tileSize = new Vector2(splat1Size, splat1Size); //Grass textures details = new DetailPrototype[3]; details[0] = new DetailPrototype(); details[0].prototypeTexture = detail0; details[0].renderMode = DetailRenderMode.GrassBillboard; details[0].healthyColor = Color.green; details[0].dryColor = Color.grey; details[1] = new DetailPrototype(); details[1].prototypeTexture = detail1; details[1].renderMode = DetailRenderMode.GrassBillboard; details[1].healthyColor = Color.green; details[1].dryColor = Color.grey; details[2] = new DetailPrototype(); details[2].prototypeTexture = detail2; details[2].renderMode = DetailRenderMode.GrassBillboard; details[2].healthyColor = Color.green; details[2].dryColor = Color.grey; }
private void ApplyTextures(TerrainData terrainData) { var flatSplat = new SplatPrototype(); var steepSplat = new SplatPrototype(); flatSplat.texture = Settings.FlatTexture; steepSplat.texture = Settings.SteepTexture; terrainData.splatPrototypes = new SplatPrototype[] { flatSplat, steepSplat }; terrainData.RefreshPrototypes(); var splatMap = new float[terrainData.alphamapResolution, terrainData.alphamapResolution, 2]; for (var zRes = 0; zRes < terrainData.alphamapHeight; zRes++) { for (var xRes = 0; xRes < terrainData.alphamapWidth; xRes++) { var normalizedX = (float)xRes / (terrainData.alphamapWidth - 1); var normalizedZ = (float)zRes / (terrainData.alphamapHeight - 1); var steepness = terrainData.GetSteepness(normalizedX, normalizedZ); var steepnessNormalized = Mathf.Clamp(steepness / 1.5f, 0, 1f); splatMap[zRes, xRes, 0] = 1f - steepnessNormalized; splatMap[zRes, xRes, 1] = steepnessNormalized; } } terrainData.SetAlphamaps(0, 0, splatMap); }
void CreateProtoTypes() { if (_AddTrees) { _treeData = new TreePrototype[_Trees.Length]; for (int i = 0; i < _Trees.Length; i++) { _treeData[i] = new TreePrototype(); _treeData[i].prefab = _Trees[i]; } } if (_AddTexture) { _TerrainTexture[0] = new SplatPrototype(); _TerrainTexture[0].texture = _TerTexture; } if (_AddGrass) { _detailData = new DetailPrototype[1]; _detailData[0] = new DetailPrototype(); _detailData[0].prototypeTexture = _Grass; _detailData[0].renderMode = DetailRenderMode.GrassBillboard; } }
// ################################################################################################################### // ############################ Methods used mostly by the Custom Editor classes ##################################### // ################################################################################################################### public override void GenerateBiome() { MaterialTerrain bioMat = BiomeManager.GetMaterial(biome); bioMat.SetShaderType(WorldManager.worldInstance.shaderType); SplatPrototype[] splats = new SplatPrototype[1]; splats[0] = new SplatPrototype(); splats[0].texture = (Texture2D)(bioMat.GetTexture("_Texture0")); terrain.terrainData.splatPrototypes = splats; if (WorldManager.worldInstance.shaderType == eShaderType.Shader_OriginalMode) { bioMat.material.SetFloat("_Transition1", terrain.terrainData.size[0] / 2); bioMat.material.SetFloat("_Transition2", terrain.terrainData.size[0] / 2); bioMat.material.SetInt("_TransitionType1", 0); bioMat.material.SetInt("_TransitionType2", 0); bioMat.material.SetFloat("_TransitionPos0", 0); bioMat.material.SetFloat("_TransitionPos1", 0); bioMat.material.SetFloat("_LimitMax", terrain.terrainData.size[0]); } bioMat.material.SetFloat("_MapHeight", terrain.terrainData.size[1]); bioMat.material.SetFloat("_MapSize", terrain.terrainData.size[0]); terrain.materialType = Terrain.MaterialType.Custom; terrain.materialTemplate = (Material)Instantiate(bioMat.material); }
/// <summary> /// Applies an array of Gaia Splat prototypes to a terrain. Uses the correct terrain API for pre and post Unity 2018.3. /// </summary> /// <param name="terrain">The terrain to assign the splat prototypes to.</param> /// <param name="splats">Array of GaiaSplatPrototypes to assign to the terrain.</param> /// <param name="terrainName">The current Gaia profile. Used for terrain layer asset filenames.</param> public static void SetGaiaSplatPrototypes(Terrain terrain, GaiaSplatPrototype[] splats, string terrainName) { if (terrain != null && splats != null) { #if UNITY_2018_3_OR_NEWER TerrainLayer[] terrainLayers = new TerrainLayer[splats.Length]; for (int i = 0; i < splats.Length; i++) { terrainLayers[i] = splats[i].Convert(); } //completely remove all old splat prototypes first to prevent build-up of abandoned files RemoveTerrainLayerAssetFiles(terrainName); //Permanently save the new layers as asset files & get a reference, else they will not work properly in the terrain for (int i = 0; i < terrainLayers.Length; i++) { terrainLayers[i] = SaveTerrainLayerAsAsset(terrainName, i.ToString(), terrainLayers[i]); } terrain.terrainData.terrainLayers = terrainLayers; #else SplatPrototype[] splatPrototypes = new SplatPrototype[splats.Length]; for (int i = 0; i < splats.Length; i++) { splatPrototypes[i] = splats[i].Convert(); } terrain.terrainData.splatPrototypes = splatPrototypes; #endif } }
/// <summary> /// Converts a Terrain Texture list to a Splat Prototype list /// If the Texture is tinted, use the tinted texture rather than the main texture /// </summary> /// <returns>The splat prototype list.</returns> /// <param name="terrainTextureList">Terrain texture list.</param> public static List <SplatPrototype> ToSplatPrototypeList(List <LBTerrainTexture> terrainTextureList) { if (terrainTextureList != null) { List <SplatPrototype> splatPrototypeList = new List <SplatPrototype>(); for (int i = 0; i < terrainTextureList.Count; i++) { SplatPrototype temp = new SplatPrototype(); temp.texture = terrainTextureList[i].GetTexture2D(); // Currently, rotation of normalmaps produces incorrect results. // If it is rotated, rotate the normalmap too //if (terrainTextureList[i].isRotated && terrainTextureList[i].normalMap != null) //{ // // At Runtime, the normalmap would need to already be readable. // temp.normalMap = LBTextureOperations.RotateTexture(terrainTextureList[i].normalMap, terrainTextureList[i].rotationAngle); //} //else { temp.normalMap = terrainTextureList[i].normalMap; } temp.normalMap = terrainTextureList[i].normalMap; temp.smoothness = terrainTextureList[i].smoothness; temp.metallic = terrainTextureList[i].metallic; temp.tileSize = terrainTextureList[i].tileSize; splatPrototypeList.Add(temp); } return(splatPrototypeList); } else { return(null); } }
// Token: 0x0600001A RID: 26 RVA: 0x00003A1C File Offset: 0x00001C1C private static void ExportSplat() { int num = LayaTerrainExporter.m_terrain.terrainData.splatPrototypes.Length; for (int i = 0; i < num; i++) { SplatPrototype splatPrototype = LayaTerrainExporter.m_terrain.terrainData.splatPrototypes[i]; Texture2D texture2D = splatPrototype.texture; string assetPath = AssetDatabase.GetAssetPath(texture2D.GetInstanceID()); TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; textureImporter.isReadable = true; textureImporter.textureCompression = 0; AssetDatabase.ImportAsset(assetPath); FileStream fileStream = File.Open(LayaTerrainExporter.m_saveLocation + "/" + texture2D.name.ToLower() + ".jpg", FileMode.Create); new BinaryWriter(fileStream).Write(texture2D.EncodeToJPG()); fileStream.Close(); if (splatPrototype.normalMap != null) { texture2D = splatPrototype.normalMap; string assetPath2 = AssetDatabase.GetAssetPath(texture2D.GetInstanceID()); TextureImporter textureImporter2 = AssetImporter.GetAtPath(assetPath2) as TextureImporter; textureImporter2.isReadable = true; textureImporter2.textureCompression = 0; AssetDatabase.ImportAsset(assetPath2); FileStream fileStream2 = File.Open(LayaTerrainExporter.m_saveLocation + "/" + texture2D.name.ToLower() + ".jpg", FileMode.Create); new BinaryWriter(fileStream2).Write(texture2D.EncodeToJPG()); fileStream2.Close(); } } }
public void BuildTerrainLayers() { Assert.AreEqual(AlphaWidth, AlphaHeight, "Alpha map width must be equal to its height"); int numLayers = Layers.Count; SplatPrototype[] splats = new SplatPrototype[numLayers]; float[,,] weights = new float[AlphaWidth, AlphaHeight, numLayers]; int i = 0; foreach (I3DTerrainLayer layer in Layers.OrderBy(x => x.Priority)) { splats[i] = layer.SplatMap; for (int y = 0; y < AlphaHeight; y++) { for (int x = 0; x < AlphaWidth; x++) { weights[x, y, i] = layer.Weights[x, y]; } } i++; } TerrainData.alphamapResolution = AlphaWidth; TerrainData.splatPrototypes = splats; TerrainData.SetAlphamaps(0, 0, weights); Terrain.terrainData = TerrainData; TerrainCollider.terrainData = TerrainData; }
void SetupTextures() { if (terrain == null || terrain.terrainData == null) { return; } var splats = new List <SplatPrototype>(); foreach (var texture in textures) { var splat = new SplatPrototype(); splat.texture = texture.diffuse; splat.normalMap = texture.normal; splat.metallic = texture.metallic; splat.tileOffset = texture.offset; if (texture.size == Vector2.zero) { texture.size = new Vector2(15, 15); } splat.tileSize = texture.size; splats.Add(splat); } var data = terrain.terrainData; data.splatPrototypes = splats.ToArray(); }
public void setTexture(Texture2D texture, int chunk_size) { Debug.Assert(0 < chunk_size); TerrainData tData = this.terrain.terrainData; Vector3 tDataSize = tData.size; tData.alphamapResolution = chunk_size; SplatPrototype[] splatprototype = new SplatPrototype[1]; splatprototype[0] = new SplatPrototype(); splatprototype[0].texture = texture; splatprototype[0].tileSize = new Vector2(tDataSize.x, tDataSize.z); tData.splatPrototypes = splatprototype; int al_w = tData.alphamapWidth; int al_h = tData.alphamapHeight; float[,,] map = new float[al_w, al_h, 1]; for (int x = 0; x < al_w; x += 1) { for (int z = 0; z < al_h; z += 1) { map[x, z, 0] = 1f; } } tData.SetAlphamaps(0, 0, map); this.terrain.terrainData = tData; }
private void ApplyTerrainSplat() { if (this.m_Terrain == null || this.m_Terrain.terrainData == null) { return; } SplatPrototype[] array = this.m_Terrain.terrainData.splatPrototypes; if (this.m_Index == -1) { SplatPrototype[] array2 = new SplatPrototype[array.Length + 1]; Array.Copy(array, 0, array2, 0, array.Length); this.m_Index = array.Length; array = array2; array[this.m_Index] = new SplatPrototype(); } array[this.m_Index].texture = this.m_Texture; array[this.m_Index].normalMap = this.m_NormalMap; array[this.m_Index].tileSize = this.m_TileSize; array[this.m_Index].tileOffset = this.m_TileOffset; array[this.m_Index].specular = this.m_Specular; array[this.m_Index].metallic = this.m_Metallic; array[this.m_Index].smoothness = this.m_Smoothness; this.m_Terrain.terrainData.splatPrototypes = array; EditorUtility.SetDirty(this.m_Terrain); }
public void AddTerrainTexture(Texture2D p_texture) { if (m_confT.TerrainTextureConfig != null) { if (m_GUI3dTerrain != null && m_GUI3dTerrain.TerrainManager != null && m_GUI3dTerrain.TerrainManager.TerrainData != null) { SplatPrototype[] splatTexturesOld = m_GUI3dTerrain.TerrainManager.TerrainData.splatPrototypes; SplatPrototype[] splatTexturesNew = new SplatPrototype[splatTexturesOld.Length + 1]; System.Array.Copy(splatTexturesOld, splatTexturesNew, splatTexturesOld.Length); int textureIndex = GetTextureIndex(p_texture); if (textureIndex >= 0) { splatTexturesNew[splatTexturesNew.Length - 1] = new SplatPrototype(); splatTexturesNew[splatTexturesNew.Length - 1].texture = m_confT.TerrainTextureConfig.TERRAIN_TEXTURES[textureIndex]; splatTexturesNew[splatTexturesNew.Length - 1].tileSize = m_confT.TerrainTextureConfig.TERRAIN_TEXTURE_SIZES[textureIndex]; splatTexturesNew[splatTexturesNew.Length - 1].tileOffset = m_confT.TerrainTextureConfig.TERRAIN_TEXTURE_OFFSETS[textureIndex]; m_GUI3dTerrain.TerrainManager.TerrainData.splatPrototypes = splatTexturesNew; m_doRebuildTerrainTab = true; // notify listeners that the level data was changed if (LE_EventInterface.OnChangeLevelData != null) { LE_EventInterface.OnChangeLevelData(this, new LE_LevelDataChangedEvent(LE_ELevelDataChangeType.TERRAIN_TEXTURES)); } } else { Debug.LogError("LE_LogicTerrain: AddTerrainTexture: could not find given texture in TerrainTextureConfig!"); } } } else { Debug.LogError("LE_LogicTerrain: AddTerrainTexture: LE_ConfigTerrain has no TerrainTextureConfig set!"); } }
/// <summary> /// Edit splatprototype /// </summary> /// <param name="terrainData"></param> /// <param name="index"></param> /// <param name="main"></param> /// <param name="norm"></param> /// <param name="size"></param> /// <param name="offset"></param> /// <param name="metallic"></param> /// <param name="smoothness"></param> /// <param name="specularColor"></param> public static void EditSplatTexture(TerrainData terrainData, int index, Texture2D main, Texture2D norm, Vector2 size, Vector2 offset, float metallic, float smoothness, Color specularColor) { if (index < 0 || index >= terrainData.splatPrototypes.Length) { return; } int ttLength = terrainData.splatPrototypes.Length; SplatPrototype[] sp = new SplatPrototype[ttLength]; for (int i = 0; i < ttLength; i++) { if (index == i) { sp[index] = new SplatPrototype(); sp[index].texture = main; sp[index].normalMap = norm; sp[index].tileSize = size; sp[index].tileOffset = offset; sp[index].metallic = metallic; sp[index].smoothness = smoothness; sp[index].specular = specularColor; } else { sp[i] = terrainData.splatPrototypes[i]; } } terrainData.splatPrototypes = sp; }
public IEnumerator CreateTerrainZoom(int zoom) { Terrain t1 = GetComponent <Terrain>(); t1.basemapDistance = 10000; Latlong RBLl = HarvenSin.pixelToLatlong(new Vector2(512 * 16, 512 * 16), latlongLT, zoom); ElevationData evData = new ElevationData(); //string ElvURL = "http://dev.virtualearth.net/REST/v1/Elevation/Bounds?bounds="+ RBLl.lati+","+latlongLT.longti+","+latlongLT.lati+","+ RBLl.longti+"&rows=32&cols=32&heights=ellipsoid&key="+bingKey; //yield return StartCoroutine(evData.GetDataFormUrl(ElvURL)); yield return(StartCoroutine(evData.GetElv(latlongLT, zoom))); float[,] heightmap = evData.GetHeightMap(); //float[,] heightmap = evData.GetHeightMap(32, 32); t1.terrainData.heightmapResolution = evData.size; t1.terrainData.SetHeights(0, 0, heightmap); //yield return StartCoroutine(GetImageFormTile(latlongLT,16,16,19)); yield return(StartCoroutine(ImageDataP.GetImageFormUrl(latlongLT, 16, 16, zoom))); SplatPrototype[] sp = new SplatPrototype[1]; t1.terrainData.size = new Vector3((float)HarvenSin.Distance(latlongLT.lati, latlongLT.longti, latlongLT.lati, RBLl.longti) * 1000, 10000f, (float)HarvenSin.Distance(RBLl.lati, latlongLT.longti, latlongLT.lati, latlongLT.longti) * 1000f); sp[0] = CreateSplatPrototype(imageCombined, new Vector2(t1.terrainData.size.x, t1.terrainData.size.z), new Vector2(0f, 0f)); File.WriteAllBytes("Assets/TerrainTexture/test.jpg", imageCombined.EncodeToJPG());//output Texture t1.terrainData.splatPrototypes = sp; }
private static void ExportSplat() { int length = m_terrain.get_terrainData().get_splatPrototypes().Length; for (int i = 0; i < length; i++) { SplatPrototype prototype = m_terrain.get_terrainData().get_splatPrototypes()[i]; Texture2D textured = prototype.get_texture(); string assetPath = UnityEditor.AssetDatabase.GetAssetPath(textured.GetInstanceID()); UnityEditor.TextureImporter atPath = UnityEditor.AssetImporter.GetAtPath(assetPath) as UnityEditor.TextureImporter; atPath.isReadable = true; atPath.textureCompression = UnityEditor.TextureImporterCompression.Uncompressed; UnityEditor.AssetDatabase.ImportAsset(assetPath); FileStream output = File.Open(m_saveLocation + "/" + textured.get_name().ToLower() + ".jpg", System.IO.FileMode.Create); new BinaryWriter(output).Write(ImageConversion.EncodeToJPG(textured)); output.Close(); if (prototype.get_normalMap() != null) { textured = prototype.get_normalMap(); string path = UnityEditor.AssetDatabase.GetAssetPath(textured.GetInstanceID()); UnityEditor.TextureImporter importer2 = UnityEditor.AssetImporter.GetAtPath(path) as UnityEditor.TextureImporter; importer2.isReadable = true; importer2.textureCompression = UnityEditor.TextureImporterCompression.Uncompressed; UnityEditor.AssetDatabase.ImportAsset(path); FileStream stream2 = File.Open(m_saveLocation + "/" + textured.get_name().ToLower() + ".jpg", System.IO.FileMode.Create); new BinaryWriter(stream2).Write(ImageConversion.EncodeToJPG(textured)); stream2.Close(); } } }
public float startTime = 0.0f; //Start Time for PerlinNoise // Use this for initialization void Start() { //Get the terrain collider of the terrain to acces information of it TerrainCollider terrainCollider = gameObject.GetComponent <TerrainCollider>(); //Error control if (terrainCollider == null) { Debug.Log("Could not get TerrainCollider from Terrain object"); Debug.Break();//Stop execution } myTerrainData = terrainCollider.terrainData; //set the data variable myTerrainData.size = worldSize; //set the size based on the editor myTerrainData.heightmapResolution = resolution; //set the resolution based on the editor //a SplatPrototype is the brush stencil to be applied in the terrain, in this case only one SplatPrototype[] terrainTexture = new SplatPrototype[1]; terrainTexture[0] = new SplatPrototype(); terrainTexture[0].texture = myTexture; //If the resolution is larger than 1K force it 1K the larger the terrain the more time it takes to fill //the height map, height maps are prefered in powers of 2 + 1 if (resolution > 513) { resolution = 513; } GeneratePerlin(); }
public static SplatPrototype[] CreateSplatPrototype() #endif { #if UNITY_2018 TerrainLayer[] splats = Resources.LoadAll <TerrainLayer>("Terrain/TerrainLayers"); return(splats); #else Texture2D[] texturesRes = Resources.LoadAll <Texture2D>("Terrain/Textures"); SplatPrototype[] splats = new SplatPrototype[texturesRes.Length]; for (int i = 0, length = splats.Length; i < length; i++) { SplatPrototype splat = new SplatPrototype { texture = texturesRes[i], }; //if (normalMaps.Count > 0) //{ // splat.normalMap = normalMaps.Dequeue(); //} splats[i] = splat; } return(splats); #endif }
SplatPrototype[] GetPrototypes() { Util.Map.Location map = global.map; string[] textures = WorldConfig.GetTilesList(map); if (textures.Length == 0) { return(new SplatPrototype[0]); } string dir = Util.File.WorldStorageDir(map); SplatPrototype[] array = new SplatPrototype[textures.Length + 1]; for (int i = 0; i < textures.Length; ++i) { if (textures[i] == null) { array[i] = new SplatPrototype(); } else { array[i] = GetSplat(Util.File.DIRECTORY_DATA + dir + "/" + textures[i] + ".jpg"); } } //simple texture from settings array[textures.Length] = new SplatPrototype(); array[textures.Length].texture = Util.Data.CreateFillTexture(WorldConfig.GetBackground(map), 256, 256); array[textures.Length].tileSize = new Vector2(Config.TileSize, Config.TileSize); return(array); }
/* TODO: Show the below struct editors using standard inspector drawing tools */ bool ShowSplatPrototype(SplatPrototype splatPrototype, int id) { bool removeThis = false; EGL.BeginVertical(GuiUtils.Skin.box); { GUILayout.Label(id.ToString() + ". " + (splatPrototype.texture != null ? splatPrototype.texture.name : "")); EGL.BeginHorizontal(); { splatPrototype.texture = EGL.ObjectField(splatPrototype.texture, typeof(Texture2D), false, GUILayout.Width(64f), GUILayout.Height(64f)) as Texture2D; EGL.BeginVertical(); { splatPrototype.tileOffset = EGL.Vector2Field("Tile Offset", splatPrototype.tileOffset); splatPrototype.tileSize = EGL.Vector2Field("Tile Size", splatPrototype.tileSize); } EGL.EndVertical(); if (GUILayout.Button("Remove", GUILayout.Width(64f), GUILayout.Height(64f))) { removeThis = true; } } EGL.EndHorizontal(); } EGL.EndVertical(); return(removeThis); }
public void GetSplatTextures() { if (!CheckValidUnityTerrain()) { return; } splatPrototypes.Clear(); for (int i = 0; i < terrain.terrainData.splatPrototypes.Length; i++) { SplatPrototype s = terrain.terrainData.splatPrototypes[i]; TC_SplatPrototype d = new TC_SplatPrototype(); d.texture = s.texture; d.normalMap = s.normalMap; d.metallic = s.metallic; d.smoothness = s.smoothness; d.tileOffset = s.tileOffset; d.tileSize = s.tileSize; splatPrototypes.Add(d); } //if (splatColors == null) splatColors = new Color[splatPrototypes.Count]; //if (splatColors.Length != splatPrototypes.Count) splatColors = new Color[splatPrototypes.Count]; //Debug.Log("Getsplat texture colors"); //for (int i = 0; i < splatColors.Length; i++) //{ // if (splatPrototypes[i].texture != null) splatColors[i] = GetTextureColor(splatPrototypes[i].texture, 1); //} }
void InitializeData(Terrain terrain, int index) { m_Terrain = terrain; m_Index = index; SplatPrototype info; if (index == -1) { info = new SplatPrototype(); } else { info = m_Terrain.terrainData.splatPrototypes[index]; } m_Texture = info.texture; m_NormalMap = info.normalMap; m_TileSize = info.tileSize; m_TileOffset = info.tileOffset; m_Specular = info.specular; m_Metallic = info.metallic; m_Smoothness = info.smoothness; CheckIfNormalMapHasCorrectTextureType(); }
private void ApplyTerrainSplat() { if ((UnityEngine.Object) this.m_Terrain == (UnityEngine.Object)null || (UnityEngine.Object) this.m_Terrain.terrainData == (UnityEngine.Object)null) { return; } SplatPrototype[] splatPrototypeArray1 = this.m_Terrain.terrainData.splatPrototypes; if (this.m_Index == -1) { SplatPrototype[] splatPrototypeArray2 = new SplatPrototype[splatPrototypeArray1.Length + 1]; Array.Copy((Array)splatPrototypeArray1, 0, (Array)splatPrototypeArray2, 0, splatPrototypeArray1.Length); this.m_Index = splatPrototypeArray1.Length; splatPrototypeArray1 = splatPrototypeArray2; splatPrototypeArray1[this.m_Index] = new SplatPrototype(); } splatPrototypeArray1[this.m_Index].texture = this.m_Texture; splatPrototypeArray1[this.m_Index].normalMap = this.m_NormalMap; splatPrototypeArray1[this.m_Index].tileSize = this.m_TileSize; splatPrototypeArray1[this.m_Index].tileOffset = this.m_TileOffset; splatPrototypeArray1[this.m_Index].specular = this.m_Specular; splatPrototypeArray1[this.m_Index].metallic = this.m_Metallic; splatPrototypeArray1[this.m_Index].smoothness = this.m_Smoothness; this.m_Terrain.terrainData.splatPrototypes = splatPrototypeArray1; EditorUtility.SetDirty((UnityEngine.Object) this.m_Terrain); }
void ApplyTerrainSplat() { if (m_Terrain == null || m_Terrain.terrainData == null) { return; } SplatPrototype[] infos = m_Terrain.terrainData.splatPrototypes; if (m_Index == -1) { var newarray = new SplatPrototype[infos.Length + 1]; System.Array.Copy(infos, 0, newarray, 0, infos.Length); m_Index = infos.Length; infos = newarray; infos[m_Index] = new SplatPrototype(); } infos[m_Index].texture = m_Texture; infos[m_Index].normalMap = m_NormalMap; infos[m_Index].tileSize = m_TileSize; infos[m_Index].tileOffset = m_TileOffset; infos[m_Index].specular = m_Specular; infos[m_Index].metallic = m_Metallic; infos[m_Index].smoothness = m_Smoothness; m_Terrain.terrainData.splatPrototypes = infos; EditorUtility.SetDirty(m_Terrain); }
/// <summary> /// Applies the textures. /// </summary> /// <param name="terrainData">Terrain data.</param> private void ApplyTextures(TerrainData terrainData) { List <SplatPrototype> splats = new List <SplatPrototype>(); for (int i = 0; i < Setting.Textures.Count; i++) { SplatPrototype splat = new SplatPrototype(); splat.texture = Setting.Textures[i]; splats.Add(splat); } terrainData.splatPrototypes = splats.ToArray(); terrainData.RefreshPrototypes(); Texture2D alphamap = NoiseProvider.GetAlphamap(); // proc alphamap var splatMap = new float[terrainData.alphamapResolution, terrainData.alphamapResolution, splats.Count]; for (var zRes = 0; zRes < terrainData.alphamapHeight; zRes++) { for (var xRes = 0; xRes < terrainData.alphamapWidth; xRes++) { Color clr = alphamap.GetPixel(zRes, xRes); splatMap[zRes, xRes, 0] = clr.r; splatMap[zRes, xRes, 1] = clr.g; splatMap[zRes, xRes, 2] = clr.b; splatMap[zRes, xRes, 3] = clr.a; } } terrainData.SetAlphamaps(0, 0, splatMap); }
public static TerrainData GenerateTerrain(float[,] heightMap, float heightMultiplier) { TerrainData terrainData = new TerrainData(); terrainData.alphamapResolution = 129; // heights terrainData.heightmapResolution = 129; terrainData.SetHeights(0, 0, heightMap); terrainData.size = new Vector3(length, height * heightMultiplier, length); // textures SplatPrototype Grass = new SplatPrototype(); SplatPrototype Rocks = new SplatPrototype(); Grass.texture = GrassTexture; Grass.tileSize = new Vector2(4f, 4f); Rocks.texture = RockTexture; Rocks.tileSize = new Vector2(4f, 4f); //terrainData.splatPrototypes = new SplatPrototype[] { Grass, Rocks }; //terrainData.RefreshPrototypes(); //terrainData.SetAlphamaps(0, 0, MakeSplatMap(terrainData)); return(terrainData); }
public void exportSplatTexture() { string fileAllPath = ""; int idx = 0; SplatPrototype splatLayer = null; string resPath = ""; string fileName = ""; int slashIdx = 0; string srcPath = ""; for (idx = 0; idx < terrainData.splatPrototypes.Length; ++idx) { splatLayer = terrainData.splatPrototypes[idx]; resPath = AssetDatabase.GetAssetPath(splatLayer.texture); // 保存目录 slashIdx = resPath.LastIndexOf("/"); fileName = resPath.Substring(slashIdx + 1); fileAllPath = string.Format("{0}/Resources/Materials/Textures/Terrain/{1}", Application.dataPath, fileName); if (!File.Exists(fileAllPath)) { slashIdx = Application.dataPath.LastIndexOf("/"); srcPath = Application.dataPath.Substring(0, slashIdx); srcPath = string.Format("{0}/{1}", srcPath, resPath); File.Copy(srcPath, fileAllPath); } } }
//in order to texture the terrain, we need to set the textures up in the terrain paint thingy private void setUpTextures(){ //remove the current textures CityGenerator.terrain.terrainData.splatPrototypes = null; //this will contain the eventual textures SplatPrototype[] textureArray = new SplatPrototype[5]; string[] textureNames = new string[] { "whiteTexture", "blackTexture", "redTexture", "greenTexture", "blueTexture" }; //Add a new SplatPrototype (= texture for terrain) for each texture for (int i = 0; i < 5; i++) { textureArray[i] = new SplatPrototype(); textureArray[i].texture = (Texture2D)Resources.Load(textureNames[i],typeof(Texture2D)); if (textureArray [i].texture == null) { Debug.LogError ("Texture not found"); } textureArray[i].tileOffset = new Vector2(0, 0); textureArray[i].tileSize = new Vector2(15, 15); textureArray[i].texture.Apply(true); } //set the new texture array CityGenerator.terrain.terrainData.splatPrototypes = textureArray; }