Beispiel #1
0
        public BaseGizmo(GraphicsDevice device, Effect effect)
        {
            _effect = effect;
            _device = device;

            // Create the gizmo geometry
            _rotationHelperGeometry = Buffer.Vertex.New <SolidVertex>(device, _rotationTrianglesCount * 3 + 2);
            _cylinder = GeometricPrimitive.Cylinder.New(_device, 1.0f, 1.0f, _lineRadiusTesselation);
            _cube     = GeometricPrimitive.Cube.New(_device, 1.0f);
            _cone     = GeometricPrimitive.Cone.New(_device, 1.0f, 1.3f, 16);

            // Create the rasterizer state for wireframe drawing
            var renderStateDesc = new SharpDX.Direct3D11.RasterizerStateDescription
            {
                CullMode                 = SharpDX.Direct3D11.CullMode.None,
                DepthBias                = 0,
                DepthBiasClamp           = 0,
                FillMode                 = SharpDX.Direct3D11.FillMode.Wireframe,
                IsAntialiasedLineEnabled = true,
                IsDepthClipEnabled       = true,
                IsFrontCounterClockwise  = false,
                IsMultisampleEnabled     = true,
                IsScissorEnabled         = false,
                SlopeScaledDepthBias     = 0
            };

            _rasterizerWireframe = RasterizerState.New(_device, renderStateDesc);
        }
Beispiel #2
0
        public void InitializeRendering(WadToolClass tool, DeviceManager deviceManager)
        {
            if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
            {
                return;
            }

            Configuration = tool.Configuration;

            base.InitializeRendering(deviceManager.Device, tool.Configuration.RenderingItem_Antialias);
            _tool = tool;

            // Legacy rendering
            {
                _device      = deviceManager.___LegacyDevice;
                _wadRenderer = new WadRenderer(deviceManager.___LegacyDevice);
                new BasicEffect(_device); // This effect is used for editor special meshes like sinks, cameras, light meshes, etc
                _rasterizerWireframe = RasterizerState.New(_device, new SharpDX.Direct3D11.RasterizerStateDescription
                {
                    CullMode                 = SharpDX.Direct3D11.CullMode.None,
                    DepthBias                = 0,
                    DepthBiasClamp           = 0,
                    FillMode                 = SharpDX.Direct3D11.FillMode.Wireframe,
                    IsAntialiasedLineEnabled = true,
                    IsDepthClipEnabled       = true,
                    IsFrontCounterClockwise  = false,
                    IsMultisampleEnabled     = true,
                    IsScissorEnabled         = false,
                    SlopeScaledDepthBias     = 0
                });
                _plane = GeometricPrimitive.GridPlane.New(_device, 8, 4);
            }
        }
Beispiel #3
0
        protected override async Task LoadContent()
        {
            await base.LoadContent();

            wireframeState = RasterizerState.New(GraphicsDevice, new RasterizerStateDescription(CullMode.Back)
            {
                FillMode = FillMode.Wireframe
            });

            simpleEffect             = new Effect(GraphicsDevice, SpriteEffect.Bytecode);
            parameterCollection      = new ParameterCollection();
            parameterCollectionGroup = new EffectParameterCollectionGroup(GraphicsDevice, simpleEffect, new [] { parameterCollection });
            parameterCollection.Set(TexturingKeys.Texture0, UVTexture);

            primitives = new List <GeometricPrimitive>();

            // Creates all primitives
            primitives = new List <GeometricPrimitive>
            {
                GeometricPrimitive.Plane.New(GraphicsDevice),
                GeometricPrimitive.Cube.New(GraphicsDevice),
                GeometricPrimitive.Sphere.New(GraphicsDevice),
                GeometricPrimitive.GeoSphere.New(GraphicsDevice),
                GeometricPrimitive.Cylinder.New(GraphicsDevice),
                GeometricPrimitive.Torus.New(GraphicsDevice),
                GeometricPrimitive.Teapot.New(GraphicsDevice),
                GeometricPrimitive.Capsule.New(GraphicsDevice, 0.5f, 0.3f),
                GeometricPrimitive.Cone.New(GraphicsDevice)
            };


            view = Matrix.LookAtRH(new Vector3(0, 0, 5), new Vector3(0, 0, 0), Vector3.UnitY);

            Window.AllowUserResizing = true;
        }
        public override void SetupResources(EffectMesh effectMesh)
        {
            var blendStateDesc = new BlendStateDescription();

            blendStateDesc.SetDefaults();
            blendStateDesc.AlphaToCoverageEnable        = false;
            blendStateDesc.IndependentBlendEnable       = false;
            blendStateDesc.RenderTargets[0].BlendEnable = true;

            blendStateDesc.RenderTargets[0].AlphaBlendFunction    = BlendFunction.Add;
            blendStateDesc.RenderTargets[0].AlphaSourceBlend      = Blend.One;
            blendStateDesc.RenderTargets[0].AlphaDestinationBlend = Blend.One;

            blendStateDesc.RenderTargets[0].ColorBlendFunction    = BlendFunction.Add;
            blendStateDesc.RenderTargets[0].ColorSourceBlend      = Blend.One;
            blendStateDesc.RenderTargets[0].ColorDestinationBlend = Blend.One;

            blendStateDesc.RenderTargets[0].ColorWriteChannels = ColorWriteChannels.All;

            // "LightPrePassAdditiveBlend"
            Effect.Parameters.Set(BlendStateKey, BlendState.New(GraphicsDevice, blendStateDesc));

            if (Debug)
            {
                var rasterizer = RasterizerState.New(GraphicsDevice, new RasterizerStateDescription()
                {
                    FillMode = FillMode.Wireframe
                });
                rasterizer.Name = "LightPrePassWireFrame";
                Effect.Parameters.Set(RasterizerStateKey, rasterizer);
            }

            Effect.PrepareMesh    += SetupMeshResources;
            Effect.UpdateMeshData += UpdateMeshResources;
        }
Beispiel #5
0
        protected override async Task LoadContent()
        {
            await base.LoadContent();

            batch           = new Sprite3DBatch(GraphicsDevice);
            sphere          = Asset.Load <Texture>("Sphere");
            rotatedImages   = Asset.Load <SpriteSheet>("RotatedImages");
            rasterizerState = RasterizerState.New(GraphicsDevice, new RasterizerStateDescription(CullMode.None));
        }
Beispiel #6
0
        protected override void Initialize()
        {
            base.Initialize();



            Window.Title = "Kaibo Crawler";

            // Setup states
            var rasterizerStateDesc = SharpDX.Direct3D11.RasterizerStateDescription.Default();

            m_backfaceCullingState = RasterizerState.New(GraphicsDevice, "CullModeBack", rasterizerStateDesc);

            var depthStencilStateDesc = SharpDX.Direct3D11.DepthStencilStateDescription.Default();

            m_depthStencilStateState = DepthStencilState.New(GraphicsDevice, "NormalZBufferUse", depthStencilStateDesc);

            depthStencilStateDesc = SharpDX.Direct3D11.DepthStencilStateDescription.Default();
            depthStencilStateDesc.IsDepthEnabled = false;


            m_noDepthStencil = DepthStencilState.New(GraphicsDevice, "NoZBufferUse", depthStencilStateDesc);


            var samplerStateDesc = SharpDX.Direct3D11.SamplerStateDescription.Default();

            samplerStateDesc.AddressV = SharpDX.Direct3D11.TextureAddressMode.Wrap;
            samplerStateDesc.AddressU = SharpDX.Direct3D11.TextureAddressMode.Wrap;
            samplerStateDesc.Filter   = SharpDX.Direct3D11.Filter.MinMagMipPoint;
            m_linearSamplerState      = SamplerState.New(GraphicsDevice, "LinearSampler", samplerStateDesc);


            var blendStateDesc = SharpDX.Direct3D11.BlendStateDescription.Default();

            m_blendStateOpaque = BlendState.New(GraphicsDevice, "Opaque", blendStateDesc);


            var blendStateDesc1 = SharpDX.Direct3D11.BlendStateDescription.Default();

            blendStateDesc1.IndependentBlendEnable = false;
            blendStateDesc1.AlphaToCoverageEnable  = false;


            m_alphaBlendState = BlendState.New(GraphicsDevice, SharpDX.Direct3D11.BlendOption.SourceAlpha,     //sourceBlend
                                               SharpDX.Direct3D11.BlendOption.InverseSourceAlpha,              //destinationBlend
                                               SharpDX.Direct3D11.BlendOperation.Add,                          //blendoperation
                                               SharpDX.Direct3D11.BlendOption.SourceAlpha,                     //source alphaBlend
                                               SharpDX.Direct3D11.BlendOption.InverseSourceAlpha,              //destination alpha blend
                                               SharpDX.Direct3D11.BlendOperation.Add,                          //alphablend operation
                                               SharpDX.Direct3D11.ColorWriteMaskFlags.All,                     //rendertarget mask
                                               -1);                                                            //mask



            Input.init(this);
        }
        /// <summary>
        /// Sets the state of the rasterizer.
        /// </summary>
        private void SetUpRasterizerState()
        {
            RasterizerStateDescription stateDescription = new RasterizerStateDescription
            {
                FillMode = FillMode.Solid,
                CullMode = CullMode.None
            };

            this.GraphicsDevice.SetRasterizerState(RasterizerState.New(this.GraphicsDevice, stateDescription));
        }
Beispiel #8
0
        public void InitializeRendering(AnimationEditor editor, DeviceManager deviceManager, WadMoveable skin)
        {
            if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
            {
                return;
            }

            base.InitializeRendering(deviceManager.Device, Configuration.RenderingItem_Antialias);
            ResetCamera();

            _editor      = editor;
            _wadRenderer = new WadRenderer(deviceManager.___LegacyDevice);
            _model       = _wadRenderer.GetMoveable(editor.Moveable);

            Configuration = _editor.Tool.Configuration;

            if (skin != null)
            {
                _skinModel = _wadRenderer.GetMoveable(skin);
            }

            // Actual "InitializeRendering"
            _fontTexture = Device.CreateTextureAllocator(new RenderingTextureAllocator.Description {
                Size = new VectorInt3(512, 512, 2)
            });
            _fontDefault = Device.CreateFont(new RenderingFont.Description {
                FontName = "Segoe UI", FontSize = 24, FontIsBold = true, TextureAllocator = _fontTexture
            });

            // Legacy rendering
            {
                _device        = deviceManager.___LegacyDevice;
                _deviceManager = deviceManager;
                new BasicEffect(_device); // This effect is used for editor special meshes like sinks, cameras, light meshes, etc
                SharpDX.Direct3D11.RasterizerStateDescription renderStateDesc =
                    new SharpDX.Direct3D11.RasterizerStateDescription
                {
                    CullMode                 = SharpDX.Direct3D11.CullMode.None,
                    DepthBias                = 0,
                    DepthBiasClamp           = 0,
                    FillMode                 = SharpDX.Direct3D11.FillMode.Wireframe,
                    IsAntialiasedLineEnabled = true,
                    IsDepthClipEnabled       = true,
                    IsFrontCounterClockwise  = false,
                    IsMultisampleEnabled     = true,
                    IsScissorEnabled         = false,
                    SlopeScaledDepthBias     = 0
                };

                _rasterizerWireframe = RasterizerState.New(deviceManager.___LegacyDevice, renderStateDesc);

                _gizmo = new GizmoAnimationEditor(editor, _device, _deviceManager.___LegacyEffects["Solid"], this);
                _plane = GeometricPrimitive.GridPlane.New(_device, 8, 4);
            }
        }
        protected override void InitializeCore()
        {
            base.InitializeCore();

            var shadowRenderState = new RasterizerStateDescription(CullMode.None)
            {
                DepthClipEnable = false
            };

            shadowRasterizerState = RasterizerState.New(Context.GraphicsDevice, shadowRenderState);
        }
Beispiel #10
0
        protected override void InitializeCore()
        {
            base.InitializeCore();

            var shadowRenderState = new RasterizerStateDescription(CullMode.None)
            {
                DepthClipEnable = false
            };

            // When rendering shadow maps, objects should not be culled by the rasterizer (in case the object is out of the frustum but cast
            // a shadow into the frustum)
            shadowModelComponentRenderer.RasterizerState = RasterizerState.New(Context.GraphicsDevice, shadowRenderState);
        }
Beispiel #11
0
        void LoadContent()
        {
            var device = this.GraphicsDevice;

            m_basicEffect = ToDispose(new BasicEffect(GraphicsDevice));

            m_basicEffect.TextureEnabled = true;
            m_basicEffect.Sampler        = device.SamplerStates.PointClamp;

            LoadCube();

            var rdesc = device.RasterizerStates.Default.Description;

            rdesc.DepthBias   = -10;
            m_rasterizerState = RasterizerState.New(device, rdesc);
        }
        private void Init(GraphicsDevice graphicsDevice)
        {
            _screenTriangle = SharpDX.Toolkit.Graphics.Buffer.Vertex.New(graphicsDevice, new[] {
                new Vector2(-1.0f, -1.0f),
                new Vector2(3.0f, -1.0f),
                new Vector2(-1.0f, 3.0f)
            }, SharpDX.Direct3D11.ResourceUsage.Immutable);

            _vertexInputLayout = VertexInputLayout.New(
                VertexBufferLayout.New(0, new VertexElement[] { new VertexElement("POSITION", 0, SharpDX.DXGI.Format.R32G32_Float, 0) }, 0));

            var rasterizerStateDesc = SharpDX.Direct3D11.RasterizerStateDescription.Default();

            rasterizerStateDesc.CullMode = SharpDX.Direct3D11.CullMode.None;
            _noneCullingState            = RasterizerState.New(graphicsDevice, "CullModeNone", rasterizerStateDesc);

            initalised = true;
        }
Beispiel #13
0
        public void InitializeRendering(WadToolClass tool, DeviceManager deviceManager)
        {
            if (LicenseManager.UsageMode != LicenseUsageMode.Runtime)
            {
                return;
            }

            base.InitializeRendering(deviceManager.Device, tool.Configuration.RenderingItem_Antialias);
            _tool = tool;

            // Actual "InitializeRendering"
            _fontTexture = Device.CreateTextureAllocator(new RenderingTextureAllocator.Description {
                Size = new VectorInt3(512, 512, 2)
            });
            _fontDefault = Device.CreateFont(new RenderingFont.Description {
                FontName = "Segoe UI", FontSize = 24, FontIsBold = true, TextureAllocator = _fontTexture
            });

            // Legacy rendering
            {
                _device        = deviceManager.___LegacyDevice;
                _deviceManager = deviceManager;
                _wadRenderer   = new WadRenderer(_device);
                new BasicEffect(_device); // This effect is used for editor special meshes like sinks, cameras, light meshes, etc
                _rasterizerWireframe = RasterizerState.New(_device, new SharpDX.Direct3D11.RasterizerStateDescription
                {
                    CullMode                 = SharpDX.Direct3D11.CullMode.None,
                    DepthBias                = 0,
                    DepthBiasClamp           = 0,
                    FillMode                 = SharpDX.Direct3D11.FillMode.Wireframe,
                    IsAntialiasedLineEnabled = true,
                    IsDepthClipEnabled       = true,
                    IsFrontCounterClockwise  = false,
                    IsMultisampleEnabled     = true,
                    IsScissorEnabled         = false,
                    SlopeScaledDepthBias     = 0
                });
                _gizmo        = new GizmoStaticEditor(_tool.Configuration, _device, _deviceManager.___LegacyEffects["Solid"], this);
                _gizmoLight   = new GizmoStaticEditorLight(_tool.Configuration, _device, _deviceManager.___LegacyEffects["Solid"], this);
                _plane        = GeometricPrimitive.GridPlane.New(_device, 8, 4);
                _littleSphere = GeometricPrimitive.Sphere.New(_device, 2 * 128.0f, 8);
                _sphere       = GeometricPrimitive.Sphere.New(_device, 1024.0f, 6);
            }
        }
Beispiel #14
0
        public PhysicsDebugShapeRendering(GraphicsDevice device)
        {
            graphicsDevice = device;

            triggerMaterial   = PhysicsDebugShapeMaterial.Create(graphicsDevice, Color.AdjustSaturation(Color.Purple, 0.77f), 1);
            staticMaterial    = PhysicsDebugShapeMaterial.Create(graphicsDevice, Color.AdjustSaturation(Color.Red, 0.77f), 1);
            dynamicMaterial   = PhysicsDebugShapeMaterial.Create(graphicsDevice, Color.AdjustSaturation(Color.Green, 0.77f), 1);
            kinematicMaterial = PhysicsDebugShapeMaterial.Create(graphicsDevice, Color.AdjustSaturation(Color.Blue, 0.77f), 1);
            characterMaterial = PhysicsDebugShapeMaterial.Create(graphicsDevice, Color.AdjustSaturation(Color.Yellow, 0.77f), 1);

            rasterizer = RasterizerState.New(graphicsDevice, new RasterizerStateDescription(CullMode.None)
            {
                FillMode = FillMode.Wireframe
            });

            triggerMaterial.Parameters.Set(Effect.RasterizerStateKey, rasterizer);
            staticMaterial.Parameters.Set(Effect.RasterizerStateKey, rasterizer);
            dynamicMaterial.Parameters.Set(Effect.RasterizerStateKey, rasterizer);
            kinematicMaterial.Parameters.Set(Effect.RasterizerStateKey, rasterizer);
            characterMaterial.Parameters.Set(Effect.RasterizerStateKey, rasterizer);
        }
Beispiel #15
0
        /// <summary>
        /// Creates and Initializes Pipeline, UI, Camera, and a terrain model
        /// </summary>
        /// <returns></returns>
        public override void Start()
        {
            //RenderPipelineLightingFactory.CreateDefaultForward(this, "SimpleTerrainEffectMain", Color.DarkBlue, false, true, "ParadoxBackground");

            CreateUI();

            CreateCamera();

            CreateLight();


            float  radToDeg = (float)(180 / Math.PI);
            Matrix camera_0 = Matrix.LookAtLH(CameraStartPosition, TargetPosition, new Vector3(0, 1, 0));
            Matrix camera   = camera_0;
            //camera.Invert();
            Vector3 outrot;
            Vector3 position;

            camera.DecomposeXYZ(out outrot);
            position = camera.TranslationVector;
            var DX = outrot.X * radToDeg;
            var DY = outrot.Y * radToDeg;
            var DZ = outrot.Z * radToDeg;

            var maxTerrainSize   = (int)Math.Pow(2, MaxTerrainSizePowerFactor);
            var maxVerticesCount = maxTerrainSize * maxTerrainSize;
            var maxIndicesCount  = 2 * maxTerrainSize * maxTerrainSize; // each index appear on average twice since the mesh is rendered as triangle strips

            CreateTerrainModelEntity(maxVerticesCount, maxIndicesCount);

            Script.AddTask(GenerateTerrain);

            defaultRasterizerState   = GraphicsDevice.Parameters.Get(Effect.RasterizerStateKey);
            wireFrameRasterizerState = RasterizerState.New(GraphicsDevice, new RasterizerStateDescription(CullMode.None)
            {
                FillMode = FillMode.Wireframe
            });

            Script.AddTask(UpdateInput);
        }
Beispiel #16
0
        public override void SetupResources(EffectMesh effectMesh)
        {
            base.SetupResources(effectMesh);

            throw new System.NotImplementedException();
            EffectShaderPass effectShaderPass;
            //var effectShaderPass = FindShaderPassFromPlugin(RenderPassPlugin);

            var rasterizer = RasterizerState.New(GraphicsDevice, new RasterizerStateDescription(CullMode.None)
            {
                FillMode = FillMode.Wireframe, DepthBias = -1000
            });

            rasterizer.Name = "WireFrame";
            effectShaderPass.Parameters.Set(RasterizerStateKey, rasterizer);

            var depthStencilState = DepthStencilState.New(GraphicsDevice, new DepthStencilStateDescription(true, false)
            {
                DepthBufferFunction = CompareFunction.LessEqual
            });

            effectShaderPass.Parameters.Set(DepthStencilStateKey, depthStencilState);
        }
Beispiel #17
0
        public NMSEntity CreateCustomMeshModel(GraphicsDevice graphicsDevice, VertexPositionNormalTexture[] Vertexes, int[] Indices, PrimitiveType PrimType, RasterizerStateDescription RasterizerDesc, Material Mat, Vector3 SpawnPos, Quaternion SpawnRot, bool SpawnNow = true)
        {
            var meshMesh = new Mesh();
            var meshDraw = new MeshDraw();
            var meshMod  = new Model();
            var meshComp = new ModelComponent();

            var buffIdx  = SiliconStudio.Xenko.Graphics.Buffer.Index.New(graphicsDevice, Indices);
            var buffVert = SiliconStudio.Xenko.Graphics.Buffer.Vertex.New(graphicsDevice, Vertexes);

            var RasterizerStateDesc = RasterizerDesc;

            meshDraw.IndexBuffer   = new IndexBufferBinding(buffIdx, true, Indices.Length);
            meshDraw.VertexBuffers = new VertexBufferBinding[] { new VertexBufferBinding(buffVert, VertexPositionNormalTexture.Layout, Vertexes.Length) };
            meshDraw.PrimitiveType = PrimType;
            meshDraw.DrawCount     = Indices.Length;

            meshMesh.Draw = meshDraw;
            meshMesh.Parameters.Set(Effect.RasterizerStateKey, RasterizerState.New(graphicsDevice, RasterizerStateDesc));
            meshMesh.MaterialIndex = 0;

            var mat = Mat;

            Vector3 min = new Vector3(float.MaxValue), max = new Vector3(float.MinValue);

            foreach (var vert in Vertexes)
            {
                if (vert.Position.X < min.X)
                {
                    min.X = vert.Position.X;
                }
                if (vert.Position.Y < min.Y)
                {
                    min.Y = vert.Position.Y;
                }
                if (vert.Position.Z < min.Z)
                {
                    min.Z = vert.Position.Z;
                }

                if (vert.Position.X > max.X)
                {
                    max.X = vert.Position.X;
                }
                if (vert.Position.Y > max.Y)
                {
                    max.Y = vert.Position.Y;
                }
                if (vert.Position.Z > max.Z)
                {
                    max.Z = vert.Position.Z;
                }
            }
            meshMod.BoundingBox = new BoundingBox(min, max);

            meshMod.Materials.Add(mat);

            meshMod.Meshes.Add(meshMesh);
            meshComp.Model = meshMod;

            var outEntity = new NMSEntity();

            outEntity.Transform.Position = SpawnPos;
            outEntity.Transform.Rotation = SpawnRot;
            outEntity.Add(meshComp);
            outEntity.Group = EntityGroup.Group29;

            outEntity.Transform.UpdateLocalMatrix();
            meshMesh.Parameters.Set(TransformationKeys.World, outEntity.Transform.LocalMatrix);

            if (SpawnNow)
            {
                SceneSystem.SceneInstance.Scene.Entities.Add(outEntity);
            }

            return(outEntity);
        }
Beispiel #18
0
        public override void Load()
        {
            base.Load();

            if (OfflineCompilation)
            {
                return;
            }

            // Declare post render pass
            PostPass = new RenderPass("PostPass").KeepAliveBy(ActiveObjects);
            RenderPass.AddPass(PostPass);


            var depthStencilTexture = Texture.New2D(GraphicsDevice, AtlasSize, AtlasSize, PixelFormat.D32_Float, TextureFlags.DepthStencil | TextureFlags.ShaderResource).KeepAliveBy(ActiveObjects);
            var depthStencilBuffer  = depthStencilTexture.ToDepthStencilBuffer(false);

            ShadowMapDepth = depthStencilBuffer;

            //MainTargetPlugin.Parameters.Set(ShadowMapKeys.Texture0, ShadowMapDepth);

            // Setup clear of this target
            var renderTargetPlugin = new RenderTargetsPlugin
            {
                Services         = Services,
                EnableClearDepth = true,
                EnableSetTargets = false,
                RenderPass       = RenderPass,
                RenderTarget     = null,
                DepthStencil     = depthStencilBuffer,
            };

            renderTargetPlugin.Apply();

            // Use Default ZTest for GBuffer
            depthStencilStateZStandard      = DepthStencilState.New(GraphicsDevice, new DepthStencilStateDescription().Default()).KeepAliveBy(ActiveObjects);
            depthStencilStateZStandard.Name = "ZStandard";

            Parameters.Set(EffectPlugin.DepthStencilStateKey, depthStencilStateZStandard);

            casterRasterizerState = RasterizerState.New(GraphicsDevice, new RasterizerStateDescription(CullMode.Back)).KeepAliveBy(ActiveObjects);

            // Variance Shadow Mapping
            // Create the blur temporary texture
            var shadowMapTextureDesc = ShadowMapDepth.Description;
            var shadowMapBlurH       = Texture.New2D(GraphicsDevice, shadowMapTextureDesc.Width, shadowMapTextureDesc.Height, PixelFormat.R32G32_Float, TextureFlags.ShaderResource | TextureFlags.RenderTarget).KeepAliveBy(ActiveObjects);
            var shadowMapBlurV       = Texture.New2D(GraphicsDevice, shadowMapTextureDesc.Width, shadowMapTextureDesc.Height, PixelFormat.R32G32_Float, TextureFlags.ShaderResource | TextureFlags.RenderTarget).KeepAliveBy(ActiveObjects);

            Texture2D    textureSourceH = ShadowMapDepth.Texture;
            Texture2D    textureSourceV = shadowMapBlurH;
            RenderTarget renderTargetH  = shadowMapBlurH.ToRenderTarget();
            RenderTarget renderTargetV  = shadowMapBlurV.ToRenderTarget();

            var blurQuadMesh = new EffectMesh[2];

            for (int j = 0; j < BlurCount; j++)
            {
                for (int i = 0; i < 2; ++i)
                {
                    blurQuadMesh[i] = new EffectMesh(j > 0 ? blurEffects[1] : blurEffects[i]).KeepAliveBy(ActiveObjects);
                    blurQuadMesh[i].Parameters.Set(PostEffectBlurKeys.Coefficients, new[] { 0.2270270270f, 0.3162162162f, 0.3162162162f, 0.0702702703f, 0.0702702703f });
                    var unit = i == 0 ? Vector2.UnitX : Vector2.UnitY;
                    blurQuadMesh[i].Parameters.Set(PostEffectBlurKeys.Offsets, new[] { Vector2.Zero, unit * -1.3846153846f, unit * +1.3846153846f, unit * -3.2307692308f, unit * +3.2307692308f });

                    PostPass.AddPass(blurQuadMesh[i].EffectPass);

                    RenderSystem.GlobalMeshes.AddMesh(blurQuadMesh[i]);
                }

                blurQuadMesh[0].Parameters.Set(TexturingKeys.Texture0, textureSourceH);
                blurQuadMesh[1].Parameters.Set(TexturingKeys.Texture0, textureSourceV);
                blurQuadMesh[0].Parameters.Set(RenderTargetKeys.RenderTarget, renderTargetH);
                blurQuadMesh[1].Parameters.Set(RenderTargetKeys.RenderTarget, renderTargetV);

                textureSourceH = shadowMapBlurV;
                textureSourceV = shadowMapBlurH;
            }

            ShadowMapVsm = shadowMapBlurV;

            // Final texture for VSM is result of blur
            //MainTargetPlugin.Parameters.Set(ShadowMapKeys.Texture0, shadowMapBlurV);
        }
Beispiel #19
0
        protected override async Task LoadContent()
        {
            await base.LoadContent();

            spriteBatch = new SpriteBatch(GraphicsDevice);
            font        = Asset.Load <SpriteFont>("Font");

            wireframeState = RasterizerState.New(GraphicsDevice, new RasterizerStateDescription(CullMode.Back)
            {
                FillMode = FillMode.Wireframe
            });

            materials.Add(Asset.Load <Material>("NoTessellation"));
            materials.Add(Asset.Load <Material>("FlatTessellation"));
            materials.Add(Asset.Load <Material>("PNTessellation"));
            materials.Add(Asset.Load <Material>("PNTessellationAE"));
            materials.Add(Asset.Load <Material>("FlatTessellationDispl"));
            materials.Add(Asset.Load <Material>("FlatTessellationDisplAE"));
            materials.Add(Asset.Load <Material>("PNTessellationDisplAE"));

            var cube = new Entity("Cube")
            {
                new ModelComponent(new ProceduralModelDescriptor(new CubeProceduralModel {
                    Size = new Vector3(80), MaterialInstance = { Material = materials[0] }
                }).GenerateModel(Services))
            };
            var sphere = new Entity("Sphere")
            {
                new ModelComponent(new ProceduralModelDescriptor(new SphereProceduralModel {
                    Radius = 50, Tessellation = 5, MaterialInstance = { Material = materials[0] }
                }).GenerateModel(Services))
            };

            var megalodon = new Entity {
                new ModelComponent {
                    Model = Asset.Load <Model>("megalodon Model")
                }
            };

            megalodon.Transform.Position = new Vector3(0, -30f, -10f);

            var knight = new Entity {
                new ModelComponent {
                    Model = Asset.Load <Model>("knight Model")
                }
            };

            knight.Transform.RotationEulerXYZ = new Vector3(-MathUtil.Pi / 2, MathUtil.Pi / 4, 0);
            knight.Transform.Position         = new Vector3(0, -50f, 20f);
            knight.Transform.Scale            = new Vector3(0.6f);

            entities.Add(sphere);
            entities.Add(cube);
            entities.Add(megalodon);
            entities.Add(knight);

            camera          = new TestCamera();
            CameraComponent = camera.Camera;
            Script.Add(camera);

            LightingKeys.EnableFixedAmbientLight(GraphicsDevice.Parameters, true);
            GraphicsDevice.Parameters.Set(EnvironmentLightKeys.GetParameterKey(LightSimpleAmbientKeys.AmbientLight, 0), (Color3)Color.White);

            ChangeModel(0);
            SetWireframe(true);

            camera.Position = new Vector3(25, 45, 80);
            camera.SetTarget(currentEntity, true);
        }
        /// <summary>
        /// Creates the rendering pipeline
        /// </summary>
        private void CreatePipeline()
        {
            var renderers         = RenderSystem.Pipeline.Renderers;
            var width             = GraphicsDevice.BackBuffer.Width;
            var height            = GraphicsDevice.BackBuffer.Height;
            var viewport          = new Viewport(0, 0, width, height);
            var clearColor        = Color.Black;
            var effectName        = "ParadoxCraftEffectMain";
            var fowardEffectName  = "ParadoxCraftEffectForward";
            var prepassEffectName = "ParadoxCraftPrepassEffect";

            // Adds a light processor that will track all the entities that have a light component.
            // This will also handle the shadows (allocation, activation etc.).
            var lightProcessor = Entities.GetProcessor <LightShadowProcessor>();

            if (lightProcessor == null)
            {
                Entities.Processors.Add(new DynamicLightShadowProcessor(GraphicsDevice, false));
            }

            // Camera
            renderers.Add(new CameraSetter(Services));

            // Create G-buffer pass
            var gbufferPipeline = new RenderPipeline("GBuffer");

            // Renders the G-buffer for opaque geometry.
            gbufferPipeline.Renderers.Add(new ModelRenderer(Services, effectName + ".ParadoxGBufferShaderPass").AddOpaqueFilter());
            var gbufferProcessor = new GBufferRenderProcessor(Services, gbufferPipeline, GraphicsDevice.DepthStencilBuffer, false);

            // Add sthe G-buffer pass to the pipeline.
            renderers.Add(gbufferProcessor);

            // Performs the light prepass on opaque geometry.
            // Adds this pass to the pipeline.
            var lightDeferredProcessor = new LightingPrepassRenderer(Services, prepassEffectName, GraphicsDevice.DepthStencilBuffer, gbufferProcessor.GBufferTexture);

            renderers.Add(lightDeferredProcessor);

            renderers.Add(new RenderTargetSetter(Services)
            {
                ClearColor       = clearColor,
                EnableClearDepth = false,
                RenderTarget     = GraphicsDevice.BackBuffer,
                DepthStencil     = GraphicsDevice.DepthStencilBuffer,
                Viewport         = viewport
            });

            renderers.Add(new RenderStateSetter(Services)
            {
                DepthStencilState = GraphicsDevice.DepthStencilStates.Default, RasterizerState = GraphicsDevice.RasterizerStates.CullBack
            });

            // Renders all the meshes with the correct lighting.
            renderers.Add(new ModelRenderer(Services, fowardEffectName).AddLightForwardSupport());

            // Blend atmoshpere inscatter on top
            renderers.Add(new AtmosphereRenderer(Services, Atmosphere, Sunlight));

            // Wireframe mode
            if (isWireframe)
            {
                GraphicsDevice.Parameters.Set(Effect.RasterizerStateKey, RasterizerState.New(GraphicsDevice, new RasterizerStateDescription(CullMode.None)
                {
                    FillMode = FillMode.Wireframe
                }));
            }

            GraphicsDevice.Parameters.Set(RenderingParameters.UseDeferred, true);
        }
Beispiel #21
0
        public VoxelRenderer(GraphicsDevice graphicsDevice, ContentManager contentManager)
        {
            _cubeVertexBuffer = Buffer.Vertex.New(
                graphicsDevice,
                new[]
            {
                // 3D coordinates              UV Texture coordinates
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f)
                },                                                                                                                                     // Front
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(0.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitZ, Texcoord = new Vector2(1.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f)
                },                                                                                                                                    // BACK
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(1.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(0.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, 1.0f), Normal = Vector3.UnitZ, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, -1.0f), Normal = Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f)
                },                                                                                                                                    // Top
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = Vector3.UnitY, Texcoord = new Vector2(0.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, 1.0f), Normal = Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, -1.0f), Normal = Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, 1.0f), Normal = Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, -1.0f), Normal = Vector3.UnitY, Texcoord = new Vector2(1.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f)
                },                                                                                                                                     // Bottom
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, 1.0f), Normal = -Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = -Vector3.UnitY, Texcoord = new Vector2(1.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitY, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitY, Texcoord = new Vector2(0.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, 1.0f), Normal = -Vector3.UnitY, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f)
                },                                                                                                                                     // Left
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, 1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(0.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, -1.0f, -1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, 1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(-1.0f, 1.0f, -1.0f), Normal = -Vector3.UnitX, Texcoord = new Vector2(1.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, -1.0f), Normal = Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f)
                },                                                                                                                                    // Right
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, 1.0f), Normal = Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, 1.0f), Normal = Vector3.UnitX, Texcoord = new Vector2(1.0f, 1.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, -1.0f, -1.0f), Normal = Vector3.UnitX, Texcoord = new Vector2(1.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, -1.0f), Normal = Vector3.UnitX, Texcoord = new Vector2(0.0f, 0.0f)
                },
                new CubeVertex()
                {
                    Position = new Vector3(1.0f, 1.0f, 1.0f), Normal = Vector3.UnitX, Texcoord = new Vector2(0.0f, 1.0f)
                }
            }, SharpDX.Direct3D11.ResourceUsage.Immutable);

            // Create an input layout from the vertices
            _vertexInputLayout = VertexInputLayout.New(
                VertexBufferLayout.New(0, new VertexElement[] { new VertexElement("POSITION_CUBE", 0, SharpDX.DXGI.Format.R32G32B32_Float, 0),
                                                                new VertexElement("NORMAL", 0, SharpDX.DXGI.Format.R32G32B32_Float, sizeof(float) * 3),
                                                                new VertexElement("TEXCOORD", 0, SharpDX.DXGI.Format.R32G32_Float, sizeof(float) * 6) }, 0),
                VertexBufferLayout.New(1, new VertexElement[] { new VertexElement("POSITION_INSTANCE", SharpDX.DXGI.Format.R32_SInt) }, 1));

            // Create instance buffer for every VoxelInfo
            _voxelTypeRenderingData = new VoxelTypeInstanceData[TypeInformation.GetNumTypes() - 1];
            for (int i = 0; i < _voxelTypeRenderingData.Length; ++i)
            {
                _voxelTypeRenderingData[i] = new VoxelTypeInstanceData(graphicsDevice, (VoxelType)(i + 1));
            }
            LoadTextures(contentManager);


            // load shader
            EffectCompilerFlags compilerFlags = EffectCompilerFlags.None;

#if DEBUG
            compilerFlags |= EffectCompilerFlags.Debug;
#endif
            var voxelShaderCompileResult = EffectCompiler.CompileFromFile("Content/voxel.fx", compilerFlags);
            if (voxelShaderCompileResult.HasErrors)
            {
                System.Console.WriteLine(voxelShaderCompileResult.Logger.Messages);
                System.Diagnostics.Debugger.Break();
            }
            _voxelEffect = new SharpDX.Toolkit.Graphics.Effect(graphicsDevice, voxelShaderCompileResult.EffectData);

            // setup states
            var rasterizerStateDesc = SharpDX.Direct3D11.RasterizerStateDescription.Default();
            rasterizerStateDesc.CullMode = SharpDX.Direct3D11.CullMode.Back;
            _backfaceCullingState        = RasterizerState.New(graphicsDevice, "CullModeBack", rasterizerStateDesc);
            rasterizerStateDesc.CullMode = SharpDX.Direct3D11.CullMode.None;
            _noneCullingState            = RasterizerState.New(graphicsDevice, "CullModeNone", rasterizerStateDesc);

            var depthStencilStateDesc = SharpDX.Direct3D11.DepthStencilStateDescription.Default();
            depthStencilStateDesc.IsDepthEnabled = true;
            _depthStencilStateState = DepthStencilState.New(graphicsDevice, "NormalZBufferUse", depthStencilStateDesc);

            var samplerStateDesc = SharpDX.Direct3D11.SamplerStateDescription.Default();
            samplerStateDesc.AddressV = SharpDX.Direct3D11.TextureAddressMode.Mirror;
            samplerStateDesc.AddressU = SharpDX.Direct3D11.TextureAddressMode.Mirror;
            samplerStateDesc.Filter   = SharpDX.Direct3D11.Filter.MinMagMipPoint;
            _pointSamplerState        = SamplerState.New(graphicsDevice, "PointSampler", samplerStateDesc);
            _voxelEffect.Parameters["PointSampler"].SetResource(_pointSamplerState);

            var blendStateDesc = SharpDX.Direct3D11.BlendStateDescription.Default();
            _blendStateOpaque = BlendState.New(graphicsDevice, "Opaque", blendStateDesc);
            blendStateDesc.RenderTarget[0].IsBlendEnabled   = true;
            blendStateDesc.RenderTarget[0].SourceBlend      = SharpDX.Direct3D11.BlendOption.SourceAlpha;
            blendStateDesc.RenderTarget[0].DestinationBlend = SharpDX.Direct3D11.BlendOption.InverseSourceAlpha;
            blendStateDesc.RenderTarget[0].BlendOperation   = SharpDX.Direct3D11.BlendOperation.Add;
            _blendStateTransparent = BlendState.New(graphicsDevice, "AlphaBlend", blendStateDesc);

            // vertexbuffer for a single instance
            _singleInstanceBuffer = Buffer.Vertex.New <Int32>(graphicsDevice, 1, SharpDX.Direct3D11.ResourceUsage.Dynamic);
        }