private GameObject CreateShapeObject(Mesh mesh, HeightPyramidSegmentShapeTemplate shapeTemplate, string name) { var go = new GameObject(name); var flatStartPos = shapeTemplate.FlatPosition; go.transform.localPosition = new Vector3(flatStartPos.x, 0, flatStartPos.y); var flatSize = shapeTemplate.FlatSize; go.transform.localScale = new Vector3(flatSize.x, 1, flatSize.y); var renderer = go.AddComponent <MeshRenderer>(); var segmentUvs = RectangleUtils.CalculateSubelementUv(_perLevelConfiguration.FloorTextureZeroCenteredWorldArea, new MyRectangle(flatStartPos.x, flatStartPos.y, flatSize.x, flatSize.y)); renderer.material = new Material(Shader.Find("Custom/ETerrain/Ground")); renderer.material.SetVector("_SegmentCoords", segmentUvs.ToVector4()); var segmentLevelUvs = RectangleUtils.CalculateSubelementUv(_perLevelConfiguration.FloorTextureZeroCenteredWorldArea.SubRectangle(new MyRectangle(1 / 6.0f, 1 / 6.0f, 4 / 6.0f, 4 / 6.0f)), new MyRectangle(flatStartPos.x, flatStartPos.y, flatSize.x, flatSize.y)); renderer.material.SetVector("_SegmentLevelCoords", segmentLevelUvs.ToVector4()); var mf = go.AddComponent <MeshFilter>(); mf.sharedMesh = mesh; return(go); }
public List <Vector2> Generate(MyRectangle generationArea, float generationCount, float exclusionRadius, int maxTries, int maxPerGridCount, IIntensitySamplingProvider intensityProvider) { var random = new UnsafeRandomProvider(); //todo! var offsetGenerationArea = new MyRectangle(0, 0, generationArea.Width, generationArea.Height); float cellSize = exclusionRadius; var width = generationArea.Width; var height = generationArea.Height; var grid = new CountingGenerationGrid( cellSize, new IntVector2(Mathf.CeilToInt(width / cellSize), Mathf.CeilToInt(height / cellSize)) ); var processList = new GenerationRandomQueue <Vector2>(); var acceptedPoints = new List <Vector2>(1000); var firstPoint = new Vector2(random.Next(0, width), random.Next(0, height)); processList.Add(firstPoint); acceptedPoints.Add(firstPoint); grid.Increment(firstPoint); int tryCount = 0; while (!processList.Empty) { var point = processList.RandomPop(); for (int i = 0; i < generationCount; i++) { Vector2 newPoint = GenerateRandomPointAround(point, exclusionRadius, random); if (offsetGenerationArea.Contains(newPoint)) { var maxPointsInGrid = intensityProvider.Sample( RectangleUtils.CalculateSubelementUv(offsetGenerationArea, newPoint)) * maxPerGridCount; if (grid.Retrive(newPoint) < maxPointsInGrid) { processList.Add(newPoint); acceptedPoints.Add(newPoint); grid.Increment(newPoint); } } tryCount++; } if (tryCount > maxTries) { break; } } return(acceptedPoints.Select(c => c + generationArea.DownLeftPoint).ToList()); }
private MyRectangle GetAlignedUv(MyRectangle queryArea, TerrainCardinalResolution resolution) { float alignLength = TerrainDescriptionConstants.DetailCellSizesPerResolution[resolution]; var alignedBox = new MyRectangle( Mathf.FloorToInt(queryArea.X / alignLength) * alignLength, Mathf.FloorToInt(queryArea.Y / alignLength) * alignLength, alignLength, alignLength ); return(RectangleUtils.CalculateSubelementUv(alignedBox, queryArea)); }
public List <Vector2> Generate(MyRectangle generationArea, int generationCount, float maxTries, float exclusionRadius, bool collisionsAreChecked, IIntensitySamplingProvider intensitySamplingProvider) { var random = new UnsafeRandomProvider(); //todo! var offsetGenerationArea = new MyRectangle(0, 0, generationArea.Width, generationArea.Height); float cellSize = exclusionRadius / Mathf.Sqrt(2); var width = generationArea.Width; var height = generationArea.Height; var grid = new SingleElementGenerationGrid( cellSize, Mathf.CeilToInt(width / cellSize), Mathf.CeilToInt(height / cellSize), exclusionRadius ); var acceptedPoints = new List <Vector2>(generationCount); int generatedPointsCount = 0; int triesCount = 0; while (generatedPointsCount < generationCount && triesCount < maxTries) { var randomPoint = new Vector2(random.Next(0, width), random.Next(0, height)); var randomPointValue = random.NextValue; var intensity = intensitySamplingProvider.Sample( RectangleUtils.CalculateSubelementUv(offsetGenerationArea, randomPoint)); if (intensity >= randomPointValue) { if (!collisionsAreChecked || !grid.Collides(randomPoint)) { if (collisionsAreChecked) { grid.Set(randomPoint); } acceptedPoints.Add(randomPoint); generatedPointsCount++; } } triesCount++; } return(acceptedPoints.Select(c => c + generationArea.DownLeftPoint).ToList()); }
public Task <TerrainDescriptionOutput> Query(TerrainDescriptionQuery query) { var outDict = new Dictionary <TerrainDescriptionElementTypeEnum, TerrainDetailElementOutput>(); foreach (var requestedDetail in query.RequestedElementDetails) { var requestedType = requestedDetail.Type; var alignedArea = RetriveAlignedArea(query.QueryArea.DownLeftPoint, requestedDetail.Resolution); var uv = RectangleUtils.CalculateSubelementUv(alignedArea, query.QueryArea); if (requestedType == TerrainDescriptionElementTypeEnum.HEIGHT_ARRAY) { outDict.Add(requestedType, new TerrainDetailElementOutput() { TokenizedElement = new TokenizedTerrainDetailElement() { DetailElement = new TerrainDetailElement() { DetailArea = alignedArea, Resolution = requestedDetail.Resolution, Texture = _heightTexture }, Token = new TerrainDetailElementToken(alignedArea, requestedDetail.Resolution, requestedType, CornersMergeStatus.NOT_MERGED) }, UvBase = uv.ToRectangle() }); } else { outDict.Add(requestedType, new TerrainDetailElementOutput() { TokenizedElement = new TokenizedTerrainDetailElement() { DetailElement = new TerrainDetailElement() { DetailArea = alignedArea, Resolution = requestedDetail.Resolution, Texture = _normalTexture }, Token = new TerrainDetailElementToken(alignedArea, requestedDetail.Resolution, requestedType, CornersMergeStatus.NOT_MERGED) }, UvBase = uv.ToRectangle() }); } } return(TaskUtils.MyFromResult(new TerrainDescriptionOutput(outDict))); }
private SegmentInTexturePlacement CalculateSegmentPlacement(IntVector2 newSegmentPositionInGrid) { var uvs = RectangleUtils.CalculateSubelementUv( new MyRectangle(0, 0, _floorSlotsCount.X, _floorSlotsCount.Y), new MyRectangle(newSegmentPositionInGrid.X, newSegmentPositionInGrid.Y, 1, 1)); var segmentPixels = new IntRectangle( Mathf.RoundToInt(uvs.X * _floorTextureSize.X), Mathf.RoundToInt(uvs.Y * _floorTextureSize.Y), Mathf.RoundToInt(uvs.Width * _floorTextureSize.X), Mathf.RoundToInt(uvs.Height * _floorTextureSize.Y) ); return(new SegmentInTexturePlacement() { Pixels = segmentPixels, Uvs = uvs }); }
private GameObject CreateShapeObject(Mesh mesh, string name, Vector2 center, Vector2 flatSize) { var go = new GameObject(name); go.transform.localPosition = new Vector3(center.x, 0, center.y); go.transform.localScale = new Vector3(1, 1, 1); var renderer = go.AddComponent <MeshRenderer>(); var segmentUvs = RectangleUtils.CalculateSubelementUv(_perLevelConfiguration.FloorTextureZeroCenteredWorldArea, new MyRectangle(center.x - flatSize.x / 2, center.y - flatSize.y / 2, flatSize.x, flatSize.y)); renderer.material = new Material(Shader.Find("Custom/ETerrain/Ground")); renderer.material.SetVector("_SegmentCoords", segmentUvs.ToVector4()); var mf = go.AddComponent <MeshFilter>(); mf.sharedMesh = mesh; return(go); }
private float?CalculateExclusionRadius( Vector2 point, MyRectangle offsetGenerationArea, MyRange exclusionRadiusRange, IIntensitySamplingProvider intensityProvider) { var uv = RectangleUtils.CalculateSubelementUv(offsetGenerationArea, point); var sample = intensityProvider.Sample(uv); var exclusionRadius = exclusionRadiusRange.Lerp(1 - sample); if (sample > 0.01f) { return(exclusionRadius); } else { return(null); } }