コード例 #1
0
        public LightingRenderer(ContentManager content, RenderCoordinator coordinator, DefaultMaterialSet materials, LightingEnvironment environment)
        {
            Materials   = materials;
            Coordinator = coordinator;

            IlluminantMaterials = new IlluminantMaterials(materials);

            StoreScissorRect       = _StoreScissorRect;
            RestoreScissorRect     = _RestoreScissorRect;
            ShadowBatchSetup       = _ShadowBatchSetup;
            IlluminationBatchSetup = _IlluminationBatchSetup;

            IlluminantMaterials.ClearStencil = materials.Get(
                materials.Clear,
                rasterizerState: RasterizerState.CullNone,
                depthStencilState: new DepthStencilState {
                StencilEnable    = true,
                StencilMask      = StencilTrue,
                StencilWriteMask = StencilTrue,
                ReferenceStencil = StencilFalse,
                StencilFunction  = CompareFunction.Always,
                StencilPass      = StencilOperation.Replace,
                StencilFail      = StencilOperation.Replace,
            },
                blendState: BlendState.Opaque
                );

            materials.Add(
                IlluminantMaterials.DebugOutlines = materials.WorldSpaceGeometry.SetStates(
                    blendState: BlendState.AlphaBlend
                    )
                );

            // If stencil == false, paint point light at this location
            PointLightStencil = new DepthStencilState {
                DepthBufferEnable = false,
                StencilEnable     = true,
                StencilMask       = StencilTrue,
                StencilWriteMask  = StencilFalse,
                StencilFunction   = CompareFunction.Equal,
                StencilPass       = StencilOperation.Keep,
                StencilFail       = StencilOperation.Keep,
                ReferenceStencil  = StencilFalse
            };

            {
                var dBegin = new[] {
                    MaterialUtil.MakeDelegate(
                        rasterizerState: RenderStates.ScissorOnly, depthStencilState: PointLightStencil
                        )
                };
                var dEnd = new[] {
                    MaterialUtil.MakeDelegate(
                        rasterizerState: RasterizerState.CullNone, depthStencilState: DepthStencilState.None
                        )
                };

#if SDL2
                materials.Add(IlluminantMaterials.PointLightExponential = new DelegateMaterial(
                                  PointLightMaterialsInner[0]           = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("PointLightExponential"), "PointLightExponential"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightLinear = new DelegateMaterial(
                                  PointLightMaterialsInner[1]      = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("PointLightLinear"), "PointLightLinear"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightExponentialRampTexture = new DelegateMaterial(
                                  PointLightMaterialsInner[2] = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("PointLightExponentialRampTexture"), "PointLightExponentialRampTexture"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightLinearRampTexture = new DelegateMaterial(
                                  PointLightMaterialsInner[3] = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("PointLightLinearRampTexture"), "PointLightLinearRampTexture"
                                      ), dBegin, dEnd
                                  ));
#else
                materials.Add(IlluminantMaterials.PointLightExponential = new DelegateMaterial(
                                  PointLightMaterialsInner[0]           = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("Illumination"), "PointLightExponential"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightLinear = new DelegateMaterial(
                                  PointLightMaterialsInner[1]      = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("Illumination"), "PointLightLinear"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightExponentialRampTexture = new DelegateMaterial(
                                  PointLightMaterialsInner[2] = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("Illumination"), "PointLightExponentialRampTexture"
                                      ), dBegin, dEnd
                                  ));

                materials.Add(IlluminantMaterials.PointLightLinearRampTexture = new DelegateMaterial(
                                  PointLightMaterialsInner[3] = new Squared.Render.EffectMaterial(
                                      content.Load <Effect>("Illumination"), "PointLightLinearRampTexture"
                                      ), dBegin, dEnd
                                  ));
#endif
            }

            // If stencil == false: set stencil to true.
            // If stencil == true: leave stencil alone, don't paint this pixel
            ShadowStencil = new DepthStencilState {
                DepthBufferEnable = false,
                StencilEnable     = true,
                StencilMask       = StencilTrue,
                StencilWriteMask  = StencilTrue,
                StencilFunction   = CompareFunction.NotEqual,
                StencilPass       = StencilOperation.Replace,
                StencilFail       = StencilOperation.Keep,
                ReferenceStencil  = StencilTrue
            };

            materials.Add(IlluminantMaterials.Shadow = new DelegateMaterial(
                              ShadowMaterialInner    = new Squared.Render.EffectMaterial(
#if SDL2
                                  content.Load <Effect>("Shadow"), "Shadow"
#else
                                  content.Load <Effect>("Illumination"), "Shadow"
#endif
                                  ),
                              new[] {
                MaterialUtil.MakeDelegate(
                    rasterizerState: RenderStates.ScissorOnly,
                    depthStencilState: ShadowStencil,
                    blendState: RenderStates.DrawNone
                    )
            },
                              new[] {
                MaterialUtil.MakeDelegate(
                    rasterizerState: RasterizerState.CullNone, depthStencilState: DepthStencilState.None
                    )
            }
                              ));

#if SDL2
            materials.Add(IlluminantMaterials.ScreenSpaceGammaCompressedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("ScreenSpaceGammaCompressedBitmap"), "ScreenSpaceGammaCompressedBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceGammaCompressedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("WorldSpaceGammaCompressedBitmap"), "WorldSpaceGammaCompressedBitmap"
                              ));

            materials.Add(IlluminantMaterials.ScreenSpaceToneMappedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("ScreenSpaceToneMappedBitmap"), "ScreenSpaceToneMappedBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceToneMappedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("WorldSpaceToneMappedBitmap"), "WorldSpaceToneMappedBitmap"
                              ));
#else
            materials.Add(IlluminantMaterials.ScreenSpaceGammaCompressedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("HDRBitmap"), "ScreenSpaceGammaCompressedBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceGammaCompressedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("HDRBitmap"), "WorldSpaceGammaCompressedBitmap"
                              ));

            materials.Add(IlluminantMaterials.ScreenSpaceToneMappedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("HDRBitmap"), "ScreenSpaceToneMappedBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceToneMappedBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("HDRBitmap"), "WorldSpaceToneMappedBitmap"
                              ));

            materials.Add(IlluminantMaterials.ScreenSpaceRampBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("RampBitmap"), "ScreenSpaceRampBitmap"
                              ));

            materials.Add(IlluminantMaterials.WorldSpaceRampBitmap = new Squared.Render.EffectMaterial(
                              content.Load <Effect>("RampBitmap"), "WorldSpaceRampBitmap"
                              ));
#endif

            Environment = environment;

            // Reduce garbage created by BufferPool<>.Allocate when creating cached sectors
            BufferPool <ShadowVertex> .MaxBufferSize = 1024 * 16;
            BufferPool <short> .MaxBufferSize        = 1024 * 32;
        }
コード例 #2
0
        public DefaultMaterialSet(IServiceProvider serviceProvider)
        {
            _ApplyViewTransformDelegate = ApplyViewTransformToMaterial;

#if SDL2
            BuiltInShaders = new ContentManager(serviceProvider, "Content/SquaredRender");
#elif !PSM
            BuiltInShaders = new ResourceContentManager(serviceProvider, Shaders.ResourceManager);
#else
            BuiltInShaders = new Squared.Render.PSM.PSMShaderManager(serviceProvider);
#endif

            Clear = new DelegateMaterial(
                new NullMaterial(),
                new Action <DeviceManager>[] { (dm) => ApplyShaderVariables() },
                null
                );


#if PSM
            ScreenSpaceBitmap   = new EffectMaterial(BuiltInShaders.Load <Effect>("ScreenSpaceBitmap"));
            WorldSpaceBitmap    = new EffectMaterial(BuiltInShaders.Load <Effect>("WorldSpaceBitmap"));
            ScreenSpaceGeometry = new EffectMaterial(BuiltInShaders.Load <Effect>("ScreenSpaceGeometry"));
            WorldSpaceGeometry  = new EffectMaterial(BuiltInShaders.Load <Effect>("WorldSpaceGeometry"));
#elif SDL2
            ScreenSpaceBitmap = new EffectMaterial(
                BuiltInShaders.Load <Effect>("ScreenSpaceBitmapTechnique"),
                "ScreenSpaceBitmapTechnique"
                );

            WorldSpaceBitmap = new EffectMaterial(
                BuiltInShaders.Load <Effect>("WorldSpaceBitmapTechnique"),
                "WorldSpaceBitmapTechnique"
                );

            ScreenSpaceGeometry = new EffectMaterial(
                BuiltInShaders.Load <Effect>("ScreenSpaceUntextured"),
                "ScreenSpaceUntextured"
                );

            WorldSpaceGeometry = new EffectMaterial(
                BuiltInShaders.Load <Effect>("WorldSpaceUntextured"),
                "WorldSpaceUntextured"
                );
#else
            var bitmapShader   = BuiltInShaders.Load <Effect>("SquaredBitmapShader");
            var geometryShader = BuiltInShaders.Load <Effect>("SquaredGeometryShader");

            ScreenSpaceBitmap = new EffectMaterial(
                bitmapShader,
                "ScreenSpaceBitmapTechnique"
                );

            WorldSpaceBitmap = new EffectMaterial(
                bitmapShader,
                "WorldSpaceBitmapTechnique"
                );

            ScreenSpaceBitmapWithDiscard = new EffectMaterial(
                bitmapShader,
                "ScreenSpaceBitmapWithDiscardTechnique"
                );

            WorldSpaceBitmapWithDiscard = new EffectMaterial(
                bitmapShader,
                "WorldSpaceBitmapWithDiscardTechnique"
                );

            ScreenSpaceGeometry = new EffectMaterial(
                geometryShader,
                "ScreenSpaceUntextured"
                );

            WorldSpaceGeometry = new EffectMaterial(
                geometryShader,
                "WorldSpaceUntextured"
                );
#endif

#if SDL2
            ScreenSpaceLightmappedBitmap = new EffectMaterial(
                BuiltInShaders.Load <Effect>("ScreenSpaceLightmappedBitmap"),
                "ScreenSpaceLightmappedBitmap"
                );

            WorldSpaceLightmappedBitmap = new EffectMaterial(
                BuiltInShaders.Load <Effect>("WorldSpaceLightmappedBitmap"),
                "WorldSpaceLightmappedBitmap"
                );
#elif !PSM
            var lightmapShader = BuiltInShaders.Load <Effect>("Lightmap");

            ScreenSpaceLightmappedBitmap = new EffectMaterial(
                lightmapShader,
                "ScreenSpaceLightmappedBitmap"
                );

            WorldSpaceLightmappedBitmap = new EffectMaterial(
                lightmapShader,
                "WorldSpaceLightmappedBitmap"
                );

            var blurShader = BuiltInShaders.Load <Effect>("GaussianBlur");

            ScreenSpaceHorizontalGaussianBlur5Tap = new EffectMaterial(
                blurShader,
                "ScreenSpaceHorizontalGaussianBlur5Tap"
                );

            ScreenSpaceVerticalGaussianBlur5Tap = new EffectMaterial(
                blurShader,
                "ScreenSpaceVerticalGaussianBlur5Tap"
                );

            WorldSpaceHorizontalGaussianBlur5Tap = new EffectMaterial(
                blurShader,
                "WorldSpaceHorizontalGaussianBlur5Tap"
                );

            WorldSpaceVerticalGaussianBlur5Tap = new EffectMaterial(
                blurShader,
                "WorldSpaceVerticalGaussianBlur5Tap"
                );
#endif

            var gds = serviceProvider.GetService(typeof(IGraphicsDeviceService)) as IGraphicsDeviceService;
            if (gds != null)
            {
                ViewTransformStack.Push(ViewTransform.CreateOrthographic(
                                            gds.GraphicsDevice.PresentationParameters.BackBufferWidth,
                                            gds.GraphicsDevice.PresentationParameters.BackBufferHeight
                                            ));
            }
            else
            {
                ViewTransformStack.Push(ViewTransform.Default);
            }
        }