public InstanceWrapper(GameObject instance, BlockBounds bounds) { this.gameObject = instance; this.bounds = bounds; this.rotatedBounds = bounds.Copy(); this.endRotation = Quaternion.identity; }
public ConstraintResult Constrains(bool lhs, BlockBounds bounds, out float distance) { Vector3 splitAxisVal = Axis * Value; float splitVal, min, max; if (splitAxisVal.x > 0) { splitVal = splitAxisVal.x; min = bounds.MinX; max = bounds.MaxX; } else if (splitAxisVal.y > 0) { splitVal = splitAxisVal.y; min = bounds.MinY; max = bounds.MaxY; } else { splitVal = splitAxisVal.z; min = bounds.MinZ; max = bounds.MaxZ; } if (BlockedByBounds(splitVal, min, max)) { distance = 0; return ConstraintResult.Blocked; } distance = GetDistFromSplit(lhs, splitVal, min, max); return distance >= 0 ? ConstraintResult.Included : ConstraintResult.Outside; }
private void Split(Vector3 axis, float value, BlockBounds bounds, out BlockBounds splitL, out BlockBounds splitR) { float splitX = axis.x * value; float splitY = axis.y * value; float splitZ = axis.z * value; if(((axis.x != 0) && (splitX > bounds.MinX)) || ((axis.y != 0) && (splitY > bounds.MinY)) || ((axis.z != 0) && (splitZ > bounds.MinZ))) { float maxX = axis.x == 0 ? bounds.MaxX : Mathf.Min(splitX, bounds.MaxX); float maxY = axis.y == 0 ? bounds.MaxY : Mathf.Min(splitY, bounds.MaxY); float maxZ = axis.z == 0 ? bounds.MaxZ : Mathf.Min(splitZ, bounds.MaxZ); splitL = new BlockBounds(bounds.MinX, bounds.MinY, bounds.MinZ, maxX, maxY, maxZ); } else { splitL = null; } if (((axis.x != 0) && (splitX < bounds.MaxX)) || ((axis.y != 0) && (splitY < bounds.MaxY)) || ((axis.z != 0) && (splitZ < bounds.MaxZ))) { float minX = axis.x == 0 ? bounds.MinX : Mathf.Max(splitX, bounds.MinX); float minY = axis.y == 0 ? bounds.MinY : Mathf.Max(splitY, bounds.MinY); float minZ = axis.z == 0 ? bounds.MinZ : Mathf.Max(splitZ, bounds.MinZ); splitR = new BlockBounds(minX, minY, minZ, bounds.MaxX, bounds.MaxY, bounds.MaxZ); } else { splitR = null; } }
private void AttachGameObject(MeshBlock meshBlock, GroupBranch groupBranch, AdjacencyCalculator adjacencyCalculator) { SplitBoundsBranch splitBoundsBranch = groupBranch.SplitBoundsBranchContaining(meshBlock.SplittedRegion); var meshBuilder = new MeshBuilder(); adjacencyCalculator.SetupNext(groupBranch.SplittedMeshBlocks, groupBranch.Splits, meshBlock); for (var x = (int)meshBlock.OriginalBounds.MinX; x < meshBlock.OriginalBounds.MaxX; x++) { for (var y = (int)meshBlock.OriginalBounds.MinY; y < meshBlock.OriginalBounds.MaxY; y++) { for (var z = (int)meshBlock.OriginalBounds.MinZ; z < meshBlock.OriginalBounds.MaxZ; z++) { AdjacencyMatrix adjacencyMatrix = adjacencyCalculator.CalculateAdjacency(x, y, z); if (!adjacencyMatrix.IsVisible()) continue; var clipBounds = new BlockBounds(x, y, z, x + 1, y + 1, z + 1); clipBounds.ClipToBounds(groupBranch.Splits); meshBlock.MeshSource.Build(new Vector3(x, y, z) - meshBlock.OriginalBounds.Position, adjacencyMatrix, meshBlock, meshBuilder, clipBounds); } } } GameObject blockGameObject = Object.Instantiate(groupBranch.Level.Prefabs.BlockPrefab); blockGameObject.name = meshBlock.Id; blockGameObject.transform.localPosition = meshBlock.OriginalBounds.Position; splitBoundsBranch.BlocksLeaf.Attach(blockGameObject); Mesh mesh = meshBuilder.DoneCreateMesh(); blockGameObject.GetComponent<MeshFilter>().mesh = mesh; }
public MeshBlock(string id, Type type, BlockBounds blockBounds, SplittedRegion splittedRegion, Quaternion rotation, int occludeOrder) { Id = id; BlockType = type; RotationQuat = rotation; OccludeOrder = occludeOrder; OriginalBounds = blockBounds; SplittedRegion = splittedRegion; RotatedBlockBounds = blockBounds.Copy(); TypeCode = TypeToTypeCode[type]; MeshSource = ProceduralMeshSource.GetInstance(TypeCode); }
public bool Contains(BlockBounds bounds) { return bounds.MinX >= MinX && bounds.MaxX <= MaxX && bounds.MinY >= MinY && bounds.MaxY <= MaxY && bounds.MinZ >= MinZ && bounds.MaxZ <= MaxZ; }
public void Build(Vector3 pos, AdjacencyMatrix adjacencyMatrix, MeshBlock block, MeshBuilder meshBuilder, BlockBounds clipBounds) { }
public SplittedRegion(BlockBounds bounds) { Bounds = bounds; }
public bool Contains(BlockBounds bounds) { return Bounds.Contains(bounds); }
public bool AproxEqual(BlockBounds bounds) { return MathHelper.VectorsEqualWError(Position, bounds.Position) && MathHelper.VectorsEqualWError(Size, bounds.Size); }
public BlockBounds UnionWith(BlockBounds bounds) { MinX = Math.Min(bounds.MinX, MinX); MinY = Math.Min(bounds.MinY, MinY); MinZ = Math.Min(bounds.MinZ, MinZ); MaxX = Math.Max(bounds.MaxX, MaxX); MaxY = Math.Max(bounds.MaxY, MaxY); MaxZ = Math.Max(bounds.MaxZ, MaxZ); return this; }
public Split.ConstraintResult Constrains(BlockBounds blockBounds, out float distance) { return _split.Constrains(Lhs, blockBounds, out distance); }
public SplitTree(BlockBounds bounds) { _root = new SplitTreeNode(null, null); _root.children = new[] { new SplitTreeNode(_root, bounds) }; _latestGeneration = _root.children; }
public SplitTreeNode(SplitTreeNode parent, BlockBounds bounds) { this.children = new SplitTreeNode[0]; this.parent = parent; this.bounds = bounds; }
public RotationData(BlockBounds originalBounds, bool blocksRotation) : this() { OriginalBounds = originalBounds; RotatedBounds = originalBounds.Copy(); BlocksRotation = blocksRotation; }
private void CalculateSplitAdjacency() { if (splits == null) return; if(((x < meshBlock.OriginalBounds.MaxX - 1) && (x > meshBlock.OriginalBounds.MinX)) && ((y < meshBlock.OriginalBounds.MaxY - 1) && (y > meshBlock.OriginalBounds.MinY)) && ((z < meshBlock.OriginalBounds.MaxX - 1) && (z > meshBlock.OriginalBounds.MinX))) return; BlockBounds cellBounds = new BlockBounds(x, y, z, x + 1, y + 1, z + 1); foreach(Split split in splits) { float distance; split.Constrains(true, cellBounds, out distance); if (distance == 0) { if (split.Axis.x > 0) { adjacencyMatrix.SetAllBits(2, 1, 1, false, false, MeshBlock.TypeSplit); } else if (split.Axis.y > 0) { adjacencyMatrix.SetAllBits(1, 2, 1, false, false, MeshBlock.TypeSplit); } else { adjacencyMatrix.SetAllBits(1, 1, 2, false, false, MeshBlock.TypeSplit); } } else { split.Constrains(false, cellBounds, out distance); if (distance == 0) { if (split.Axis.x > 0) { adjacencyMatrix.SetAllBits(0, 1, 1, false, false, MeshBlock.TypeSplit); } else if (split.Axis.y > 0) { adjacencyMatrix.SetAllBits(1, 0, 1, false, false, MeshBlock.TypeSplit); } else { adjacencyMatrix.SetAllBits(1, 1, 0, false, false, MeshBlock.TypeSplit); } } } } }
public MeshBlock CopyOf(BlockBounds blockBounds, SplittedRegion splittedRegion) { return new MeshBlock(Id, BlockType, blockBounds, splittedRegion, RotationQuat, OccludeOrder); }
public BlockBounds SetToRotationFrom(Quaternion rotation, Vector3 pivot, BlockBounds originalBounds = null) { if (originalBounds == null) { originalBounds = this; } _rotation = rotation; Vector3 r1 = MathHelper.RotateAroundPivot(new Vector3(originalBounds.MinX, originalBounds.MinY, originalBounds.MinZ), pivot, rotation); Vector3 r2 = MathHelper.RotateAroundPivot(new Vector3(originalBounds.MaxX, originalBounds.MaxY, originalBounds.MaxZ), pivot, rotation); MinX = MathHelper.RoundToDp(Mathf.Min(r1.x, r2.x), 4); MinY = MathHelper.RoundToDp(Mathf.Min(r1.y, r2.y), 4); MinZ = MathHelper.RoundToDp(Mathf.Min(r1.z, r2.z), 4); MaxX = MathHelper.RoundToDp(Mathf.Max(r1.x, r2.x), 4); MaxY = MathHelper.RoundToDp(Mathf.Max(r1.y, r2.y), 4); MaxZ = MathHelper.RoundToDp(Mathf.Max(r1.z, r2.z), 4); return this; }
public void Build(Vector3 pos, AdjacencyMatrix adjacencyMatrix, MeshBlock block, MeshBuilder meshBuilder, BlockBounds clipBounds) { Quaternion rot = block.RotationQuat; Matrix4x4 transform = Matrix4x4.TRS(pos + new Vector3(0.5f, 0.5f, 0.5f), rot, new Vector3(1, 1, 1)); BlockBounds localClipBounds = new BlockBounds (clipBounds.Position - block.OriginalBounds.Position - pos, clipBounds.Size); localClipBounds.SetToRotationFrom (Quaternion.Inverse(block.RotationQuat), new Vector3(0.5f, 0.5f, 0.5f)); meshBuilder.BeforeNext (transform, new Vector3(-0.5f, -0.5f, -0.5f), localClipBounds); foreach (Part part in _blockParts) { if ((part.Direction == null) || (!adjacencyMatrix.RPt(part.Direction[0], part.Direction[1], part.Direction[2]).Occluded)) { IProcMeshGenerator meshGenerator; if(!_meshGenerators.TryGetValue(part.MeshGenerator, out meshGenerator)){ _meshGenerators[part.MeshGenerator] = (meshGenerator = (IProcMeshGenerator)Activator.CreateInstance(part.MeshGenerator)); } meshGenerator.BuildMesh(part.PartType, adjacencyMatrix, meshBuilder, localClipBounds); } } }