public async Task <TextureWithSize> GenerateHeightDetailElementAsync(MyRectangle requestedArea,
                                                                             TerrainCardinalResolution resolution, RequiredCornersMergeStatus cornersMergeStatus)
        {
            var textureWithSize = await RetriveFoundationTextureAsync(requestedArea, resolution, cornersMergeStatus);

            var workTexture = new TextureWithCoords(sizedTexture: textureWithSize, coords: requestedArea);

            foreach (var applier in _featureAppliers.Where(c => c.AvalibleResolutions.Contains(resolution))
                     .OrderBy(c => c.Rank))
            {
                workTexture = await applier.Applier.ApplyFeatureAsync(workTexture, resolution, false); //todo

                var localTexture = workTexture;
                await _commonExecutor.AddAction(() => localTexture.Texture.wrapMode = TextureWrapMode.Clamp);
            }

            var textureWithMipMaps = await GenerateTextureWithMipMapsAsync(new TextureWithSize()
            {
                Size    = textureWithSize.Size,
                Texture = workTexture.Texture
            });

            return(new TextureWithSize()
            {
                Size = textureWithSize.Size,
                Texture = textureWithMipMaps
            });
        }
 public Task <int> CalculateMemoryUsage(NullableESurfaceTexturesPack entity)
 {
     if (entity.Pack == null)
     {
         return(TaskUtils.MyFromResult(0));
     }
     return(_commonExecutor.AddAction(() => entity.Pack.MainTexture.width * entity.Pack.MainTexture.height * 4 * 2));
 }
Esempio n. 3
0
        public async Task <ESurfaceTexturesPack> ProvideSurfaceDetailAsync(MyRectangle inGamePosition, FlatLod flatLod)
        {
            var devisedPatches = _patchesCreator.CreatePatchAsync(inGamePosition.ToRectangle(), flatLod.ScalarValue).Result;

            Preconditions.Assert(devisedPatches.Count <= 1,
                                 $"More than one patches created: {devisedPatches.Count}, rect is {inGamePosition}");
            if (!devisedPatches.Any())
            {
                return(null);
            }

            Preconditions.Assert(devisedPatches.Count == 1, "There are more than one devised patch. Exacly " + devisedPatches.Count);
            var onlyPatch    = devisedPatches.First();
            var stampedSlice = await _patchStamper.FinalizeGPatchCreation(onlyPatch, flatLod.ScalarValue);

            await _commonExecutor.AddAction(() => { onlyPatch.Destroy(); });

            if (stampedSlice != null)
            {
                if (_mipmapLevelToExtract != 0)
                {
                    var mipMappedMainTexture = await _mipmapExtractor.ExtractMipmapAsync(new TextureWithSize()
                    {
                        Size    = stampedSlice.Resolution,
                        Texture = stampedSlice.ColorStamp
                    }, RenderTextureFormat.ARGB32, _mipmapLevelToExtract);

                    var mipMappedNormalTexture = await _mipmapExtractor.ExtractMipmapAsync(new TextureWithSize()
                    {
                        Size    = stampedSlice.Resolution,
                        Texture = stampedSlice.NormalStamp
                    }, RenderTextureFormat.ARGB32, _mipmapLevelToExtract);

                    await _commonExecutor.AddAction(() => { stampedSlice.Destroy(); });

                    return(new ESurfaceTexturesPack()
                    {
                        MainTexture = mipMappedMainTexture.Texture,
                        NormalTexture = mipMappedNormalTexture.Texture
                    });
                }
                else
                {
                    return(new ESurfaceTexturesPack()
                    {
                        MainTexture = stampedSlice.ColorStamp,
                        NormalTexture = stampedSlice.NormalStamp
                    });
                }
            }

            return(null);
        }
Esempio n. 4
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));
        }
Esempio n. 5
0
 public Task DestroyAsset(TextureWithSize entity)
 {
     return(_commonExecutor.AddAction(() =>
     {
         GameObject.Destroy(entity.Texture);
     }));
 }
Esempio n. 6
0
        private async Task <Texture2D> ChangeNormalTextureToTexture2DAsync(Texture inputTexture)
        {
            if (inputTexture is Texture2D)
            {
                return(inputTexture as Texture2D);
            }

            if (inputTexture is RenderTexture)
            {
                return(await _commonExecutor.AddAction(
                           () => UltraTextureRenderer.RenderTextureToTexture2D(inputTexture as RenderTexture)));
            }
            else
            {
                Preconditions.Fail("Cannot change texture " + inputTexture + " to texture2D");
                return(null);
            }
        }
        public async Task <NullableESurfaceTexturesPack> RetriveAssetAsync(string filename, ESurfaceTexturesPackToken query)
        {
            var isNullFilePresent = await _commonExecutor.AddAction(() => File.Exists(_mainDictionaryPath + filename + _extensionForNullFile));

            if (isNullFilePresent)
            {
                return(new NullableESurfaceTexturesPack()
                {
                    Pack = null
                });
            }
            return(new NullableESurfaceTexturesPack()
            {
                Pack = new ESurfaceTexturesPack()
                {
                    MainTexture = await LoadTextureFromFile(_mainDictionaryPath + filename + _extensionForMainTexture + _extension),
                    NormalTexture = await LoadTextureFromFile(_mainDictionaryPath + filename + _extensionForNormalTexture + _extension),
                }
            });
        }
Esempio n. 8
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));
        }
Esempio n. 9
0
        public int RegisterTerrain(WeldingRegistrationTerrain terrain, Action <TerrainWeldUvs> callback)
        {
            Preconditions.Assert(_weldingEnabled, " E83 Welding is not enabled");
            Action <TerrainWeldUvs> newCallback = (t) =>
            {
                _commonExecutorUtProxy.AddAction(() =>
                {
                    callback(t);
                });
            };

            return(_welderProxy.RegisterTerrain(new WeldingInputTerrain()
            {
                Texture = terrain.Texture,
                DetailGlobalArea = terrain.DetailGlobalArea,
                Resolution = terrain.Resolution,
                TerrainLod = terrain.TerrainLod,
                UvCoordsPositions2D = terrain.UvCoordsPositions2D,
                WeldModificationCallback = newCallback
            }));
        }
 public async Task <RenderTexture> EncodedHeightTextureToPlainAsync(TextureWithSize encodedTexture)
 {
     return(await _commonExecutor.AddAction(() => { return EncodedHeightTextureToPlain(encodedTexture); }));
 }
        public async Task <TextureWithSize> MergeHeightDetailCorners(MyRectangle queryArea,
                                                                     TerrainCardinalResolution cardinalResolution, TextureWithSize baseTexture)
        {
            RetriveTerrainDetailProvider();

            var griddedTerrainDetail     = _alignmentCalculator.GetGriddedTerrainArea(queryArea, cardinalResolution);
            var sourceTerrainDetailTasks = new List <TerrainDetailNeighbourhoodDirections>()
            {
                TerrainDetailNeighbourhoodDirections.Left,
                TerrainDetailNeighbourhoodDirections.BottomLeft,
                TerrainDetailNeighbourhoodDirections.Bottom,
            }.Select(direction => new
            {
                direction,
                alignedPosition = _alignmentCalculator.GetAlignedTerrainArea(griddedTerrainDetail + direction.Movement, cardinalResolution)
            })
            .Where(p => _alignmentCalculator.AreaInMap(p.alignedPosition))
            .ToDictionary(
                p => p.direction,
                p => _terrainDetailProvider.RetriveTerrainDetailAsync(
                    TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY, p.alignedPosition, cardinalResolution, RequiredCornersMergeStatus.NOT_IMPORTANT)
                );

            var sourceTerrainDetails = new Dictionary <TerrainDetailNeighbourhoodDirections, TokenizedTerrainDetailElement>();

            foreach (var pair in sourceTerrainDetailTasks)
            {
                sourceTerrainDetails.Add(pair.Key, (await pair.Value).TokenizedElement);
            }

            var activeTerrainDetails = new TerrainDetailElement()
            {
                CornersMergeStatus = CornersMergeStatus.NOT_MERGED,
                DetailArea         = new MyRectangle(0, 0, 1, 1),
                Resolution         = cardinalResolution,
                Texture            = baseTexture
            };

            var outputTexture = await CreateOutputTexture(baseTexture);

            var scratchTextureSize = 121; //todo configurable
            var scratchTexture     = await _textureConciever.ConcieveRenderTextureAsync(new MyRenderTextureTemplate(scratchTextureSize, scratchTextureSize, RenderTextureFormat.RFloat, false, FilterMode.Point));

            var scratchTextureWithSize = new TextureWithSize()
            {
                Texture = scratchTexture,
                Size    = new IntVector2(scratchTextureSize, scratchTextureSize)
            };

            await MergeDetailObject(new Dictionary <TerrainDetailCorner, TerrainDetailElement>()
            {
                { TerrainDetailCorner.BottomLeft, activeTerrainDetails },
            }, baseTexture, outputTexture, TerrainDetailCorner.TopRight, scratchTextureWithSize);

            if (sourceTerrainDetails.ContainsKey(TerrainDetailNeighbourhoodDirections.Left)) // do not merge if one of merging elements was out of map boundaries
            {
                await MergeDetailObject(new Dictionary <TerrainDetailCorner, TerrainDetailElement>()
                {
                    { TerrainDetailCorner.BottomRight, activeTerrainDetails },
                    { TerrainDetailCorner.BottomLeft, sourceTerrainDetails[TerrainDetailNeighbourhoodDirections.Left].DetailElement },
                }, baseTexture, outputTexture, TerrainDetailCorner.TopLeft, scratchTextureWithSize);
            }

            if (sourceTerrainDetails.ContainsKey(TerrainDetailNeighbourhoodDirections.Left) &&
                sourceTerrainDetails.ContainsKey(TerrainDetailNeighbourhoodDirections.BottomLeft) &&
                sourceTerrainDetails.ContainsKey(TerrainDetailNeighbourhoodDirections.Bottom))
            {
                await MergeDetailObject(new Dictionary <TerrainDetailCorner, TerrainDetailElement>()
                {
                    { TerrainDetailCorner.TopRight, activeTerrainDetails },
                    { TerrainDetailCorner.TopLeft, sourceTerrainDetails[TerrainDetailNeighbourhoodDirections.Left].DetailElement },
                    { TerrainDetailCorner.BottomLeft, sourceTerrainDetails[TerrainDetailNeighbourhoodDirections.BottomLeft].DetailElement },
                    { TerrainDetailCorner.BottomRight, sourceTerrainDetails[TerrainDetailNeighbourhoodDirections.Bottom].DetailElement },
                }, baseTexture, outputTexture, TerrainDetailCorner.BottomLeft, scratchTextureWithSize);
            }

            if (sourceTerrainDetails.ContainsKey(TerrainDetailNeighbourhoodDirections.Bottom))
            {
                await MergeDetailObject(new Dictionary <TerrainDetailCorner, TerrainDetailElement>()
                {
                    { TerrainDetailCorner.TopLeft, activeTerrainDetails },
                    { TerrainDetailCorner.BottomLeft, sourceTerrainDetails[TerrainDetailNeighbourhoodDirections.Bottom].DetailElement },
                }, baseTexture, outputTexture, TerrainDetailCorner.BottomRight, scratchTextureWithSize);
            }

            await _commonExecutor.AddAction(() => GameObject.Destroy(scratchTexture));

            await TaskUtils.WhenAll(sourceTerrainDetails.Values.Select(c => c.Token)
                                    .Select(c => _terrainDetailProvider.RemoveTerrainDetailAsync(c)));

            return(outputTexture);
        }