コード例 #1
0
 private void EnsureParticlesInitialized()
 {
     if (particles == null)
     {
         this.MaxParticleCount = EstimateMaxParticleCount();
         this.particles        = new Particle[MaxParticleCount];
         this.primitive        = new DynamicPrimitive(GraphicsDevice, MaxParticleCount * 6, 32768);
         UpdateMaterial();
     }
 }
コード例 #2
0
        public override Scene CreateScene(GraphicsDevice graphics, ContentLoader content)
        {
            var scene          = new Scene();
            var solidPrimitive = new DynamicPrimitive(graphics)
            {
                DepthBias = 0
            };
            var linePrimitive = new DynamicPrimitive(graphics)
            {
                DepthBias = 0.0002f
            };

            var frustum = new BoundingFrustum(
                Matrix.CreateLookAt(new Vector3(0, 15, 15), Vector3.Zero, Vector3.Up) *
                Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 1, 1, 10));

            linePrimitive.AddLine(new Vector3[] { Vector3.Zero, Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ }, null, Color.White, 1);

            linePrimitive.AddAxis(Matrix.CreateScale(4) * Matrix.CreateTranslation(-8, 0, 0), 4);

            linePrimitive.AddGrid(8, 16, 16, null, Color.Black, 2);
            linePrimitive.AddGrid(1, 128, 128, null, Color.White * 0.25f, 1);

            linePrimitive.AddSphere(new Vector3(16, 0, 16), 4, 24, null, Color.White, 2);
            solidPrimitive.AddSolidSphere(new Vector3(16, 0, 8), 4, 24, null, new Color(128, 21, 199));

            linePrimitive.AddBox(Vector3.Zero, Vector3.One * 4, null, Color.White, 2);
            solidPrimitive.AddSolidBox(new Vector3(8, 0, 0), Vector3.One * 4, null, new Color(255, 255, 0, 255) * 0.2f);

            linePrimitive.AddCircle(new Vector3(16, 0, -8), 4, 32, null, new Color(255, 255, 0, 255), 2);
            solidPrimitive.AddSolidSphere(new BoundingSphere(Vector3.UnitX * 4, 1), 24, null, new Color(255, 0, 0, 255) * 0.2f);

            linePrimitive.AddFrustum(frustum, null, Color.White, 2);
            solidPrimitive.AddSolidFrustum(frustum, null, new Color(255, 192, 203, 255) * 0.5f);

            linePrimitive.AddCylinder(new Vector3(-16, 0, 8), 2, 1, 12, null, Color.White, 2);
            solidPrimitive.AddSolidCylinder(new Vector3(-5, 0, -6), 2, 1, 24, null, new Color(230, 230, 255, 255) * 0.3f);

            scene.Add(solidPrimitive);
            scene.Add(linePrimitive);
            return(scene);
        }
コード例 #3
0
ファイル: DrawingContext.cs プロジェクト: Hengle/Engine-Nine
        /// <summary>
        /// Draws the debug overlay of the target scene.
        /// </summary>
        internal void DrawDiagnostics()
        {
            if (debugDrawables == null)
            {
                debugDrawables = CreateSpatialQuery <IDebugDrawable>(drawable => drawable.Visible);
                debugBounds    = CreateSpatialQuery <ISpatialQueryable>(queryable => true);
                debugPrimitive = new DynamicPrimitive(graphics)
                {
                    DepthBias = 0.0002f
                };
                debugDrawablesInViewFrustum = new FastList <IDebugDrawable>();
                debugBoundsInViewFrustum    = new FastList <ISpatialQueryable>();
            }

            BoundingFrustum viewFrustum = ViewFrustum;

            debugDrawables.FindAll(viewFrustum, debugDrawablesInViewFrustum);
            debugBounds.FindAll(viewFrustum, debugBoundsInViewFrustum);

            // TODO:
            //debugPrimitive.AddBox(BoundingBox, null, Constants.SceneBoundsColor, 4);

            for (int i = 0; i < debugBoundsInViewFrustum.Count; ++i)
            {
                //debugPrimitive.AddBox(debugBoundsInViewFrustum[i].BoundingBox, null, Constants.BoundingBoxColor, Constants.MiddleLineWidth);
            }

            for (int i = 0; i < debugDrawablesInViewFrustum.Count; ++i)
            {
                debugDrawablesInViewFrustum[i].Draw(this, debugPrimitive);
            }

            graphics.DepthStencilState = DepthStencilState.Default;
            debugPrimitive.Draw(this, null);
            debugPrimitive.Clear();
            debugDrawablesInViewFrustum.Clear();
            debugBoundsInViewFrustum.Clear();
        }
コード例 #4
0
ファイル: PointLight.cs プロジェクト: Hengle/Engine-Nine
 internal override void Draw(DrawingContext context, DynamicPrimitive primitive)
 {
     primitive.AddSphere(BoundingSphere, 8, null, Constants.LightFrustumColor, Constants.MiddleLineWidth);
     base.Draw(context, primitive);
 }
コード例 #5
0
 protected internal override void OnRender(DynamicPrimitive dynamicPrimitive)
 {
     base.OnRender(dynamicPrimitive);
 }
コード例 #6
0
 public DynamicPrimitiveRenderer(GraphicsDevice graphics)
     : base(graphics)
 {
     dynamicPrimitive = new DynamicPrimitive(graphics);
 }
コード例 #7
0
ファイル: TriangleTest.cs プロジェクト: Hengle/Engine-Nine
        public void Draw(DrawingContext context, Material material)
        {
            Vector3[] positions;
            ushort[]  indices;

            if (geometryQuery == null)
            {
                geometryQuery = context.CreateSpatialQuery <IGeometry>(null);
            }

            if (primitive == null)
            {
                primitive = new DynamicPrimitive(context.GraphicsDevice);
            }

            // Ray picking
            var mouseState = Mouse.GetState();
            var pickRay    = context.GraphicsDevice.Viewport.CreatePickRay(mouseState.X, mouseState.Y, context.View, context.Projection);

            geometryQuery.FindAll(ref pickRay, geometry =>
            {
                if (geometry.TryGetTriangles(out positions, out indices))
                {
                    var rayInGeometrySpace = pickRay.Transform(Matrix.Invert(geometry.Transform));
                    for (int i = 0; i < indices.Length; i += 3)
                    {
                        var triangle = new Triangle(positions[indices[i]], positions[indices[i + 1]], positions[indices[i + 2]]);
                        if (triangle.Intersects(rayInGeometrySpace).HasValue)
                        {
                            primitive.AddTriangle(triangle, geometry.Transform, new Color(255, 255, 0), 2);
                        }
                    }
                }
            });

            // Box picking
            var speed    = 0.01f;
            var keyboard = Keyboard.GetState();

            if (keyboard.IsKeyDown(Keys.Left))
            {
                pickBox.Min += Vector3.Left * speed;
                pickBox.Max += Vector3.Left * speed;
            }
            if (keyboard.IsKeyDown(Keys.Right))
            {
                pickBox.Min += Vector3.Right * speed;
                pickBox.Max += Vector3.Right * speed;
            }
            if (keyboard.IsKeyDown(Keys.Up))
            {
                pickBox.Min += Vector3.Forward * speed;
                pickBox.Max += Vector3.Forward * speed;
            }
            if (keyboard.IsKeyDown(Keys.Down))
            {
                pickBox.Min += Vector3.Backward * speed;
                pickBox.Max += Vector3.Backward * speed;
            }
            if (keyboard.IsKeyDown(Keys.PageUp))
            {
                pickBox.Min += Vector3.Up * speed;
                pickBox.Max += Vector3.Up * speed;
            }
            if (keyboard.IsKeyDown(Keys.PageDown))
            {
                pickBox.Min += Vector3.Down * speed;
                pickBox.Max += Vector3.Down * speed;
            }

            primitive.AddBox(pickBox, null, Color.Black, 2);

            geometryQuery.FindAll(ref pickBox, geometry =>
            {
                Matrix transform = geometry.Transform;
                if (geometry.TryGetTriangles(out positions, out indices))
                {
                    for (int i = 0; i < indices.Length; i += 3)
                    {
                        var triangle = new Triangle(positions[indices[i]], positions[indices[i + 1]], positions[indices[i + 2]]);

                        Vector3.Transform(ref triangle.V1, ref transform, out triangle.V1);
                        Vector3.Transform(ref triangle.V2, ref transform, out triangle.V2);
                        Vector3.Transform(ref triangle.V3, ref transform, out triangle.V3);

                        var count = triangle.Intersects(ref pickBox, intersections, 0);
                        if (count > 2)
                        {
                            intersections[count++] = intersections[0];
                            primitive.AddLine(intersections.Take(count), null, new Color(255, 0, 0), 1);
                        }
                    }
                }
            });


            // Box Frustum test
            primitive.AddFrustum(testFrustum, null, new Color(0, 128, 0) * 0.2f, 4);
            var intersectionCount = pickBox.Intersects(testFrustum, intersections, 0);

            if (intersectionCount > 2)
            {
                intersections[intersectionCount++] = intersections[0];
                primitive.AddLine(intersections.Take(intersectionCount), null, new Color(0, 0, 255), 4);
            }

            primitive.Draw(context, null);
            primitive.Clear();
        }
コード例 #8
0
 protected internal virtual void OnRender(DynamicPrimitive dynamicPrimitive)
 {
 }
コード例 #9
0
 void IDebugDrawable.Draw(DrawingContext context, DynamicPrimitive primitive)
 {
     Draw(context, primitive);
 }
コード例 #10
0
 /// <summary>
 /// Draws the light frustum.
 /// </summary>
 internal virtual void Draw(DrawingContext context, DynamicPrimitive primitive)
 {
     primitive.AddFrustum(ShadowFrustum, null, Constants.ShadowFrustumColor, Constants.TinyLineWidth);
 }
コード例 #11
0
 protected internal override void OnRender(DynamicPrimitive dynamicPrimitive)
 {
     base.OnRender(dynamicPrimitive);
     dynamicPrimitive.AddLine(new Vector3(P1, 0), new Vector3(P2, 0), Stroke.ToColor(), StrokeThickness);
 }