|
| 1 | +import { ResizeHandler } from "./Helpers/ResizeHandler.js"; |
| 2 | +import { GlManager } from "./Helpers/GlManager.js"; |
| 3 | +import { Program } from "./Entities/Program.js"; |
| 4 | +import { ParticleSystem } from "./Entities/ParticleSystem.js"; |
| 5 | +import { ShaderSource } from "./ShaderSource/ShaderSource.js"; |
| 6 | +import { ShaderManager } from "./Helpers/ShaderManager.js"; |
| 7 | +import { Utils } from "./Helpers/Utils.js"; |
| 8 | +import { FrameLoop } from "./Helpers/FrameLoop.js"; |
| 9 | + |
| 10 | +const frameLoop = new FrameLoop(60); |
| 11 | + |
| 12 | +/** |
| 13 | + * @param config {Object} |
| 14 | + * @param config.fpsCountViewElement {Element} |
| 15 | + * @param config.settings.flameCount {number} |
| 16 | + * @param config.settings.flameParticleCount {number} |
| 17 | + * @param config.settings.widthRange {[number, number]} |
| 18 | + * @param config.settings.heightRange {[number, number]} |
| 19 | + * @param config.settings.noiseRange {[number, number]} |
| 20 | + * @param config.settings.speedRange {[number, number]} |
| 21 | + * @param config.settings.particleTopColor {[number, number, number]} |
| 22 | + * @param config.settings.particleBottomColor {[number, number, number]} |
| 23 | + * |
| 24 | + * @returns {{destroy:Function}} |
| 25 | + */ |
| 26 | +const renderApp = (config) => { |
| 27 | + const { fpsCountViewElement, settings } = config; |
| 28 | + |
| 29 | + let unsubscriber = null; |
| 30 | + |
| 31 | + const MAX_PARTICLE_COUNT = settings.flameCount * settings.flameParticleCount; |
| 32 | + |
| 33 | + const { canvas, ctx } = GlManager.getRenderer("canvas"); |
| 34 | + |
| 35 | + ResizeHandler.handle(canvas, ctx); |
| 36 | + |
| 37 | + ctx.enable(ctx.BLEND); |
| 38 | + ctx.blendFunc(ctx.SRC_ALPHA, ctx.ONE); |
| 39 | + |
| 40 | + const vertexShader = ShaderManager.compileVertex(ctx, ShaderSource.VERTEX_SHADER_SOURCE); |
| 41 | + const fragmentShader = ShaderManager.compileFragment(ctx, ShaderSource.FRAGMENT_SHADER_SOURCE); |
| 42 | + |
| 43 | + const program = new Program(ctx, vertexShader, fragmentShader); |
| 44 | + |
| 45 | + program.use(); |
| 46 | + |
| 47 | + program.setUniform3fv(ShaderSource.TOP_COLOR_UNIFORM, settings.particleTopColor); |
| 48 | + program.setUniform3fv(ShaderSource.BOTTOM_COLOR_UNIFORM, settings.particleBottomColor); |
| 49 | + |
| 50 | + const positionAttrLocation = program.getAttribLocation(ShaderSource.POSITION_ATTRIBUTE); |
| 51 | + const sizeAttrLocation = program.getAttribLocation(ShaderSource.SIZE_ATTRIBUTE); |
| 52 | + const timeAttrLocation = program.getAttribLocation(ShaderSource.TIME_ATTRIBUTE); |
| 53 | + |
| 54 | + ctx.enableVertexAttribArray(positionAttrLocation); |
| 55 | + ctx.enableVertexAttribArray(sizeAttrLocation); |
| 56 | + ctx.enableVertexAttribArray(timeAttrLocation); |
| 57 | + |
| 58 | + const positionArray = new Float32Array(MAX_PARTICLE_COUNT * 2); |
| 59 | + const positionBuffer = ctx.createBuffer(); |
| 60 | + |
| 61 | + const timeArray = new Float32Array(MAX_PARTICLE_COUNT); |
| 62 | + const timeBuffer = ctx.createBuffer(); |
| 63 | + |
| 64 | + const sizeArray = new Float32Array(MAX_PARTICLE_COUNT); |
| 65 | + const sizeBuffer = ctx.createBuffer(); |
| 66 | + |
| 67 | + const particleSystem = new ParticleSystem({ |
| 68 | + canvas, |
| 69 | + flameCount: settings.flameCount, |
| 70 | + flameParticlesCount: settings.flameParticleCount, |
| 71 | + widthRange: settings.widthRange, |
| 72 | + heightRange: settings.heightRange, |
| 73 | + noiseRange: settings.noiseRange, |
| 74 | + speedRange: settings.speedRange |
| 75 | + }); |
| 76 | + |
| 77 | + let frames = 0; |
| 78 | + let fps = 0; |
| 79 | + let lastFpsUpdate = 0; |
| 80 | + |
| 81 | + let previousFrameTimestamp = performance.now(); |
| 82 | + |
| 83 | + unsubscriber = frameLoop.subscribe( |
| 84 | + () => runRenderLoop(previousFrameTimestamp), |
| 85 | + ) |
| 86 | + |
| 87 | + const runRenderLoop = (timestamp) => { |
| 88 | + const now = performance.now(); |
| 89 | + const deltaTime = Utils.msToSeconds(now - timestamp); |
| 90 | + |
| 91 | + previousFrameTimestamp = now; |
| 92 | + |
| 93 | + frames++; |
| 94 | + |
| 95 | + if (now - lastFpsUpdate >= 1000) { |
| 96 | + fps = frames; |
| 97 | + frames = 0; |
| 98 | + lastFpsUpdate = now; |
| 99 | + |
| 100 | + fpsCountViewElement.innerText = `${fps} FPS`; |
| 101 | + } |
| 102 | + |
| 103 | + particleSystem.update(deltaTime); |
| 104 | + particleSystem.fillBuffers(positionArray, timeArray, sizeArray); |
| 105 | + |
| 106 | + ctx.bindBuffer(ctx.ARRAY_BUFFER, positionBuffer); |
| 107 | + ctx.bufferData(ctx.ARRAY_BUFFER, positionArray, ctx.DYNAMIC_DRAW); |
| 108 | + ctx.vertexAttribPointer(positionAttrLocation, 2, ctx.FLOAT, false, 0, 0); |
| 109 | + |
| 110 | + ctx.bindBuffer(ctx.ARRAY_BUFFER, sizeBuffer); |
| 111 | + ctx.bufferData(ctx.ARRAY_BUFFER, sizeArray, ctx.DYNAMIC_DRAW); |
| 112 | + ctx.vertexAttribPointer(sizeAttrLocation, 1, ctx.FLOAT, false, 0, 0); |
| 113 | + |
| 114 | + ctx.bindBuffer(ctx.ARRAY_BUFFER, timeBuffer); |
| 115 | + ctx.bufferData(ctx.ARRAY_BUFFER, timeArray, ctx.DYNAMIC_DRAW); |
| 116 | + ctx.vertexAttribPointer(timeAttrLocation, 1, ctx.FLOAT, false, 0, 0); |
| 117 | + |
| 118 | + ctx.clearColor(0, 0, 0, 1); |
| 119 | + ctx.clear(ctx.COLOR_BUFFER_BIT); |
| 120 | + ctx.drawArrays(ctx.POINTS, 0, MAX_PARTICLE_COUNT); |
| 121 | + } |
| 122 | + |
| 123 | + runRenderLoop(performance.now()); |
| 124 | + |
| 125 | + return { |
| 126 | + destroy: () => { |
| 127 | + if (unsubscriber !== null) { |
| 128 | + unsubscriber(); |
| 129 | + } |
| 130 | + |
| 131 | + ctx.deleteBuffer(positionBuffer); |
| 132 | + ctx.deleteBuffer(sizeBuffer); |
| 133 | + ctx.deleteBuffer(timeBuffer); |
| 134 | + |
| 135 | + ctx.deleteProgram(program.program); |
| 136 | + ctx.deleteShader(vertexShader); |
| 137 | + ctx.deleteShader(fragmentShader); |
| 138 | + |
| 139 | + ctx.bindBuffer(ctx.ARRAY_BUFFER, null); |
| 140 | + ctx.useProgram(null); |
| 141 | + }, |
| 142 | + }; |
| 143 | +} |
| 144 | + |
| 145 | +export { renderApp }; |
0 commit comments