/**
 * @author       Richard Davey <rich@photonstorm.com>
 * @copyright    2020 Photon Storm Ltd.
 * @license      {@link https://opensource.org/licenses/MIT|MIT License}
 */

var Identity = require('../../renderer/webgl/mvp/Identity');
var Scale = require('../../renderer/webgl/mvp/Scale');
var Translate = require('../../renderer/webgl/mvp/Translate');
var ViewIdentity = require('../../renderer/webgl/mvp/ViewIdentity');
var ViewLoad2D = require('../../renderer/webgl/mvp/ViewLoad2D');

/**
 * Renders this Game Object with the WebGL Renderer to the given Camera.
 *
 * The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
 * This method should not be called directly. It is a utility function of the Render module.
 *
 * A Static Tilemap Layer renders immediately and does not use any batching.
 *
 * @method Phaser.Tilemaps.StaticTilemapLayer#renderWebGL
 * @since 3.0.0
 * @private
 *
 * @param {Phaser.Renderer.WebGL.WebGLRenderer} renderer - A reference to the current active WebGL renderer.
 * @param {Phaser.Tilemaps.StaticTilemapLayer} src - The Game Object being rendered in this call.
 * @param {number} interpolationPercentage - Reserved for future use and custom pipelines.
 * @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
 */
var StaticTilemapLayerWebGLRenderer = function (renderer, src, interpolationPercentage, camera)
{
    var gl = renderer.gl;
    var pipeline = src.pipeline;

    //  Restore when we're done
    var pipelineVertexBuffer = pipeline.vertexBuffer;

    Identity(pipeline);
    Translate(pipeline, src.x - (camera.scrollX * src.scrollFactorX), src.y - (camera.scrollY * src.scrollFactorY), 0);
    Scale(pipeline, src.scaleX, src.scaleY, 1);
    ViewLoad2D(pipeline, camera.matrix.matrix);

    //  This calls mvpUpdate during pipeline.bind, so make sure we call it _after_ setting the MVP stuff above
    renderer.setPipeline(pipeline);

    for (var i = 0; i < src.tileset.length; i++)
    {
        var tileset = src.tileset[i];
        var vertexCount = src.vertexCount[i];

        src.upload(camera, i);

        if (vertexCount > 0)
        {
            if (pipeline.forceZero)
            {
                //  Light Pipeline, or similar
                pipeline.setGameObject(src, tileset);
            }
            else
            {
                renderer.setTextureZero(tileset.glTexture);
            }

            gl.drawArrays(gl.TRIANGLES, 0, vertexCount);
        }
    }

    //  Restore the pipeline
    pipeline.vertexBuffer = pipelineVertexBuffer;

    ViewIdentity(pipeline);
    Identity(pipeline);
};

module.exports = StaticTilemapLayerWebGLRenderer;

@@ -352,77 +352,8 @@
 ');%0A
-var ViewIdentity = require('../../renderer/webgl/mvp/ViewIdentity');%0A
 var 
@@ -1432,16 +1432,39 @@
 eline;%0A%0A
+    renderer.flush();%0A%0A
     //  
@@ -1551,32 +1551,27 @@
    Identity(
-pipeline
+src
 );%0A    Trans
@@ -1575,24 +1575,19 @@
 anslate(
-pipeline
+src
 , src.x 
@@ -1686,24 +1686,19 @@
   Scale(
-pipeline
+src
 , src.sc
@@ -1734,24 +1734,19 @@
 wLoad2D(
-pipeline
+src
 , camera
@@ -1771,53 +1771,78 @@
     
-//  This calls mvpUpdate during pipeline.bind
+renderer.setPipeline(pipeline);%0A%0A    //  The above alters the uniforms
 , so
@@ -1915,33 +1915,259 @@
 erer.set
-Pipeline(pipeline
+Matrix4(pipeline.program, 'uModelMatrix', false, src.modelMatrix);%0A    renderer.setMatrix4(pipeline.program, 'uViewMatrix', false, src.viewMatrix);%0A    renderer.setMatrix4(pipeline.program, 'uProjectionMatrix', false, pipeline.projectionMatrix
 );%0A%0A    
@@ -2714,24 +2714,55 @@
    %7D%0A    %7D%0A%0A
+    renderer.resetTextures();%0A%0A
     //  Rest
@@ -2777,16 +2777,23 @@
 pipeline
+ buffer
 %0A    pip
@@ -2844,53 +2844,393 @@
     
-ViewIdentity(pipeline);%0A    Identity(pipeline
+renderer.currentVertexBuffer = pipelineVertexBuffer;%0A%0A    pipeline.setAttribPointers();%0A%0A    //  Reset the uniforms%0A    renderer.setMatrix4(pipeline.program, 'uModelMatrix', false, pipeline.modelMatrix);%0A    renderer.setMatrix4(pipeline.program, 'uViewMatrix', false, pipeline.viewMatrix);%0A    renderer.setMatrix4(pipeline.program, 'uProjectionMatrix', false, pipeline.projectionMatrix
 );%0A%7D

Tidy up everything after rendering