예제 #1
0
파일: Terrain.cs 프로젝트: johang88/triton
        public Terrain(Backend backend, TerrainData data, int chunks, int resolution, Resources.Material material, Resources.Material compositeMaterial)
        {
            if (backend == null)
                throw new ArgumentNullException("backend");
            if (data == null)
                throw new ArgumentNullException("data");

            Backend = backend;
            Data = data;

            var chunkSize = new Vector2(data.WorldSize.X, data.WorldSize.Z) / (float)chunks;

            // Setup the chunks
            for (var y = 0; y < chunks; y++)
            {
                for (var x = 0; x < chunks; x++)
                {
                    var chunkPosition = new Vector2(chunkSize.X * x, chunkSize.Y * y);
                    var chunk = new Chunk(this, chunkPosition, chunkSize, resolution, chunks);
                    // TODO: Setup correct materials per chunk so that we can have different splatmaps etc ...
                    chunk.Mesh.Material = material;
                    chunk.SplatMaterial = material;
                    chunk.CompositeMaterial = compositeMaterial;
                    Chunks.Add(chunk);
                }
            }

            Mesh = new Resources.Mesh("terrain", "");
            Mesh.SubMeshes = Chunks.Select(c => c.Mesh).ToArray();
            Mesh.State = Common.ResourceLoadingState.Loaded;
        }
예제 #2
0
 public SkeletonInstance(Resources.Mesh mesh)
 {
     Mesh = mesh;
 }
예제 #3
0
        public DeferredRenderer(Common.ResourceManager resourceManager, Backend backend, int width, int height)
        {
            if (resourceManager == null)
                throw new ArgumentNullException("resourceManager");
            if (backend == null)
                throw new ArgumentNullException("backend");

            Settings.ShadowQuality = ShadowQuality.High;
            Settings.EnableShadows = true;
            Settings.ShadowRenderDistance = 128.0f;

            ResourceManager = resourceManager;
            Backend = backend;

            ScreenSize = new Vector2(width, height);

            GBuffer = Backend.CreateRenderTarget("gbuffer", new Definition(width, height, true, new List<Definition.Attachment>()
            {
                new Definition.Attachment(Definition.AttachmentPoint.Color, Renderer.PixelFormat.Rgba, Renderer.PixelInternalFormat.Rgba16f, Renderer.PixelType.Float, 0),
                new Definition.Attachment(Definition.AttachmentPoint.Color, Renderer.PixelFormat.Rgba, Renderer.PixelInternalFormat.Rgba16f, Renderer.PixelType.UnsignedByte, 1),
                new Definition.Attachment(Definition.AttachmentPoint.Color, Renderer.PixelFormat.Rgba, Renderer.PixelInternalFormat.Rgba16f, Renderer.PixelType.Float, 2),
                new Definition.Attachment(Definition.AttachmentPoint.Depth, Renderer.PixelFormat.DepthComponent, Renderer.PixelInternalFormat.DepthComponent24, Renderer.PixelType.Float, 0)
            }));

            LightAccumulation = Backend.CreateRenderTarget("light_accumulation", new Definition(width, height, false, new List<Definition.Attachment>()
            {
                new Definition.Attachment(Definition.AttachmentPoint.Color, Renderer.PixelFormat.Rgba, Renderer.PixelInternalFormat.Rgba16f, Renderer.PixelType.Float, 0),
                new Definition.Attachment(Definition.AttachmentPoint.Depth, Renderer.PixelFormat.DepthComponent, Renderer.PixelInternalFormat.DepthComponent24, Renderer.PixelType.Float, 0)
            }));

            Temporary = Backend.CreateRenderTarget("deferred_temp", new Definition(width, height, false, new List<Definition.Attachment>()
            {
                new Definition.Attachment(Definition.AttachmentPoint.Color, Renderer.PixelFormat.Rgba, Renderer.PixelInternalFormat.Rgba16f, Renderer.PixelType.Float, 0)
            }));

            Output = Backend.CreateRenderTarget("deferred_output", new Definition(width, height, false, new List<Definition.Attachment>()
            {
                new Definition.Attachment(Definition.AttachmentPoint.Color, Renderer.PixelFormat.Rgba, Renderer.PixelInternalFormat.Rgba16f, Renderer.PixelType.Float, 0)
            }));

            SpotShadowsRenderTarget = Backend.CreateRenderTarget("spot_shadows", new Definition(512, 512, true, new List<Definition.Attachment>()
            {
                new Definition.Attachment(Definition.AttachmentPoint.Depth, Renderer.PixelFormat.DepthComponent, Renderer.PixelInternalFormat.DepthComponent16, Renderer.PixelType.Float, 0),
            }));

            PointShadowsRenderTarget = Backend.CreateRenderTarget("point_shadows", new Definition(512, 512, true, new List<Definition.Attachment>()
            {
                new Definition.Attachment(Definition.AttachmentPoint.Depth, Renderer.PixelFormat.DepthComponent, Renderer.PixelInternalFormat.DepthComponent16, Renderer.PixelType.Float, 0),
            }, true));

            int cascadeResolution = 2048;
            DirectionalShadowsRenderTarget = new RenderTarget[]
            {
                Backend.CreateRenderTarget("directional_shadows_csm0", new Definition(cascadeResolution, cascadeResolution, true, new List<Definition.Attachment>()
                {
                    new Definition.Attachment(Definition.AttachmentPoint.Depth, Renderer.PixelFormat.DepthComponent, Renderer.PixelInternalFormat.DepthComponent16, Renderer.PixelType.Float, 0),
                })),
                Backend.CreateRenderTarget("directional_shadows_csm1", new Definition(cascadeResolution, cascadeResolution, true, new List<Definition.Attachment>()
                {
                    new Definition.Attachment(Definition.AttachmentPoint.Depth, Renderer.PixelFormat.DepthComponent, Renderer.PixelInternalFormat.DepthComponent16, Renderer.PixelType.Float, 0),
                })),
                Backend.CreateRenderTarget("directional_shadows_csm2", new Definition(cascadeResolution, cascadeResolution, true, new List<Definition.Attachment>()
                {
                    new Definition.Attachment(Definition.AttachmentPoint.Depth, Renderer.PixelFormat.DepthComponent, Renderer.PixelInternalFormat.DepthComponent16, Renderer.PixelType.Float, 0),
                }))
            };

            AmbientLightShader = ResourceManager.Load<Triton.Graphics.Resources.ShaderProgram>("/shaders/deferred/ambient");

            // Init light shaders
            var lightTypes = new string[] { "DIRECTIONAL_LIGHT", "POINT_LIGHT", "SPOT_LIGHT" };
            var lightPermutations = new string[] { "NO_SHADOWS", "SHADOWS;SHADOW_QUALITY_LOWEST", "SHADOWS;SHADOW_QUALITY_LOW", "SHADOWS;SHADOW_QUALITY_MEDIUM", "SHADOWS;SHADOW_QUALITY_HIGH" };

            LightShaders = new Resources.ShaderProgram[lightTypes.Length * lightPermutations.Length];
            LightParams = new LightParams[lightTypes.Length * lightPermutations.Length];

            for (var l = 0; l < lightTypes.Length; l++)
            {
                var lightType = lightTypes[l];
                for (var p = 0; p < lightPermutations.Length; p++)
                {
                    var index = l * lightPermutations.Length + p;
                    var defines = lightType + ";" + lightPermutations[p];

                    if (lightType == "POINT_LIGHT")
                        defines += ";SHADOWS_CUBE";

                    LightShaders[index] = ResourceManager.Load<Triton.Graphics.Resources.ShaderProgram>("/shaders/deferred/light", defines);
                    LightParams[index] = new LightParams();
                }
            }

            DirectionalShaderOffset = 0;
            PointLightShaderOffset = 1 * lightPermutations.Length;
            SpotLightShaderOffset = 2 * lightPermutations.Length;

            CombineShader = ResourceManager.Load<Resources.ShaderProgram>("/shaders/deferred/combine");
            RenderShadowsShader = ResourceManager.Load<Resources.ShaderProgram>("/shaders/deferred/render_shadows");
            RenderShadowsSkinnedShader = ResourceManager.Load<Resources.ShaderProgram>("/shaders/deferred/render_shadows", "SKINNED");
            RenderShadowsCubeShader = ResourceManager.Load<Resources.ShaderProgram>("/shaders/deferred/render_shadows_cube");
            RenderShadowsSkinnedCubeShader = ResourceManager.Load<Resources.ShaderProgram>("/shaders/deferred/render_shadows_cube", "SKINNED");
            FogShader = ResourceManager.Load<Resources.ShaderProgram>("/shaders/deferred/fog");

            RandomNoiseTexture = ResourceManager.Load<Triton.Graphics.Resources.Texture>("/textures/random_n");

            QuadMesh = Backend.CreateBatchBuffer();
            QuadMesh.Begin();
            QuadMesh.AddQuad(new Vector2(-1, -1), new Vector2(2, 2), Vector2.Zero, new Vector2(1, 1));
            QuadMesh.End();

            UnitSphere = ResourceManager.Load<Triton.Graphics.Resources.Mesh>("/models/unit_sphere");
            UnitCone = ResourceManager.Load<Triton.Graphics.Resources.Mesh>("/models/unit_cone");

            AmbientRenderState = Backend.CreateRenderState(true, false, false, Triton.Renderer.BlendingFactorSrc.One, Triton.Renderer.BlendingFactorDest.One);
            LightAccumulatinRenderState = Backend.CreateRenderState(true, false, false, Triton.Renderer.BlendingFactorSrc.One, Triton.Renderer.BlendingFactorDest.One);
            ShadowsRenderState = Backend.CreateRenderState(false, true, true);
            DirectionalRenderState = Backend.CreateRenderState(true, false, false, Triton.Renderer.BlendingFactorSrc.One, Triton.Renderer.BlendingFactorDest.One, Renderer.CullFaceMode.Back, true, Triton.Renderer.DepthFunction.Lequal);
            LightInsideRenderState = Backend.CreateRenderState(true, false, false, Triton.Renderer.BlendingFactorSrc.One, Triton.Renderer.BlendingFactorDest.One, Triton.Renderer.CullFaceMode.Front, true, Renderer.DepthFunction.Gequal);
            LightOutsideRenderState = Backend.CreateRenderState(true, false, false, Triton.Renderer.BlendingFactorSrc.One, Triton.Renderer.BlendingFactorDest.One, Renderer.CullFaceMode.Back, true, Triton.Renderer.DepthFunction.Lequal);

            ShadowSampler = Backend.RenderSystem.CreateSampler(new Dictionary<Renderer.SamplerParameterName, int>
            {
                { SamplerParameterName.TextureMinFilter, (int)TextureMinFilter.Linear },
                { SamplerParameterName.TextureMagFilter, (int)TextureMinFilter.Linear },
                { SamplerParameterName.TextureCompareFunc, (int)DepthFunction.Lequal },
                { SamplerParameterName.TextureCompareMode, (int)TextureCompareMode.CompareRToTexture },
                { SamplerParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge },
                { SamplerParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge }
            });
        }