public void SetupViewProj(BoundingBox worldBBox) { Vector3 lightPos = worldBBox.Center() - LightDir; Vector3 up = Vector3.Up; LightView = Matrix.CreateLookAt(lightPos, worldBBox.Center(), up); Vector3[] corners = worldBBox.GetCorners(); Vector3[] transformedCorners = new Vector3[8]; for (int i = 0; i < 8; i++) { transformedCorners[i] = Vector3.Transform(corners[i], LightView); } BoundingBox transformedBBox = new BoundingBox { Min = MathFunctions.Min(transformedCorners), Max = MathFunctions.Max(transformedCorners) }; float width = transformedBBox.Max.X - transformedBBox.Min.X; float height = transformedBBox.Max.Y - transformedBBox.Min.Y; float near = transformedBBox.Min.Z - 10; float far = transformedBBox.Max.Z + 10; LightProj = Matrix.CreateOrthographic(width, height, near, far); //LightProj = Matrix.CreatePerspective(ShadowWith, ShadowHeight, 0.1f, 10000); //LightProj = Matrix.CreatePerspectiveFieldOfView(MathHelper.Pi - 0.5f, 1f, 1f, 1000f); }
/// <summary> /// Compute a vector that would move a bounding box into alignment with another bounding box. /// </summary> /// <param name="diagramToMove"></param> /// <param name="diagramToCompare"></param> /// <param name="verticalAlignment"></param> /// <param name="horizontalAlignment"></param> /// <returns></returns> public static Vector ComputeAlignment( BoundingBox boundsToMove, BoundingBox boundsToCompare, VerticalAlignment?verticalAlignment, HorizontalAlignment?horizontalAlignment) { var verticalOffset = verticalAlignment switch { VerticalAlignment.Bottom => boundsToCompare.YMin - boundsToMove.YMin, VerticalAlignment.Top => boundsToCompare.YMax - boundsToMove.YMax, VerticalAlignment.Center => boundsToCompare.Center().Y - boundsToMove.Center().Y, _ => 0M }; var horizontalOffset = horizontalAlignment switch { HorizontalAlignment.Left => boundsToCompare.XMin - boundsToMove.XMin, HorizontalAlignment.Right => boundsToCompare.XMax - boundsToMove.XMax, HorizontalAlignment.Center => boundsToCompare.Center().X - boundsToMove.Center().X, _ => 0M }; return(new Vector(horizontalOffset, verticalOffset)); }
public override void UpdateNotRender(RenderContext context) { base.UpdateNotRender(context); if (Octree != null) { Center = Octree.Bound.Center(); } else { BoundingBox box = new BoundingBox(); int i = 0; for (; i < Items.Count; ++i) { if (Items[i] is IDynamicReflectable) { box = Items[i].BoundsWithTransform; break; } } for (; i < Items.Count; ++i) { if (Items[i] is IDynamicReflectable) { box = BoundingBox.Merge(box, Items[i].BoundsWithTransform); } } Center = box.Center(); } }
/// <summary> /// Returns true, iff all the nodes are registered, have been moved to the center (if required) and the renderer is loaded. /// </summary> protected bool CheckReady() { lock (boundingBoxes) { if (!hasMoved) { if (!boundingBoxes.ContainsValue(null)) { Vector3d moving = new Vector3d(transform.position) - overallBoundingBox.Center(); foreach (BoundingBox bb in boundingBoxes.Values) { bb.MoveAlong(moving); } overallBoundingBox.MoveAlong(moving); hasMoved = true; waiterForBoundingBoxUpdate.Set(); } else { return(false); } } } lock (pRenderer) { //Checking, weither all RootNodes are there if (pRenderer.GetRootNodeCount() != boundingBoxes.Count) { return(false); } } return(true); }
public static void SetupBestiaryModelWidget(BestiaryCreatureInfo info, ModelWidget modelWidget, Vector3 offset, bool autoRotate, bool autoAspect) { modelWidget.Model = ContentManager.Get <Model>(info.ModelName); modelWidget.TextureOverride = ContentManager.Get <Texture2D>(info.TextureOverride); Matrix[] absoluteTransforms = new Matrix[modelWidget.Model.Bones.Count]; modelWidget.Model.CopyAbsoluteBoneTransformsTo(absoluteTransforms); BoundingBox boundingBox = modelWidget.Model.CalculateAbsoluteBoundingBox(absoluteTransforms); float x = MathUtils.Max(boundingBox.Size().X, 1.4f * boundingBox.Size().Y, boundingBox.Size().Z); modelWidget.ViewPosition = new Vector3(boundingBox.Center().X, 1.5f, boundingBox.Center().Z) + 2.6f * MathUtils.Pow(x, 0.75f) * offset; modelWidget.ViewTarget = boundingBox.Center(); modelWidget.ViewFov = 0.3f; modelWidget.AutoRotationVector = (autoRotate ? new Vector3(0f, MathUtils.Clamp(1.7f / boundingBox.Size().Length(), 0.25f, 1.4f), 0f) : Vector3.Zero); if (autoAspect) { float num = MathUtils.Clamp(boundingBox.Size().XZ.Length() / boundingBox.Size().Y, 1f, 1.5f); modelWidget.Size = new Vector2(modelWidget.Size.Y * num, modelWidget.Size.Y); } }
/// <summary> /// Returns true, iff all the nodes are registered, have been moved to the center (if required) and the renderer is loaded. /// </summary> protected bool CheckReady() { if (!IsInitialized()) { return(false); } lock (boundingBoxes) { if (!hasMoved) { if (boundingBoxes.Count == 0) { //nothing to move along... hasMoved = true; waiterForBoundingBoxUpdate.Set(); } else if (!boundingBoxes.ContainsValue(null)) { moving = -overallTightBoundingBox.Center(); foreach (BoundingBox bb in boundingBoxes.Values) { bb.MoveAlong(moving); } foreach (BoundingBox tbb in tightBoundingBoxes.Values) { tbb.MoveAlong(moving); } overallBoundingBox.MoveAlong(moving); overallTightBoundingBox.MoveAlong(moving); hasMoved = true; waiterForBoundingBoxUpdate.Set(); } else { return(false); } } } lock (pRenderer) { //Checking, weither all RootNodes are there if (pRenderer.GetRootNodeCount() != boundingBoxes.Count) { return(false); } } return(true); }
//This loads bounding boxes and also point cloud meta data (if showpoints is enabled). //The meshes itself have to be created on the MainThread, so if it's necessary, //this function only sets the flag _createMesh, which will be used later private void LoadBoundingBoxes() { BoundingBox overallBoundingBox = new BoundingBox(double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity, double.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity); foreach (PointCloudLoader loader in _loaders) { string path = loader.cloudPath; if (!path.EndsWith("/")) { path += "/"; } PointCloudMetaData metaData = CloudLoader.LoadMetaData(path, false); BoundingBox currentBoundingBox = metaData.tightBoundingBox; overallBoundingBox.Lx = Math.Min(overallBoundingBox.Lx, currentBoundingBox.Lx); overallBoundingBox.Ly = Math.Min(overallBoundingBox.Ly, currentBoundingBox.Ly); overallBoundingBox.Lz = Math.Min(overallBoundingBox.Lz, currentBoundingBox.Lz); overallBoundingBox.Ux = Math.Max(overallBoundingBox.Ux, currentBoundingBox.Ux); overallBoundingBox.Uy = Math.Max(overallBoundingBox.Uy, currentBoundingBox.Uy); overallBoundingBox.Uz = Math.Max(overallBoundingBox.Uz, currentBoundingBox.Uz); if (_showPoints) { Node rootNode = new Node("", metaData, metaData.boundingBox, null); CloudLoader.LoadPointsForNode(rootNode); _nodes.Add(rootNode); } } if (_setToPreview.moveCenterToTransformPosition) { Vector3d moving = -overallBoundingBox.Center(); overallBoundingBox.MoveAlong(moving); foreach (Node n in _nodes) { n.BoundingBox.MoveAlong(moving); } } _currentBB = overallBoundingBox; if (_showPoints) { _createMesh = true; } else { _loaders = null; _nodes = null; } }
public static void BoundingBoxvsBoundingBox(Manafold manafold) { BoundingBox A = manafold.bodyA.shape.GetAABB(); BoundingBox B = manafold.bodyB.shape.GetAABB(); vector2 ABVector = B.Position - A.Position; vector2 AMid = A.Center(); vector2 BMid = B.Center(); float OverlapX = AMid.X + BMid.X - Math.Abs(ABVector.X); if (OverlapX > 0) { float OverlapY = AMid.Y + BMid.Y - Math.Abs(ABVector.Y); if (OverlapY > 0) { if (OverlapX > OverlapY) { if (ABVector.X > 0) { manafold.Normal = new vector2(1, 0); } else { manafold.Normal = new vector2(-1, 0); } manafold.Penetration = OverlapX; } else { if (ABVector.Y > 0) { manafold.Normal = new vector2(0, 1); } else { manafold.Normal = new vector2(0, -1); } manafold.Penetration = OverlapY; } } } //if ((A.Max.X < B.Min.X) || (A.Min.X > B.Max.X)) return; //if ((A.Max.Y < B.Min.Y) || (A.Min.Y > B.Max.Y)) return; }
private static BoundingBox EnsureNonZeroDimensions(BoundingBox input) { var width = input.Width; var height = input.Height; if (input.Width == 0) { width = 1; } if (input.Height == 0) { height = 1; } return(BoundingBox.Create(width, height, input.Center())); }
public Rectangle GetScreenRect(BoundingBox Box, Camera Camera) { Vector3 ext = (Box.Max - Box.Min); Vector3 center = Box.Center(); Vector3 p1 = Camera.Project(Box.Min); Vector3 p2 = Camera.Project(Box.Max); Vector3 p3 = Camera.Project(Box.Min + new Vector3(ext.X, 0, 0)); Vector3 p4 = Camera.Project(Box.Min + new Vector3(0, ext.Y, 0)); Vector3 p5 = Camera.Project(Box.Min + new Vector3(0, 0, ext.Z)); Vector3 p6 = Camera.Project(Box.Min + new Vector3(ext.X, ext.Y, 0)); Vector3 min = MathFunctions.Min(p1, p2, p3, p4, p5, p6); Vector3 max = MathFunctions.Max(p1, p2, p3, p4, p5, p6); return(new Rectangle((int)min.X, (int)min.Y, (int)(max.X - min.X), (int)(max.Y - min.Y))); }
public override void Render(DwarfGame game, GraphicsDevice graphics, DwarfTime time) { NamedImageFrame frame = new NamedImageFrame("newgui/pointers", 32, 6, 0); // Draw a bounding box around the currently selected bodies. foreach (Body body in Player.BodySelector.CurrentBodies) { if (body.Tags.Contains("Resource")) { Drawer2D.DrawText(body.Name, body.Position, Color.White, Color.Black); BoundingBox bounds = body.BoundingBox; bounds.Min += Vector3.Up * 0.5f; bounds.Max += Vector3.Up * 0.5f; bounds = bounds.Expand(0.25f); Drawer3D.DrawBox(bounds, Color.Orange, 0.02f, false); Drawer2D.DrawSprite(frame, bounds.Center(), Vector2.One, Vector2.Zero, new Color(255, 255, 255, 100)); } } }
private void ComputeLightTransform(List <Mesh> scene, Vector3 lightDir) { BoundingBox sceneBbox = scene[0].bbox; foreach (Mesh m in scene) { sceneBbox.Merge(m.bbox); } float radius = (sceneBbox.MaxPoint - sceneBbox.MinPoint).Length; var lightDirLeft = Vector3.Cross(new Vector3(0, 1, 0), lightDir); var lightDirDown = Vector3.Cross(lightDirLeft, lightDir); var newCenter = (lightDirLeft + lightDirDown) * radius + sceneBbox.Center(); // This gets us light space translation and rotation var spaceTransform = Matrix4.LookAt(newCenter - lightDir * radius, newCenter, new Vector3(0, 1, 0)); // but we can add few extra scaling and translation to prepare it for binning var binTransform = spaceTransform * Matrix4.CreateScale(0.5f / radius); _lightTransform = binTransform; }
public void Start() { World.MakeAnnouncement("A storm is coming!", null); switch (TypeofStorm) { case StormType.RainStorm: SoundManager.PlaySound(ContentPaths.Audio.Oscar.sfx_gui_rain_storm_alert, 0.15f); break; case StormType.SnowStorm: SoundManager.PlaySound(ContentPaths.Audio.Oscar.sfx_gui_snow_storm_alert, 0.15f); break; } BoundingBox bounds = World.ChunkManager.Bounds; Vector3 extents = bounds.Extents(); Vector3 center = bounds.Center(); Vector3 windNormalized = WindSpeed / WindSpeed.Length(); Vector3 offset = new Vector3(-windNormalized.X * extents.X + center.X, bounds.Max.Y + 5, -windNormalized.Z * extents.Z + center.Z); Vector3 perp = new Vector3(-windNormalized.Z, 0, windNormalized.X); int numClouds = (int)(MathFunctions.RandInt(10, 100) * Intensity); int numCloudLayers = MathFunctions.RandInt(1, 5); for (int layer = 0; layer < numCloudLayers; layer++) { for (int i = 0; i < numClouds; i++) { Vector3 cloudPos = offset + perp * 5 * (i - numClouds / 2) + MathFunctions.RandVector3Cube() * 10 + windNormalized * 2 * layer; Cloud cloud = new Cloud(World.ComponentManager, Intensity, 5, offset.Y + MathFunctions.Rand(-3.0f, 3.0f), cloudPos, TypeofStorm == StormType.RainStorm ? 0.15f : 0.0f) { Velocity = WindSpeed * 0.5f, TypeofStorm = TypeofStorm }; Clouds.Add(cloud); World.ComponentManager.RootComponent.AddChild(cloud); } } IsInitialized = true; }
public Rectangle GetScreenRect(Camera camera) { BoundingBox box = GetBoundingBox(); Vector3 ext = (box.Max - box.Min); Vector3 center = box.Center(); Vector3 p1 = camera.Project(box.Min); Vector3 p2 = camera.Project(box.Max); Vector3 p3 = camera.Project(box.Min + new Vector3(ext.X, 0, 0)); Vector3 p4 = camera.Project(box.Min + new Vector3(0, ext.Y, 0)); Vector3 p5 = camera.Project(box.Min + new Vector3(0, 0, ext.Z)); Vector3 p6 = camera.Project(box.Min + new Vector3(ext.X, ext.Y, 0)); Vector3 min = MathFunctions.Min(p1, p2, p3, p4, p5, p6); Vector3 max = MathFunctions.Max(p1, p2, p3, p4, p5, p6); return(new Rectangle((int)min.X, (int)min.Y, (int)(max.X - min.X), (int)(max.Y - min.Y))); }
public void Start() { World.MakeAnnouncement("A storm is coming!", null); BoundingBox bounds = World.ChunkManager.Bounds; Vector3 extents = bounds.Extents(); Vector3 center = bounds.Center(); Vector3 windNormalized = WindSpeed / WindSpeed.Length(); Vector3 offset = new Vector3(-windNormalized.X * extents.X + center.X, bounds.Max.Y + 5, -windNormalized.Z * extents.Z + center.Z); Vector3 perp = new Vector3(-windNormalized.Z, 0, windNormalized.X); int numClouds = (int)(MathFunctions.RandInt(10, 100) * Intensity); for (int i = 0; i < numClouds; i++) { Vector3 cloudPos = offset + perp * 5 * (i - numClouds / 2) + MathFunctions.RandVector3Cube() * 10; Cloud cloud = new Cloud(World.ComponentManager, Intensity, 5, offset.Y + MathFunctions.Rand(-3.0f, 3.0f), cloudPos) { Velocity = WindSpeed, TypeofStorm = TypeofStorm }; } IsInitialized = true; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if ((value != null) && (value is MVMIH.IPositionHandle ph)) { var me = ph as IMachineElement; var parent = me.Parent; var eh = parent as MVMIH.IElementHandle; if (eh != null) { var bb = new BoundingBox(new Vector3((float)eh.MinX, (float)eh.MinY, (float)eh.MinZ), new Vector3((float)eh.MaxX, (float)eh.MaxY, (float)eh.MaxZ)); var c = bb.Center(); var diameter = GetDiameter(bb, ph.Type); var distance = GetDistance(bb, ph.Type); var length = diameter * 5.0f; var direction = GetDirection(ph.Type); var p1 = c + direction * distance; var p2 = c - direction * distance; var p3 = c + direction * (distance + length); var p4 = c - direction * (distance + length); var builder = new MeshBuilder(); builder.AddArrow(p1, p3, diameter); builder.AddArrow(p2, p4, diameter); return(builder.ToMesh()); } else { return(null); } } else { return(null); } }
/// <summary> /// This function draws a bounding box using Debug.DrawLine, so Gizmos have to be enabled to see them. /// This functionality has been provided by Garrison Price. Adapted by Simon M. Fraiss. /// </summary> /// <param name="boundingBox">Bounding Box to dislpay</param> /// <param name="transform">Transform that should be applied to the bounding box, or null</param> /// <param name="color">Color to use</param> /// <param name="showPosition">If true, the position will be marked by three vectors</param> /// <param name="duration">Duration to display the box (default is 0.0f, which means just one frame)</param> public static void DrawBoundingBox(BoundingBox boundingBox, Transform transform, Color color, bool showPosition, float duration = 0.0f) { Vector3[] cornerPositions = new Vector3[8]; GetCornerPositions(boundingBox.GetBoundsObject(), transform, ref cornerPositions); if (showPosition) { Debug.DrawLine(boundingBox.Center().ToFloatVector(), boundingBox.Center().ToFloatVector() + Vector3.up, Color.green, duration, true); Debug.DrawLine(boundingBox.Center().ToFloatVector(), boundingBox.Center().ToFloatVector() + Vector3.forward, Color.blue, duration, true); Debug.DrawLine(boundingBox.Center().ToFloatVector(), boundingBox.Center().ToFloatVector() + Vector3.right, Color.red, duration, true); } Debug.DrawLine(cornerPositions[0], cornerPositions[1], color, duration, true); Debug.DrawLine(cornerPositions[1], cornerPositions[3], color, duration, true); Debug.DrawLine(cornerPositions[3], cornerPositions[2], color, duration, true); Debug.DrawLine(cornerPositions[2], cornerPositions[0], color, duration, true); Debug.DrawLine(cornerPositions[4], cornerPositions[5], color, duration, true); Debug.DrawLine(cornerPositions[5], cornerPositions[7], color, duration, true); Debug.DrawLine(cornerPositions[7], cornerPositions[6], color, duration, true); Debug.DrawLine(cornerPositions[6], cornerPositions[4], color, duration, true); Debug.DrawLine(cornerPositions[0], cornerPositions[4], color, duration, true); Debug.DrawLine(cornerPositions[1], cornerPositions[5], color, duration, true); Debug.DrawLine(cornerPositions[3], cornerPositions[7], color, duration, true); Debug.DrawLine(cornerPositions[2], cornerPositions[6], color, duration, true); }
private void CreateBackgroundMesh(GraphicsDevice Device, BoundingBox worldBounds) { int resolution = 4; int width = 256; int height = 256; int numVerts = (width * height) / resolution; BackgroundMesh = new VertexBuffer(Device, VertexPositionColor.VertexDeclaration, numVerts, BufferUsage.None); VertexPositionColor[] verts = new VertexPositionColor[numVerts]; Perlin noise = new Perlin(MathFunctions.RandInt(0, 1000)); Vector2 posCenter = new Vector2(width, height) * 0.5f; Vector3 extents = worldBounds.Extents(); float scale = 16; Vector3 offset = new Vector3(extents.X, 0, extents.Z) * 0.5f * scale - new Vector3(worldBounds.Center().X, worldBounds.Min.Y, worldBounds.Center().Z); int i = 0; for (int x = 0; x < width; x += resolution) { for (int y = 0; y < height; y += resolution) { float dist = MathFunctions.Clamp( (new Vector2(x, y) - posCenter).Length() * 0.1f, 0, 4); float landHeight = (noise.Generate(x * 0.01f, y * 0.01f)) * 8.0f * dist; verts[i].Position = new Vector3(((float)x) / width * extents.X * scale, ((int)(landHeight / 10.0f)) * 10.0f, ((float)y) / height * extents.Z * scale) - offset; if (worldBounds.Contains(verts[i].Position) == ContainmentType.Contains) { verts[i].Position = new Vector3(verts[i].Position.X, Math.Min(verts[i].Position.Y, worldBounds.Min.Y), verts[i].Position.Z); } i++; } } BackgroundMesh.SetData(verts); int[] indices = WorldGenerator.SetUpTerrainIndices(width / resolution, height / resolution); BackgroundIndex = new IndexBuffer(Device, typeof(int), indices.Length, BufferUsage.None); BackgroundIndex.SetData(indices); }
/// <summary>Returns Box.Max in Box coordinates</summary> public static Vector3 Extents(this BoundingBox box) { return(box.Max - box.Center()); }
public void AddParticleSystems(int value, int x, int y, int z) { if (Terrain.ExtractContents(value) != 227) { return; } int data = Terrain.ExtractData(value); int rotation = FurnitureBlock.GetRotation(data); int designIndex = FurnitureBlock.GetDesignIndex(data); FurnitureDesign design = GetDesign(designIndex); if (design == null) { return; } List <FireParticleSystem> list = new List <FireParticleSystem>(); BoundingBox[] torchPoints = design.GetTorchPoints(rotation); if (torchPoints.Length != 0) { BoundingBox[] array = torchPoints; for (int i = 0; i < array.Length; i++) { BoundingBox boundingBox = array[i]; float num = (boundingBox.Size().X + boundingBox.Size().Y + boundingBox.Size().Z) / 3f; float size = MathUtils.Clamp(1.5f * num, 0.1f, 1f); FireParticleSystem fireParticleSystem = new FireParticleSystem(new Vector3(x, y, z) + boundingBox.Center(), size, 24f); m_subsystemParticles.AddParticleSystem(fireParticleSystem); list.Add(fireParticleSystem); } } if (list.Count > 0) { m_particleSystemsByCell[new Point3(x, y, z)] = list; } }