private Vector2 CalculateFollowerMarginUv(WeldSideSource biggerTerrain, WeldSideSource smallerTerrain)
        {
            var biggerSideRange = new Vector2(0, 0);

            if (biggerTerrain.SideType.GetOrientation() == WeldOrientation.Horizontal)
            {
                biggerSideRange = RectangleUtils.CalculateSubPosition(biggerTerrain.Terrain.DetailGlobalArea,
                                                                      biggerTerrain.Terrain.UvCoordsPositions2D).XRange;
            }
            else
            {
                biggerSideRange = RectangleUtils.CalculateSubPosition(biggerTerrain.Terrain.DetailGlobalArea,
                                                                      biggerTerrain.Terrain.UvCoordsPositions2D).YRange;
            }

            var smallerSideRange = new Vector2(0, 0);

            if (smallerTerrain.SideType.GetOrientation() == WeldOrientation.Horizontal)
            {
                smallerSideRange = RectangleUtils.CalculateSubPosition(smallerTerrain.Terrain.DetailGlobalArea,
                                                                       smallerTerrain.Terrain.UvCoordsPositions2D).XRange;
            }
            else
            {
                smallerSideRange = RectangleUtils.CalculateSubPosition(smallerTerrain.Terrain.DetailGlobalArea,
                                                                       smallerTerrain.Terrain.UvCoordsPositions2D).YRange;
            }

            var uv = VectorUtils.CalculateSubelementUv(biggerSideRange, smallerSideRange);

            Preconditions.Assert(uv.IsNormalized(),
                                 $"E76 Margin uv is not normalized: {uv}, biggerSideRange:{biggerSideRange}, smallerSideRange {smallerSideRange}");
            return(uv);
        }
Exemplo n.º 2
0
        public float Sample(Vector2 uv)
        {
            var subUv    = RectangleUtils.CalculateSubPosition(_intensityFieldFigureWithUv.Uv, uv);
            var toReturn = _intensityFieldFigureWithUv.FieldFigure.GetPixelWithUv(subUv);

            return(toReturn);
        }
Exemplo n.º 3
0
        public void Start()
        {
            var msw = new MyStopWatch();

            msw.StartSegment("Loading");
            var loader = new HabitatMapOsmLoader();
            var fields = loader.Load(@"C:\inz\osm\map.osm");

            msw.StartSegment("Translating");
            var translator = new HabitatFieldPositionTranslator(GeoCoordsToUnityTranslator.DefaultTranslator);

            fields = fields.Select(c => translator.Translate(c)).ToList();

            msw.StartSegment("MapCreating");
            var map = HabitatMap.Create(
                new MyRectangle(62 * 720, 67 * 720, 8 * 720, 8 * 720),
                new Vector2(90 * 8, 90 * 8),
                fields,
                HabitatType.NotSpecified,
                HabitatTypePriorityResolver.Default);


            GeoCoordsToUnityTranslator translator2 = GeoCoordsToUnityTranslator.DefaultTranslator;
            var         geoPos1  = new GeoCoordinate(49.600f, 19.543f);
            var         lifePos1 = translator2.TranslateToUnity(geoPos1);
            var         geoPos2  = new GeoCoordinate(49.610f, 19.552f);
            var         lifePos2 = translator2.TranslateToUnity(geoPos2);
            MyRectangle territoryUsedToCreateStainTexture =
                new MyRectangle(lifePos1.x, lifePos1.y, lifePos2.x - lifePos1.x, lifePos2.y - lifePos1.y);

            territoryUsedToCreateStainTexture = RectangleUtils.CalculateSubPosition(territoryUsedToCreateStainTexture,
                                                                                    new MyRectangle(0.65f, 0.65f, 0.1f, 0.1f));

            var treeX         = map.QueryMap(territoryUsedToCreateStainTexture);
            var parentObjectX = new GameObject($"MAGA");

            treeX.QueryAll()
            .ForEach(c => HabitatMapOsmLoaderDebugObject.CreateDebugHabitatField(c.Field, 0.01f, parentObjectX));

            msw.StartSegment("MapQuerying");
            for (int x = 0; x < 8; x++)
            {
                for (int y = 0; y < 8; y++)
                {
                    var tree = map.QueryMap(
                        new MyRectangle(62 * 720 + x * 720, 67 * 720 + y * 720, 720, 720));
                    var parentObject = new GameObject($"x:{x} y:{y}");
                    tree.QueryAll()
                    .ForEach(c => HabitatMapOsmLoaderDebugObject.CreateDebugHabitatField(c.Field, 0.01f,
                                                                                         parentObject));
                }
            }
            Debug.Log("T64: " + msw.CollectResults());
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        public async Task PlaceSegmentAsync(Texture segmentTexture, PlacementDetails placementDetails)
        {
            var          segmentPlacement0 = CalculateSegmentPlacement(placementDetails.ModuledPositionInGrid);
            UniformsPack uniforms0         = new UniformsPack();

            uniforms0.SetTexture("_SegmentHeightTexture", segmentTexture);
            uniforms0.SetUniform("_SegmentCoords", segmentPlacement0.Uvs.ToVector4());

            await _renderer.AddOrder(new TextureRenderingTemplate()
            {
                CanMultistep            = false,
                CreateTexture2D         = false,
                RenderTextureToModify   = _heightMap,
                ShaderName              = "Custom/ETerrain/SegmentPlacer",
                UniformPack             = uniforms0,
                RenderingRectangle      = segmentPlacement0.Pixels,
                RenderTargetSize        = _floorTextureSize,
                RenderTextureArraySlice = _heightMapSliceIndex
            });

            if (_modifyCorners)
            {
                var modifiedCornerBuffer = await _commonExecutor.AddAction(() =>
                {
                    var tex = new RenderTexture(_modifiedCornerBufferSize.X, _modifiedCornerBufferSize.Y, 0, _heightMap.format);
                    tex.Create();
                    return(tex);
                });

                foreach (var cornerModification in placementDetails.CornersToModify)
                {
                    var cornerMask        = _cornerMaskInformations[cornerModification.Corner];
                    var segmentPlacement1 = CalculateSegmentPlacement(cornerModification.ModuledPositionOfSegment);

                    var uniforms1 = new UniformsPack();
                    uniforms1.SetTexture("_HeightMap", _heightMap);
                    uniforms1.SetUniform("_WeldingAreaCoords",
                                         RectangleUtils.CalculateSubPosition(segmentPlacement1.Uvs, cornerMask.SegmentSubPositionUv)
                                         .ToVector4());
                    uniforms1.SetUniform("_MarginSize", _interSegmentMarginSize);
                    uniforms1.SetUniform("_CornerToWeld", cornerMask.CornerToWeldVector);
                    uniforms1.SetUniform("_PixelSizeInUv", 1f / _floorTextureSize.X);
                    uniforms1.SetUniform("_HeightMapSliceIndex", _heightMapSliceIndex);

                    await _renderer.AddOrder(new TextureRenderingTemplate()
                    {
                        CanMultistep          = false,
                        CreateTexture2D       = false,
                        RenderTextureToModify = modifiedCornerBuffer,
                        ShaderName            = "Custom/ETerrain/GenerateNewCorner",
                        UniformPack           = uniforms1,
                        RenderingRectangle    = new IntRectangle(0, 0, segmentPlacement1.Pixels.Width, segmentPlacement1.Pixels.Height),
                        RenderTargetSize      = new IntVector2(segmentPlacement1.Pixels.Width, segmentPlacement1.Pixels.Height)
                    });

                    var uniforms2 = new UniformsPack();
                    uniforms2.SetTexture("_ModifiedCornerBuffer", modifiedCornerBuffer);

                    await _renderer.AddOrder(new TextureRenderingTemplate()
                    {
                        CanMultistep          = false,
                        CreateTexture2D       = false,
                        RenderTextureToModify = _heightMap,
                        ShaderName            = "Custom/ETerrain/CornerPlacer",
                        UniformPack           = uniforms2,
                        RenderingRectangle    = RectangleUtils.CalculateSubPosition(segmentPlacement1.Pixels.ToFloatRectangle(),
                                                                                    cornerMask.SegmentSubPositionUv).ToIntRectange(),
                        RenderTargetSize        = _floorTextureSize,
                        RenderTextureArraySlice = _heightMapSliceIndex
                    });
                }

                await _commonExecutor.AddAction(() => GameObject.Destroy(modifiedCornerBuffer));
            }
        }
        private static WeldTextureDrawingSideInfo CreateDrawingSideInfo(StripSide leader,
                                                                        Vector2 normalizedMarginUvOfTerrain)
        {
            var texSize = new Vector2(leader.HeightTexture.Size.X - 1, leader.HeightTexture.Size.Y - 1);

            texSize = new Vector2(texSize.x / Mathf.Pow(2, leader.Lod), texSize.y / Mathf.Pow(2, leader.Lod));
            var texAreaUvd = RectangleUtils.CalculateSubPosition(new MyRectangle(0, 0, texSize.x, texSize.y),
                                                                 leader.HeightTextureUvs);

            var constantCoord = 0;

            if (leader.WeldSideType == WeldSideType.Right)
            {
                constantCoord = Mathf.RoundToInt(texAreaUvd.MaxX);
            }
            else if (leader.WeldSideType == WeldSideType.Left)
            {
                constantCoord = Mathf.RoundToInt(texAreaUvd.X);
            }
            else if (leader.WeldSideType == WeldSideType.Top)
            {
                constantCoord = Mathf.RoundToInt(texAreaUvd.MaxY);
            }
            else if (leader.WeldSideType == WeldSideType.Bottom)
            {
                constantCoord = Mathf.RoundToInt(texAreaUvd.Y);
            }

            Vector2 baseFullSideRange;

            if (leader.WeldSideType.GetOrientation() == WeldOrientation.Horizontal)
            {
                baseFullSideRange = new Vector2(texAreaUvd.X, texAreaUvd.MaxX);
            }
            else
            {
                baseFullSideRange = new Vector2(texAreaUvd.Y, texAreaUvd.MaxY);
            }
            baseFullSideRange = VectorUtils.CalculateSubPosition(baseFullSideRange, normalizedMarginUvOfTerrain);

            var leaderSideLength = Mathf.RoundToInt(texAreaUvd.Width);

            if (leaderSideLength % 5 == 1)
            {
                leaderSideLength--; //change 241 to 240 etc
            }
            //leaderSideLength /= Mathf.RoundToInt(Mathf.Pow(2, leader.Lod));

            int samplingDistance = 240 / leaderSideLength;


            WeldTextureDrawingSideInfo firstSideInfo = new WeldTextureDrawingSideInfo()
            {
                ConstantCoord          = constantCoord,
                FullLodSidePixelsRange = new IntVector2(Mathf.RoundToInt(baseFullSideRange.x),
                                                        Mathf.RoundToInt(baseFullSideRange.y + 1)),
                HeightTexture    = leader.HeightTexture,
                LodLevel         = leader.Lod,
                SamplingDistance = samplingDistance,
                SideType         = leader.WeldSideType
            };

            return(firstSideInfo);
        }
        public Task RegisterTerrain(WeldingInputTerrain inputTerrain)
        {
            _terrains[inputTerrain.WeldingInputTerrainId] = new WeldingTerrainEntity()
            {
                Uvs = new TerrainWeldUvs(),
                WeldModificationCallback = inputTerrain.WeldModificationCallback
            };

            var globalSubPosition = RectangleUtils.CalculateSubPosition(inputTerrain.DetailGlobalArea,
                                                                        inputTerrain.UvCoordsPositions2D);

            var weldPositions = new Dictionary <WeldSideType, WeldPosition>();

            weldPositions[WeldSideType.Bottom] = new WeldPosition()
            {
                ConstantAxisPosition = Mathf.RoundToInt(globalSubPosition.Y),
                Orientation          = WeldOrientation.Horizontal,
                Range = new IntVector2(Mathf.RoundToInt(globalSubPosition.X), Mathf.RoundToInt(globalSubPosition.MaxX))
            };
            weldPositions[WeldSideType.Top] = new WeldPosition()
            {
                ConstantAxisPosition = Mathf.RoundToInt(globalSubPosition.MaxY),
                Orientation          = WeldOrientation.Horizontal,
                Range = new IntVector2(Mathf.RoundToInt(globalSubPosition.X), Mathf.RoundToInt(globalSubPosition.MaxX))
            };
            weldPositions[WeldSideType.Left] = new WeldPosition()
            {
                ConstantAxisPosition = Mathf.RoundToInt(globalSubPosition.X),
                Orientation          = WeldOrientation.Vertical,
                Range = new IntVector2(Mathf.RoundToInt(globalSubPosition.Y), Mathf.RoundToInt(globalSubPosition.MaxY))
            };
            weldPositions[WeldSideType.Right] = new WeldPosition()
            {
                ConstantAxisPosition = Mathf.RoundToInt(globalSubPosition.MaxX),
                Orientation          = WeldOrientation.Vertical,
                Range = new IntVector2(Mathf.RoundToInt(globalSubPosition.Y), Mathf.RoundToInt(globalSubPosition.MaxY))
            };

            var orders = new List <WeldRegenerationOrder>();

            foreach (var weldPositionPair in weldPositions)
            {
                var weldPosition = weldPositionPair.Value;
                var orientation  = weldPositionPair.Key.GetOrientation();
                var weldsDict    = _welds[orientation];

                if (!weldsDict.ContainsKey(weldPosition.ConstantAxisPosition))
                {
                    weldsDict[weldPosition.ConstantAxisPosition] = new WeldLine();
                }

                var line = weldsDict[weldPosition.ConstantAxisPosition];
                orders.AddRange(line.RegisterWeld(weldPosition, weldPositionPair.Key, inputTerrain));
            }

            //var weldsUvs = TaskUtils.WhenAll(orders.Select(c => _mapLevel1Manager.Process(c))).Result;
            var weldsUvs = orders.Select(c => _mapLevel1Manager.Process(c)).ToList();

            foreach (var x in weldsUvs.SelectMany(c => c))
            {
                var terrainId = x.Key;
                var weldUv    = x.Value;

                var terrain = _terrains[terrainId];
                terrain.Uvs.Merge(weldUv);
                terrain.WeldModificationCallback(terrain.Uvs);
            }

            return(TaskUtils.EmptyCompleted());
        }
 public float GetSideLength()
 {
     return(RectangleUtils.CalculateSubPosition(DetailGlobalArea, UvCoordsPositions2D).Width);
 }