public GridInfo(GameObject gridObj, Units thickness, uint width, uint height) { GridObject = gridObj; BorderSize = thickness; BlockSize = 1.0f; CombinedSize = BlockSize + BorderSize; GridWidth = width; GridHeight = height; Dimensions = new uint[2] { width, height }; uint domIndex = width >= height ? 0u : 1; uint weakIndex = (domIndex) ^ 1u; Real32 domLen = CalculateAxisLength(domIndex); Real32 weakLen = CalculateAxisLength(weakIndex); DominantAxis = new GridAxis(domIndex, domLen); WeakAxis = new GridAxis(weakIndex, weakLen); ScalingCoefficient = CalculateScalingCoefficient(); Scaled_borderSize = BorderSize * ScalingCoefficient; Scaled_blockSize = BlockSize * ScalingCoefficient; Scaled_combinedSize = Scaled_blockSize + Scaled_borderSize; MeshStart = CalculateMeshStart(); LowerLeftCorner = new WorldCoord( MeshStart.x * GetGridWidthInUnits(), MeshStart.z, MeshStart.y * GetGridHeightInUnits()); }
public int GetGridPosition(float position, GridAxis axis) { float interval = GetSize(axis) / GetDivisions(axis); float relativePosition = position - GetBoundsMin(axis); return(Mathf.FloorToInt(relativePosition / interval)); }
/// <summary> /// Make a room adjacent to the wall of another one. If the room cannot be made, null will be returned instead. /// </summary> /// <param name="wall"></param> /// <param name="maxTriesForRoomCreation"></param> /// <returns>The room placed or null.</returns> public Room MakeAdjacentRoomFromWall(Wall wall, RoomType type, Point minSize, Point maxSize, int maxTriesForRoomCreation = 50) { if (wall.CheckOuterEdgeOfWall() != null) { GridAxis edgeOut = (GridAxis)wall.CheckOuterEdgeOfWall(); for (int try_ = 0; try_ < maxTriesForRoomCreation; try_++) { Point roomSize = new Point(r.Next(minSize.X, maxSize.X + 1), r.Next(minSize.Y, maxSize.Y + 1)); // Add 1 to maxSize because r.Next excludes the max number when randomizing Point startingPos = new Point(-256, -256); // Set the starting pos for the room. switch (edgeOut) { case GridAxis.positiveX: // Build room expanding on the X axis and randomly setting the Y axis startingPos = new Point(wall.Pos.X, wall.Pos.Y - r.Next(1, roomSize.Y - 1)); break; case GridAxis.negativeX: // Build room expanding on the -X axis and randomly setting the Y axis startingPos = new Point(wall.Pos.X - roomSize.X + 1, wall.Pos.Y - r.Next(1, roomSize.Y - 1)); break; case GridAxis.positiveY: // Build room randomly setting the X axis and expanding on the Y axis startingPos = new Point(wall.Pos.X - r.Next(1, roomSize.X - 1), wall.Pos.Y); break; case GridAxis.negativeY: // Build room randomly setting the X axis and expanding on the -Y axis startingPos = new Point(wall.Pos.X - r.Next(1, roomSize.X - 1), wall.Pos.Y - roomSize.Y + 1); break; default: break; } bool isCollidingWithOtherRooms = false; foreach (Room r in rooms) { if (r?.FloorIntersects(new Rectangle(startingPos, roomSize)) ?? false) { isCollidingWithOtherRooms = true; // Floor intersects with any room's walls or floor? nope, repeat } } if (!isCollidingWithOtherRooms) { Room room = GenerateRoom(startingPos, roomSize, type); return(room); } } return(null); } else { return(null); } }
private int GetDivisions(GridAxis axis) { switch (axis) { case GridAxis.X: return(divisions.x); case GridAxis.Y: return(divisions.y); case GridAxis.Z: return(divisions.z); } return(int.MinValue); }
private float GetSize(GridAxis axis) { switch (axis) { case GridAxis.X: return(bounds.size.x); case GridAxis.Y: return(bounds.size.y); case GridAxis.Z: return(bounds.size.z); } return(float.MinValue); }
private float GetBoundsMin(GridAxis axis) { switch (axis) { case GridAxis.X: return(bounds.min.x); case GridAxis.Y: return(bounds.min.y); case GridAxis.Z: return(bounds.min.z); } return(float.MinValue); }
private static float[] Extract(List <Vector3> points, GridAxis axis) { switch (axis) { case GridAxis.X: return(points.Select(p => p.x).ToArray()); case GridAxis.Y: return(points.Select(p => p.y).ToArray()); case GridAxis.Z: return(points.Select(p => p.z).ToArray()); default: return(null); } }
static void DrawGrid(Camera camera, Vector3 cameraPosition, Vector3 gridCenter, Quaternion gridRotation, GridAxis axis, float alpha, GridMode gridMode) { if (alpha <= 0.03f) { return; } // find the nearest point on the plane var normal = gridRotation * MathConstants.upVector3; var d = Vector3.Dot(normal, gridCenter); var t = (Vector3.Dot(normal, cameraPosition) - d) / Vector3.Dot(normal, normal); var snap_x = RealtimeCSG.CSGSettings.SnapVector.x; var snap_y = RealtimeCSG.CSGSettings.SnapVector.y; var snap_z = RealtimeCSG.CSGSettings.SnapVector.z; // calculate a point on the camera at the same distance as the camera is to the grid, in world-space var forward = camera.cameraToWorldMatrix.MultiplyVector(MathConstants.forwardVector3); var projectedCenter = cameraPosition - (t * forward); // calculate the snap sizes relatively to that point in the center (this makes them relative to camera position, but not rotation) var sideways = camera.cameraToWorldMatrix.MultiplyVector(MathConstants.leftVector3); var screenPointCenter = camera.WorldToScreenPoint(projectedCenter); var screenPointAxisX = camera.WorldToScreenPoint(projectedCenter + (sideways * snap_x)); var screenPointAxisY = camera.WorldToScreenPoint(projectedCenter + (sideways * snap_y)); var screenPointAxisZ = camera.WorldToScreenPoint(projectedCenter + (sideways * snap_z)); var pixelSizeX = (screenPointAxisX - screenPointCenter).magnitude; // size in pixels var pixelSizeY = (screenPointAxisY - screenPointCenter).magnitude; // size in pixels var pixelSizeZ = (screenPointAxisZ - screenPointCenter).magnitude; // size in pixels float screenPixelSize; switch (axis) { default: case GridAxis.AxisXZ: { screenPixelSize = Mathf.Min(pixelSizeX, pixelSizeZ); break; } //X/Z case GridAxis.AxisYZ: { screenPixelSize = Mathf.Min(pixelSizeY, pixelSizeZ); break; } //Y/Z case GridAxis.AxisXY: { screenPixelSize = Mathf.Min(pixelSizeX, pixelSizeY); break; } //X/Y } const float minPixelSize = 64.0f; float gridLevelPow = (minPixelSize / screenPixelSize); var gridMaterial = GridMaterial; var gridMesh = GridMesh; if (!gridMaterial || !gridMesh) { return; } gridMaterial.SetFloat(gridSizeID, Mathf.Max(0, gridLevelPow)); gridMaterial.SetVector(gridSpacingID, RealtimeCSG.CSGSettings.SnapVector); var gridColor = ColorSettings.gridColorW; gridColor.a *= alpha; gridMaterial.SetColor(gridCenterColorID, gridColor); gridColor = ColorSettings.gridColor1W; gridColor.a *= alpha; gridMaterial.SetColor(gridColorID, gridColor); camera.depthTextureMode = DepthTextureMode.Depth; gridMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off); gridMaterial.SetInt("_ZWrite", 0); gridMaterial.SetInt("_ZTest", (int)UnityEngine.Rendering.CompareFunction.LessEqual); gridMaterial.SetPass(0); var gridMatrix = Matrix4x4.TRS(gridCenter, gridRotation, Vector3.one); Graphics.DrawMeshNow(gridMesh, gridMatrix, 0); }
public void Flip(GridAxis axis) { throw new NotImplementedException(); }