Beispiel #1
0
 public TerrainMapRenderSystem()
 {
     sphereConvert      = new BoundingSphereToWorldSpace();
     boxConvert         = new BoundingBoxToWorldSpace();
     modelRenderMethods = new ModelRenderMethods();
 }
Beispiel #2
0
        public void Render(GraphicsDevice graphicsDevice, GameTime gameTime)
        {
            if (renderBoxInitialised.Equals(false))
            {
                boxConvert           = new BoundingBoxToWorldSpace();
                boxRenderer          = new DebugRenderBoundingBox(graphicsDevice);
                renderBoxInitialised = true;
            }

            Entity              e                  = ComponentManager.Instance.GetFirstEntityOfType <TerrainMapComponent>();
            Entity              c                  = ComponentManager.Instance.GetFirstEntityOfType <CameraComponent>();
            CameraComponent     camera             = ComponentManager.Instance.GetEntityComponent <CameraComponent>(c);
            TerrainMapComponent terrainComponent   = ComponentManager.Instance.GetEntityComponent <TerrainMapComponent>(e);
            TransformComponent  transformComponent = ComponentManager.Instance.GetEntityComponent <TransformComponent>(e);

            if (terrainComponent != null)
            {
                if (transformComponent != null)
                {
                    /*RasterizerState r = new RasterizerState();
                     * r.CullMode = CullMode.None;
                     * //r.FillMode = FillMode.WireFrame;
                     * graphicsDevice.RasterizerState = r;//*/

                    terrainComponent.numChunksInView = 0;
                    terrainComponent.numModelsInView = 0;

                    for (int i = 0; i < terrainComponent.terrainChunks.Count; ++i)
                    {
                        terrainComponent.terrainChunks[i].effect.TextureEnabled     = true;
                        terrainComponent.terrainChunks[i].effect.VertexColorEnabled = false;
                        terrainComponent.terrainChunks[i].effect.Texture            = terrainComponent.terrainChunks[i].terrainTex;
                        terrainComponent.terrainChunks[i].effect.Projection         = camera.projectionMatrix;
                        terrainComponent.terrainChunks[i].effect.View  = camera.viewMatrix;
                        terrainComponent.terrainChunks[i].effect.World = transformComponent.World * Matrix.CreateTranslation(terrainComponent.terrainChunks[i].offsetPosition);
                        terrainComponent.terrainChunks[i].effect.EnableDefaultLighting();

                        BoundingBox box = boxConvert.ConvertBoundingBoxToWorldCoords(terrainComponent.terrainChunks[i].boundingBox, terrainComponent.terrainChunks[i].effect.World);

                        if (box.Intersects(camera.cameraFrustrum))
                        {
                            foreach (EffectPass p in terrainComponent.terrainChunks[i].effect.CurrentTechnique.Passes)
                            {
                                p.Apply();
                                graphicsDevice.Indices = terrainComponent.terrainChunks[i].iBuffer;
                                graphicsDevice.SetVertexBuffer(terrainComponent.terrainChunks[i].vBuffer);
                                graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, terrainComponent.terrainChunks[0].indicesLenDiv3);
                            }
                            boxRenderer.RenderBoundingBox(terrainComponent.terrainChunks[i].boundingBox, terrainComponent.terrainChunks[i].effect.World, camera.viewMatrix, camera.projectionMatrix);
                            terrainComponent.numChunksInView++;

                            //go through all the entities in the current chunk
                            foreach (Entity staticModelEnt in terrainComponent.terrainChunks[i].staticModels)
                            {
                                //get the transform and modelcomponent
                                TransformComponent           tComp = ComponentManager.Instance.GetEntityComponent <TransformComponent>(staticModelEnt);
                                ModelComponent               mComp = ComponentManager.Instance.GetEntityComponent <ModelComponent>(staticModelEnt);
                                ModelBoundingSphereComponent sp    = ComponentManager.Instance.GetEntityComponent <ModelBoundingSphereComponent>(staticModelEnt);

                                BoundingSphere newSphere = sphereConvert.ConvertBoundingSphereToWorldCoords(sp.sphere, Matrix.CreateTranslation(tComp.Position));
                                newSphere.Radius += 0.5f;

                                foreach (var pair in mComp.meshTransforms)
                                {
                                    //update the model transforms
                                    ChangeBoneTransform(mComp, pair.Key, pair.Value);
                                }

                                //if they use the basic effect
                                if (mComp.useBasicEffect)
                                {
                                    //and are within the camera frustrum
                                    if (camera.cameraFrustrum.Contains(newSphere) != ContainmentType.Disjoint)
                                    {
                                        terrainComponent.numModelsInView++;

                                        //render the model with basic effects
                                        modelRenderMethods.RenderBasicEffectModel(mComp, tComp, camera, false);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #3
0
        public void Render(GraphicsDevice graphicsDevice, GameTime gameTime)
        {
            if (renderBoxInitialised.Equals(false))
            {
                boxConvert           = new BoundingBoxToWorldSpace();
                boxRenderer          = new DebugRenderBoundingBox(graphicsDevice);
                renderBoxInitialised = true;
            }

            List <List <Entity> > sceneEntities = SceneManager.Instance.GetActiveScene().GetAllLayers();
            Entity              Ecamera         = ComponentManager.Instance.GetFirstEntityOfType <CameraComponent>();
            CameraComponent     c            = ComponentManager.Instance.GetEntityComponent <CameraComponent>(Ecamera);
            Entity              modelCount   = ComponentManager.Instance.GetFirstEntityOfType <ModelCountComponent>();
            ModelCountComponent modelsInView = ComponentManager.Instance.GetEntityComponent <ModelCountComponent>(modelCount);

            if (sceneEntities == null || c == null)
            {
                return;
            }
            if (modelsInView != null)
            {
                modelsInView.numModelsInView = 0;
            }

            for (int i = 0; i < sceneEntities.Count; ++i)
            {
                foreach (Entity entity in sceneEntities[i])
                {
                    if (entity.Visible)
                    {
                        ModelComponent            m = ComponentManager.Instance.GetEntityComponent <ModelComponent>(entity);
                        ModelBoundingBoxComponent b = ComponentManager.Instance.GetEntityComponent <ModelBoundingBoxComponent>(entity);

                        //if the entity has a model component
                        if (m != null)
                        {
                            if (m.meshTransforms.Count > 0)
                            {
                                //loop through all mesh transforms in the model
                                foreach (var pair in m.meshTransforms)
                                {
                                    //update the model transforms
                                    ChangeBoneTransform(m, pair.Key, pair.Value);
                                }
                            }
                            TransformComponent t = ComponentManager.Instance.GetEntityComponent <TransformComponent>(entity);
                            //if there is a transform component
                            if (t != null)
                            {
                                //if the model has bounding boxes
                                if (b != null)
                                {
                                    modelInCameraFrustrum = false;
                                    foreach (BoundingBox bb in b.boundingBoxes)
                                    {
                                        Vector3 leftRightVector = Matrix.Transpose(c.viewMatrix).Right;
                                        boxRenderer.RenderBoundingBox(bb, t.World, c.viewMatrix, c.projectionMatrix);
                                        BoundingBox    box = boxConvert.ConvertBoundingBoxToWorldCoords(b.boundingBoxes[0], Matrix.CreateTranslation(t.Position));
                                        BoundingSphere s   = BoundingSphere.CreateFromBoundingBox(box);

                                        if (c.cameraFrustrum.Contains(s) != ContainmentType.Disjoint)
                                        {
                                            modelInCameraFrustrum = true;
                                            break;
                                        }
                                    }
                                    if (modelInCameraFrustrum == true)
                                    {
                                        if (modelsInView != null)
                                        {
                                            modelsInView.numModelsInView++;
                                        }

                                        //If the model uses monogames built-in basic effects
                                        if (m.useBasicEffect)
                                        {
                                            //render the model with basic effects
                                            modelRenderMethods.RenderBasicEffectModel(m, t, c, renderOnlyNonStaticModels);
                                        }
                                    }
                                }
                                //if the model doesn't have any bounding boxes
                                else
                                {
                                    //If the model uses monogames built-in basic effects
                                    if (m.useBasicEffect)
                                    {
                                        //render the model with basic effects
                                        modelRenderMethods.RenderBasicEffectModel(m, t, c, renderOnlyNonStaticModels);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }