|
| 1 | +/* This is a WebGL ONLY component */ |
| 2 | +var RenderPass = { |
| 3 | + outputStage: { |
| 4 | + renderTarget: null, |
| 5 | + enableDepthTest: false, |
| 6 | + enableStencilTest: false, |
| 7 | + enableBlending: false, |
| 8 | + |
| 9 | + /* Blend State */ |
| 10 | + blendLogicOp: 0, |
| 11 | + blendSrcRgb: 0, |
| 12 | + blendDstRgb: 0, |
| 13 | + blendSrcAlpha: 0, |
| 14 | + blendDstAlpha: 0, |
| 15 | + blendEqRgb: 0, |
| 16 | + blendEqAlpha: 0, |
| 17 | + blendRed: 0, |
| 18 | + blendGreen: 0, |
| 19 | + blendBlue: 0, |
| 20 | + blendAlpha: 0, |
| 21 | + |
| 22 | + /* Depth-Stencil State */ |
| 23 | + depthFunc: 0, |
| 24 | + depthMask: 0, |
| 25 | + stencilFunc: 0, |
| 26 | + stencilFail: 0, |
| 27 | + stencilZFail: 0, |
| 28 | + stencilZPass: 0 |
| 29 | + }, |
| 30 | + |
| 31 | + renderPass: { |
| 32 | + shaderPipeline: null, |
| 33 | + textures: [], |
| 34 | + topology: 0 |
| 35 | + }, |
| 36 | + |
| 37 | + /* Needed for getting constant values |
| 38 | + * Form the WebGL context. |
| 39 | + */ |
| 40 | + glContext: null, |
| 41 | + |
| 42 | + /* Utility functions */ |
| 43 | + initRenderComponent: function () |
| 44 | + { |
| 45 | + var renderingContext = this.state.game.renderer.gl; |
| 46 | + |
| 47 | + if ((renderingContext instanceof WebGLRenderingContext) || (renderingContext !== null && renderingContext.rawgl !== undefined)) |
| 48 | + { |
| 49 | + this.glContext = renderingContext; |
| 50 | + } |
| 51 | + }, |
| 52 | + |
| 53 | + setRenderTarget: function (renderTarget) |
| 54 | + { |
| 55 | + this.outputStage.renderTarget = renderTarget; |
| 56 | + return this; |
| 57 | + }, |
| 58 | + |
| 59 | + setDefaultDepthStencilState: function () |
| 60 | + { |
| 61 | + var gl = this.renderingContext; |
| 62 | + var outputStage = this.outputStage; |
| 63 | + |
| 64 | + outputStage.depthEnabled = false; |
| 65 | + outputStage.stencilEnabled = false; |
| 66 | + outputStage.depthMask = true; |
| 67 | + outputStage.depthFunc = gl.LESS; |
| 68 | + outputStage.stencilFunc = gl.NEVER; |
| 69 | + outputStage.stencilZFail = gl.KEEP; |
| 70 | + outputStage.stencilZPass = gl.KEEP; |
| 71 | + |
| 72 | + return this; |
| 73 | + }, |
| 74 | + |
| 75 | + setBlendColor: function (r, g, b, a) |
| 76 | + { |
| 77 | + var outputStage = this.outputStage; |
| 78 | + |
| 79 | + outputStage.blendRed = r; |
| 80 | + outputStage.blendGreen = g; |
| 81 | + outputStage.blendBlue = b; |
| 82 | + outputStage.blendAlpha = a; |
| 83 | + |
| 84 | + return this; |
| 85 | + }, |
| 86 | + |
| 87 | + setBlendFunc: function (src, dst, eq) |
| 88 | + { |
| 89 | + var outputStage = this.outputStage; |
| 90 | + |
| 91 | + outputStage.blendSrcRgb = outputStage.blendSrcAlpha = src; |
| 92 | + outputStage.blendDstRgb = outputStage.blendDstAlpha = dst; |
| 93 | + outputStage.blendEqRgb = outputStage.blendEqAlpha = eq; |
| 94 | + |
| 95 | + return this; |
| 96 | + }, |
| 97 | + |
| 98 | + setBlendFuncSeparate: function (srcRgb, srcAlpha, dstRgb, dstAlpha, eqRgb, eqAlpha) |
| 99 | + { |
| 100 | + var outputStage = this.outputStage; |
| 101 | + |
| 102 | + outputStage.blendSrcRgb = srcRgb; |
| 103 | + outputStage.blendSrcAlpha = srcAlpha; |
| 104 | + outputStage.blendDstRgb = dstRgb; |
| 105 | + outputStage.blendDstAlpha = dstAlpha; |
| 106 | + outputStage.blendEqRgb = eqRgb; |
| 107 | + outputStage.blendEqAlpha = eqAlpha; |
| 108 | + |
| 109 | + return this; |
| 110 | + }, |
| 111 | + |
| 112 | + setDefaultBlending: function () { |
| 113 | + var gl = this.glContext; |
| 114 | + |
| 115 | + this.setBlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ADD); |
| 116 | + |
| 117 | + return this; |
| 118 | + }, |
| 119 | + |
| 120 | + setNoBlending: function () { |
| 121 | + var gl = this.glContext; |
| 122 | + |
| 123 | + this.setBlendFunc(gl.ONE, gl.ZERO, gl.ADD); |
| 124 | + |
| 125 | + return this; |
| 126 | + }, |
| 127 | + |
| 128 | + setTexture: function (texture, textureUnit) { |
| 129 | + this.renderPass.textures[textureUnit] = texture; |
| 130 | + return this; |
| 131 | + }, |
| 132 | + |
| 133 | + setTopology: function (topology) { |
| 134 | + this.renderPass.topology = topology; |
| 135 | + return this; |
| 136 | + }, |
| 137 | + |
| 138 | + setShaderPipeline: function (shaderPipeline) { |
| 139 | + this.renderPass.shaderPipeline = shaderPipeline; |
| 140 | + return this; |
| 141 | + }, |
| 142 | + |
| 143 | + /* Call this on render pass */ |
| 144 | + dispatchRenderPassState: function () { |
| 145 | + var gl = this.glContext; |
| 146 | + var textures = this.textures; |
| 147 | + var length = textures.length; |
| 148 | + var outputStage = this.outputStage; |
| 149 | + |
| 150 | + for (var index = 0; index < length; ++index) { |
| 151 | + if (textures[index] !== null) { |
| 152 | + gl.activeTexture(gl.TEXTURE0 + index); |
| 153 | + gl.bindTexture(gl.TEXTURE_2D, textures[index].texture); |
| 154 | + } else { |
| 155 | + gl.activeTexture(gl.TEXTURE0 + index); |
| 156 | + gl.bindTexture(gl.TEXTURE_2D, null); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if (outputStage.enableBlending) { |
| 161 | + gl.enable(gl.BLEND); |
| 162 | + gl.blendFuncSeparate(outputStage.blendSrcRGB, outputStage.blendDstRGB, outputStage.blendSrcAlpha, outputStage.blendDstAlpha); |
| 163 | + gl.blendEquationSeparate(outputStage.blendEqRgb, outputStage.blendEqAlpha); |
| 164 | + gl.blendColor(outputStage.blendRed, outputStage.blendGreen, outputStage.blendBlue, outputStage.blendAlpha) |
| 165 | + } else { |
| 166 | + gl.disable(gl.BLEND); |
| 167 | + } |
| 168 | + |
| 169 | + if (outputStage.enableDepthTest) { |
| 170 | + gl.enable(gl.DEPTH_TEST); |
| 171 | + gl.depthFunc(outputStage.depthFunc); |
| 172 | + gl.depthMask(outputStage.depthMask); |
| 173 | + } else { |
| 174 | + gl.disable(gl.DEPTH_TEST); |
| 175 | + } |
| 176 | + |
| 177 | + if (outputStage.enableStencilTest) { |
| 178 | + gl.enable(gl.STENCIL_TEST); |
| 179 | + gl.stencilFunc(this.stencilFunc, 0, 1); |
| 180 | + gl.stencilOp(this.stencilFail, this.stencilZFail, this.stencilZPass); |
| 181 | + } else { |
| 182 | + gl.disable(gl.STENCIL_TEST); |
| 183 | + } |
| 184 | + } |
| 185 | +}; |
| 186 | + |
| 187 | +module.exports = Render; |
0 commit comments