private async Task <Texture> GenerateTextureWithMipMapsAsync(TextureWithSize tex) { IntVector2 outTextureSize = tex.Size; UniformsPack pack = new UniformsPack(); pack.SetTexture("_SourceTexture", tex.Texture); ConventionalTextureInfo outTextureInfo = new ConventionalTextureInfo(outTextureSize.X, outTextureSize.Y, TextureFormat.ARGB32, true); TextureRenderingTemplate template = new TextureRenderingTemplate() { CanMultistep = false, Coords = new MyRectangle(0, 0, 1, 1), OutTextureInfo = outTextureInfo, RenderTextureFormat = RenderTextureFormat.RFloat, ShaderName = "Custom/TerGen/Cutting", UniformPack = pack, CreateTexture2D = false, RenderTextureMipMaps = true }; var outTex = await _rendererProxy.AddOrder(template); await _commonExecutor.AddAction(() => outTex.wrapMode = TextureWrapMode.Clamp); return(outTex); }
public async Task <TextureWithSize> ExtractMipmapAsync(TextureWithSize inputTexture, RenderTextureFormat format, int mipmapLevelToExtract) { var pack = new UniformsPack(); pack.SetTexture("_InputTexture", inputTexture.Texture); pack.SetUniform("_MipmapLevelToExtract", mipmapLevelToExtract); var outSize = ComputeMipmappedOutSize(inputTexture.Size, mipmapLevelToExtract); var newTexture = await _textureRenderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = true, Coords = new MyRectangle(0, 0, 1, 1), CreateTexture2D = false, OutTextureInfo = new ConventionalTextureInfo(outSize.X, outSize.Y, TextureFormat.ARGB32, true), RenderTextureFormat = format, RenderTextureMipMaps = true, ShaderName = "Custom/Tool/ExtractMipmap", UniformPack = pack, }); return(new TextureWithSize() { Texture = newTexture, Size = outSize }); }
public async Task <TextureWithCoords> ApplyFeatureAsync(TextureWithCoords texture, TerrainCardinalResolution resolution, bool canMultistep) { var configuration = _configurations[resolution]; var detailedHeightmapArray = await TaskUtils .RunInThreadPool( () => { var creator = new DiamondSquareCreator(_randomProviderGenerator.GetRandom()); var initialArray = creator.CreateDiamondSquareNoiseArray( texture.TextureSize, configuration.DiamondSquareWorkingArrayLength); return(initialArray); }); var detailedTexture = await _commonExecutor .AddAction(() => HeightmapUtils.CreateTextureFromHeightmap(detailedHeightmapArray)); UniformsPack pack = new UniformsPack(); pack.SetTexture("_Texture1", texture.Texture); pack.SetTexture("_Texture2", detailedTexture); pack.SetUniform("_Texture2Weight", configuration.DiamondSquareWeight); var renderCoords = new MyRectangle(0, 0, 1, 1); var outTextureSize = texture.TextureSize; ConventionalTextureInfo outTextureInfo = new ConventionalTextureInfo(outTextureSize.X, outTextureSize.Y, TextureFormat.ARGB32, true); TextureRenderingTemplate template = new TextureRenderingTemplate() { CanMultistep = canMultistep, Coords = renderCoords, OutTextureInfo = outTextureInfo, RenderTextureFormat = RenderTextureFormat.RFloat, ShaderName = "Custom/TerrainCreation/DiamondSquareTextureAddingPlain", UniformPack = pack, CreateTexture2D = false }; var renderedTexture = await _rendererProxy.AddOrder(template); await _commonExecutor.AddAction(() => GameObject.Destroy(detailedTexture)); return(new TextureWithCoords(sizedTexture: new TextureWithSize() { Texture = renderedTexture, Size = texture.TextureSize }, coords: texture.Coords)); }
public DebugFlatTerrainShapeDb(UTTextureRendererProxy textureRenderer) { var size = new IntVector2(241, 241); var tex = 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)); } } tex.Apply(); _blankTexture = new TextureWithSize() { Texture = tex, Size = size }; var pack = new UniformsPack(); pack.SetTexture("_HeightmapTex", tex); 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 }; }
public async Task <TextureWithCoords> ApplyFeatureAsync(TextureWithCoords inputTexture, TerrainCardinalResolution resolution, bool CanMultistep = false) { if (!TaskUtils.GetGlobalMultithreading()) { Preconditions.Assert(inputTexture.Texture.width == inputTexture.Texture.height, "Only square inputTextures are supported"); } UniformsPack pack = new UniformsPack(); await _commonExecutor.AddAction(() => inputTexture.Texture.filterMode = FilterMode.Point); pack.SetTexture("_SourceTexture", inputTexture.Texture); pack.SetUniform("_InputGlobalCoords", inputTexture.Coords.ToVector4()); pack.SetUniform("_QuantingResolution", inputTexture.TextureSize.X - 1); var configuration = _configurations[resolution]; pack.SetUniform("_DetailResolutionMultiplier", configuration.DetailResolutionMultiplier); pack.SetUniform("_NoiseStrengthMultiplier", configuration.NoiseStrengthMultiplier); var renderCoords = new MyRectangle(0, 0, 1, 1); var outTextureSize = inputTexture.TextureSize; ConventionalTextureInfo outTextureInfo = new ConventionalTextureInfo(outTextureSize.X, outTextureSize.Y, TextureFormat.ARGB32, true); TextureRenderingTemplate template = new TextureRenderingTemplate() { CanMultistep = false, Coords = renderCoords, OutTextureInfo = outTextureInfo, RenderTextureFormat = RenderTextureFormat.RFloat, ShaderName = "Custom/TerrainCreation/NoiseAddingPlain", UniformPack = pack, CreateTexture2D = false }; return(new TextureWithCoords(sizedTexture: new TextureWithSize() { Texture = await _rendererProxy.AddOrder(template), Size = inputTexture.TextureSize }, coords: inputTexture.Coords)); }
public Texture2D WrapTexture(Texture2D inputTexture) { UniformsPack uniforms = new UniformsPack(); uniforms.SetUniform("_Margin", _configuration.Margin); uniforms.SetTexture("_InputTex", inputTexture); var outSize = (new Vector2(inputTexture.width, inputTexture.height) / (1 + 2 * _configuration.Margin)).ToIntVector2(); outSize = new IntVector2(Mathf.ClosestPowerOfTwo(outSize.X), Mathf.ClosestPowerOfTwo(outSize.Y)); return((Texture2D)_textureRenderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = false, Coords = new MyRectangle(0, 0, 1, 1), CreateTexture2D = true, OutTextureInfo = new ConventionalTextureInfo(outSize.X, outSize.Y, inputTexture.format, false), UniformPack = uniforms, ShaderName = "Custom/NPR/ArtMapWrapping" }).Result); //todo async }
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 }; }
private async Task MergeDetailObject(Dictionary <TerrainDetailCorner, TerrainDetailElement> sourceTextures, TextureWithSize sourceTexture, TextureWithSize outTexture, TerrainDetailCorner activeCorner, TextureWithSize scratchTexture) { var uniforms = new UniformsPack(); var cornersPresent = new Vector4(); var cornersMerged = new Vector4(); int activeCornerIndex = 0; var i = 0; foreach (var corner in TerrainDetailCorner.OrderedDirections) { if (corner == activeCorner) { activeCornerIndex = i; } if (sourceTextures.ContainsKey(corner)) { cornersPresent[i] = 10; if (sourceTextures[corner].CornersMergeStatus == CornersMergeStatus.MERGED) { cornersMerged[i] = 10; } uniforms.SetTexture("_Corner" + GetCornerTexUniformName(corner) + "Tex", sourceTextures[corner].Texture.Texture); } i++; } uniforms.SetUniform("_ActiveCornerIndex", activeCornerIndex); uniforms.SetUniform("_CornersMerged", cornersMerged); uniforms.SetTexture("_ScratchTex", scratchTexture.Texture); uniforms.SetUniform("_MergeMargin", _configuration.MergeMarginSize); // todo await _renderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = false, CreateTexture2D = false, RenderTextureToModify = scratchTexture.Texture as RenderTexture, // todo ShaderName = "Custom/TerrainDetailMerger/MergeIntoScratch", UniformPack = uniforms, RenderingRectangle = new IntRectangle(0, 0, scratchTexture.Size.X, scratchTexture.Size.Y), RenderTargetSize = new IntVector2(scratchTexture.Size.X, scratchTexture.Size.Y), }); IntRectangle renderingRectangle; if (activeCorner == TerrainDetailCorner.BottomLeft) { renderingRectangle = new IntRectangle(0, 0, 121, 121); } else if (activeCorner == TerrainDetailCorner.BottomRight) { renderingRectangle = new IntRectangle(120, 0, 121, 121); } else if (activeCorner == TerrainDetailCorner.TopLeft) { renderingRectangle = new IntRectangle(0, 120, 121, 121); } else if (activeCorner == TerrainDetailCorner.TopRight) { renderingRectangle = new IntRectangle(120, 120, 121, 121); } else { Preconditions.Fail("Unsupported activeCorner " + activeCorner); renderingRectangle = new IntRectangle(0, 0, 1, 1); } await _renderer.AddOrder(new TextureRenderingTemplate() { CanMultistep = false, CreateTexture2D = false, RenderTextureToModify = (RenderTexture)outTexture.Texture, ShaderName = "Custom/TerrainDetailMerger/ScratchToActive", UniformPack = uniforms, RenderingRectangle = renderingRectangle, RenderTargetSize = new IntVector2(241, 241), }); }