public HeightArrayGenerator(TerrainShapeDbProxy terrainShapeDb, TerrainTextureFormatTransformator transformator, TerrainCardinalResolution resolution) { _terrainShapeDb = terrainShapeDb; _transformator = transformator; _resolution = resolution; }
public async Task <TextureWithSize> RetriveHeightDetailElementAsync(string filename) { IntVector2 textureSize = new IntVector2(241, 241); var path = _mainDictionaryPath + filename + _extension; Preconditions.Assert(File.Exists(path), $"Cannot retrive heightDetailElement of path {path} as it does not exist"); { var texture = await _commonExecutor.AddAction(() => { var tex = SavingFileManager.LoadPngTextureFromFile(path, textureSize.X, textureSize.Y, TextureFormat.ARGB32, true, true); tex.wrapMode = TextureWrapMode.Clamp; return(tex); }); var transformator = new TerrainTextureFormatTransformator(_commonExecutor); var plainTexture = await transformator.EncodedHeightTextureToPlainAsync(new TextureWithSize() { Texture = texture, Size = textureSize }); return(new TextureWithSize() { Size = textureSize, Texture = plainTexture }); } }
public void Start() { var heightmapTextureSize = new IntVector2(3600, 3600); var rgbaMainTexture = SavingFileManager.LoadPngTextureFromFile(_filePathsConfiguration.HeightmapFilePath, heightmapTextureSize.X, heightmapTextureSize.Y, TextureFormat.ARGB32, true, false); TerrainTextureFormatTransformator transformator = new TerrainTextureFormatTransformator(_gameInitializationFields.Retrive <CommonExecutorUTProxy>()); var globalHeightTexture = transformator.EncodedHeightTextureToPlain(new TextureWithSize() { Size = heightmapTextureSize, Texture = rgbaMainTexture }); if (true) { _gameInitializationFields.SetField(new TerrainDetailAlignmentCalculator(240)); TerrainDetailGenerator terrainDetailGenerator = CreateTerrainDetailGenerator( globalHeightTexture, _gameInitializationFields.Retrive <UTTextureRendererProxy>(), _gameInitializationFields.Retrive <CommonExecutorUTProxy>(), _gameInitializationFields.Retrive <UnityThreadComputeShaderExecutorObject>(), _gameInitializationFields.Retrive <ComputeShaderContainerGameObject>()); var commonExecutorUtProxy = _gameInitializationFields.Retrive <CommonExecutorUTProxy>(); TerrainDetailProvider terrainDetailProvider = CreateTerrainDetailProvider(terrainDetailGenerator, commonExecutorUtProxy); var terrainDetailFileManager = new TerrainDetailFileManager(_configuration.TerrainDetailCachePath, commonExecutorUtProxy); var terrainShapeDb = CreateTerrainShapeDb(terrainDetailProvider , commonExecutorUtProxy , _gameInitializationFields.Retrive <TerrainDetailAlignmentCalculator>() , _configuration.TerrainShapeDbConfiguration.MergeTerrainDetail , _configuration.TerrainShapeDbConfiguration.UseTextureSavingToDisk , _configuration.TerrainShapeDbConfiguration.UseTextureLoadingFromDisk, terrainDetailFileManager); TerrainShapeDbProxy terrainShapeDbProxy = new TerrainShapeDbProxy(terrainShapeDb); var baseTerrainDetailProvider = BaseTerrainDetailProvider.CreateFrom(terrainShapeDb); _gameInitializationFields.SetField(baseTerrainDetailProvider); terrainDetailGenerator.SetBaseTerrainDetailProvider(baseTerrainDetailProvider); _ultraUpdatableContainer.AddOtherThreadProxy(terrainShapeDbProxy); _gameInitializationFields.SetField(terrainShapeDbProxy); } else { ITerrainShapeDb terrainShapeDbProxy = new DebugSlopedTerrainShapeDb( _gameInitializationFields.Retrive <UTTextureRendererProxy>() ); terrainShapeDbProxy = new RecordingTerrainShapeDb(terrainShapeDbProxy); _gameInitializationFields.SetField((RecordingTerrainShapeDb)terrainShapeDbProxy); } }
private Task <Texture2D> ChangeHeightTextureToTexture2DAsync(Texture inputTexture) { var transformator = new TerrainTextureFormatTransformator(_commonExecutor); return(transformator.PlainToEncodedHeightTextureAsync(new TextureWithSize() { Texture = inputTexture, Size = new IntVector2(241, 241) })); }
private TextureWithSize CreateBaseTexture(float height, DebugTerrainCharacter character) { if (character == DebugTerrainCharacter.Noise) { var texture = new RenderTexture(241, 241, 0, RenderTextureFormat.RFloat); texture.wrapMode = TextureWrapMode.Clamp; texture.filterMode = FilterMode.Point; var material = new Material(Shader.Find("Custom/Tool/FillHeightTextureWithRandomValues")); material.SetTexture("_HeightTexture", texture); Graphics.Blit(texture, (RenderTexture)texture, material); return(new TextureWithSize() { Texture = texture, Size = new IntVector2(241, 241) }); } else { float[,] heightArray; if (character == DebugTerrainCharacter.Flat) { heightArray = MyArrayUtils.CreateFilled(241, 241, height); } else //if (character == DebugTerrainCharacter.Volcano) { heightArray = new float[241, 241]; float minHeight = height * 0.2f; float maxHeight = height; for (int x = 0; x < 241; x++) { for (int y = 0; y < 241; y++) { heightArray[x, y] = Mathf.Lerp(minHeight, maxHeight, 1 - Vector2.Distance(new Vector2(x, y), new Vector2(120, 120)) / 200f); } } } var heightTex = HeightmapUtils.CreateTextureFromHeightmap(new HeightmapArray(heightArray)); var transformator = new TerrainTextureFormatTransformator(new CommonExecutorUTProxy()); var plainHeightTexture = transformator.EncodedHeightTextureToPlain(new TextureWithSize() { Size = new IntVector2(241, 241), Texture = heightTex }); plainHeightTexture.wrapMode = TextureWrapMode.Clamp; return(new TextureWithSize() { Size = new IntVector2(241, 241), Texture = plainHeightTexture }); } }
public void Start() { TaskUtils.SetGlobalMultithreading(false); var rgbaMainTexture = SavingFileManager.LoadPngTextureFromFile(@"C:\inz\cont\n49_e019_1arc_v3.png", 3600, 3600, TextureFormat.ARGB32, true, false); TerrainTextureFormatTransformator transformator = new TerrainTextureFormatTransformator(new CommonExecutorUTProxy()); var mainTexture = transformator.EncodedHeightTextureToPlainAsync(new TextureWithSize() { Size = new IntVector2(3600, 3600), Texture = rgbaMainTexture }).Result; var gameObject = GameObject.CreatePrimitive(PrimitiveType.Quad); var material = new Material(Shader.Find("Custom/Terrain/Ring1")); TerrainAndNormalTexture pair = CreateTerrainAndNormalTexture(mainTexture); material.SetTexture("_HeightmapTex", pair.HeightTexture); material.SetTexture("_NormalmapTex", pair.NormalTexture); gameObject.GetComponent <MeshRenderer>().material = material; gameObject.name = "Terrain"; gameObject.transform.localRotation = Quaternion.Euler(0, 0, 0); var unitySideLength = 10f; var realSideLength = 240 * 24; var metersPerUnit = realSideLength / unitySideLength; if (_terrainResolution == TerrainCardinalResolution.MIN_RESOLUTION) { gameObject.transform.localScale = new Vector3(10, (6500 / metersPerUnit), 10); gameObject.transform.localPosition = new Vector3(0, 0, 0); } else if (_terrainResolution == TerrainCardinalResolution.MID_RESOLUTION) { gameObject.transform.localScale = new Vector3(10, (6500 / metersPerUnit) * 8, 10); gameObject.transform.localPosition = new Vector3(0, 0, 0); } else { gameObject.transform.localScale = new Vector3(10, (6500 / metersPerUnit) * 8 * 8, 10); gameObject.transform.localPosition = new Vector3(0, -30, 0); } gameObject.GetComponent <MeshFilter>().mesh = PlaneGenerator.CreateFlatPlaneMesh(240, 240); }
public HeightArrayDb(TerrainShapeDbProxy terrainShapeDb, CommonExecutorUTProxy commonExecutor) { TerrainTextureFormatTransformator transformator = new TerrainTextureFormatTransformator(commonExecutor); foreach (var resolution in TerrainCardinalResolution.AllResolutions) { SpatialDbConfiguration configuration = new SpatialDbConfiguration() { QueryingCellSize = VectorUtils.FillVector2(TerrainDescriptionConstants.DetailCellSizesPerResolution[resolution]) }; IStoredPartsGenerator <MySimpleArray <float> > partsGenerator = new HeightArrayGenerator(terrainShapeDb, transformator, resolution); _resolutionDbs[resolution] = new SpatialDb <MySimpleArray <float> >(partsGenerator, configuration); } }
private static RenderTexture CreateGlobalHeightTexture(CommonExecutorUTProxy commonExecutorUtProxy) { var rgbaMainTexture = SavingFileManager.LoadPngTextureFromFile(@"C:\mgr\PwMgrProject\precomputedResources\allTerrainF1.png", 3600, 3600, TextureFormat.ARGB32, true, false); TerrainTextureFormatTransformator transformator = new TerrainTextureFormatTransformator(commonExecutorUtProxy); var mirroredImage = transformator.MirrorHeightTexture(new TextureWithSize() { Size = new IntVector2(3600, 3600), Texture = rgbaMainTexture }); var globalHeightTexture = transformator.EncodedHeightTextureToPlain(new TextureWithSize() { Size = new IntVector2(3600, 3600), Texture = mirroredImage }); return(globalHeightTexture); }
public static Texture BaseCreateHeightTexture(Func <int, int, float> heightResolver) { var traditionalHeightMap = new Texture2D(241, 241, TextureFormat.ARGB32, true); for (int x = 0; x < 241; x++) { for (int y = 0; y < 241; y++) { float height = heightResolver(x, y); traditionalHeightMap.SetPixel(x, y, HeightColorTransform.EncodeHeight(height)); } } traditionalHeightMap.Apply(true); var transformator = new TerrainTextureFormatTransformator(new CommonExecutorUTProxy()); return(transformator.EncodedHeightTextureToPlain(new TextureWithSize() { Texture = traditionalHeightMap, Size = new IntVector2(241, 241) })); }
private void TestHeightNormalization() { var db = _gameInitializationFields.Retrive <TerrainShapeDbProxy>(); var translator = GeoCoordsToUnityTranslator.DefaultTranslator; //var unityPosition = translator.TranslateToUnity(new GeoCoordinate(49.613, 19.5510)); var unityPosition = translator.TranslateToUnity(new GeoCoordinate(49.573343, 19.528953)); var queryArea = new MyRectangle(unityPosition.x - 10, unityPosition.y - 10, 20, 20); var uvdHeight = db.Query(new TerrainDescriptionQuery() { QueryArea = queryArea, RequestedElementDetails = new List <TerrainDescriptionQueryElementDetail>() { new TerrainDescriptionQueryElementDetail() { Resolution = TerrainCardinalResolution.MAX_RESOLUTION, Type = TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY } } }).Result.GetElementOfType(TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY); var uvBase = uvdHeight.UvBase; var plainHeightTexture = uvdHeight.TokenizedElement.DetailElement.Texture; var transformator = new TerrainTextureFormatTransformator(new CommonExecutorUTProxy()); var encodedTexture = transformator.PlainToEncodedHeightTextureAsync(plainHeightTexture).Result; var pixelPoint = RectangleUtils.CalculateSubPosition(new MyRectangle(0, 0, 241, 241), uvBase.Center); var intCenterPoint = new IntVector2(Mathf.RoundToInt(pixelPoint.x), Mathf.RoundToInt(pixelPoint.y)); var color = encodedTexture.GetPixel(intCenterPoint.X, intCenterPoint.Y); var height = HeightColorTransform.DecodeHeight(color); Debug.Log("Normalized Height is: " + height); }
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 void Start() { TaskUtils.SetGlobalMultithreading(true); _computeShaderExecutorObject = new UnityThreadComputeShaderExecutorObject(); _commonExecutorUtProxy = new CommonExecutorUTProxy(); _utTextureRendererProxy = new UTTextureRendererProxy(new TextureRendererService( new MultistepTextureRenderer(ContainerGameObject), new TextureRendererServiceConfiguration() { StepSize = new Vector2(500, 500) })); var rgbaMainTexture = SavingFileManager.LoadPngTextureFromFile(@"C:\inz\cont\n49_e019_1arc_v3.png", 3600, 3600, TextureFormat.ARGB32, true, false); TerrainTextureFormatTransformator transformator = new TerrainTextureFormatTransformator(_commonExecutorUtProxy); transformator.EncodedHeightTextureToPlainAsync(new TextureWithSize() { Size = new IntVector2(3600, 3600), Texture = rgbaMainTexture }).ContinueWith(x => { var mainTexture = x.Result; TerrainDetailGenerator generator = CreateTerrainDetailGenerator(new TextureWithSize() { Size = new IntVector2(3600, 3600), Texture = mainTexture }); TerrainDetailProvider terrainDetailProvider = CreateTerrainDetailProvider(generator); var db = FETerrainShapeDbInitialization.CreateTerrainShapeDb(terrainDetailProvider, _commonExecutorUtProxy, new TerrainDetailAlignmentCalculator(240), false, false, false, null); MyRectangle queryArea = null; if (_terrainResolution == TerrainCardinalResolution.MIN_RESOLUTION) { queryArea = new MyRectangle(0, 0, 24 * 240, 24 * 240); } else if (_terrainResolution == TerrainCardinalResolution.MID_RESOLUTION) { queryArea = new MyRectangle(3 * 240, 3 * 240, 3 * 240, 3 * 240); } else { queryArea = new MyRectangle(5 * 0.375f * 240, 5 * 0.375f * 240, 0.375f * 240, 0.375f * 240); } var outputOfGeneration = db.QueryAsync(new TerrainDescriptionQuery() { QueryArea = queryArea, RequestedElementDetails = new List <TerrainDescriptionQueryElementDetail>() { new TerrainDescriptionQueryElementDetail() { Resolution = _terrainResolution.LowerResolution, Type = TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY }, new TerrainDescriptionQueryElementDetail() { Resolution = _terrainResolution, Type = TerrainDescriptionElementTypeEnum.NORMAL_ARRAY }, } }); outputOfGeneration.ContinueWith(c => { GeneratedTerrainElements elem = new GeneratedTerrainElements(); elem.HeightElement = c.Result.GetElementOfType(TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY); elem.NormalElement = c.Result.GetElementOfType(TerrainDescriptionElementTypeEnum.NORMAL_ARRAY); _generatedElements.Add(elem); }).ContinueWith(q => { Debug.Log("Error: Executing task"); Debug.Log("Error descr is " + q.Exception); }, TaskContinuationOptions.OnlyOnFaulted ); }); }
public DebugSlopedTerrainShapeDb(UTTextureRendererProxy textureRenderer) { var size = new IntVector2(241, 241); var tex = new Texture2D(size.X, size.Y, TextureFormat.ARGB32, true, true); var encodedHeightTex = new Texture2D(size.X, size.Y, TextureFormat.ARGB32, true, true); for (int x = 0; x < size.X; x++) { for (int y = 0; y < size.Y; y++) { tex.SetPixel(x, y, new Color(0, 0, 0, 0)); //var distanceToCenter = 1 - (Vector2.Distance(new Vector2(120, 120), new Vector2(x, y)) / // Mathf.Sqrt(120 * 120)) / 2; //encodedHeightTex.SetPixel(x, y, HeightColorTransform.EncodeHeight(distanceToCenter / 100)); //var distanceToCenter = Mathf.Clamp01( // Mathf.Min( // Mathf.Abs(y - 100) / 50.0f, // Mathf.Abs(x - 100) / 50.0f) // ) / 300; //encodedHeightTex.SetPixel(x, y, HeightColorTransform.EncodeHeight(distanceToCenter)); var heightInUnits = HeightDenormalizer.Default.Normalize(5); var encodedHeight = HeightColorTransform.EncodeHeight(heightInUnits); encodedHeightTex.SetPixel(x, y, encodedHeight); } } tex.Apply(); encodedHeightTex.Apply(); _blankTexture = new TextureWithSize() { Texture = tex, Size = size }; var transformator = new TerrainTextureFormatTransformator(new CommonExecutorUTProxy()); var encodedHeightTexture = new TextureWithSize() { Texture = encodedHeightTex, Size = new IntVector2(241, 241) }; var plainTex = transformator.EncodedHeightTextureToPlain(encodedHeightTexture); _heightTexture = new TextureWithSize() { Size = new IntVector2(241, 241), Texture = plainTex }; var pack = new UniformsPack(); pack.SetTexture("_HeightmapTex", plainTex); pack.SetUniform("_HeightMultiplier", 80); ConventionalTextureInfo outTextureInfo = new ConventionalTextureInfo(size.X, size.Y, TextureFormat.ARGB32, true); var renderCoords = new MyRectangle(0, 0, 1, 1); TextureRenderingTemplate template = new TextureRenderingTemplate() { CanMultistep = false, Coords = renderCoords, OutTextureInfo = outTextureInfo, RenderTextureFormat = RenderTextureFormat.ARGB32, ShaderName = "Custom/Terrain/NormalmapGenerator", UniformPack = pack, CreateTexture2D = false }; var outNormalTex = textureRenderer.AddOrder(template).Result; _normalTexture = new TextureWithSize() { Size = size, Texture = outNormalTex }; }