|
1 | 1 | import {DebugRendererLayer} from "./DebugRenderer"; |
2 | 2 | import {gameMap} from "../../../game/GameData"; |
3 | 3 | import {getSettingObject} from "../../../util/settings/UserSettingManager"; |
| 4 | +import {GameGLContext, WebGLUniforms} from "../../GameGLContext"; |
| 5 | +import {linearLookupFragmentShader, mapGridLookupVertexShader} from "../../shader/ShaderManager"; |
| 6 | +import {BaseRendererLayer} from "../BaseRendererLayer"; |
4 | 7 |
|
5 | 8 | //@module renderer-debug |
6 | 9 |
|
7 | | -export class TerrainInfluenceRenderer implements DebugRendererLayer { |
| 10 | +export class TerrainInfluenceRenderer extends BaseRendererLayer implements DebugRendererLayer{ |
8 | 11 | readonly useCache = true; |
| 12 | + private program: WebGLProgram; |
| 13 | + private vao: WebGLVertexArrayObject; |
| 14 | + private uniforms: WebGLUniforms<"size" | "palette_data" | "length">; |
| 15 | + private palette: WebGLTexture; |
| 16 | + private positionBuffer: WebGLBuffer; |
| 17 | + private influenceBuffer: WebGLBuffer; |
9 | 18 |
|
10 | 19 | /** |
11 | 20 | * Create a new terrain influence renderer |
12 | 21 | * @param simplified Whether to show tiles of the same area in the same color. |
13 | 22 | * @param navigableOnly Whether to only show navigable tiles. |
14 | 23 | */ |
15 | | - constructor(private simplified: boolean, private navigableOnly: boolean) {} |
| 24 | + constructor(private simplified: boolean, private navigableOnly: boolean) { |
| 25 | + super(); |
| 26 | + } |
| 27 | + |
| 28 | + setup(context: GameGLContext): void { |
| 29 | + this.program = context.requireProgram(mapGridLookupVertexShader, linearLookupFragmentShader, "Terrain influence debug renderer failed to init"); |
| 30 | + this.positionBuffer = context.createBuffer(); |
| 31 | + this.influenceBuffer = context.createBuffer(); |
| 32 | + this.vao = context.createVertexArray(this.program, {name: "pos", size: 1, type: WebGL2RenderingContext.UNSIGNED_INT, buffer: this.positionBuffer, asInt: true}, {name: "id", size: 1, type: WebGL2RenderingContext.UNSIGNED_SHORT, buffer: this.influenceBuffer, asInt: true}); |
| 33 | + this.uniforms = context.loadUniforms(this.program, "size", "palette_data", "length"); |
| 34 | + this.palette = context.createTexture(3, 1, new Uint8Array([255, 0, 0, 128, 255, 255, 0, 128, 0, 255, 0, 128, 0, 255, 255, 128, 0, 0, 255, 128, 255, 0, 255, 128, 255, 0, 0, 128]), {internalFormat: WebGL2RenderingContext.RGBA, format: WebGL2RenderingContext.RGBA, magFilter: WebGL2RenderingContext.LINEAR}); |
| 35 | + } |
| 36 | + |
| 37 | + render(context: GameGLContext): void { |
| 38 | + context.bind(this.program, this.vao); |
| 39 | + context.bindTexture(this.palette); |
16 | 40 |
|
17 | | - render(context: CanvasRenderingContext2D): void { |
18 | | - const colorMap = new Map<number, string>(); |
| 41 | + const tiles = []; |
| 42 | + const influence = []; |
| 43 | + const colorMap: number[] = []; |
19 | 44 | for (let i = 0; i < gameMap.width * gameMap.height; i++) { |
20 | 45 | if (this.navigableOnly && !gameMap.getTile(i).navigable) continue; |
21 | 46 | const area = this.simplified ? gameMap.areaMap[gameMap.tileInfluence[i]] : gameMap.tileInfluence[i]; |
22 | | - let color = colorMap.get(area); |
| 47 | + let color = colorMap[area]; |
23 | 48 | if (!color) { |
24 | | - color = `rgba(${Math.random() * 255}, ${Math.random() * 255}, ${Math.random() * 255}, 0.5)`; |
25 | | - colorMap.set(area, color); |
| 49 | + color = Math.floor(Math.random() * 6 * 255); |
| 50 | + colorMap[area] = color; |
26 | 51 | } |
27 | | - context.fillStyle = color; |
28 | | - context.fillRect(i % gameMap.width, Math.floor(i / gameMap.width), 1, 1); |
| 52 | + tiles.push(i); |
| 53 | + influence.push(color); |
29 | 54 | } |
| 55 | + |
| 56 | + this.uniforms.set2i("size", gameMap.width, gameMap.height); |
| 57 | + this.uniforms.set1ui("length", 6 * 255); |
| 58 | + this.uniforms.set1i("palette_data", 0); |
| 59 | + |
| 60 | + context.bufferData(this.positionBuffer, new Uint32Array(tiles), WebGL2RenderingContext.DYNAMIC_DRAW); |
| 61 | + context.bufferData(this.influenceBuffer, new Uint16Array(influence), WebGL2RenderingContext.DYNAMIC_DRAW); |
| 62 | + |
| 63 | + context.drawPoints(gameMap.width * gameMap.height); |
30 | 64 | } |
31 | 65 | } |
32 | 66 |
|
|
0 commit comments