public void SetTerrainLayers(int[,] biomeMap) { SetSplats(biomeMap); TerrainLayer[] layers = new TerrainLayer[Splats.Length]; for (int i = 0; i < Splats.Length; i++) { SplatDetailNode sd = Splats[i]; TerrainLayer layer = new TerrainLayer(); layer.metallic = 0; layer.smoothness = 0.2f; layer.tileOffset = sd.Offset; layer.tileSize = sd.Tiling; layer.normalMapTexture = sd.Normal; layer.diffuseTexture = sd.Diffuse; layers[i] = layer; } _terrain.terrainData.terrainLayers = layers; }
public void SetSplatPrototypes(float[,,] biomeMap) { SetSplats(biomeMap); SplatPrototype[] prototypes = new SplatPrototype[Splats.Length]; for (int i = 0; i < Splats.Length; i++) { SplatDetailNode sd = Splats[i]; SplatPrototype sp = new SplatPrototype(); sp.metallic = 0; sp.smoothness = 0.2f; sp.tileOffset = sd.Offset; sp.tileSize = sd.Tiling; sp.normalMap = sd.Normal; sp.texture = sd.Diffuse; prototypes[i] = sp; } _terrain.terrainData.splatPrototypes = prototypes; }
private float[] GetSplatWeights(int x, int y, int[,] biomeMap, int resolution) { float[] weights = new float[Splats.Length]; Vector2 norm = new Vector2(x / (float)resolution, y / (float)resolution); Vector2 world = MathUtil.NormalToWorld(_tile.GridPosition, norm); int splatOffset = 0; float height = _angleHeightResults.Heights[x, y]; float angle = _angleHeightResults.Angles[x, y]; for (int z = 0; z < Biomes.Length; z++) { SplatDetailNode[] splats = Biomes[z].GetSplatInputs(); if (splats == null) { continue; } float[] splatsWeights = new float[splats.Length]; float curMax = 1f; float oldMax = 1f; float sum = 0f; for (var i = 0; i < splats.Length; i++) { //If no constraint, display SplatDetailNode splat = splats[i]; ConstraintNode cons = splat.GetConstraintValue(); if (cons == null) { splatsWeights[i] = biomeMap[x, y] == z ? 1f : 0f; continue; } //Check whether this SplatData fits required height and angle constraints bool passHeight = cons.ConstrainHeight && cons.HeightConstraint.Fits(height) || !cons.ConstrainHeight; bool passAngle = cons.ConstrainAngle && cons.AngleConstraint.Fits(angle) || !cons.ConstrainAngle; if (!passHeight && !passAngle || !splat.ShouldPlaceAt(world.x, world.y, height, angle)) { continue; } //If it passes height or angle constraints what are the texturing weights float weight = 0; int count = 0; if (passHeight && !passAngle) { weight += cons.HeightConstraint.Weight(height, splat.Blend); count++; } if (passAngle) { weight += cons.AngleConstraint.Weight(angle, splat.Blend); count++; } splatsWeights[i] = weight / count; } // for (int i = 0; i < splats.Length; i++) { // float val = splatsWeights[i]; // float remapped = MathUtil.Map(val, 0f, oldMax, 0f, curMax); // // if (sum >= 1f) { // //No remaining weight to assign, terminate // break; // } // if (i == splats.Length - 1) { // //All remaining weight goes to last idx // splatsWeights[i] = 1 - sum; // break; // } // // sum += remapped; // splatsWeights[i] = remapped; // oldMax = curMax; // curMax = 1 - remapped; // } for (int i = 0; i < splatsWeights.Length; i++) { weights[i + splatOffset] = splatsWeights[i]; } splatOffset += splats.Length; } return(weights); }