/** * Compte le nombre de chaque éléments en texture sur le Terrain. */ public static void Ground(TerrainData tData, Texture[] textures){ Init(); for (int y = 0; y < tData.alphamapHeight; y++) { for (int x = 0; x < tData.alphamapWidth; x++) { if(tData.GetHeight(y,x) < textures[Texture.SAND].maxHeight){ NB_SAND++; }else if(tData.GetHeight(y,x) > textures[Texture.GRASSHILL].minHeight && tData.GetHeight(y,x) < textures[Texture.GRASSHILL].maxHeight){ NB_GRASSHILL++; }else if(tData.GetHeight(y,x) > textures[Texture.GRASSROCKY].minHeight && tData.GetHeight(y,x) < textures[Texture.GRASSROCKY].maxHeight){ NB_GRASSROCKY++; }else if(tData.GetHeight(y,x) > textures[Texture.MUDROCKY].minHeight){ NB_MUDROCKY++; } } } }
void UpdateHighestPoint(TerrainData terrainData) { if(disableProceduralTerrain) return; float maxHeight = float.NegativeInfinity; float heightToTerrainCoordsW = terrainData.size.x/terrainData.heightmapWidth; float heightToTerrainCoordsH = terrainData.size.z/terrainData.heightmapHeight; for (int i = 0; i < terrainData.heightmapWidth; i++) { for (int j = 0; j < terrainData.heightmapHeight; j++) { float currHeight = terrainData.GetHeight(i,j); if(terrainData.GetHeight(i,j) > maxHeight) { maxHeight = currHeight; _highestPoint.x = i * heightToTerrainCoordsW; _highestPoint.z = j * heightToTerrainCoordsH; _highestPoint.y = maxHeight; } } } }
void texturizeTerrain(TerrainData terrainData, float max_altura) { float[, ,] map = new float[tamAlpha, tamAlpha, terrainData.alphamapLayers]; Random.seed = 0; for (int x = 0; x < tamAlpha; x++) { for (int z = 0; z < tamAlpha; z++) { //** texturizado float normX = x * 1.0f / (tamAlpha - 1); float normZ = z * 1.0f / (tamAlpha - 1); float height = terrainData.GetHeight(Mathf.RoundToInt(normX * terrainData.heightmapHeight), Mathf.RoundToInt(normZ * terrainData.heightmapWidth)); float[] splatWeights = new float[terrainData.alphamapLayers]; float peso = 0; if (height <= max_altura * AlphaCorte1) { peso = height / (max_altura * AlphaCorte1); splatWeights[0] = 1 - peso; splatWeights[1] = peso; } if ((height > max_altura * AlphaCorte1) && (height <= max_altura * AlphaCorte2)) { float altura2 = height - (max_altura * AlphaCorte1); float maximo2 = (max_altura * AlphaCorte2) - (max_altura * AlphaCorte1); peso = altura2 / maximo2; splatWeights[1] = 1 - peso; splatWeights[2] = peso; } if ((height > max_altura * AlphaCorte2) && (height <= max_altura)) { float altura2 = height - (max_altura * AlphaCorte2); float maximo2 = max_altura - (max_altura * AlphaCorte2); peso = altura2 / maximo2; splatWeights[2] = 1 - peso; splatWeights[3] = peso; } float suma = splatWeights.Sum(); //Recorremos las texturas y las pintamos. for (int i = 0; i < terrainData.alphamapLayers; i++) { splatWeights[i] /= suma; map[z, x, i] = splatWeights[i]; } } } terrainData.alphamapResolution = tamAlpha; terrainData.SetAlphamaps(0, 0, map); }
float maxHeight(TerrainData terrainData, float max_altura) { for (int x = 0; x < tamAlpha; x++) { for (int z = 0; z < tamAlpha; z++) { float normX = x * 1.0f / (tamAlpha - 1); float normZ = z * 1.0f / (tamAlpha - 1); float height = terrainData.GetHeight(Mathf.RoundToInt(normX * terrainData.heightmapHeight), Mathf.RoundToInt(normZ * terrainData.heightmapWidth)); if (height > max_altura) max_altura = height; } } return (max_altura); }
private void LoadTrees(string xvalue, string yvalue, TerrainData tdata) { if (_trees.Count > 0) { System.Collections.Generic.List<TreeInstance> treelist = new System.Collections.Generic.List<TreeInstance>(); TreePrototype[] prototypes = new TreePrototype[_trees.Count]; for (int k = 0; k < _trees.Count; k++) { TreeMapInfo2 tree = _trees[k]; // Load splatmap file string treefile = string.Format(tree.Filemask, xvalue, yvalue); string[] treefiles = System.IO.Directory.GetFiles(_imagesPath, treefile + ".*"); System.Uri uri = new System.Uri(treefiles[0]); WWW imageloader = new WWW(uri.AbsoluteUri); treefiles = null; while (!imageloader.isDone) { } Color[] imagecolors = imageloader.texture.GetPixels(); int imagewidth = imageloader.texture.width; imageloader.Dispose(); // Create Tree Prototype TreePrototype tp = new TreePrototype(); tp.prefab = tree.Prefab; tp.bendFactor = tree.BendFactor; prototypes[k] = tp; float treefactor = 0; for (int u = 0; u < _heightmapsize.x; u++) { for (int v = 0; v < _heightmapsize.y; v++) { //if (imagecolors[(((imagewidth - 1) - v) * imagewidth) + u].grayscale >= 0.035) //{ // Get treemap resolution / terrain size bias float xbias = _terrainsize.x/imagewidth; float ybias = _terrainsize.z/imagewidth; // Calculate Tree Position float terrainheight = tdata.size.y; float terrainwidth = tdata.size.x; Vector3 treeposition = new Vector3(); treeposition.x = (v*xbias)/terrainwidth; treeposition.y = tdata.GetHeight(v, u)/terrainheight; treeposition.z = (u*ybias)/terrainwidth; TreeInstance ti = new TreeInstance(); ti.prototypeIndex = k; ti.position = treeposition; ti.lightmapColor = new Color(1, 1, 1); ti.heightScale = (tree.HeightScale/100) + UnityEngine.Random.Range(-(tree.HeightVariation/100), (tree.HeightVariation/100)); ti.widthScale = (tree.WidthScale/100) + UnityEngine.Random.Range(-(tree.WidthVariation/100), (tree.WidthVariation/100)); ti.color = new Color(1, 1, 1); float treestoplace = imagecolors[((imagewidth - 1) - u)*imagewidth + v].r*tree.MaxDensity; treefactor += treestoplace; if (treefactor > (1.0f + UnityEngine.Random.Range(-0.2f, 0.2f))) { treelist.Add(ti); treefactor = 0; } } } } tdata.treePrototypes = prototypes; tdata.treeInstances = treelist.ToArray(); prototypes = null; treelist.RemoveRange(0, treelist.Count); treelist = null; System.GC.Collect(0); } }
int[,] getDetailLayers(TerrainData td) { int[,] details = new int[width, length]; for (int i = 0; i < width; i++) { for (int j = 0; j < length; j++) { var ht = td.GetHeight(i, j); if (ht > height * 0.3 && ht < height * 0.335) { var t = 1; details[i, j] = t; } else { if (ht < height * 0.3) details[i, j] = 2; Debug.Log("ROCK"); } //if (i == width || j == length) //details[i, j] = 1; } } return details; }
/// <summary> /// Generate textures for the terrain /// </summary> float[,,] GenerateAlphaMap(TerrainData data) { //make sure textures have been set if (_splatPrototypes.Length < 1) { return null; } //create map var width = data.alphamapWidth; var height = data.alphamapHeight; var layers = data.alphamapLayers - 1; //ignore the road layer var alphamap = new float[width, height, layers + 1]; for (int x = 0; x < width; ++x) { for (int z = 0; z < height; ++z) { var xNorm = (float)x / (float)width; var zNorm = (float)z / (float)height; //sample height at this location var h = data.GetHeight(z, x); //get normalized coordinates relative to the overall terrain dimensions var normal = data.GetInterpolatedNormal(zNorm, xNorm); //get steepnes at the coordinate var angle = data.GetSteepness(zNorm, xNorm); var weights = new float[layers]; if (layers > 1) { weights[0] = 0.5f; } if (layers > 2) { //more influence at steep heights weights[1] = angle / 90f; } if (layers > 3 && h < 0.4f) { weights[2] = 1f; } float sum = weights.Sum(); //go over all terrain textures for (int i = 1; i < layers; i++) { //normalize weights[i - 1] /= sum; //set weight for correct texture alphamap[x, z, i] = weights[i - 1]; } } } return alphamap; }