コード例 #1
0
        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);
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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
            };
        }
コード例 #4
0
        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));
        }
コード例 #5
0
        public async Task <TextureWithSize> GenerateNormalDetailElementAsync(MyRectangle requestedArea,
                                                                             TerrainCardinalResolution resolution, RequiredCornersMergeStatus cornersMergeStatus)
        {
            var baseHeightTextureOutput =
                await _baseTerrainDetailProvider.RetriveTerrainDetailAsync(
                    TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY, requestedArea, resolution, cornersMergeStatus);

            var baseHeightTexture = baseHeightTextureOutput.TokenizedElement.DetailElement;

            IntVector2   outTextureSize = TerrainShapeUtils.RetriveTextureSize(requestedArea, resolution);
            UniformsPack pack           = new UniformsPack();

            float heightMultiplier = 0;

            if (resolution == TerrainCardinalResolution.MIN_RESOLUTION)
            {
                heightMultiplier = 1.25f;
            }
            else if (resolution == TerrainCardinalResolution.MID_RESOLUTION)
            {
                heightMultiplier = 10f;
            }
            else
            {
                heightMultiplier = 80f;
            }

            pack.SetTexture("_HeightmapTex", baseHeightTexture.Texture.Texture);
            //pack.SetUniform("_HeightMultiplier", heightMultiplier);
            pack.SetUniform("_HeightMultiplier", 1);
            pack.SetUniform("_GlobalCoords", requestedArea.ToVector4());
            ConventionalTextureInfo outTextureInfo =
                new ConventionalTextureInfo(outTextureSize.X, outTextureSize.Y, TextureFormat.ARGB32, true);

            var renderCoords =
                TerrainShapeUtils.ComputeUvOfSubElement(requestedArea, baseHeightTexture.DetailArea);

            TextureRenderingTemplate template = new TextureRenderingTemplate()
            {
                CanMultistep        = false,
                Coords              = renderCoords,
                OutTextureInfo      = outTextureInfo,
                RenderTextureFormat = RenderTextureFormat.ARGB32,
                ShaderName          = "Custom/Terrain/NormalmapGenerator",
                UniformPack         = pack,
                CreateTexture2D     = false
            };
            var outTex = await _rendererProxy.AddOrder(template);

            await _commonExecutor.AddAction(() => outTex.filterMode = FilterMode.Trilinear);

            await _baseTerrainDetailProvider.RemoveTerrainDetailAsync(baseHeightTextureOutput.TokenizedElement.Token);

            //SavingFileManager.SaveTextureToPngFile(@"C:\temp\norm1.png", outTex as Texture2D);

            return(new TextureWithSize()
            {
                Size = new IntVector2(outTextureSize.X, outTextureSize.Y),
                Texture = outTex
            });
        }
コード例 #6
0
        private async Task <TextureWithSize> RetriveFoundationTextureAsync(MyRectangle requestedArea,
                                                                           TerrainCardinalResolution resolution, RequiredCornersMergeStatus cornersMergeStatus)
        {
            Texture     fundationTexture = null;
            MyRectangle renderCoords     = null;

            TerrainDetailElementToken retrivedElementToken = null;

            if (resolution == TerrainCardinalResolution.MIN_RESOLUTION)
            {
                fundationTexture = _fullFundationTextureData.Texture;
                renderCoords     =
                    TerrainShapeUtils.ComputeUvOfSubElement(requestedArea, _fullFundationTextureData.Coords);
            }
            else
            {
                var lowerResolution    = resolution.LowerResolution;
                var fundationQueryArea = TerrainShapeUtils.GetAlignedTerrainArea(requestedArea, lowerResolution,
                                                                                 _configuration.TerrainDetailImageSideDisjointResolution);

                var foundationOutput = await _baseTerrainDetailProvider.RetriveTerrainDetailAsync(
                    TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY, fundationQueryArea, lowerResolution, cornersMergeStatus);

                retrivedElementToken = foundationOutput.TokenizedElement.Token;
                var fundationDetailElement = foundationOutput.TokenizedElement.DetailElement;

                renderCoords =
                    TerrainShapeUtils.ComputeUvOfSubElement(requestedArea, fundationDetailElement.DetailArea);
                fundationTexture = fundationDetailElement.Texture.Texture;
                await _commonExecutor.AddAction(() => fundationTexture.filterMode = FilterMode.Bilinear);
            }

            IntVector2   outTextureSize = TerrainShapeUtils.RetriveTextureSize(requestedArea, resolution);
            UniformsPack pack           = new UniformsPack();

            pack.SetTexture("_SourceTexture", fundationTexture);
            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/TerGen/Cutting",
                UniformPack         = pack,
                CreateTexture2D     = false
            };
            var outTex = await _rendererProxy.AddOrder(template);

            await _commonExecutor.AddAction(() => outTex.wrapMode = TextureWrapMode.Clamp);

            if (retrivedElementToken != null)
            {
                await _baseTerrainDetailProvider.RemoveTerrainDetailAsync(retrivedElementToken);
            }

            return(new TextureWithSize()
            {
                Size = outTextureSize,
                Texture = outTex
            });
        }
コード例 #7
0
        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
            };
        }