示例#1
0
        public TerrainDeformableCoverContainer(RenderManager RenderManager, WorldFile File, TerrainGeometryContainer TerrainGeometry, SettingsContainer WorldSettings, SceneRenderTarget FromBelowDepth)
        {
            WorldFile = File;
            Name      = "Cover: " + File.FileName;
            InitializeGeometry(RenderManager);

            FromBelowDepthTarget = FromBelowDepth;

            TerrainBottom = TerrainGeometry;

            DisplacementTarget = new RenderTarget2D(RenderManager.Graphics, FromBelowDepthTarget.GetWidth(), FromBelowDepthTarget.GetHeight(), false, SurfaceFormat.Single, DepthFormat.Depth24Stencil8);

            DisplacementSimulator = new OffScreenTarget("Displacement", RenderManager.Graphics, RenderManager.Shaders["TerrainCoverDisplacement"].Clone());

            DisplacementSimulator.SetTargetPing(DisplacementTarget);

            DisplacementSimulator.AttachOutPut("HeightMap", Geom.Shader);
            DisplacementSimulator.AddParameter("HeightMap", TerrainGeometry.HeightMap);
            DisplacementSimulator.AddParameter("FarPlane", WorldSettings.FromBelowFarPlane);
            DisplacementSimulator.AddParameter("DepthOffset", WorldFile.TerrainDepth);

            DisplacementSimulator.DebugTextureName = "SceneDepthDebug";

            FromBelowDepthTarget.Attach(DisplacementSimulator.Shader);
            RenderManager.AddOffScreenTarget(DisplacementSimulator);
        }
示例#2
0
        private void GenerateTerrainGeometries()
        {
            SettingsContainer Settings         = FileManager.MasteryFile.Settings;
            List <WorldFile>  ActiveWorldFiles = FileManager.MasteryFile.ActiveWorldFiles;

            for (int i = 0; i < ActiveWorldFiles.Count; i++)
            {
                bool HasGeometry = false;
                for (int j = 0; j < TerrainGeometries.Count; j++)
                {
                    if (ActiveWorldFiles[i] == TerrainGeometries[j].WorldFile)
                    {
                        HasGeometry = true;
                    }
                }

                if (!HasGeometry)
                {
                    Node3D Node = null;
                    for (int j = 0; j < RenderNodeLayers.Count; j++)
                    {
                        if (RenderNodeLayers[j].Name.Replace("Layer:", "") == ActiveWorldFiles[i].LODID + "")
                        {
                            Node = RenderNodeLayers[j];
                        }
                    }

                    if (Node != null)
                    {
                        TerrainGeometryContainer NewContainer = new TerrainGeometryContainer(Render, Settings, ActiveWorldFiles[i]);

                        //TerrainDeformableCoverContainer NewDeformedCover = new TerrainDeformableCoverContainer(Render,ActiveWorldFiles[i],NewContainer, Settings,FromBelowDepthTarget);

                        TerrainWaterContainer WaterContainer = new TerrainWaterContainer(
                            Render, NewContainer, ReflectionNode, RefractionNode, PropNode, ActiveWorldFiles[i], Settings, ActiveWorldFiles[i].GetPosition(), ReflectionRenderTarget, RefractionRenderTarget, DepthMapRenderTarget, FromBelowDepthTarget);

                        DepthMapRenderTarget.DebugTextureName = "SceneWaterDepthDebug";

                        TerrainGeometries.Add(NewContainer);
                        //DeformedTerrainGeometries.Add(NewDeformedCover);

                        WaterGeometries.Add(WaterContainer);

                        ReflectionNode.Attach(NewContainer.TerrainGeometry);
                        RefractionNode.Attach(NewContainer.TerrainGeometry);

                        Node.Attach(WaterContainer.Geom);
                        //Node.Attach(NewDeformedCover.Geom);
                        Node.Attach(NewContainer.TerrainGeometry);
                    }
                }
            }
        }
示例#3
0
        public CollisionResults RayCastTerrain(Vector3 Origin, Vector3 Direction, int LOD, float Range, bool HasCurrentChunkLock)
        {
            if (FileManager.MasteryFile == null)
            {
                return(new CollisionResults());
            }

            SettingsContainer Settings         = FileManager.MasteryFile.Settings;
            List <WorldFile>  ActiveWorldFiles = FileManager.MasteryFile.ActiveWorldFiles;

            List <Vector2> QuadCheck = new List <Vector2>();
            List <Vector3> Vertices  = new List <Vector3>();

            CollisionResults Results = new CollisionResults();

            int CurrentWorldFileLOD = FileManager.CurrentWorldFile == null ? 0 : FileManager.CurrentWorldFile.LODID;

            if (HasCurrentChunkLock)
            {
                if (FileManager.CurrentWorldFile != null)
                {
                    float TerrainScale = FileManager.CurrentWorldFile.TerrainScale;
                    int[,] HeightMap = FileManager.CurrentWorldFile.HeightMap;

                    float Precision = TerrainScale * 0.5f;

                    for (float j = 0; j < Range; j += Precision)
                    {
                        Vector3 CurrentPosition       = Origin + (Direction * j);
                        Vector3 CurrentVertexPosition = (CurrentPosition - FileManager.CurrentWorldFile.GetPosition()) / TerrainScale;

                        int VX = (int)Math.Floor(CurrentVertexPosition.X);
                        int VY = (int)Math.Floor(CurrentVertexPosition.Y);

                        if (VX < 0 || VY < 0 || VX >= HeightMap.GetLength(0) - 1 || VY >= HeightMap.GetLength(1) - 1)
                        {
                            continue;
                        }

                        Vector3[] QuadVertices =
                        {
                            new Vector3(0,                       0, HeightMap[VX,     VY]),
                            new Vector3(TerrainScale,            0, HeightMap[VX + 1, VY]),
                            new Vector3(0,            TerrainScale, HeightMap[VX,     VY + 1]),
                            new Vector3(TerrainScale, TerrainScale, HeightMap[VX + 1, VY + 1])
                        };

                        if (!QuadCheck.Contains(new Vector2(VX, VY)))
                        {
                            QuadCheck.Add(new Vector2(VX, VY));

                            Vector3 VertexWorldPosition = FileManager.CurrentWorldFile.GetPosition() + new Vector3(VX * TerrainScale, VY * TerrainScale, 0);
                            for (int k = 0; k < Indices.Length; k += 3)
                            {
                                Vector3 V0 = VertexWorldPosition + QuadVertices[Indices[k]];
                                Vector3 V1 = VertexWorldPosition + QuadVertices[Indices[k + 1]];
                                Vector3 V2 = VertexWorldPosition + QuadVertices[Indices[k + 2]];

                                //if (CollisionUtil.Intersect(Vertices[i], Vertices[i + 1], Vertices[i + 2], Origin, Direction))
                                //{
                                if (CollisionUtil.Intersect(V0, V1, V2, Origin, Direction))
                                {
                                    TerrainGeometryContainer TerrainGeom = TerrainGeometries.Where(x => x.WorldFile.FileName == FileManager.CurrentWorldFile.FileName).First();

                                    Vector3         CollisionPoint = CollisionUtil.GetCollisionPoint(V0, V1, V2, Origin, Direction);
                                    CollisionResult Result         = new CollisionResult(
                                        V0,
                                        V1,
                                        V2,
                                        Origin,
                                        CollisionPoint,
                                        TerrainGeom.TerrainGeometry);
                                    Results.Add(Result);
                                }

                                //Vertices.Add(VertexWorldPosition + QuadVertices[Indices[k]]);
                            }
                        }
                    }
                }
            }
            else
            {
                int PX = (int)Math.Floor(Origin.X / Settings.ChunkSize);
                int PY = (int)Math.Floor(Origin.Y / Settings.ChunkSize);

                for (int i = 0; i < ActiveWorldFiles.Count; i++)
                {
                    int ValidLOD = CurrentWorldFileLOD == -1 ? ActiveWorldFiles[i].LODID : CurrentWorldFileLOD;

                    if (Math.Abs(ActiveWorldFiles[i].IDX - PX) <= Math.Ceiling(Settings.ChunkDistance / Range) && Math.Abs(ActiveWorldFiles[i].IDY - PY) <= Math.Ceiling(Settings.ChunkDistance / Range) && ActiveWorldFiles[i].LODID == ValidLOD)
                    {
                        float TerrainScale = ActiveWorldFiles[i].TerrainScale;
                        float HeightScale  = ActiveWorldFiles[i].HeightScale;
                        int[,] HeightMap = ActiveWorldFiles[i].HeightMap;

                        float Precision = TerrainScale * 0.5f;

                        for (float j = 0; j < Range; j += Precision)
                        {
                            Vector3 CurrentPosition       = Origin + (Direction * j);
                            Vector3 CurrentVertexPosition = (CurrentPosition - ActiveWorldFiles[i].GetPosition()) / TerrainScale;

                            int VX = (int)Math.Floor(CurrentVertexPosition.X);
                            int VY = (int)Math.Floor(CurrentVertexPosition.Y);

                            if (VX < 0 || VY < 0 || VX >= HeightMap.GetLength(0) - 1 || VY >= HeightMap.GetLength(1) - 1)
                            {
                                continue;
                            }
                            Vector3[] QuadVertices =
                            {
                                new Vector3(0,                       0, HeightMap[VX,     VY] * HeightScale),
                                new Vector3(TerrainScale,            0, HeightMap[VX + 1, VY] * HeightScale),
                                new Vector3(0,            TerrainScale, HeightMap[VX,     VY + 1] * HeightScale),
                                new Vector3(TerrainScale, TerrainScale, HeightMap[VX + 1, VY + 1] * HeightScale)
                            };

                            if (!QuadCheck.Contains(new Vector2(VX, VY)))
                            {
                                QuadCheck.Add(new Vector2(VX, VY));

                                Vector3 VertexWorldPosition = ActiveWorldFiles[i].GetPosition() + new Vector3(VX * TerrainScale, VY * TerrainScale, 0);
                                for (int k = 0; k < Indices.Length; k += 3)
                                {
                                    Vector3 V0 = VertexWorldPosition + QuadVertices[Indices[k]];
                                    Vector3 V1 = VertexWorldPosition + QuadVertices[Indices[k + 1]];
                                    Vector3 V2 = VertexWorldPosition + QuadVertices[Indices[k + 2]];

                                    if (CollisionUtil.Intersect(V0, V1, V2, Origin, Direction))
                                    {
                                        TerrainGeometryContainer TerrainGeom = TerrainGeometries.Where(x => x.WorldFile.FileName == FileManager.CurrentWorldFile.FileName).First();

                                        Vector3         CollisionPoint = CollisionUtil.GetCollisionPoint(V0, V1, V2, Origin, Direction);
                                        CollisionResult Result         = new CollisionResult(
                                            V0,
                                            V1,
                                            V2,
                                            Origin,
                                            CollisionPoint,
                                            TerrainGeom.TerrainGeometry);
                                        Results.Add(Result);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(Results);
        }
示例#4
0
        public CollisionResults RayCastTerrain(Vector3 Origin, Vector3 Direction, float Range, WorldFile File)
        {
            if (FileManager.MasteryFile == null)
            {
                return(new CollisionResults());
            }

            SettingsContainer Settings         = FileManager.MasteryFile.Settings;
            List <WorldFile>  ActiveWorldFiles = FileManager.MasteryFile.ActiveWorldFiles;

            List <Vector2>   QuadCheck = new List <Vector2>();
            CollisionResults Results   = new CollisionResults();

            float TerrainScale = File.TerrainScale;
            float HeighScale   = File.HeightScale;

            int[,] HeightMap = File.HeightMap;

            float Precision = TerrainScale * 0.5f;

            for (float j = 0; j < Range; j += Precision)
            {
                Vector3 CurrentPosition       = Origin + (Direction * j);
                Vector3 CurrentVertexPosition = (CurrentPosition - File.GetPosition()) / TerrainScale;

                int VX = (int)Math.Floor(CurrentVertexPosition.X);
                int VY = (int)Math.Floor(CurrentVertexPosition.Y);

                if (VX < 0 || VY < 0 || VX >= HeightMap.GetLength(0) - 1 || VY >= HeightMap.GetLength(1) - 1)
                {
                    continue;
                }

                Vector3[] QuadVertices =
                {
                    new Vector3(0,                       0, HeightMap[VX,     VY] * HeighScale),
                    new Vector3(TerrainScale,            0, HeightMap[VX + 1, VY] * HeighScale),
                    new Vector3(0,            TerrainScale, HeightMap[VX,     VY + 1] * HeighScale),
                    new Vector3(TerrainScale, TerrainScale, HeightMap[VX + 1, VY + 1] * HeighScale)
                };

                if (!QuadCheck.Contains(new Vector2(VX, VY)))
                {
                    QuadCheck.Add(new Vector2(VX, VY));

                    Vector3 VertexWorldPosition = File.GetPosition() + new Vector3(VX * TerrainScale, VY * TerrainScale, 0);
                    for (int k = 0; k < Indices.Length; k += 3)
                    {
                        Vector3 V0 = VertexWorldPosition + QuadVertices[Indices[k]];
                        Vector3 V1 = VertexWorldPosition + QuadVertices[Indices[k + 1]];
                        Vector3 V2 = VertexWorldPosition + QuadVertices[Indices[k + 2]];

                        if (CollisionUtil.Intersect(V0, V1, V2, Origin, Direction))
                        {
                            TerrainGeometryContainer TerrainGeom = TerrainGeometries.Where(x => x.WorldFile.FileName == FileManager.CurrentWorldFile.FileName).First();

                            Vector3         CollisionPoint = CollisionUtil.GetCollisionPoint(V0, V1, V2, Origin, Direction);
                            CollisionResult Result         = new CollisionResult(
                                V0,
                                V1,
                                V2,
                                Origin,
                                CollisionPoint,
                                TerrainGeom.TerrainGeometry);
                            Results.Add(Result);
                        }
                    }
                }
            }
            return(Results);
        }
示例#5
0
 public CollisionResults RayCastTerrain(Vector3 Origin, Vector3 Direction, float Range, TerrainGeometryContainer TerrainGeometry)
 {
     return(RayCastTerrain(Origin, Direction, Range, TerrainGeometry.WorldFile));
 }
示例#6
0
        //bool FrameCycle = false;

        public TerrainWaterContainer(RenderManager RenderManager, TerrainGeometryContainer TerrainGeometry, Node3D ReflectionNode, Node3D RefractionNode, Node3D PropNode, WorldFile File, SettingsContainer Settings, Vector3 Pos, SceneRenderTarget ReflectionTarget, SceneRenderTarget RefractionTarget, SceneRenderTarget DepthTarget, SceneRenderTarget FromBelowTarget)
        {
            WorldFile     = File;
            WorldSettings = Settings;
            Terrain       = TerrainGeometry;
            Name          = "Water: " + File.FileName;
            Position      = Pos;

            ReflectionRenderTarget = ReflectionTarget;
            RefractionRenderTarget = RefractionTarget;
            DepthMapRenderTarget   = DepthTarget;

            WaterData0 = new Texture2D(RenderManager.Graphics, WorldFile.FlowXMap.GetLength(0), WorldFile.FlowXMap.GetLength(1));
            WaterData1 = new Texture2D(RenderManager.Graphics, WorldFile.WaterMap.GetLength(0), WorldFile.WaterMap.GetLength(1), false, SurfaceFormat.Alpha8);

            WaterColorData0 = new Texture2D(RenderManager.Graphics, WorldFile.WaterColorR.GetLength(0), WorldFile.WaterColorR.GetLength(1));
            WaterColorData1 = new Texture2D(RenderManager.Graphics, WorldFile.WaterNormalMap.GetLength(0), WorldFile.WaterNormalMap.GetLength(1));
            //FoamDataMap0 = new Texture2D(RenderManager.Graphics, WorldFile.FoamRampMap0.GetLength(0), WorldFile.FoamRampMap0.GetLength(1));
            WaveData0 = new Texture2D(RenderManager.Graphics, WorldFile.WaveHeightMap.GetLength(0), WorldFile.WaveHeightMap.GetLength(1));

            InitializeGeometry(RenderManager);
            ReflectionRenderTarget.Attach(Geom);
            RefractionRenderTarget.Attach(Geom);
            DepthMapRenderTarget.Attach(Geom);

            //Depth Input
            FromBelowDepth = FromBelowTarget;

            //DepthTarget0 = new RenderTarget2D(RenderManager.Graphics, 1025, 1025, false, SurfaceFormat.Single, DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.PreserveContents);
            //DepthTarget1 = new RenderTarget2D(RenderManager.Graphics, 1025, 1025, false, SurfaceFormat.Single, DepthFormat.Depth24Stencil8, 0, RenderTargetUsage.PreserveContents);

            //SetHeight = new TextureTask(Name + "SetHeight", TaskType.FillFloat, TaskStage.PreRenderTarget, TaskUsage.FrameLimit,TaskSwitchType.PingPongOut);
            //SetHeight.AddParameter(FloatParameter.ColorFloat, WorldFile.TerrainDepth / Settings.FromBelowFarPlane);
            //SetHeight.SetRenderTarget(DepthTarget0, true);
            //SetHeight.SetRenderTarget(DepthTarget1, false);
            //SetHeight.FrameLimit = 2;

            //Begin PingPong
            //Depth
            //MergeDepth = new TextureTask("MergeDepth", TaskType.MergeMinFloat, TaskStage.PreOffScreenTarget, TaskUsage.Preserve, TaskSwitchType.PingPongIn);
            // MergeDepth.AddParameter(TextureParameter.InputTexture, FromBelowDepth.RenderTarget);

            //MergeDepth.AddParameter(TextureParameter.InputTexture2, (Texture2D)DepthTarget0, true);
            //MergeDepth.AddParameter(TextureParameter.InputTexture2, (Texture2D)DepthTarget1, false);
            //Current Frame Texture
            //MergeDepth.SetRenderTarget(DepthTarget1, true);
            //MergeDepth.SetRenderTarget(DepthTarget0, false);

            //WaterSimulation = new OffScreenTarget(RenderManager.Graphics, RenderManager.Shaders["WaterSimulation"].Clone());
            //NormalGenerator = new OffScreenTarget(RenderManager.Graphics, RenderManager.Shaders["WaterSimulation"].Clone());
            //NormalMap = new RenderTarget2D(RenderManager.Graphics, 1025, 1025);

            //FromBelowDepth.Attach(MergeDepth);
            //WaterSimulation.Attach(NormalGenerator.Shader);
            //WaterSimulation.Attach(Geom.Shader);
            WaterHeightMap = new Texture2D(RenderManager.Graphics, WorldFile.WaterHeightMap.GetLength(0), WorldFile.WaterHeightMap.GetLength(1), false, SurfaceFormat.Color);
            WaterClipMap   = new Texture2D(RenderManager.Graphics, WorldFile.WaterHeightMap.GetLength(0), WorldFile.WaterHeightMap.GetLength(1), false, SurfaceFormat.Color);
            //MergeDepth.DebugTextureName = "SceneWaterReflectionDebug";

            //NormalGenerator.Attach(Geom.Shader);
            //RenderManager.AddTextureModificationTask(SetHeight);
            //RenderManager.AddTextureModificationTask(MergeDepth);

            //RenderManager.AddOffScreenTarget(WaterSimulation);
            //RenderManager.AddOffScreenTarget(NormalGenerator);
        }