public void TestRenderBoxMesh()
        {
            XNAGame game;
            BoxMesh mesh = null;

            game = new XNAGame();

            game.InitializeEvent +=
                delegate
            {
                mesh = new BoxMesh();

                mesh.Initialize(game);
            };

            game.DrawEvent +=
                delegate
            {
                game.LineManager3D.AddAABB(mesh.BoundingBox, Matrix.Identity, Color.Red);
                if (game.Keyboard.IsKeyPressed(Microsoft.Xna.Framework.Input.Keys.R))
                {
                    mesh.ReloadShader(game);
                }
                mesh.Render(game);
            };


            game.Run();
        }
Exemple #2
0
        private void drawBox()
        {
            TangentVertex[] vertices;
            short[]         sIndices;

            BoxMesh.CreateUnitBoxVerticesAndIndices(out vertices, out sIndices);
            int[] indices = new int[sIndices.Length];
            for (int i = 0; i < sIndices.Length; i++)
            {
                indices[i] = sIndices[i];
            }


            GraphicsDevice.VertexDeclaration = tangentVertexDeclaration;


            renderGBufferShader.SetParameter("World", Matrix.Identity);
            renderGBufferShader.SetParameter("View", game.Camera.View);
            renderGBufferShader.SetParameter("Projection", game.Camera.Projection);
            renderGBufferShader.SetParameter("Texture", boxTexture);

            renderGBufferShader.RenderMultipass(delegate
            {
                GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, sIndices,
                                                         0, sIndices.Length / 3);
            });
        }
        public void AddBox(Vector3 min, Vector3 max, MeshMaterial material)
        {
            if (!parts.ContainsKey(material))
            {
                throw new InvalidOperationException("Material not created by the meshbuilder");
            }
            TangentVertex[] vertices;
            short[]         indices;
            BoxMesh.CreateUnitBoxVerticesAndIndices(out vertices, out indices);

            var data = parts[material];

            for (int i = 0; i < indices.Length; i++)
            {
                var pos = vertices[indices[i]].pos;

                pos.X = pos.X * (max.X - min.X);
                pos.Y = pos.Y * (max.Y - min.Y);
                pos.Z = pos.Z * (max.Z - min.Z);

                data.Positions.Add(pos + min.xna());
                data.Normals.Add(vertices[indices[i]].normal);
                data.Texcoords.Add(vertices[indices[i]].uv);
            }
        }
Exemple #4
0
        public void RenderCubeUsingGameCamera()
        {
            var cubeMesh = new BoxMesh(Vector3D.One, Color.DarkGreen);

            new Model(new ModelData(cubeMesh), new Vector3D(0.0f, 0.0f, 0.0f));
            new Line3D(2 * Vector3D.UnitX, -3 * Vector3D.UnitY, Color.Red);
        }
Exemple #5
0
        protected override Entity Create()
        {
            debugRootEntity = new Entity($"Voxel volume of {Component.Entity.Name}");

            material = GizmoUniformColorMaterial.Create(GraphicsDevice, Color.CornflowerBlue);

            box = new BoxMesh(GraphicsDevice);
            box.Build();

            debugEntity = new Entity($"Voxel volume mesh of {Component.Entity.Name}")
            {
                new ModelComponent
                {
                    Model = new Model
                    {
                        material,
                        new Mesh {
                            Draw = box.MeshDraw
                        },
                    },
                    RenderGroup = RenderGroup,
                }
            };

            return(debugRootEntity);
        }
    public BoxMesh SpawnBoxMesh(GameObject go, Vector3 halfSize)
    {
        BoxMesh mesh = go.AddComponent <BoxMesh>();

        mesh.material = defaultMaterial;
        mesh.SetHalfSize(halfSize.x, halfSize.y, halfSize.z);
        return(mesh);
    }
Exemple #7
0
        public void ChangeColor()
        {
            var box = new BoxMesh(Vector3D.UnitY, Color.Red)
            {
                Color = Color.Blue
            };

            Assert.AreEqual(Color.Blue, box.Color);
        }
Exemple #8
0
        public void ChangeSize()
        {
            var box = new BoxMesh(Vector3D.UnitX, Color.Red)
            {
                Size = Vector3D.UnitZ
            };

            Assert.AreEqual(Vector3D.UnitZ, box.Size);
        }
    public AutoMesh SpawnMesh(IBoxShape shape)
    {
        GameObject go   = new GameObject();
        BoxMesh    mesh = go.AddComponent <BoxMesh>();

        mesh.material = defaultMaterial;
        mesh.SetHalfSize(shape.HalfSize.x, shape.HalfSize.y, shape.HalfSize.z);
        return(mesh);
    }
Exemple #10
0
		public void CreateBox()
		{
			var box = new BoxMesh(Vector3D.UnitY, Color.Red);
			Assert.AreEqual(Vector3D.UnitY, box.Size);
			Assert.AreEqual(Color.Red, box.Color);
			Assert.AreEqual(8, box.Geometry.NumberOfVertices);
			Assert.AreEqual(36, box.Geometry.NumberOfIndices);
			Assert.AreEqual(VertexFormat.Position3DColor,
				(box.Material.Shader as ShaderWithFormat).Format);
		}
Exemple #11
0
        public void TestIntervalCaller()
        {
            XNAGame game = new XNAGame();

            IntervalCaller caller1 = null, caller2 = null;
            BoxMesh        box1 = null;
            BoxMesh        box2 = null;

            box1 = new BoxMesh();
            box2 = new BoxMesh();
            game.AddXNAObject(box1);
            game.AddXNAObject(box2);

            var pos = new Vector3(2, 0, 0);

            caller1 = new IntervalCaller(delegate
            {
                if (box1.Color.R == 255)
                {
                    box1.Color = Color.Green;
                }
                else
                {
                    box1.Color = Color.Red;
                }
            }, 1 / 5f);

            caller2 = new IntervalCaller(delegate
            {
                if (pos.Z == 0)
                {
                    pos.Z = 2;
                }
                else
                {
                    pos.Z = 0;
                }
                box2.WorldMatrix = Matrix.CreateTranslation(pos);
            }, 1 / 3f);



            game.InitializeEvent += delegate
            {
            };

            game.DrawEvent +=
                delegate
            {
                caller1.Update(game.Elapsed);
                caller2.Update(game.Elapsed);
            };

            game.Run();
        }
Exemple #12
0
        public void TestXNAGameWpfEditBoxMesh()
        {
            var game = new XNAGame();
            var mesh = new BoxMesh();

            game.AddXNAObject(mesh);

            game.Wpf.CreateClassForm(mesh);

            game.Run();
        }
        public void TestTriangleMesh()
        {
            XNAGame game = new XNAGame();

            game.SpectaterCamera.CameraPosition = new Vector3(0, 0, -40);
            PhysicsEngine engine = new PhysicsEngine();

            //game.AddXNAObject(engine);



            game.InitializeEvent += delegate
            {
                engine.Initialize();

                PhysicsDebugRendererXNA debugRenderer = new PhysicsDebugRendererXNA(game, engine.Scene);

                game.AddXNAObject(debugRenderer);

                TangentVertex[] vertices;
                short[]         indices;
                BoxMesh.CreateUnitBoxVerticesAndIndices(out vertices, out indices);

                var positions = new Vector3[vertices.Length];
                for (int i = 0; i < positions.Length; i++)
                {
                    positions[i] = vertices[i].pos;
                }

                int[] intIndices = new int[indices.Length];
                for (int i = 0; i < intIndices.Length; i++)
                {
                    intIndices[i] = indices[i];
                }

                var triangleMesh = CreateTriangleMesh(positions, intIndices, engine.Scene);

                var triangleMeshShapeDesc = new TriangleMeshShapeDescription();
                triangleMeshShapeDesc.TriangleMesh = triangleMesh;

                var actorDesc = new ActorDescription(triangleMeshShapeDesc);

                var actor = engine.Scene.CreateActor(actorDesc);
            };

            game.UpdateEvent += delegate
            {
                engine.Update(game.Elapsed);
            };

            game.Run();

            engine.Dispose();
        }
Exemple #14
0
        public void CreateBox()
        {
            var box = new BoxMesh(Vector3D.UnitY, Color.Red);

            Assert.AreEqual(Vector3D.UnitY, box.Size);
            Assert.AreEqual(Color.Red, box.Color);
            Assert.AreEqual(8, box.Geometry.NumberOfVertices);
            Assert.AreEqual(36, box.Geometry.NumberOfIndices);
            Assert.AreEqual(VertexFormat.Position3DColor,
                            (box.Material.Shader as ShaderWithFormat).Format);
        }
Exemple #15
0
    // Use this for initialization
    void CreateBuilding(Vector2 buildingPosition)
    {
        // These variables determine the shape of the building
        // They're random for now
        Vector2 b0 = new Vector2(Random.Range(-10, 0), Random.Range(10, 0));
        Vector2 b1 = new Vector2(Random.Range(10, 0), Random.Range(10, 0));
        Vector2 b2 = new Vector2(Random.Range(10, 0), Random.Range(-10, 0));
        Vector2 b3 = new Vector2(Random.Range(-10, 0), Random.Range(-10, 0));

        GameObject[] building = new GameObject[15];

        building [0] = new GameObject();
        building[0].transform.Translate(buildingPosition.x, 0, buildingPosition.y);

        BoxMesh boxMesh = building[0].AddComponent("BoxMesh") as BoxMesh;

        boxMesh.p0 = b0;
        boxMesh.p1 = b1;
        boxMesh.p2 = b2;
        boxMesh.p3 = b3;

        // pick a texture randomly
        boxMesh.buildingTex = "building" + Random.Range(1, 56);

        //building[0].AddComponent ("BoxMesh");

        // Pile boxes in a random high
        int numberOfFloors = Random.Range(5, 15);

        for (int i = 1; i < numberOfFloors; i++)
        {
            building[i] = Object.Instantiate(building[0]) as GameObject;
            building[i].transform.Translate(0, 4 * i, 0);
            //building[i].AddComponent ("BoxMesh");
        }

        for (int i = 1; i < numberOfFloors; i++)
        {
            building[i].transform.parent = building[0].transform;
        }
    }
Exemple #16
0
        private void CreatePyramid()
        {
            var boxMesh = new BoxMesh(BoxSize, new Material(ShaderFlags.LitTextured, "BoxDiffuse"));

            for (int height = 0; height < PyramidSize; height++)
            {
                for (int width = height; width < PyramidSize; width++)
                {
                    var box = new PhysicalEntity3D();
                    box.Position = new Vector3D(
                        (width - height * 0.5f) * BoxSize.X * 1.1f - BoxSize.X * 1.1f * PyramidSize / 2.0f + 1.1f,
                        0.0f, 1.0f + height * BoxSize.Z * 1.0f);
                    var shape = new PhysicsShape(ShapeType.Box)
                    {
                        Size = BoxSize
                    };
                    box.PhysicsBody = physics.CreateBody(shape, box.Position, box.Mass, 0.0f);
                    box.AddChild(new Model(new ModelData(boxMesh), Vector3D.Zero));
                }
            }
        }
Exemple #17
0
        public void TestEditBoxMesh()
        {
            ClassForm <BoxMesh> form = null;



            var game = new XNAGame();
            var mesh = new BoxMesh();

            game.AddXNAObject(mesh);



            var         ev  = new AutoResetEvent(false);
            Application app = null;
            var         t   = new Thread(delegate()
            {
                app  = new Application();
                form = new ClassForm <BoxMesh>();

                form.DataContext = mesh;

                form.Show();
                ev.Set();
                app.Run();
            });

            t.SetApartmentState(ApartmentState.STA);
            t.Start();

            ev.WaitOne();
            game.UpdateEvent += delegate
            {
                form.WriteDataContext();
                form.ReadDataContext();
            };

            game.Run();
        }
        protected override Entity Create()
        {
            material = GizmoEmissiveColorMaterial.Create(GraphicsDevice, Color.CornflowerBlue);

            box = new BoxMesh(GraphicsDevice);
            box.Build();

            debugEntity = new Entity($"Navigation volume of {Component.Entity.Name}")
            {
                new ModelComponent
                {
                    Model = new Model
                    {
                        material,
                        new Mesh {
                            Draw = box.MeshDraw
                        },
                    },
                    RenderGroup = RenderGroup,
                }
            };
            return(debugEntity);
        }
        public void TestDefaultRendererRenderModel()
        {
            //TODO: maybe detach this from the defaultrenderer?


            DefaultRenderer renderer = new DefaultRenderer();


            TangentVertex[] vertices;
            short[]         indices;
            var             mat = new DefaultModelMaterialTextured();

            BoxMesh.CreateUnitBoxVerticesAndIndices(out vertices, out indices);



            var renderable = renderer.CreateModelRenderable(vertices, indices, mat);

            var el = renderer.CreateRenderElement(renderable);


            XNAGame game = new XNAGame();

            game.InitializeEvent += delegate
            {
                //TODO: make the renderer manage textures!!!

                using (var fs = new FileStream(TestFiles.WoodPlanksBareJPG, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    mat.DiffuseTexture = Texture2D.FromFile(game.GraphicsDevice, fs);
                }
            };

            game.AddXNAObject(renderer);

            game.Run();
        }
    void CreateBlocks(Transform fullSquare, Vector2 buildingPosition, Vector2 b0, Vector2 b1, Vector2 b2, Vector2 b3)
    {
        // TODO: Make it create blocks instead of buildings
        GameObject[] building = new GameObject[15];

        building [0] = new GameObject();
        building[0].transform.Translate(buildingPosition.x, 0, buildingPosition.y);

        BoxMesh boxMesh = building[0].AddComponent("BoxMesh") as BoxMesh;

        boxMesh.p0 = b0;
        boxMesh.p1 = b1;
        boxMesh.p2 = b2;
        boxMesh.p3 = b3;

        // pick a texture randomly
        boxMesh.buildingTex = "building" + Random.Range(1, 3);

        //building[0].AddComponent ("BoxMesh");

        // Pile boxes in a random high
        int numberOfFloors = Random.Range(5, 15);

        for (int i = 1; i < numberOfFloors; i++)
        {
            building[i] = Object.Instantiate(building[0]) as GameObject;
            building[i].transform.Translate(0, 4 * i, 0);
            //building[i].AddComponent ("BoxMesh");
        }

        for (int i = 1; i < numberOfFloors; i++)
        {
            building[i].transform.parent = building[0].transform;
        }

        building[0].transform.parent = fullSquare.transform;
    }
Exemple #21
0
        private void drawBoxShadowMap()
        {
            TangentVertex[] vertices;
            short[]         sIndices;

            BoxMesh.CreateUnitBoxVerticesAndIndices(out vertices, out sIndices);
            int[] indices = new int[sIndices.Length];
            for (int i = 0; i < sIndices.Length; i++)
            {
                indices[i] = sIndices[i];
            }


            GraphicsDevice.VertexDeclaration = tangentVertexDeclaration;


            shadowMapShader.SetParameter("g_matWorld", Matrix.Identity);

            shadowMapShader.RenderMultipass(delegate
            {
                GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, sIndices,
                                                         0, sIndices.Length / 3);
            });
        }
Exemple #22
0
		public void ChangeColor()
		{
			var box = new BoxMesh(Vector3D.UnitY, Color.Red) { Color = Color.Blue };
			Assert.AreEqual(Color.Blue, box.Color);
		}
 public void Initialize(IXNAGame _game)
 {
     ghostFace = new BoxMesh();
     _game.AddXNAObject(ghostFace);
 }
        public static Mesh Construct(BoxMesh metaResource)
        {
            /*
             *  0----1
             *  |  / |
             *  | /  |
             *  2----3
             *
             * */

            /* UV layout for box mapped
             * |---|---|---|
             * | X | Y | Z |
             * |---|---|---|
             * |-X |-Y |-Z |
             * |---|---|---|
             *
             * (e.g. -Z = bottom, Z = top etc..)
             * */

            var mesh = new Mesh
            {
                MeshType           = MeshType.Indexed,
                VertexStreamLayout = Vertex.Position3Normal3Texcoord3.Instance,
                NVertices          = 0,
                NFaces             = 0
            };

            List <Vertex.Position3Normal3Texcoord3> verts = new List <Vertex.Position3Normal3Texcoord3>();

            //-Z
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Min.Y, metaResource.Min.Z), -Vector3.UnitZ, metaResource.BoxMap ? new Vector3(2 / 3f, 1 / 2f, 0) : new Vector3(0, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Min.Y, metaResource.Min.Z), -Vector3.UnitZ, metaResource.BoxMap ? new Vector3(2 / 3f, 2 / 2f, 0) : new Vector3(0, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Max.Y, metaResource.Min.Z), -Vector3.UnitZ, metaResource.BoxMap ? new Vector3(3 / 3f, 1 / 2f, 0) : new Vector3(1, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Max.Y, metaResource.Min.Z), -Vector3.UnitZ, metaResource.BoxMap ? new Vector3(3 / 3f, 2 / 2f, 0) : new Vector3(1, 1, 0)));

            //+Z
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Min.Y, metaResource.Max.Z), Vector3.UnitZ, metaResource.BoxMap ? new Vector3(2 / 3f, 1 / 2f, 0) : new Vector3(0, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Max.Y, metaResource.Max.Z), Vector3.UnitZ, metaResource.BoxMap ? new Vector3(1, 1 / 2f, 0) : new Vector3(1, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Min.Y, metaResource.Max.Z), Vector3.UnitZ, metaResource.BoxMap ? new Vector3(2 / 3f, 0, 0) : new Vector3(0, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Max.Y, metaResource.Max.Z), Vector3.UnitZ, metaResource.BoxMap ? new Vector3(1, 0, 0) : new Vector3(1, 0, 0)));

            //-X
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Min.Y, metaResource.Min.Z), -Vector3.UnitX, metaResource.BoxMap ? new Vector3(0, 1 / 2f + 1 / 2f, 0) : new Vector3(0, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Max.Y, metaResource.Min.Z), -Vector3.UnitX, metaResource.BoxMap ? new Vector3(1 / 3f, 1 / 2f + 1 / 2f, 0) : new Vector3(1, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Min.Y, metaResource.Max.Z), -Vector3.UnitX, metaResource.BoxMap ? new Vector3(0, 1 / 2f, 0) : new Vector3(0, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Max.Y, metaResource.Max.Z), -Vector3.UnitX, metaResource.BoxMap ? new Vector3(1 / 3f, 1 / 2f, 0) : new Vector3(1, 0, 0)));

            //+X
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Max.Y, metaResource.Min.Z), Vector3.UnitX, metaResource.BoxMap ? new Vector3(0, 1 / 2f, 0) : new Vector3(0, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Min.Y, metaResource.Min.Z), Vector3.UnitX, metaResource.BoxMap ? new Vector3(1 / 3f, 1 / 2f, 0) : new Vector3(1, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Max.Y, metaResource.Max.Z), Vector3.UnitX, metaResource.BoxMap ? new Vector3(0, 0, 0) : new Vector3(0, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Min.Y, metaResource.Max.Z), Vector3.UnitX, metaResource.BoxMap ? new Vector3(1 / 3f, 0, 0) : new Vector3(1, 0, 0)));

            //-Y
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Min.Y, metaResource.Min.Z), -Vector3.UnitY, metaResource.BoxMap ? new Vector3(1 / 3f, 2 / 2f, 0) : new Vector3(0, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Min.Y, metaResource.Min.Z), -Vector3.UnitY, metaResource.BoxMap ? new Vector3(2 / 3f, 2 / 2f, 0) : new Vector3(1, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Min.Y, metaResource.Max.Z), -Vector3.UnitY, metaResource.BoxMap ? new Vector3(1 / 3f, 1 / 2f, 0) : new Vector3(0, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Min.Y, metaResource.Max.Z), -Vector3.UnitY, metaResource.BoxMap ? new Vector3(2 / 3f, 1 / 2f, 0) : new Vector3(1, 0, 0)));

            //+Y
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Max.Y, metaResource.Min.Z), Vector3.UnitY, metaResource.BoxMap ? new Vector3(1 / 3f, 1 / 2f, 0) : new Vector3(0, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Max.Y, metaResource.Min.Z), Vector3.UnitY, metaResource.BoxMap ? new Vector3(2 / 3f, 1 / 2f, 0) : new Vector3(1, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Max.Y, metaResource.Max.Z), Vector3.UnitY, metaResource.BoxMap ? new Vector3(1 / 3f, 0, 0) : new Vector3(0, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Max.Y, metaResource.Max.Z), Vector3.UnitY, metaResource.BoxMap ? new Vector3(2 / 3f, 0, 0) : new Vector3(1, 0, 0)));


            if ((metaResource.Facings & Facings.Frontside) != 0)
            {
                mesh.NVertices += 4 * 2 * 3;
            }
            if ((metaResource.Facings & Facings.Backside) != 0)
            {
                mesh.NVertices += 4 * 2 * 3;
                for (int i = 0; i < verts.Count; i++)
                {
                    var v = verts[i];
                    v.Normal = -v.Normal;
                    verts[i] = v;
                }
            }
            mesh.VertexBuffer = new VertexBuffer <Vertex.Position3Normal3Texcoord3>(verts.ToArray());

            List <int> indices = new List <int>();

            if ((metaResource.Facings & Facings.Frontside) != 0)
            {
                mesh.NFaces += 6 * 2;
                for (int b = 0; b < 6; b++)
                {
                    indices.Add(b * 4 + 0);
                    indices.Add(b * 4 + 2);
                    indices.Add(b * 4 + 1);

                    indices.Add(b * 4 + 1);
                    indices.Add(b * 4 + 2);
                    indices.Add(b * 4 + 3);
                }
            }
            if ((metaResource.Facings & Facings.Backside) != 0)
            {
                mesh.NFaces += 6 * 2;
                for (int b = 0; b < 6; b++)
                {
                    indices.Add(b * 4 + 0);
                    indices.Add(b * 4 + 1);
                    indices.Add(b * 4 + 2);

                    indices.Add(b * 4 + 1);
                    indices.Add(b * 4 + 3);
                    indices.Add(b * 4 + 2);
                }
            }
            mesh.IndexBuffer = new IndexBuffer(indices.ToArray());

            return(mesh);
        }
Exemple #25
0
        public static void InitGizmo(Device d3dDevice)
        {
            #region Creating Streams From Resources
            MemoryStream x_NullStream  = new MemoryStream(Resources.x_null);
            MemoryStream y_NullStream  = new MemoryStream(Resources.y_null);
            MemoryStream z_NullStream  = new MemoryStream(Resources.z_null);
            MemoryStream x_MoveStream  = new MemoryStream(Resources.x_move);
            MemoryStream y_MoveStream  = new MemoryStream(Resources.y_move);
            MemoryStream z_MoveStream  = new MemoryStream(Resources.z_move);
            MemoryStream xy_MoveStream = new MemoryStream(Resources.xy_move);
            MemoryStream zx_MoveStream = new MemoryStream(Resources.zx_move);
            MemoryStream zy_MoveStream = new MemoryStream(Resources.zy_move);

            MemoryStream x_RotationStream = new MemoryStream(Resources.x_rotation);
            MemoryStream y_RotationStream = new MemoryStream(Resources.y_rotation);
            MemoryStream z_RotationStream = new MemoryStream(Resources.z_rotation);

            MemoryStream x_ScaleStream = new MemoryStream(Resources.x_scale);
            MemoryStream y_ScaleStream = new MemoryStream(Resources.y_scale);
            MemoryStream z_ScaleStream = new MemoryStream(Resources.z_scale);
            #endregion

            #region Temporary ExtendedMaterials
            #endregion

            #region Loading Meshes and Materials from Streams
            XMoveMesh = Mesh.FromStream(x_MoveStream, MeshFlags.Managed, d3dDevice, out ExtendedMaterial[] xMaterials);
            YMoveMesh = Mesh.FromStream(y_MoveStream, MeshFlags.Managed, d3dDevice, out ExtendedMaterial[] yMaterials);
            ZMoveMesh = Mesh.FromStream(z_MoveStream, MeshFlags.Managed, d3dDevice, out ExtendedMaterial[] zMaterials);

            XNullMesh = Mesh.FromStream(x_NullStream, MeshFlags.Managed, d3dDevice);
            YNullMesh = Mesh.FromStream(y_NullStream, MeshFlags.Managed, d3dDevice);
            ZNullMesh = Mesh.FromStream(z_NullStream, MeshFlags.Managed, d3dDevice);

            XYMoveMesh = Mesh.FromStream(xy_MoveStream, MeshFlags.Managed, d3dDevice, out ExtendedMaterial[] doubleAxisMaterials);
            ZXMoveMesh = Mesh.FromStream(zx_MoveStream, MeshFlags.Managed, d3dDevice);
            ZYMoveMesh = Mesh.FromStream(zy_MoveStream, MeshFlags.Managed, d3dDevice);

            XRotateMesh = Mesh.FromStream(x_RotationStream, MeshFlags.Managed, d3dDevice);
            YRotateMesh = Mesh.FromStream(y_RotationStream, MeshFlags.Managed, d3dDevice);
            ZRotateMesh = Mesh.FromStream(z_RotationStream, MeshFlags.Managed, d3dDevice);

            XScaleMesh = Mesh.FromStream(x_ScaleStream, MeshFlags.Managed, d3dDevice);
            YScaleMesh = Mesh.FromStream(y_ScaleStream, MeshFlags.Managed, d3dDevice);
            ZScaleMesh = Mesh.FromStream(z_ScaleStream, MeshFlags.Managed, d3dDevice);

            BoxMesh = Mesh.Box(d3dDevice, 1, 1, 1);

            Mesh TexturedBox = BoxMesh.Clone(BoxMesh.Options.Value,
                                             VertexFormats.Position | VertexFormats.Normal | VertexFormats.Texture0 |
                                             VertexFormats.Texture1, BoxMesh.Device);

            //The following code makes the assumption that the vertices of the box are
            // generated the same way as they are in the April 2005 SDK
            using (VertexBuffer vb = TexturedBox.VertexBuffer)
            {
                CustomVertex.PositionNormalTextured[] verts =
                    (CustomVertex.PositionNormalTextured[])vb.Lock(0,
                                                                   typeof(CustomVertex.PositionNormalTextured), LockFlags.None,
                                                                   TexturedBox.NumberVertices);
                try
                {
                    for (int i = 0; i < verts.Length; i += 4)
                    {
                        verts[i + 0].Tu = 0.0f;
                        verts[i + 0].Tv = 0.0f;
                        verts[i + 1].Tu = 1.0f;
                        verts[i + 1].Tv = 0.0f;
                        verts[i + 2].Tu = 1.0f;
                        verts[i + 2].Tv = 1.0f;
                        verts[i + 3].Tu = 0.0f;
                        verts[i + 3].Tv = 1.0f;
                    }
                }
                finally
                {
                    vb.Unlock();
                }
            }

            BoxMesh = TexturedBox;

            XMaterial = new NJS_MATERIAL()
            {
                DiffuseColor = xMaterials[0].Material3D.Diffuse, Exponent = 0f, UseTexture = false, IgnoreLighting = true, IgnoreSpecular = true
            };
            YMaterial = new NJS_MATERIAL()
            {
                DiffuseColor = yMaterials[0].Material3D.Diffuse, Exponent = 0f, UseTexture = false, IgnoreLighting = true, IgnoreSpecular = true
            };
            ZMaterial = new NJS_MATERIAL()
            {
                DiffuseColor = zMaterials[0].Material3D.Diffuse, Exponent = 0f, UseTexture = false, IgnoreLighting = true, IgnoreSpecular = true
            };
            DoubleAxisMaterial = new NJS_MATERIAL()
            {
                DiffuseColor = doubleAxisMaterials[0].Material3D.Diffuse, Exponent = 0f, UseTexture = false, IgnoreLighting = true, IgnoreSpecular = true
            };
            HighlightMaterial = new NJS_MATERIAL()
            {
                DiffuseColor = Color.LightGoldenrodYellow, Exponent = 0f, UseTexture = false, IgnoreLighting = true, IgnoreSpecular = true
            };

            ATexture         = Texture.FromBitmap(d3dDevice, Resources.PointATexture, Usage.AutoGenerateMipMap, Pool.Managed);
            BTexture         = Texture.FromBitmap(d3dDevice, Resources.PointBTexture, Usage.AutoGenerateMipMap, Pool.Managed);
            StandardMaterial = new NJS_MATERIAL()
            {
                DiffuseColor = Color.Gray, IgnoreLighting = true, IgnoreSpecular = true, UseAlpha = false, UseTexture = true, Exponent = 100f
            };
            #endregion

            #region Cleanup
            x_NullStream.Close();
            y_NullStream.Close();
            z_NullStream.Close();

            x_MoveStream.Close();
            y_MoveStream.Close();
            z_MoveStream.Close();

            x_RotationStream.Close();
            y_RotationStream.Close();
            z_RotationStream.Close();
            #endregion
        }
Exemple #26
0
		public void ChangeSize()
		{
			var box = new BoxMesh(Vector3D.UnitX, Color.Red) { Size = Vector3D.UnitZ };
			Assert.AreEqual(Vector3D.UnitZ, box.Size);
		}
        public static Mesh Construct(BoxMesh metaResource)
        {
            /*
             *  0----1
             *  |  / |
             *  | /  |
             *  2----3
             *
             * */

            /* UV layout for box mapped
             * |---|---|---|
             * | X | Y | Z |
             * |---|---|---|
             * |-X |-Y |-Z |
             * |---|---|---|
             *
             * (e.g. -Z = bottom, Z = top etc..)
             * */

            var mesh = new Mesh
            {
                MeshType = MeshType.Indexed,
                VertexStreamLayout = Vertex.Position3Normal3Texcoord3.Instance,
                NVertices = 0,
                NFaces = 0
            };

            List<Vertex.Position3Normal3Texcoord3> verts = new List<Vertex.Position3Normal3Texcoord3>();
            //-Z
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Min.Y, metaResource.Min.Z), -Vector3.UnitZ, metaResource.BoxMap ? new Vector3(2 / 3f, 1 / 2f, 0) : new Vector3(0, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Min.Y, metaResource.Min.Z), -Vector3.UnitZ, metaResource.BoxMap ? new Vector3(2 / 3f, 2 / 2f, 0) : new Vector3(0, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Max.Y, metaResource.Min.Z), -Vector3.UnitZ, metaResource.BoxMap ? new Vector3(3 / 3f, 1 / 2f, 0) : new Vector3(1, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Max.Y, metaResource.Min.Z), -Vector3.UnitZ, metaResource.BoxMap ? new Vector3(3 / 3f, 2 / 2f, 0) : new Vector3(1, 1, 0)));

            //+Z
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Min.Y, metaResource.Max.Z), Vector3.UnitZ, metaResource.BoxMap ? new Vector3(2 / 3f, 1 / 2f, 0) : new Vector3(0, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Max.Y, metaResource.Max.Z), Vector3.UnitZ, metaResource.BoxMap ? new Vector3(1, 1 / 2f, 0) : new Vector3(1, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Min.Y, metaResource.Max.Z), Vector3.UnitZ, metaResource.BoxMap ? new Vector3(2 / 3f, 0, 0) : new Vector3(0, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Max.Y, metaResource.Max.Z), Vector3.UnitZ, metaResource.BoxMap ? new Vector3(1, 0, 0) : new Vector3(1, 0, 0)));

            //-X
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Min.Y, metaResource.Min.Z), -Vector3.UnitX, metaResource.BoxMap ? new Vector3(0, 1 / 2f + 1 / 2f, 0) : new Vector3(0, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Max.Y, metaResource.Min.Z), -Vector3.UnitX, metaResource.BoxMap ? new Vector3(1 / 3f, 1 / 2f + 1 / 2f, 0) : new Vector3(1, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Min.Y, metaResource.Max.Z), -Vector3.UnitX, metaResource.BoxMap ? new Vector3(0, 1 / 2f, 0) : new Vector3(0, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Max.Y, metaResource.Max.Z), -Vector3.UnitX, metaResource.BoxMap ? new Vector3(1 / 3f, 1 / 2f, 0) : new Vector3(1, 0, 0)));

            //+X
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Max.Y, metaResource.Min.Z), Vector3.UnitX, metaResource.BoxMap ? new Vector3(0, 1 / 2f, 0) : new Vector3(0, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Min.Y, metaResource.Min.Z), Vector3.UnitX, metaResource.BoxMap ? new Vector3(1 / 3f, 1 / 2f, 0) : new Vector3(1, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Max.Y, metaResource.Max.Z), Vector3.UnitX, metaResource.BoxMap ? new Vector3(0, 0, 0) : new Vector3(0, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Min.Y, metaResource.Max.Z), Vector3.UnitX, metaResource.BoxMap ? new Vector3(1 / 3f, 0, 0) : new Vector3(1, 0, 0)));

            //-Y
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Min.Y, metaResource.Min.Z), -Vector3.UnitY, metaResource.BoxMap ? new Vector3(1 / 3f, 2 / 2f, 0) : new Vector3(0, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Min.Y, metaResource.Min.Z), -Vector3.UnitY, metaResource.BoxMap ? new Vector3(2 / 3f, 2 / 2f, 0) : new Vector3(1, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Min.Y, metaResource.Max.Z), -Vector3.UnitY, metaResource.BoxMap ? new Vector3(1 / 3f, 1 / 2f, 0) : new Vector3(0, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Min.Y, metaResource.Max.Z), -Vector3.UnitY, metaResource.BoxMap ? new Vector3(2 / 3f, 1 / 2f, 0) : new Vector3(1, 0, 0)));

            //+Y
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Max.Y, metaResource.Min.Z), Vector3.UnitY, metaResource.BoxMap ? new Vector3(1 / 3f, 1 / 2f, 0) : new Vector3(0, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Max.Y, metaResource.Min.Z), Vector3.UnitY, metaResource.BoxMap ? new Vector3(2 / 3f, 1 / 2f, 0) : new Vector3(1, 1, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Min.X, metaResource.Max.Y, metaResource.Max.Z), Vector3.UnitY, metaResource.BoxMap ? new Vector3(1 / 3f, 0, 0) : new Vector3(0, 0, 0)));
            verts.Add(new Vertex.Position3Normal3Texcoord3(new Vector3(metaResource.Max.X, metaResource.Max.Y, metaResource.Max.Z), Vector3.UnitY, metaResource.BoxMap ? new Vector3(2 / 3f, 0, 0) : new Vector3(1, 0, 0)));

            if ((metaResource.Facings & Facings.Frontside) != 0)
            {
                mesh.NVertices += 4 * 2 * 3;
            }
            if ((metaResource.Facings & Facings.Backside) != 0)
            {
                mesh.NVertices += 4 * 2 * 3;
                for (int i = 0; i < verts.Count; i++)
                {
                    var v = verts[i];
                    v.Normal = -v.Normal;
                    verts[i] = v;
                }
            }
            mesh.VertexBuffer = new VertexBuffer<Vertex.Position3Normal3Texcoord3>(verts.ToArray());

            List<int> indices = new List<int>();
            if ((metaResource.Facings & Facings.Frontside) != 0)
            {
                mesh.NFaces += 6 * 2;
                for (int b = 0; b < 6; b++)
                {
                    indices.Add(b * 4 + 0);
                    indices.Add(b * 4 + 2);
                    indices.Add(b * 4 + 1);

                    indices.Add(b * 4 + 1);
                    indices.Add(b * 4 + 2);
                    indices.Add(b * 4 + 3);
                }
            }
            if ((metaResource.Facings & Facings.Backside) != 0)
            {
                mesh.NFaces += 6 * 2;
                for (int b = 0; b < 6; b++)
                {
                    indices.Add(b * 4 + 0);
                    indices.Add(b * 4 + 1);
                    indices.Add(b * 4 + 2);

                    indices.Add(b * 4 + 1);
                    indices.Add(b * 4 + 3);
                    indices.Add(b * 4 + 2);
                }
            }
            mesh.IndexBuffer = new IndexBuffer(indices.ToArray());

            return mesh;
        }
Exemple #28
0
        void Initializtion()
        {
            //画星空
            {
                SphereMesh StarMesh = new SphereMesh();
                StarMesh.Slices = 8;
                StarMesh.Stacks = 4;
                StarMesh.Radius = 0.6;
                MeshGeometry3D Star = StarMesh.Geometry;

                var      Star3DGroup      = new Model3DGroup();
                Material specularMaterial = new SpecularMaterial(new SolidColorBrush(Color.FromArgb(255, 255, 255, 255)), 1024);

                for (int i = 0; i < 1500; i++)
                {
                    //创建每个球体的GeometryModel并添加进显示窗口
                    MaterialGroup materialGroup = new MaterialGroup();

                    byte value = (byte)random.Next(150, 255);

                    DiffuseMaterial diffuseMaterial = new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(value, value, value)));
                    materialGroup.Children.Add(diffuseMaterial);
                    materialGroup.Children.Add(specularMaterial);

                    var StarGeometryModel = new GeometryModel3D(Star, materialGroup);

                    Vector3D starPosition;

                    while (true)
                    {
                        double range = 300;
                        double x     = GetRandomInRange(range);
                        double y     = GetRandomInRange(range);
                        double z     = GetRandomInRange(range);

                        if (Math.Abs(x) < Length && Math.Abs(y) < Width * 2 && Math.Abs(z) < Height * 2)
                        {
                            continue;
                        }
                        else
                        {
                            starPosition = new Vector3D(x, y, z);
                            break;
                        }
                    }

                    StarGeometryModel.Transform = new TranslateTransform3D(starPosition);

                    Star3DGroup.Children.Add(StarGeometryModel);
                }

                //StarMesh.Slices = 8;
                //StarMesh.Stacks = 4;
                //StarMesh.Radius = 2;
                //Star = StarMesh.Geometry;

                //for (int i = 0; i < 500; i++)
                //{
                //    //创建每个球体的GeometryModel并添加进显示窗口
                //    MaterialGroup materialGroup = new MaterialGroup();

                //    byte value = (byte)random.Next(150, 255);

                //    DiffuseMaterial diffuseMaterial = new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(value, value, value)));
                //    materialGroup.Children.Add(diffuseMaterial);
                //    materialGroup.Children.Add(specularMaterial);

                //    var StarGeometryModel = new GeometryModel3D(Star, materialGroup);

                //    Vector3D starPosition;

                //    while (true)
                //    {
                //        double range = 600;
                //        double x = GetRandomInRange(range);
                //        double y = GetRandomInRange(range);
                //        double z = GetRandomInRange(range);

                //        if (Math.Abs(x) < Length && Math.Abs(y) < Width * 1.5 && Math.Abs(z) < Height * 1.5)
                //        {
                //            continue;
                //        }
                //        else
                //        {
                //            starPosition = new Vector3D(x, y, z);
                //            break;
                //        }
                //    }

                //    StarGeometryModel.Transform = new TranslateTransform3D(starPosition);

                //    Star3DGroup.Children.Add(StarGeometryModel);
                //}

                {
                    ModelVisual3D starSetModel = new ModelVisual3D();
                    starSetModel.Content = Star3DGroup;
                    gameWindows.model.Children.Add(starSetModel);
                }
            }

            //画球台
            {
                BoxMesh boardMeshBase = new BoxMesh();

                boardMeshBase.Width  = Length;
                boardMeshBase.Height = Height;
                boardMeshBase.Depth  = Width;

                Geometry3D      boardMesh          = boardMeshBase.Geometry;
                GeometryModel3D boardGeometryModel = new GeometryModel3D(boardMesh, null);

                boardGeometryModel.Material = null;//材料

                ImageBrush imageBrush = new ImageBrush();
                imageBrush.ImageSource = new BitmapImage(new Uri("res/board.jpg", UriKind.Relative));
                //imageBrush.Viewbox = new Rect(0, 0, imageBrush.ImageSource.Width, imageBrush.ImageSource.Height);
                imageBrush.Viewbox = new Rect(0, 0, 100, 100);
                //imageBrush.TileMode = TileMode.FlipXY;
                imageBrush.TileMode     = TileMode.Tile;
                imageBrush.ViewboxUnits = BrushMappingMode.Absolute;
                imageBrush.Viewport     = new Rect(0, 0, 0.1, 0.1);

                MaterialGroup materialGroup = new MaterialGroup();
                //materialGroup.Children.Add(new SpecularMaterial(new SolidColorBrush(Colors.Gray), 1024));
                //materialGroup.Children.Add(new DiffuseMaterial(new SolidColorBrush(Colors.GreenYellow)));
                materialGroup.Children.Add(new DiffuseMaterial(imageBrush));

                boardGeometryModel.BackMaterial = materialGroup;

                ModelVisual3D moleculeSetModel = new ModelVisual3D();
                moleculeSetModel.Content = boardGeometryModel;
                gameWindows.model.Children.Add(moleculeSetModel);
            }

            gridMapOrigin = new Point3D(-0.5 * length, -0.5 * width, -0.5 * height);

            //画球体
            {
                SphereMesh sphereMesh = new SphereMesh();
                sphereMesh.Slices = 72 / 1;
                sphereMesh.Stacks = 36 / 1;
                sphereMesh.Radius = radius;
                MeshGeometry3D sphere = sphereMesh.Geometry;


                //用于保存所有小球的Model
                moleculeModel3DGroup = new Model3DGroup();

                Material material         = (Material)gameWindows.viewport.Resources["ER_Vector___Glossy_Yellow___MediumMR2"];
                Material specularMaterial = new SpecularMaterial(new SolidColorBrush(Color.FromArgb(255, 255, 255, 255)), 1024);

                //定义二维数组,分别存放molelecule的数量,和x,y,z的坐标
                double[,] moleculePositionSet = new double[moleculeNum, 3];
                moleculePositionSet[0, 0]     = 50;
                moleculePositionSet[0, 1]     = 0;
                moleculePositionSet[0, 2]     = 0;
                //layer1 begin
                moleculePositionSet[1, 0] = 0.0000000001;
                moleculePositionSet[1, 1] = 0;
                moleculePositionSet[1, 2] = 0;
                //layer2
                moleculePositionSet[2, 0] = -8;
                moleculePositionSet[2, 1] = 4.8;
                moleculePositionSet[2, 2] = 0;
                moleculePositionSet[3, 0] = -8;
                moleculePositionSet[3, 1] = -2.4;
                moleculePositionSet[3, 2] = 4;
                moleculePositionSet[4, 0] = -8;
                moleculePositionSet[4, 1] = -2.4;
                moleculePositionSet[4, 2] = -4;
                //layer3
                moleculePositionSet[5, 0] = -16;
                moleculePositionSet[5, 1] = 9.6;
                moleculePositionSet[5, 2] = 0;
                moleculePositionSet[6, 0] = -16;
                moleculePositionSet[6, 1] = 2.4;
                moleculePositionSet[6, 2] = 4;
                moleculePositionSet[7, 0] = -16;
                moleculePositionSet[7, 1] = 2.4;
                moleculePositionSet[7, 2] = -4;

                moleculePositionSet[8, 0]  = -16;
                moleculePositionSet[8, 1]  = -4.8;
                moleculePositionSet[8, 2]  = 0;
                moleculePositionSet[9, 0]  = -16;
                moleculePositionSet[9, 1]  = -4.8;
                moleculePositionSet[9, 2]  = 8;
                moleculePositionSet[10, 0] = -16;
                moleculePositionSet[10, 1] = -4.8;
                moleculePositionSet[10, 2] = -8;

                //layer4 begin
                moleculePositionSet[11, 0] = -24;
                moleculePositionSet[11, 1] = 14.4;
                moleculePositionSet[11, 2] = 0;
                moleculePositionSet[12, 0] = -24;
                moleculePositionSet[12, 1] = 7.2;
                moleculePositionSet[12, 2] = 4;
                moleculePositionSet[13, 0] = -24;
                moleculePositionSet[13, 1] = 7.2;
                moleculePositionSet[13, 2] = -4;
                moleculePositionSet[14, 0] = -24;
                moleculePositionSet[14, 1] = 0;
                moleculePositionSet[14, 2] = 0;
                moleculePositionSet[15, 0] = -24;
                moleculePositionSet[15, 1] = 0;
                moleculePositionSet[15, 2] = 8;
                moleculePositionSet[16, 0] = -24;
                moleculePositionSet[16, 1] = 0;
                moleculePositionSet[16, 2] = -8;
                moleculePositionSet[17, 0] = -24;
                moleculePositionSet[17, 1] = -7.2;
                moleculePositionSet[17, 2] = 4;
                moleculePositionSet[18, 0] = -24;
                moleculePositionSet[18, 1] = -7.2;
                moleculePositionSet[18, 2] = -4;
                moleculePositionSet[19, 0] = -24;
                moleculePositionSet[19, 1] = -7.2;
                moleculePositionSet[19, 2] = 12;
                moleculePositionSet[20, 0] = -24;
                moleculePositionSet[20, 1] = -7.2;
                moleculePositionSet[20, 2] = -12;

                for (int i = 0; (i < moleculeNum); i++)
                {
                    //创建并初始化molecule的属性
                    Molecule molecule = new Molecule()
                    {
                        //position = new Point3D(GetRandomInRange(positionRange), GetRandomInRange(positionRange), GetRandomInRange(positionRange)),
                        position = new Point3D(moleculePositionSet[i, 0], moleculePositionSet[i, 1], moleculePositionSet[i, 2]),
                        //currentVelocity = new Vector3D(GetRandomInRange(velocityRange), GetRandomInRange(velocityRange), GetRandomInRange(velocityRange)),
                        mass   = 1,
                        radius = radius
                    };

                    //创建每个球体的GeometryModel并添加进显示窗口
                    MaterialGroup materialGroup = new MaterialGroup();

                    int R, G, B;

                    while (true)
                    {
                        R = random.Next(255);
                        G = random.Next(255);
                        B = random.Next(255);

                        if (R + G + B < 350)
                        {
                            continue;
                        }
                        else
                        {
                            break;
                        }
                    }

                    DiffuseMaterial diffuseMaterial = new DiffuseMaterial(new SolidColorBrush(Color.FromRgb((byte)R, (byte)G, (byte)B)));
                    materialGroup.Children.Add(diffuseMaterial);
                    materialGroup.Children.Add(specularMaterial);

                    molecule.MoleculeGeometryModel = new GeometryModel3D(sphere, materialGroup);
                    //molecule.MoleculeGeometryModel.BackMaterial = materialGroup;

                    molecule.MoleculeGeometryModel.Transform = new TranslateTransform3D(molecule.position.X, molecule.position.Y, molecule.position.Z);

                    moleculeModel3DGroup.Children.Add(molecule.MoleculeGeometryModel);

                    MoleculeSet.Add(molecule);
                }

                {
                    ModelVisual3D moleculeSetModel = new ModelVisual3D();
                    moleculeSetModel.Content = moleculeModel3DGroup;
                    gameWindows.model.Children.Add(moleculeSetModel);
                }

                //设置白球
                whiteBall = MoleculeSet[0];
                ((MaterialGroup)whiteBall.MoleculeGeometryModel.Material).Children[0] = new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(255, 255, 255)));
                //Material specularMaterial = new SpecularMaterial(new SolidColorBrush(Color.FromArgb(255, 255, 255, 255)), 1024);
                //DiffuseMaterial white= new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(255,255,255)));
                //whiteBall.Add(white);
            }

            //画球棍
            stick.Length = 120;
            stick.Init(gameWindows.model, new Vector3D(whiteBall.position.X, whiteBall.position.Y, whiteBall.position.Z), shotDirection);
            stick.Visible = false;

            //初始化碰撞检测引擎
            CDE.InitCollisionDetectionEngine(MoleculeSet, gridMapOrigin, length, width, height, radius * 2);

            CDE.CollisionResponse += delegate(int index1, int index2)
            {
                gameWindows.PlayBallCollisionSound();
                gameWindows.myMediaElement.Stop();
                gameWindows.myMediaElement.Play();

                PhysicEngine.UpdateVelocityByCollide(MoleculeSet[index1].position, MoleculeSet[index2].position, ref MoleculeSet[index1].currentVelocity, ref MoleculeSet[index2].currentVelocity,
                                                     MoleculeSet[index1].mass, MoleculeSet[index2].mass, MoleculeSet[index1].radius, MoleculeSet[index2].radius);
            };

            CDE.CollideWithWall += delegate(int index)
            {
                Molecule m = MoleculeSet[index];

                if (m.position.X < gridMapOrigin.X + m.radius)
                {
                    m.position.X        = gridMapOrigin.X + m.radius;
                    m.currentVelocity.X = -m.currentVelocity.X;

                    if (BallInHole(m))
                    {
                        BallInHoleList.Add(m);
                        gameWindows.PlayBallCollisionWithWallSound();
                    }
                    else
                    {
                        gameWindows.PlayBallInHoleSound();
                    }
                }
                if (m.position.X > gridMapOrigin.X + length - m.radius)
                {
                    m.position.X        = gridMapOrigin.X + length - m.radius;
                    m.currentVelocity.X = -m.currentVelocity.X;

                    if (BallInHole(m))
                    {
                        BallInHoleList.Add(m);
                        gameWindows.PlayBallCollisionWithWallSound();
                    }
                    else
                    {
                        gameWindows.PlayBallInHoleSound();
                    }
                }

                if (m.position.Y < gridMapOrigin.Y + m.radius)
                {
                    m.position.Y        = gridMapOrigin.Y + m.radius;
                    m.currentVelocity.Y = -m.currentVelocity.Y;

                    if (BallInHole(m))
                    {
                        BallInHoleList.Add(m);
                        gameWindows.PlayBallCollisionWithWallSound();
                    }
                    else
                    {
                        gameWindows.PlayBallInHoleSound();
                    }
                }
                if (m.position.Y > gridMapOrigin.Y + width - m.radius)
                {
                    m.position.Y        = gridMapOrigin.Y + width - m.radius;
                    m.currentVelocity.Y = -m.currentVelocity.Y;

                    if (BallInHole(m))
                    {
                        BallInHoleList.Add(m);
                        gameWindows.PlayBallCollisionWithWallSound();
                    }
                    else
                    {
                        gameWindows.PlayBallInHoleSound();
                    }
                }

                if (m.position.Z < gridMapOrigin.Z + m.radius)
                {
                    m.position.Z        = gridMapOrigin.Z + m.radius;
                    m.currentVelocity.Z = -m.currentVelocity.Z;

                    if (BallInHole(m))
                    {
                        BallInHoleList.Add(m);
                        gameWindows.PlayBallCollisionWithWallSound();
                    }
                    else
                    {
                        gameWindows.PlayBallInHoleSound();
                    }
                }
                if (m.position.Z > gridMapOrigin.Z + height - m.radius)
                {
                    m.position.Z        = gridMapOrigin.Z + height - m.radius;
                    m.currentVelocity.Z = -m.currentVelocity.Z;

                    if (BallInHole(m))
                    {
                        BallInHoleList.Add(m);
                        gameWindows.PlayBallCollisionWithWallSound();
                    }
                    else
                    {
                        gameWindows.PlayBallInHoleSound();
                    }
                }
            };
        }