Ejemplo n.º 1
0
        protected override void Rendering(GraphicsDevice graphics, CustomRenderProperties game)
        {
            var device  = graphics.D3DDevice;
            var context = graphics.ImmediateContext;

            foreach (var en in entities)
            {
                var render     = en.GetComponent <D3DTerrainRenderComponent>();
                var geo        = en.GetComponent <TerrainGeometryCellsComponent>();
                var material   = en.GetComponent <D3DTexturedMaterialSamplerComponent>();
                var transform  = en.GetComponent <TransformComponent>();
                var components = en.GetComponents <ID3DRenderable>();
                var data       = en.GetComponent <ClipmapsTerrainComponent>();
                var conf       = en.GetComponent <Systems.TerrainConfigurationComponent>();

                foreach (var com in components)
                {
                    if (com.IsModified)
                    {
                        com.Update(graphics);
                    }
                }

                if (render.IsModified || !pass.IsCompiled)  //update
                {
                    if (!pass.IsCompiled)
                    {
                        pass.Compile(graphics.Compilator);
                    }

                    UpdateShaders(graphics, render, pass, layconst);
                    render.PrimitiveTopology = PrimitiveTopology.TriangleList;

                    render.IsModified = false;
                }

                UpdateTransformWorld(graphics, render, transform);
                //SetShaders(context, render);

                graphics.SetVertexShader(render.VertexShader);
                graphics.SetPixelShader(render.PixelShader);

                if (material.IsModified)
                {
                    render.TextureResources.Set(ConvertToResources(material, graphics.TexturedLoader));
                    render.SampleState.Set(graphics.CreateSampler(material.SampleDescription));
                    material.IsModified = false;
                }


                var rd = new TerrainClipmapsData {
                    OneOverWidth           = 256,         //data.OneOverWidth,
                    ZScaleFactor           = 1,           //data.ZScaleFactor,
                    AlphaOffset            = Vector2.One, //data.AlphaOffset,
                    FineTextureBlockOrigin = Vector4.One, //data.FineTextureBlockOrigin,
                    ScaleFactor            = Vector4.One, //data.ScaleFactor,
                    LightDirection         = -Vector3.UnitY,
                    ViewerPos           = Vector2.Zero,
                    WorldViewProjMatrix = game.CameraState.ViewMatrix * game.CameraState.ProjectionMatrix
                };

                //var tr = new TransforStructBuffer();
                //var test = graphics.CreateDynamicBuffer(ref tr, Unsafe.SizeOf<TransforStructBuffer>());

                var buffData = graphics.CreateDynamicBuffer(ref rd, TerrainClipmapsData.Size);//132



                var resources = render.TextureResources.Get();

                //graphics.RegisterConstantBuffer(context.VertexShader,
                //    GameStructBuffer.RegisterResourceSlot, game.Game);
                //graphics.RegisterConstantBuffer(context.VertexShader,
                //    LightStructBuffer.RegisterResourceSlot, game.Lights);



                graphics.RegisterConstantBuffer(context.VertexShader,
                                                TerrainClipmapsData.SlotId, buffData);

                var res = graphics.TexturedLoader.LoadBitmapShaderResource(conf.Texture);

                context.VertexShader.SetShaderResource(0, res);

                //

                //graphics.RegisterConstantBuffer(context.PixelShader,
                //    GameStructBuffer.RegisterResourceSlot, game.Game);
                //graphics.RegisterConstantBuffer(context.PixelShader,
                //    LightStructBuffer.RegisterResourceSlot, game.Lights);
                graphics.RegisterConstantBuffer(context.PixelShader,
                                                TerrainClipmapsData.SlotId, buffData);

                context.PixelShader.SetShaderResources(0, resources);
                // context.PixelShader.SetSampler(0, render.SampleState.Get());

                if (geo.IsModified)
                {
                    UpdateCellBuffers(graphics, geo, render);
                    geo.MarkAsRendered();
                }

                TerrainRenderTechnique.RenderCells(graphics, CameraStateHelper.GetFrustum(game.CameraState), geo, render, transform);
            }
        }
Ejemplo n.º 2
0
        protected override void Rendering(GraphicsDevice graphics, CustomRenderProperties game)
        {
            var device  = graphics.D3DDevice;
            var context = graphics.ImmediateContext;

            foreach (var en in entities)
            {
                var render     = en.GetComponent <D3DTerrainRenderComponent>();
                var geo        = en.GetComponent <TerrainGeometryCellsComponent>();
                var material   = en.GetComponent <D3DTexturedMaterialSamplerComponent>();
                var transform  = en.GetComponent <TransformComponent>();
                var components = en.GetComponents <ID3DRenderable>();

                foreach (var com in components)
                {
                    if (com.IsModified)
                    {
                        com.Update(graphics);
                    }
                }

                if (render.IsModified || !pass.IsCompiled)  //update
                {
                    if (!pass.IsCompiled)
                    {
                        pass.Compile(graphics.Compilator);
                    }

                    UpdateShaders(graphics, render, pass, layconst);
                    render.PrimitiveTopology = PrimitiveTopology.TriangleList;

                    render.IsModified = false;
                }

                UpdateTransformWorld(graphics, render, transform);
                SetShaders(context, render);

                if (material.IsModified)
                {
                    render.TextureResources.Set(ConvertToResources(material, graphics.TexturedLoader));
                    render.SampleState.Set(graphics.CreateSampler(material.SampleDescription));
                    material.IsModified = false;
                }
                //

                context.VertexShader.SetConstantBuffer(GameStructBuffer.RegisterResourceSlot, game.Game);
                context.VertexShader.SetConstantBuffer(LightStructBuffer.RegisterResourceSlot, game.Lights);

                //context.PixelShader.SetConstantBuffer(GameStructBuffer.RegisterResourceSlot, game.Game);
                //context.PixelShader.SetConstantBuffer(LightStructBuffer.RegisterResourceSlot, game.Lights);

                context.PixelShader.SetConstantBuffer(GameStructBuffer.RegisterResourceSlot, game.Game);
                context.PixelShader.SetConstantBuffer(LightStructBuffer.RegisterResourceSlot, game.Lights);
                context.PixelShader.SetShaderResources(0, render.TextureResources.Get());
                context.PixelShader.SetSampler(0, render.SampleState.Get());

                if (geo.IsModified)
                {
                    UpdateCellBuffers(graphics, geo, render);

                    /*
                     * <remark>https://docs.microsoft.com/en-us/dotnet/api/system.buffers.arraypool-1?view=netcore-2.2<remark>
                     */
                    /*
                     * var pool = ArrayPool<TerrainVertex>.Shared;
                     * TerrainVertex[] vertex = null;
                     * try {
                     * var pos = geo.Positions;
                     * var normals = geo.Normals;
                     * //vertex = new TerrainVertex[pos.Length];
                     * vertex = pool.Rent(pos.Length);//
                     * for (var i = 0; i < pos.Length; i++) {
                     *     vertex[i] = new TerrainVertex {
                     *         position = pos[i],
                     *         normal = normals[i],
                     *         tangent = geo.Tangents[i],
                     *         binormal = geo.Binormal[i],
                     *         color = geo.Colors[i],
                     *         texcoor = geo.TextureCoordinates[i],
                     *         normapMapTexCoor = geo.NormalMapTexCoordinates[i],
                     *     };
                     * }
                     *
                     * render.VertexBuffer.Set(graphics.CreateBuffer(BindFlags.VertexBuffer, vertex));
                     * render.IndexBuffer.Set(graphics.CreateBuffer(BindFlags.IndexBuffer, geo.Indices.ToArray()));
                     *
                     * geo.MarkAsRendered();
                     * } finally {
                     * pool.Return(vertex);
                     * }
                     */
                    geo.MarkAsRendered();
                }

                RenderCells(graphics, CameraStateHelper.GetFrustum(game.CameraState), geo, render, transform);

                //Frustum frustum = null;
                //for (var i = 0; i < render.CellCount; ++i) {
                //    var cell = render.GetCell(0, frustum);

                //    Render(graphics, context, render, TerrainVertex.Size);

                //    foreach (var com in components) {
                //        com.Render(graphics);
                //    }

                //    graphics.ImmediateContext.DrawIndexed(geo.Indices.Length, 0, 0);
                ////}
            }
        }