コード例 #1
0
        public void UpdateLight()
        {
            RenderTexture filterMap  = entity.world.lightSystem.filterFrame;
            RenderTexture lightAtlas = entity.world.lightSystem.lightAtlas;

            // Render filtermap under the light
            _view.Center   = worldPosition;
            _view.Size     = new Vector2f(2f * _radius, 2f * _radius);
            _view.Viewport = new FloatRect(0, 0, 1, 1);
            filterMap.SetView(_view);
            filterMap.Clear(Color.White);
            entity.world.graphics.Render(filterMap, RenderMode.LIGHT_FILTER, layerMask);
            filterMap.Display();

            // Calculate light atlas viewport
            int       framesX  = LightSystem.ATLAS_SIZE / LightSystem.ATLAS_FRAME_SIZE;
            int       framesY  = LightSystem.ATLAS_SIZE / LightSystem.ATLAS_FRAME_SIZE;
            int       frameX   = atlasIndex % framesX;
            int       frameY   = atlasIndex / framesX;
            FloatRect viewport = new FloatRect(
                (float)frameX / (float)framesX,
                (float)frameY / (float)framesY,
                1f / (float)framesX,
                1f / (float)framesY
                );

            _view.Reset(new FloatRect(
                            0, 0, LightSystem.ATLAS_FRAME_SIZE, LightSystem.ATLAS_FRAME_SIZE
                            ));
            _view.Viewport = viewport;

            // Configure light shader
            Shader shader = entity.world.lightSystem.lightShader;

            shader.SetParameter("lightPos", new Vector2f(filterMap.Size.X / 2, filterMap.Size.Y / 2));
            shader.SetParameter("filterMap", filterMap.Texture);
            shader.SetParameter("lightColor", color);
            shader.SetParameter("fragOffset", new Vector2f(
                                    frameX * LightSystem.ATLAS_FRAME_SIZE,
                                    LightSystem.ATLAS_SIZE - LightSystem.ATLAS_FRAME_SIZE - frameY * LightSystem.ATLAS_FRAME_SIZE
                                    ));

            // Render the light in a sub-rectangle of the lightAtlas
            RenderStates lightRenderStates = new RenderStates(shader);

            lightRenderStates.BlendMode = BlendMode.None;
            _fillRect.Size      = _view.Size;
            _fillRect.FillColor = new Color(255, 255, 255, 255);
            lightAtlas.SetView(_view);
            lightAtlas.Draw(_fillRect, lightRenderStates);

            // Draw cookie
            if (_cookieSprite != null)
            {
                _cookieSprite.Position = _view.Size / 2;
                lightAtlas.Draw(_cookieSprite, new RenderStates(BlendMode.Multiply));
            }

            needUpdate = false;
        }