|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2020 Photon Storm Ltd. |
| 4 | + * @license {@link https://opensource.org/licenses/MIT|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Class = require('../../../utils/Class'); |
| 8 | +var GetFastValue = require('../../../utils/object/GetFastValue'); |
| 9 | +var PointLightShaderSourceFS = require('../shaders/PointLight-frag.js'); |
| 10 | +var PointLightShaderSourceVS = require('../shaders/PointLight-vert.js'); |
| 11 | +var WebGLPipeline = require('../WebGLPipeline'); |
| 12 | + |
| 13 | +var PointLightPipeline = new Class({ |
| 14 | + |
| 15 | + Extends: WebGLPipeline, |
| 16 | + |
| 17 | + initialize: |
| 18 | + |
| 19 | + function PointLightPipeline (config) |
| 20 | + { |
| 21 | + config.vertShader = GetFastValue(config, 'vertShader', PointLightShaderSourceVS); |
| 22 | + config.fragShader = GetFastValue(config, 'fragShader', PointLightShaderSourceFS); |
| 23 | + config.uniforms = GetFastValue(config, 'uniforms', [ |
| 24 | + 'uProjectionMatrix', |
| 25 | + 'uResolution' |
| 26 | + ]); |
| 27 | + config.attributes = GetFastValue(config, 'attributes', [ |
| 28 | + { |
| 29 | + name: 'inPosition', |
| 30 | + size: 2 |
| 31 | + }, |
| 32 | + { |
| 33 | + name: 'inLightPosition', |
| 34 | + size: 2 |
| 35 | + }, |
| 36 | + { |
| 37 | + name: 'inLightRadius' |
| 38 | + }, |
| 39 | + { |
| 40 | + name: 'inLightFalloff' |
| 41 | + }, |
| 42 | + { |
| 43 | + name: 'inLightAttenuation' |
| 44 | + }, |
| 45 | + { |
| 46 | + name: 'inLightColor', |
| 47 | + size: 4 |
| 48 | + } |
| 49 | + ]); |
| 50 | + |
| 51 | + WebGLPipeline.call(this, config); |
| 52 | + }, |
| 53 | + |
| 54 | + onBind: function () |
| 55 | + { |
| 56 | + this.set2f('uResolution', this.width, this.height); |
| 57 | + }, |
| 58 | + |
| 59 | + batchPointLight: function (light, camera, x0, y0, x1, y1, x2, y2, x3, y3, lightX, lightY) |
| 60 | + { |
| 61 | + var color = light.color; |
| 62 | + var intensity = light.intensity; |
| 63 | + var radius = light.radius; |
| 64 | + var falloff = light.falloff; |
| 65 | + var attenuation = light.attenuation; |
| 66 | + |
| 67 | + var r = color.r * intensity; |
| 68 | + var g = color.g * intensity; |
| 69 | + var b = color.b * intensity; |
| 70 | + var a = camera.alpha * light.alpha; |
| 71 | + |
| 72 | + if (this.shouldFlush(6)) |
| 73 | + { |
| 74 | + this.flush(); |
| 75 | + } |
| 76 | + |
| 77 | + this.batchLightVert(x0, y0, lightX, lightY, radius, falloff, attenuation, r, g, b, a); |
| 78 | + this.batchLightVert(x1, y1, lightX, lightY, radius, falloff, attenuation, r, g, b, a); |
| 79 | + this.batchLightVert(x2, y2, lightX, lightY, radius, falloff, attenuation, r, g, b, a); |
| 80 | + this.batchLightVert(x0, y0, lightX, lightY, radius, falloff, attenuation, r, g, b, a); |
| 81 | + this.batchLightVert(x2, y2, lightX, lightY, radius, falloff, attenuation, r, g, b, a); |
| 82 | + this.batchLightVert(x3, y3, lightX, lightY, radius, falloff, attenuation, r, g, b, a); |
| 83 | + }, |
| 84 | + |
| 85 | + batchLightVert: function (x, y, lightX, lightY, radius, falloff, attenuation, r, g, b, a) |
| 86 | + { |
| 87 | + var vertexViewF32 = this.vertexViewF32; |
| 88 | + |
| 89 | + var vertexOffset = (this.vertexCount * this.currentShader.vertexComponentCount) - 1; |
| 90 | + |
| 91 | + vertexViewF32[++vertexOffset] = x; |
| 92 | + vertexViewF32[++vertexOffset] = y; |
| 93 | + vertexViewF32[++vertexOffset] = lightX; |
| 94 | + vertexViewF32[++vertexOffset] = lightY; |
| 95 | + vertexViewF32[++vertexOffset] = radius; |
| 96 | + vertexViewF32[++vertexOffset] = falloff; |
| 97 | + vertexViewF32[++vertexOffset] = attenuation; |
| 98 | + vertexViewF32[++vertexOffset] = r; |
| 99 | + vertexViewF32[++vertexOffset] = g; |
| 100 | + vertexViewF32[++vertexOffset] = b; |
| 101 | + vertexViewF32[++vertexOffset] = a; |
| 102 | + |
| 103 | + this.vertexCount++; |
| 104 | + } |
| 105 | + |
| 106 | +}); |
| 107 | + |
| 108 | +module.exports = PointLightPipeline; |
0 commit comments