Пример #1
0
        /// <summary>
        /// Draw this model.
        /// </summary>
        /// <param name="worldTransformations">World transformations to apply on this entity (this is what you should use to draw this entity).</param>
        public override void DoEntityDraw(ref Matrix worldTransformations)
        {
            // call base draw entity
            base.DoEntityDraw(ref worldTransformations);

            // reset last radius
            _lastRadius = 0f;
            float scaleLen = worldTransformations.Scale.Length();

            // iterate model meshes
            foreach (var mesh in Model.Meshes)
            {
                // iterate over mesh effects and apply them (set world matrix etc)
                foreach (var effect in mesh.Effects)
                {
                    Materials.MaterialAPI material = effect.GetMaterial();
                    material.Apply(ref worldTransformations, ref _lastBoundingSphere);
                }

                // update last radius
                _lastRadius = System.Math.Max(_lastRadius, mesh.BoundingSphere.Radius * scaleLen);

                // draw the mesh itself
                mesh.Draw();
            }
        }
Пример #2
0
        /// <summary>
        /// Draw this model.
        /// </summary>
        /// <param name="worldTransformations">World transformations to apply on this entity (this is what you should use to draw this entity).</param>
        public override void DoEntityDraw(ref Matrix worldTransformations)
        {
            // call base draw entity
            base.DoEntityDraw(ref worldTransformations);

            // decompose transformations
            Vector3 position; Quaternion rotation; Vector3 scale;

            worldTransformations.Decompose(out scale, out rotation, out position);

            // add position offset
            position += PositionOffset;

            // create a new world matrix for the billboard
            Matrix newWorld;

            // if facing camera, create billboard world matrix
            if (FaceCamera)
            {
                // set rotation based on camera with locked axis
                if (LockedAxis != null)
                {
                    newWorld = Matrix.CreateScale(scale) *
                               Matrix.CreateConstrainedBillboard(position, GraphicsManager.ActiveCamera.Position,
                                                                 LockedAxis.Value, null, null);
                }
                // set rotation based on camera without any locked axis
                else
                {
                    newWorld = Matrix.CreateScale(scale) *
                               Matrix.CreateBillboard(position, GraphicsManager.ActiveCamera.Position,
                                                      Vector3.Up, null);
                }
            }
            // if not facing camera, just use world transformations
            else
            {
                newWorld = worldTransformations;
            }

            // update per-entity override properties
            Materials.MaterialAPI material = MaterialOverride.Apply(Material);

            // setup material
            material.Apply(ref newWorld, ref _lastBoundingSphere);

            // draw sprite
            // draw the cube vertices
            material.IterateEffectPasses((EffectPass pass) =>
            {
                GraphicsManager.GraphicsDevice.DrawUserIndexedPrimitives
                <VertexPositionNormalTexture>(
                    PrimitiveType.TriangleList,
                    _spritesheetStep.Vertices, 0, 4,
                    _spritesheetStep.Indexes, 0, 2);
            });
        }
Пример #3
0
        /// <summary>
        /// Draw this model.
        /// </summary>
        /// <param name="worldTransformations">World transformations to apply on this entity (this is what you should use to draw this entity).</param>
        public override void DoEntityDraw(ref Matrix worldTransformations)
        {
            // call base draw entity
            base.DoEntityDraw(ref worldTransformations);

            // reset last radius
            _lastRadius = 0f;
            float scaleLen = Utils.ExtendedMath.GetScale(ref worldTransformations).Length();

            // iterate model meshes
            foreach (var mesh in Model.Meshes)
            {
                // check if in this mesh we have shared materials, eg same effects used for several mesh parts
                bool gotSharedEffects = mesh.Effects.Count != mesh.MeshParts.Count;

                // iterate over mesh parts
                int index = 0;
                foreach (var meshPart in mesh.MeshParts)
                {
                    // get material for this mesh and effect index
                    Materials.MaterialAPI material = GetMaterial(mesh.Name, index);

                    // no material found? skip.
                    // note: this can happen if user set alternative materials array with less materials than original mesh file
                    if (material == null)
                    {
                        break;
                    }

                    // update per-entity override properties
                    material = MaterialOverride.Apply(material);

                    // if we don't have shared effects, eg every mesh part has its own effect, update material transformations
                    if (!gotSharedEffects)
                    {
                        material.Apply(ref worldTransformations, ref _lastBoundingSphere);
                    }

                    // apply material effect on the mesh part. note: we first store original effect in mesh part's tag.
                    meshPart.Tag    = meshPart.Effect;
                    meshPart.Effect = material.Effect;

                    // next index.
                    ++index;
                }

                // if we have shared effects, eg more than one mesh part with the same effect, we apply all materials here
                // this is to prevent applying the same material more than once
                if (gotSharedEffects)
                {
                    foreach (var effect in mesh.Effects)
                    {
                        effect.GetMaterial().Apply(ref worldTransformations, ref _lastBoundingSphere);
                    }
                }

                // update last radius
                _lastRadius = System.Math.Max(_lastRadius, mesh.BoundingSphere.Radius * scaleLen);

                // iterate mesh parts
                if (ProcessMeshParts)
                {
                    foreach (ModelMeshPart part in mesh.MeshParts)
                    {
                        // call the before-drawing-mesh-part callback
                        BeforeDrawingMeshPart(part);
                    }
                }

                // draw the mesh itself
                mesh.Draw();

                // restore original effect to mesh parts
                foreach (var meshPart in mesh.MeshParts)
                {
                    meshPart.Effect = meshPart.Tag as Effect;
                    meshPart.Tag    = null;
                }
            }
        }