public void GenerateMap() { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); Color[,] map; if (randSeed) { seed = System.DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString(); } // IPerlinLayer blue = new BasePerlinLayer(fill, frequency, octaves, octaveFreqMultiplier, octaveAmpMultiplier, "hello world"); IPerlinLayer red = new BasePerlinLayer(fill, frequency, octaves, octaveFreqMultiplier, octaveAmpMultiplier, seed); // IPerlinLayer green = new CompositePerlinLayer(blue, red, PerlinOperation.Intersection); PerlinMap <Color> perlinMap = new PerlinMap <Color>(Color.black, red, Color.white); // perlinMap.AddLayer(blue, Color.blue); // perlinMap.AddLayer(red, Color.red); stopwatch.Start(); map = perlinMap.GetCells(0, 0, width, height); stopwatch.Stop(); genTimeText.text = string.Format("Generated {0} cells in {1}ms.", width * height, stopwatch.ElapsedMilliseconds == 0 ? "<1" : stopwatch.ElapsedMilliseconds.ToString()); if (perlinVisualizer != null) { perlinVisualizer.SetMap(map); } }
// Use this for initialization void Start() { perlinMap = waveMesh.GetComponent <PerlinMap>(); length = this.gameObject.GetComponent <CapsuleCollider>().height *this.transform.lossyScale.z; width = this.gameObject.GetComponent <CapsuleCollider>().radius *this.transform.lossyScale.x * 2; }
// Use this for initialization void Start() { perlinMap = waveMesh.GetComponent<PerlinMap>(); length = this.gameObject.GetComponent<CapsuleCollider>().height * this.transform.lossyScale.z; width = this.gameObject.GetComponent<CapsuleCollider>().radius * this.transform.lossyScale.x * 2; }
private void btnGo_Click(object sender, EventArgs e) { tvOctavas.Nodes.Clear(); int w, h, o, s; Int32.TryParse(nudAncho.Value.ToString(), out w); Int32.TryParse(nudAlto.Value.ToString(), out h); Int32.TryParse(nudOctavas.Value.ToString(), out o); float a; float p; float.TryParse(nudAmplitud.Value.ToString(), out a); float.TryParse(tbPersistencia.Text, out p); if (!Int32.TryParse(tbSemilla.Text, out s)) { map = new PerlinMap(w, h, a, p, o); } else { map = new PerlinMap(w, h, a, p, o, s); } for (int i = 0; i < o; i++) { int num = i + 1; tvOctavas.Nodes.Add("Octava " + num); } tvOctavas.Nodes.Add("Resultado final"); pbMapa.Image = map.DoPerlin(); }
public void GenerateMap() { // Refresh all layers. Inefficient, only need to refresh comp layers that use a base layer that changed. foreach (IPerlinLayerController layer in layers.Values) { layer.RefreshLayer(); } int width = (int)widthSlider.value; int height = (int)heightSlider.value; System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); Color[,] map; // Create PerlinMap with its layers. PerlinMap <Color> perlinMap = new PerlinMap <Color>(Color.black); layerOrder.ForEach(layerName => { IPerlinLayerController controller = layers[layerName]; if (!controller.IsVisible()) { return; } perlinMap.AddLayer(controller.GetLayer(), controller.GetValue()); }); stopwatch.Start(); map = perlinMap.GetCells(0, 0, width, height); stopwatch.Stop(); genTimeText.text = string.Format("Generated {0} cells in {1}ms.", width * height, stopwatch.ElapsedMilliseconds == 0 ? "<1" : stopwatch.ElapsedMilliseconds.ToString()); visualizer.SetMap(map); }
public void Generate() { if (Randomize) { Offset = Random.Range(0f, 9999f); } List <DetailPrototype> detailPrototypes = new List <DetailPrototype>(); foreach (var g in GrassTextures) { detailPrototypes.Add(new DetailPrototype() { prototypeTexture = g }); } TerrainData terrainData = Terrain.activeTerrain.terrainData; terrainData.detailPrototypes = detailPrototypes.ToArray(); float[,] noiseMap = new PerlinMap() { Size = terrainData.detailWidth, Octaves = Octaves, Scale = Scale, Offset = Offset, Persistance = Persistance, Lacunarity = Lacunarity }.Generate(); for (int i = 0; i < terrainData.detailPrototypes.Length; i++) { int[,] detailLayer = terrainData.GetDetailLayer(0, 0, terrainData.detailWidth, terrainData.detailHeight, i); for (int x = 0; x < terrainData.alphamapWidth; x++) { for (int y = 0; y < terrainData.alphamapHeight; y++) { float height = terrainData.GetHeight(x, y); float xScaled = (x + Random.Range(-1f, 1f)) / terrainData.alphamapWidth; float yScaled = (y + Random.Range(-1f, 1f)) / terrainData.alphamapHeight; float steepness = terrainData.GetSteepness(xScaled, yScaled); if (noiseMap[x, y] < IslandsSize && steepness <MaxSteepness && height> MinLevel && height < MaxLevel) { detailLayer[x, y] = Density; } else { detailLayer[x, y] = 0; } } } terrainData.SetDetailLayer(0, 0, i, detailLayer); } }
private void Awake() { //singleton pattern if (!instance) { instance = this; } else if (instance != this) { Destroy(this); } InitPerlinMap(); InitTerrainProp(); }
float[,] GenerateNoise(float[,] falloffMap = null) { AnimationCurve heightCurve = new AnimationCurve(HeightCurve.keys); float maxLocalNoiseHeight; float minLocalNoiseHeight; float[,] noiseMap = new PerlinMap() { Size = Width, Octaves = Octaves, Scale = Scale, Offset = Offset, Persistance = Persistance, Lacunarity = Lacunarity }.Generate(out maxLocalNoiseHeight, out minLocalNoiseHeight); for (int y = 0; y < Height; y++) { for (int x = 0; x < Width; x++) { var lerp = Mathf.InverseLerp(minLocalNoiseHeight, maxLocalNoiseHeight, noiseMap[x, y]); if (falloffMap != null) { lerp -= falloffMap[x, y]; } if (lerp >= 0) { noiseMap[x, y] = heightCurve.Evaluate(lerp); } else { noiseMap[x, y] = 0; } } } return(noiseMap); }
public void Generate() { if (Randomize) { Offset = Random.Range(0f, 9999f); } List <TreePrototype> treePrototypes = new List <TreePrototype>(); foreach (var t in TreePrototypes) { treePrototypes.Add(new TreePrototype() { prefab = t }); } TerrainData terrainData = Terrain.activeTerrain.terrainData; terrainData.treePrototypes = treePrototypes.ToArray(); terrainData.treeInstances = new TreeInstance[0]; List <Vector3> treePos = new List <Vector3>(); float maxLocalNoiseHeight; float minLocalNoiseHeight; float[,] noiseMap = new PerlinMap() { Size = terrainData.alphamapWidth, Octaves = Octaves, Scale = Scale, Offset = Offset, Persistance = Persistance, Lacunarity = Lacunarity }.Generate(out maxLocalNoiseHeight, out minLocalNoiseHeight); for (int x = 0; x < terrainData.alphamapWidth; x++) { for (int y = 0; y < terrainData.alphamapHeight; y++) { float height = terrainData.GetHeight(x, y); float heightScaled = height / terrainData.size.y; float xScaled = (x + Random.Range(-1f, 1f)) / terrainData.alphamapWidth; float yScaled = (y + Random.Range(-1f, 1f)) / terrainData.alphamapHeight; float steepness = terrainData.GetSteepness(xScaled, yScaled); float noiseStep = Random.Range(0f, 1f); float noiseVal = noiseMap[x, y]; if ( noiseStep < Density && noiseVal < IslandsSize && steepness < MaxSteepness && height > MinLevel && height < MaxLevel ) { treePos.Add(new Vector3(xScaled, heightScaled, yScaled)); } } } TreeInstance[] treeInstances = new TreeInstance[treePos.Count]; for (int ii = 0; ii < treeInstances.Length; ii++) { treeInstances[ii].position = treePos[ii]; treeInstances[ii].prototypeIndex = Random.Range(0, treePrototypes.Count); treeInstances[ii].color = new Color(Random.Range(100, 255), Random.Range(100, 255), Random.Range(100, 255)); treeInstances[ii].lightmapColor = Color.white; treeInstances[ii].heightScale = 1.0f + Random.Range(-0.25f, 0.5f); treeInstances[ii].widthScale = 1.0f + Random.Range(-0.5f, 0.25f); } terrainData.treeInstances = treeInstances; Debug.Log(treeInstances.Length + " trees were created"); }
// Update is called once per frame void Update() { if (perlinMap == null) { perlinMap = waveMesh.GetComponent <PerlinMap>(); } Vector3 nextPosition = this.transform.position + this.rigidbody.velocity * Time.deltaTime; float x = nextPosition.x; float z = nextPosition.z; targetHeight = perlinMap.GetHeight(x, z) + 0.08f; float NHeight = perlinMap.GetHeight(x, z + length / 2) + 0.08f; float EHeight = perlinMap.GetHeight(x + width / 2, z) + 0.08f; float SHeight = perlinMap.GetHeight(x, z - length / 2) + 0.08f; float WHeight = perlinMap.GetHeight(x - width / 2, z) + 0.08f; pointN = nextPosition + this.transform.forward * length / 2; pointN = new Vector3(pointN.x, NHeight, pointN.z); pointS = nextPosition - this.transform.forward * length / 2; pointS = new Vector3(pointS.x, SHeight, pointS.z); pointE = nextPosition + this.transform.right * width / 2; pointE = new Vector3(pointE.x, EHeight, pointE.z); pointW = nextPosition - this.transform.right * width / 2; pointW = new Vector3(pointW.x, WHeight, pointW.z); Vector3 NSVector = (pointN - pointS).normalized; Vector3 EWVector = (pointE - pointW).normalized; zAngle = AngleSigned(this.transform.forward, NSVector, this.transform.right); xAngle = AngleSigned(this.transform.right, EWVector, this.transform.forward); //float xAngle = AngleSigned(this.transform.right, xVector.normalized, this.transform.forward); //float zAngle = AngleSigned(this.transform.forward, zVector.normalized, this.transform.right); Debug.DrawLine(pointW, pointE, Color.red); Debug.DrawLine(pointS, pointN, Color.blue); //Vector3 targetRotation = new Vector3(zAngle, this.transform.rotation.y, xAngle); //Quaternion qX = Quaternion.AngleAxis(xAngle, this.transform.forward); //Quaternion qZ = Quaternion.AngleAxis(zAngle, this.transform.right); //this.transform.localRotation = qX * qZ; //this.transform.Rotate(this.transform.forward, xAngle - this.transform.eulerAngles.x); //this.transform.Rotate(this.transform.right, zAngle - this.transform.eulerAngles.z); //Debug.Log(zAngle); Vector3 localVelocity = this.transform.InverseTransformDirection(this.rigidbody.velocity); if (this.gameObject.tag == "Player") { forceController fc = this.gameObject.GetComponent <forceController>(); if (sinking) { fc.enabled = false; } else { if (localVelocity.z >= 0f) { if (wakeSystemL != null && wakeSystemR != null) { wakeSystemL.emissionRate = 500f * Mathf.Abs(fc.curSpeed / fc.maxSpeed); wakeSystemR.emissionRate = 500f * Mathf.Abs(fc.curSpeed / fc.maxSpeed); } } } } else if (this.gameObject.tag == "Enemy") { enemyController fc = this.gameObject.GetComponent <enemyController>(); if (sinking) { fc.enabled = false; } else { if (localVelocity.z >= 0f) { if (wakeSystemL != null && wakeSystemR != null) { wakeSystemL.emissionRate = 500f * Mathf.Abs(fc.curSpeed / fc.maxSpeed); wakeSystemR.emissionRate = 500f * Mathf.Abs(fc.curSpeed / fc.maxSpeed); } } } } if (this.transform.position.y <= -2f) { Destroy(this.gameObject); } }
// Update is called once per frame void Update() { if (perlinMap == null) { perlinMap = waveMesh.GetComponent<PerlinMap>(); } float x = this.transform.position.x; float z = this.transform.position.z; //float currentHeight = this.transform.position.y; targetHeight = perlinMap.GetHeight(x, z) + 0.065f; float NHeight = perlinMap.GetHeight(x, z+length/2) + 0.065f; float EHeight = perlinMap.GetHeight(x+width/2, z) + 0.065f; float SHeight = perlinMap.GetHeight(x, z-length/2) + 0.065f; float WHeight = perlinMap.GetHeight(x-width/2, z) + 0.065f; waveMesh.transform.position = new Vector3(x, waveMesh.transform.position.y, z); //float newHeight = Mathf.Lerp(currentHeight, targetHeight, Time.deltaTime * lerpFactor); //this.transform.position = new Vector3(x, targetHeight, z); pointN = this.transform.position + this.transform.forward*length/2; pointN = new Vector3(pointN.x, NHeight, pointN.z); pointS = this.transform.position - this.transform.forward*length/2; pointS = new Vector3(pointS.x, SHeight, pointS.z); pointE = this.transform.position + this.transform.right*width/2; pointE = new Vector3(pointE.x, EHeight, pointE.z); pointW = this.transform.position - this.transform.right*width/2; pointW = new Vector3(pointW.x, WHeight, pointW.z); Vector3 NSVector = (pointN - pointS).normalized; Vector3 EWVector = (pointE - pointW).normalized; zAngle = AngleSigned(this.transform.forward, NSVector, this.transform.right); xAngle = AngleSigned(this.transform.right, EWVector, this.transform.forward); //float xAngle = AngleSigned(this.transform.right, xVector.normalized, this.transform.forward); //float zAngle = AngleSigned(this.transform.forward, zVector.normalized, this.transform.right); Debug.DrawLine(pointW, pointE, Color.red); Debug.DrawLine(pointS, pointN, Color.blue); //Vector3 targetRotation = new Vector3(zAngle, this.transform.rotation.y, xAngle); //Quaternion qX = Quaternion.AngleAxis(xAngle, this.transform.forward); //Quaternion qZ = Quaternion.AngleAxis(zAngle, this.transform.right); //this.transform.localRotation = qX * qZ; //this.transform.Rotate(this.transform.forward, xAngle - this.transform.eulerAngles.x); //this.transform.Rotate(this.transform.right, zAngle - this.transform.eulerAngles.z); //Debug.Log(zAngle); }