Exemplo n.º 1
0
        /// <summary>
        /// Creates a graphics mesh with the triangle mesh data of the given shape and the given
        /// diffuse and specular material properties.
        /// </summary>
        /// <param name="contentManager">The contentManager manager.</param>
        /// <param name="graphicsService">The graphics service.</param>
        /// <param name="submesh">The submesh.</param>
        /// <param name="diffuse">The diffuse material color.</param>
        /// <param name="specular">The specular material color.</param>
        /// <param name="specularPower">The specular power of the material.</param>
        /// <returns>The graphics mesh.</returns>
        /// <remarks>
        /// This method does not set the bounding shape of the mesh. (The default is an infinite shape
        /// which is not optimal for performance.)
        /// </remarks>
        public static Mesh CreateMesh(ContentManager contentManager, IGraphicsService graphicsService, Submesh submesh,
                                      Vector3 diffuse, Vector3 specular, float specularPower)
        {
            Mesh mesh = new Mesh();

            mesh.Submeshes.Add(submesh);

            // Build material.
            // We could load a predefined material (*.drmat file)
            // with the content manager.
            //var material = contentManager.Load<Material>("MyMaterialName");

            // Alternatively, we can load some effects and build the material here:
            Material material = new Material();

            // We need an EffectBinding for each render pass.
            // The "Default" pass uses a BasicEffectBinding (which is an EffectBinding
            // for the XNA BasicEffect).
            // Note: The "Default" pass is not used by the DeferredLightingScreen, so
            // we could ignore this pass in this sample project.
            BasicEffectBinding defaultEffectBinding = new BasicEffectBinding(graphicsService, null)
            {
                LightingEnabled    = true,
                TextureEnabled     = true,
                VertexColorEnabled = false
            };

            defaultEffectBinding.Set("Texture", graphicsService.GetDefaultTexture2DWhite());
            defaultEffectBinding.Set("DiffuseColor", new Vector4((Vector3)diffuse, 1));
            defaultEffectBinding.Set("SpecularColor", (Vector3)specular);
            defaultEffectBinding.Set("SpecularPower", specularPower);
            material.Add("Default", defaultEffectBinding);

            // EffectBinding for the "ShadowMap" pass.
            // Note: EffectBindings which are used in a Material must be marked with
            // the EffectParameterHint Material.
            EffectBinding shadowMapEffectBinding = new EffectBinding(
                graphicsService,
                contentManager.Load <Effect>("DigitalRune\\Materials\\ShadowMap"),
                null,
                EffectParameterHint.Material);

            material.Add("ShadowMap", shadowMapEffectBinding);

            // EffectBinding for the "GBuffer" pass.
            EffectBinding gBufferEffectBinding = new EffectBinding(
                graphicsService,
                contentManager.Load <Effect>("DigitalRune\\Materials\\GBuffer"),
                null,
                EffectParameterHint.Material);

            gBufferEffectBinding.Set("SpecularPower", specularPower);
            material.Add("GBuffer", gBufferEffectBinding);

            // EffectBinding for the "Material" pass.
            EffectBinding materialEffectBinding = new EffectBinding(
                graphicsService,
                contentManager.Load <Effect>("DigitalRune\\Materials\\Material"),
                null,
                EffectParameterHint.Material);

            materialEffectBinding.Set("DiffuseTexture", graphicsService.GetDefaultTexture2DWhite());
            materialEffectBinding.Set("DiffuseColor", (Vector3)diffuse);
            materialEffectBinding.Set("SpecularColor", (Vector3)specular);
            material.Add("Material", materialEffectBinding);

            // Assign this material to the submesh.
            submesh.SetMaterial(material);

            return(mesh);
        }
Exemplo n.º 2
0
        // OnLoad() is called when the GameObject is added to the IGameObjectService.
        protected override void OnLoad()
        {
            var graphicsService   = _services.GetInstance <IGraphicsService>();
            var gameObjectService = _services.GetInstance <IGameObjectService>();

            // Check if the game object manager has another ProceduralObject instance.
            var otherProceduralObject = gameObjectService.Objects
                                        .OfType <ProceduralObject>()
                                        .FirstOrDefault(o => o != this);
            Mesh mesh;

            if (otherProceduralObject != null)
            {
                // This ProceduralObject is not the first. We re-use rigid body data and
                // the mesh from the existing instance.
                var otherBody = otherProceduralObject._rigidBody;
                _rigidBody = new RigidBody(otherBody.Shape, otherBody.MassFrame, otherBody.Material);
                mesh       = otherProceduralObject._meshNode.Mesh;
            }
            else
            {
                // This is the first ProceduralObject instance. We create a new rigid body
                // and a new mesh.
                var shape = new MinkowskiSumShape(new GeometricObject(new SphereShape(0.05f)), new GeometricObject(new BoxShape(0.5f, 0.5f, 0.5f)));
                _rigidBody = new RigidBody(shape);

                // Create a DigitalRune.Geometry.Meshes.TriangleMesh from the RigidBody's
                // shape and convert this to a DigitalRune.Graphics.Mesh.
                var triangleMesh = _rigidBody.Shape.GetMesh(0.01f, 4);
                mesh = new Mesh {
                    Name = "ProceduralObject"
                };
                var submesh = CreateSubmeshWithTexCoords(graphicsService.GraphicsDevice, triangleMesh, MathHelper.ToRadians(70));
                mesh.Submeshes.Add(submesh);

                // Next, we need a material. We can load a predefined material (*.drmat file)
                // with the content manager.
                //var material = content.Load<Material>("Default");

                // Alternatively, we can load some effects and build the material here:
                Material material = new Material();

                // We need an EffectBinding for each render pass.
                // The "Default" pass uses a BasicEffectBinding (which is an EffectBinding
                // for the XNA BasicEffect).
                // Note: The "Default" pass is not used by the DeferredLightingScreen, so
                // we could ignore this pass in this sample project.
                BasicEffectBinding defaultEffectBinding = new BasicEffectBinding(graphicsService, null)
                {
                    LightingEnabled    = true,
                    TextureEnabled     = true,
                    VertexColorEnabled = false
                };
                defaultEffectBinding.Set("Texture", graphicsService.GetDefaultTexture2DWhite());
                defaultEffectBinding.Set("DiffuseColor", new Vector3(1, 1, 1));
                defaultEffectBinding.Set("SpecularColor", new Vector3(1, 1, 1));
                defaultEffectBinding.Set("SpecularPower", 100f);
                material.Add("Default", defaultEffectBinding);

                // EffectBinding for the "ShadowMap" pass.
                // Note: EffectBindings which are used in a Material must be marked with
                // the EffectParameterHint Material.
                var           content = _services.GetInstance <ContentManager>();
                EffectBinding shadowMapEffectBinding = new EffectBinding(
                    graphicsService,
                    content.Load <Effect>("DigitalRune\\Materials\\ShadowMap"),
                    null,
                    EffectParameterHint.Material);
                material.Add("ShadowMap", shadowMapEffectBinding);

                // EffectBinding for the "GBuffer" pass.
                EffectBinding gBufferEffectBinding = new EffectBinding(
                    graphicsService,
                    content.Load <Effect>("DigitalRune\\Materials\\GBuffer"),
                    null,
                    EffectParameterHint.Material);
                gBufferEffectBinding.Set("SpecularPower", 100f);
                material.Add("GBuffer", gBufferEffectBinding);

                // EffectBinding for the "Material" pass.
                EffectBinding materialEffectBinding = new EffectBinding(
                    graphicsService,
                    content.Load <Effect>("DigitalRune\\Materials\\Material"),
                    null,
                    EffectParameterHint.Material);
                materialEffectBinding.Set("DiffuseTexture", graphicsService.GetDefaultTexture2DWhite());
                materialEffectBinding.Set("DiffuseColor", new Vector3(1, 1, 1));
                materialEffectBinding.Set("SpecularColor", new Vector3(1, 1, 1));
                material.Add("Material", materialEffectBinding);

                // Assign this material to the submesh.
                submesh.SetMaterial(material);
            }

            // Create a scene graph node for the mesh.
            _meshNode = new MeshNode(mesh);

            // Set a random pose.
            var randomPosition = new Vector3F(
                RandomHelper.Random.NextFloat(-10, 10),
                RandomHelper.Random.NextFloat(2, 5),
                RandomHelper.Random.NextFloat(-20, 0));

            _rigidBody.Pose     = new Pose(randomPosition, RandomHelper.Random.NextQuaternionF());
            _meshNode.PoseWorld = _rigidBody.Pose;

            // Add mesh node to scene graph.
            var scene = _services.GetInstance <IScene>();

            scene.Children.Add(_meshNode);

            // Add rigid body to the physics simulation.
            var simulation = _services.GetInstance <Simulation>();

            simulation.RigidBodies.Add(_rigidBody);
        }