public void Start() { TaskUtils.SetGlobalMultithreading(false); var heightTex = SavingFileManager.LoadPngTextureFromFile(@"C:\inz\cont\smallCut.png", 240, 240, TextureFormat.RGBA32, true, true); _currentHeightmap = HeightmapUtils.CreateHeightmapArrayFromTexture(heightTex); MyArrayUtils.Normalize(_currentHeightmap.HeightmapAsArray); MyArrayUtils.InvertNormalized(_currentHeightmap.HeightmapAsArray); //for (int x = 0; x < 40; x++) //{ // for (int y = 0; y < 40; y++) // { // _currentHeightmap.HeightmapAsArray[90+x, 90+y] = 0; // } //} CreateComparisionObject(); MyArrayUtils.Multiply(_currentHeightmap.HeightmapAsArray, 400); Mei_RenderComputeShader(); }
public void Start_Mai() { var heightTexture1 = SavingFileManager.LoadPngTextureFromFile(@"C:\inz\cont\smallCut.png", 240, 240, TextureFormat.RGBA32, true, true); var heightmap1 = HeightmapUtils.CreateHeightmapArrayFromTexture(heightTexture1); //DiamondSquareCreator creator = new DiamondSquareCreator(new RandomProvider(22)); //var heightmap2 = creator.CreateDiamondSquareNoiseArray(new IntVector2(240, 240), 64); //MyArrayUtils.Multiply(heightmap2.HeightmapAsArray, 0.1f); HeightmapArray workingHeightmap = LoadHeightmapFromTextureFile(@"C:\inz\cont\temp3.png"); MyArrayUtils.Normalize(workingHeightmap.HeightmapAsArray); MyArrayUtils.InvertNormalized(workingHeightmap.HeightmapAsArray); var generatedArrays = GenerateErodedArrays(workingHeightmap, new List <MeiHydraulicEroderConfiguration>() { new MeiHydraulicEroderConfiguration() { StepCount = 10, A_PipeCrossSection = 0.00005f, ConstantWaterAdding = 1 / 16f, GravityAcceleration = 9.81f, DeltaT = 1 / 60f, DepositionConstant = 0.0001f * 12 * 10f, DissolvingConstant = 0.0001f * 12 * 10f, EvaporationConstant = 0.00011f * 0.5f * 10, GridSize = new Vector2(1, 1), L_PipeLength = 1, SedimentCapacityConstant = 25000 }, }); CreateTestObject(workingHeightmap, generatedArrays); }
private Texture2D GenerateNormalTexture(HeightmapArray array) { var normalArray = _normalArrayGenerator.GenerateNormalArray(array, 10f); var normalAsTexture = NormalUtils.CreateTextureFromNormalArray(normalArray); return(normalAsTexture); }
public void Start() { TaskUtils.SetGlobalMultithreading(false); //DiamondSquareCreator creator = new DiamondSquareCreator(new RandomProvider(22)); //_currentHeightmap = creator.CreateDiamondSquareNoiseArray(_sizeOfTexture, 64); //MyArrayUtils.Multiply(_currentHeightmap.HeightmapAsArray, 0.1f); var heightTex = SavingFileManager.LoadPngTextureFromFile(@"C:\inz\cont\smallCut.png", 240, 240, TextureFormat.RGBA32, true, true); _currentHeightmap = HeightmapUtils.CreateHeightmapArrayFromTexture(heightTex); MyArrayUtils.Normalize(_currentHeightmap.HeightmapAsArray); MyArrayUtils.InvertNormalized(_currentHeightmap.HeightmapAsArray); CreateComparisionObject(); //MyArrayUtils.Multiply(_currentHeightmap.HeightmapAsArray, 400); //float[,] newHeightArray = new float[256,256]; //for (int x = 0; x < 256; x++) //{ // for (int y = 0; y < 256; y++) // { // var distance = (new Vector2(x,y) - new Vector2(128, 128)).magnitude; // newHeightArray[x, y] = distance; // } //} //MyArrayUtils.Normalize(newHeightArray); //_currentHeightmap = new HeightmapArray(newHeightArray); //Hydraulic_RenderComputeShader(); Thermal_RenderComputeShader(); //Hydraulic_RenderComputeShader(); }
public HeightmapBundle GenerateBundle(HeightmapArray inputArray, int mipCount) { List <OneLevelHeightmapPack> packList = new List <OneLevelHeightmapPack>(); var timer = new Stopwatch(); timer.Start(); packList.Add(new OneLevelHeightmapPack( heightmapArray: inputArray, heightmapTexture: HeightmapUtils.CreateTextureFromHeightmap(inputArray), normalTexture: GenerateNormalTexture(inputArray))); timer.Stop(); UnityEngine.Debug.Log("T78 executed bundle " + 1 + " took " + timer.ElapsedMilliseconds); timer.Reset(); for (int i = 1; i < mipCount; i++) { timer.Start(); var divisor = ((int)Mathf.Pow(2, i)); var currentWidth = inputArray.Width / divisor; var currentHeight = inputArray.Height / divisor; var simplifiedMap = simplyfyer.SimplyfyHeightmapNoMargins(inputArray, currentWidth, currentHeight); var heightmapAsTexture = HeightmapUtils.CreateTextureFromHeightmap(simplifiedMap); var normalTexture = GenerateNormalTexture(simplifiedMap); packList.Add(new OneLevelHeightmapPack(simplifiedMap, heightmapAsTexture, normalTexture)); timer.Stop(); UnityEngine.Debug.Log("T78 executed bundle " + i + " took " + timer.ElapsedMilliseconds); timer.Reset(); } return(new HeightmapBundle(packList, inputArray.Width)); }
public void AddElementArray(HeightmapArray heightmap, int textureIndex) { var colorArray = HeightmapUtils.CreateHeightTextureArray(heightmap); _textureArray.SetPixels(colorArray, textureIndex); _addedTexturesCount++; }
public HeightmapArray SimplyfyHeightmapNoMargins(HeightmapArray heightmap, int newWidth, int newHeight) { Preconditions.Assert(!heightmap.HasMargin, "heightmap Does Have margins"); float[,] newHeightmap = SimplyfyByBlockAverageNoMargins(heightmap.HeightmapAsArray, newWidth, newHeight); return(new HeightmapArray(newHeightmap)); }
public HeightmapArray SimplyfyHeightmap(HeightmapArray heightmap, int newWorkingWidth, int newWorkingHeight) { Preconditions.Assert(heightmap.HasMargin, "heightmap Does Not Have margins"); float[,] newHeightmap = SimplyfyByBlockAverageIgnoreMergeMargins(heightmap.HeightmapAsArray, newWorkingWidth, newWorkingHeight); return(new HeightmapArray(newHeightmap)); }
public static HeightmapArray CutSubArray(HeightmapArray input, IntArea area) { float[,] newArray = new float[area.Width, area.Height]; float[,] oldArray = input.HeightmapAsArray; for (int y = 0; y < area.Height; y++) { Array.Copy(oldArray, (y + area.Y) * input.Width + area.X, newArray, y * area.Width, area.Width); } return(new HeightmapArray(newArray)); }
public List <Submap> Slice(HeightmapArray input, List <SubmapPosition> slicingPositions, int inputLodFactor) { return (slicingPositions.Select( pos => new Submap( new HeightmapArray(GetSubarrayWithEmptyMargins(input.HeightmapAsArray, pos.DownLeftX, pos.DownLeftY, pos.Width, pos.Height)), pos, inputLodFactor)).ToList()); }
public static HeightmapArray Multiply(float multiplier, HeightmapArray inputArray) { float[,] newArray = new float[inputArray.Width, inputArray.Height]; for (int x = 0; x < inputArray.Width; x++) { for (int y = 0; y < inputArray.Height; y++) { newArray[x, y] = inputArray.HeightmapAsArray[x, y] * multiplier; } } return(new HeightmapArray(newArray)); }
public static HeightmapArray AddConstant(float valueToAdd, HeightmapArray inputArray) { float[,] newArray = new float[inputArray.Width, inputArray.Height]; for (int x = 0; x < inputArray.Width; x++) { for (int y = 0; y < inputArray.Height; y++) { newArray[x, y] = inputArray.HeightmapAsArray[x, y] + valueToAdd; } } return(new HeightmapArray(newArray)); }
public HeightmapArray CreateRing1HeightmapArray(HeightmapArray inputHeightmap) { assertInputHeightmapHasProperSize(inputHeightmap); var inputTexture = HeightmapUtils.CreateTextureFromHeightmap(inputHeightmap); // input texture is 256x256 (5.7x5.7) p22.3m var conventionalRing1Texture = RenderConventionalRing1Texture(inputTexture, new UniformsPack()); var heightmapArray = HeightmapUtils.CreateHeightmapArrayFromTexture(conventionalRing1Texture); DiamondSquareCreator diamondSquareCreator = new DiamondSquareCreator(new RandomProvider()); heightmapArray = diamondSquareCreator.AddDetail(heightmapArray); return(heightmapArray); }
public static void SaveToFile(String path, HeightmapArray array) { using (BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Create))) { for (int x = 0; x < array.Width; x++) { for (int y = 0; y < array.Height; y++) { writer.Write(array.HeightmapAsArray[x, y]); } } } }
public static Color[] CreateHeightTextureArray(HeightmapArray inputHeightmap) { Color[] colorArray = new Color[inputHeightmap.Width * inputHeightmap.Height]; for (int y = 0; y < inputHeightmap.Height; y++) { for (int x = 0; x < inputHeightmap.Width; x++) { float pixelHeight = inputHeightmap.GetHeight(x, y); colorArray[y * inputHeightmap.Height + x] = HeightColorTransform.EncodeHeight(pixelHeight); } } return(colorArray); }
public static HeightmapArray SmoothHeightChanges(HeightmapArray afterGenerationHeightmapArray) { var heightArray = afterGenerationHeightmapArray.HeightmapAsArray; var smoothedHeightArray = new float[heightArray.GetLength(0), heightArray.GetLength(1)]; var smoothingFactors = new SmoothingFactorData[heightArray.GetLength(0), heightArray.GetLength(1)]; int xStartPoint = 0; int xEndPoint = 0; for (int y = 0; y < heightArray.GetLength(1); y++) { float lastValue = 0; int flatnessStartIndex = 0; float flasnessStartValue = 0; for (int x = 0; x < heightArray.GetLength(0); x++) { float currentValue = heightArray[x, y]; if (Math.Abs(currentValue - lastValue) > 0.001f) { for (int nx = flatnessStartIndex + 1; nx < x; x++) { if (smoothingFactors[nx, y] == null) { smoothingFactors[nx, y] = new SmoothingFactorData(); } smoothingFactors[nx, y].SetXSmoothingStart(flasnessStartValue, flatnessStartIndex); smoothingFactors[nx, y].SetXSmoothingEnd(currentValue, x); } flatnessStartIndex = x; flasnessStartValue = currentValue; } } } for (int x = 0; x < heightArray.GetLength(0); x++) { for (int y = 0; y < heightArray.GetLength(1); y++) { if (smoothingFactors[x, y] != null) { smoothedHeightArray[x, y] = smoothingFactors[x, y].CalculateSmoothedHeight(x, y); } else { smoothedHeightArray[x, y] = heightArray[x, y]; } } } return(new HeightmapArray(smoothedHeightArray)); }
CreateHeightmap_TODO_DELETE( HeightmapArray heightmapArray) //input heightmap has 256x256 map, spans 5,7x5,7km p22.3m real life terrain { var tessalationRequirementTextureGenerator = new TessalationRequirementTextureGenerator(); var tessalationReqTexture = tessalationRequirementTextureGenerator.GenerateTessalationRequirementTexture(heightmapArray, 64); var heightmapBundle = SavingFileManager.LoadHeightmapBundlesFromFiles("heightmapBundle", 4, 2048); var submapTextures = new Ring1SubmapTextures(heightmapBundle, tessalationReqTexture); _rootNode = CreateRootNode_TODO_DELETE(submapTextures); _rootNode.UpdateLod(); }
public static HeightmapArray AddAnotherHeightmap(HeightmapArray inputArray, HeightmapArray addedArray, float addedConstantValue = 0, float multiplier = 1) { Preconditions.Assert(inputArray.Width == addedArray.Width, "Width of both arrays is not equal"); Preconditions.Assert(inputArray.Height == addedArray.Height, "Height of both arrays is not equal"); float[,] newArray = new float[inputArray.Width, inputArray.Height]; for (int x = 0; x < inputArray.Width; x++) { for (int y = 0; y < inputArray.Height; y++) { newArray[x, y] = inputArray.HeightmapAsArray[x, y] + (addedArray.HeightmapAsArray[x, y] * multiplier) + addedConstantValue; } } return(new HeightmapArray(newArray)); }
public static Texture2D CreateTextureFromHeightmap(HeightmapArray inputHeightmap) { var inputTexture = new Texture2D(inputHeightmap.Width, inputHeightmap.Height, TextureFormat.RGBA32, false); Color[] textureArray = new Color[inputHeightmap.Width * inputHeightmap.Height]; for (int y = 0; y < inputHeightmap.Height; y++) { for (int x = 0; x < inputHeightmap.Width; x++) { float pixelHeight = inputHeightmap.GetHeight(x, y); textureArray[y * inputHeightmap.Height + x] = //new Color(0.6f, 0.6f, 0.6f, 0.6f); HeightColorTransform.EncodeHeight(pixelHeight); } } inputTexture.SetPixels(textureArray); inputTexture.Apply(); return(inputTexture); }
public HeightmapArray AddDetail(HeightmapArray inputArray) { ExtendedArray extendedArray = new ExtendedArray(inputArray.Width, inputArray.Height); InitializeArray(extendedArray); iterationIndex = 1; for (int sideLength = extendedArray.SideLength() - 1; sideLength >= 2; sideLength /= 2) { iterationIndex++; DiamondStep(extendedArray, sideLength); SquareStep(extendedArray, sideLength); } var outArray = extendedArray.getOriginalSizedArray(); //NormalizeArray(outArray); return(new HeightmapArray(outArray)); }
public void AddArray(string name, HeightmapArray array, int stepNo) { if (!_arraysDict.ContainsKey(name)) { _arraysDict[name] = new List <HeightmapArray>(); } var clone = MyArrayUtils.DeepClone(array.HeightmapAsArray); //for (int x = 10; x < 10 + stepNo * 5; x++) //{ // for (int y = 10; y < 10 + stepNo * 5; y++) // { // clone[x, y] = array.HeightmapAsArray[10, 10]; // } //} var copyArray = new HeightmapArray(clone); _arraysDict[name].Add(copyArray); }
public Ring1NodeDirectHeightTerrain( Ring1Node ring1Node, Ring1VisibilityTextureChangeGrabber ring1VisibilityTextureChangeGrabber, Texture2D visibilityTexture, GameObject terrainParentObject, UnityCoordsCalculator coordsCalculator, Ring1PaintingOrderGrabber orderGrabber, TerrainShapeDbProxy terrainShapeDb, HeightmapArray globalHeightmap, MeshGeneratorUTProxy meshGenerator ) : base(ring1Node, ring1VisibilityTextureChangeGrabber, orderGrabber, terrainParentObject) { _visibilityTexture = visibilityTexture; _coordsCalculator = coordsCalculator; _globalHeightmap = globalHeightmap; _meshGenerator = meshGenerator; _terrainShapeDb = terrainShapeDb; }
public static HeightmapArray Resize(HeightmapArray inputHeightmap, int newWidth, int newHeight) //todo delete { if (inputHeightmap.Width == newWidth && inputHeightmap.Height == newHeight) { return(inputHeightmap); } Preconditions.Assert(newWidth % inputHeightmap.Width == 0, "New width is not multiplication of old width"); Preconditions.Assert(newHeight % inputHeightmap.Height == 0, "New height is not multiplication of old width"); float[,] oldHeightmap = inputHeightmap.HeightmapAsArray; float[,] newHeightmap = new float[newWidth, newHeight]; for (int x = 0; x < inputHeightmap.Width; x++) //todo pararelization { for (int y = 0; y < inputHeightmap.Height; y++) { } } throw new NotImplementedException("TODO"); }
public static Texture2D CreateTextureFromHeightmap_OLD(HeightmapArray inputHeightmap) { var inputTexture = new Texture2D(inputHeightmap.EvenWidth, inputHeightmap.EvenHeight, TextureFormat.RFloat, false); float[] textureArray = new float[inputHeightmap.EvenWidth * inputHeightmap.EvenHeight]; for (int y = 0; y < inputHeightmap.EvenHeight; y++) { for (int x = 0; x < inputHeightmap.EvenWidth; x++) { float pixelHeight = inputHeightmap.GetHeight(x, y); textureArray[y * inputHeightmap.EvenHeight + x] = pixelHeight; } } byte[] rawTextureArray = new byte[sizeof(float) * (inputHeightmap.EvenWidth * inputHeightmap.EvenHeight)]; System.Buffer.BlockCopy(textureArray, 0, rawTextureArray, 0, rawTextureArray.Length); inputTexture.LoadRawTextureData(rawTextureArray); inputTexture.Apply(); return(inputTexture); }
public List <HeightmapArray> GenerateErodedArrays(HeightmapArray baseArray, List <MeiHydraulicEroderConfiguration> configurations) { var msw = new MyStopWatch(); List <HeightmapArray> outArray = new List <HeightmapArray>(); int i = 0; foreach (var aConfiguration in configurations) { var copyArray = MyArrayUtils.DeepClone(baseArray.HeightmapAsArray); var currentHeightArray = new SimpleHeightArray(copyArray); msw.StartSegment("eroding-" + i); var eroder = new MeiHydraulicEroder(); eroder.Erode(currentHeightArray, aConfiguration); msw.StopSegment(); outArray.Add(SimpleHeightArray.ToHeightmap(currentHeightArray)); i++; } Debug.Log("T22: " + msw.CollectResults()); return(outArray); }
public static UpdatedTerrainTextures GenerateDebugUpdatedTerrainTextures() { var height = 1; var heightArray = new float[12, 12]; for (int x = 0; x < 12; x++) { for (int y = 0; y < 12; y++) { heightArray[x, y] = height; } } var ha = new HeightmapArray(heightArray); var encodedHeightTex = HeightmapUtils.CreateTextureFromHeightmap(ha); var transformer = new TerrainTextureFormatTransformator(new CommonExecutorUTProxy()); var plainHeightTex = transformer.EncodedHeightTextureToPlain(TextureWithSize.FromTex2D(encodedHeightTex)); var normalArray = new Vector3[12, 12]; for (int x = 0; x < 12; x++) { for (int y = 0; y < 12; y++) { normalArray[x, y] = new Vector3(0.0f, 0.0f, 1.0f).normalized; } } var normalTexture = HeightmapUtils.CreateNormalTexture(normalArray); return(new UpdatedTerrainTextures() { HeightTexture = plainHeightTex, NormalTexture = normalTexture, TextureCoords = new MyRectangle(0, 0, 1, 1), TextureGlobalPosition = new MyRectangle(0, 0, 90, 90), }); }
public SubmapPreparingOutput PrepareSubmaps(HeightmapArray globalHeightArray, List <SubmapPreparmentOrder> preparmentOrders) { var slicedSubmaps = _slicer.Slice(globalHeightArray, preparmentOrders.Select(c => c.Position).ToList(), 0); var submaps = slicedSubmaps.Select( (submap, index) => _simplyfyer.SimplyfySubmap(submap, preparmentOrders[index].LodFactor)).ToList(); _merger.MergeMargins(submaps); var output = new SubmapPreparingOutput( submaps.Select((submap, index) => new { submap, index }) .Where(e => preparmentOrders[e.index].RingNumber == 0) .Select(e => e.submap) .ToList(), submaps.Select((submap, index) => new { submap, index }) .Where(e => preparmentOrders[e.index].RingNumber == 1) .Select(e => e.submap) .ToList() ); return(output); }
private void CreateTestObject(HeightmapArray baseArray, List <HeightmapArray> otherArrays) { _go = GameObject.CreatePrimitive(PrimitiveType.Quad); var material = new Material(Shader.Find("Custom/Terrain/Terrain_Debug_Comparision")); _go.GetComponent <MeshRenderer>().material = material; _go.name = "Terrain"; _go.transform.localRotation = Quaternion.Euler(0, 0, 0); _go.transform.localScale = new Vector3(10, 1, 10); _go.transform.localPosition = new Vector3(0, 0, 0); _go.GetComponent <MeshFilter>().mesh = PlaneGenerator.CreateFlatPlaneMesh(240, 240); MyHeightTextureArray heightTextureArray = new MyHeightTextureArray(240, 240, otherArrays.Count + 1, TextureFormat.ARGB32, false, true); heightTextureArray.AddElementArray(baseArray, 0); for (int i = 0; i < otherArrays.Count; i++) { heightTextureArray.AddElementArray(otherArrays[i], i + 1); } _go.GetComponent <MeshRenderer>().material .SetTexture("_HeightmapTexArray", heightTextureArray.ApplyAndRetrive()); }
public void AddElementArray(HeightmapArray heightmap) { AddElementArray(heightmap, _addedTexturesCount); }
public OneLevelHeightmapPack(HeightmapArray heightmapArray, Texture2D heightmapTexture, Texture2D normalTexture) { this._heightmapArray = heightmapArray; _heightmapTexture = heightmapTexture; _normalTexture = normalTexture; }