Skip to content

Commit c52031c

Browse files
committed
add the hot reload pipeline (shader changes) + prerendered zbuffer 1/2 step
2 parents 9c512b6 + 99e9263 commit c52031c

33 files changed

+1057
-248
lines changed

public/templates/prerendered/source/readme.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ matrix[0][0] = matrix[0][0] * -1;
1717
matrix[1][0] = matrix[1][0] * -1;
1818
matrix[2][0] = matrix[2][0] * -1;
1919
matrix[3][0] = matrix[3][0] * -1;
20-
print(matrix);
20+
print(matrix);
21+
22+
18 aout à 15h
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Ident": "JSM", "NumVertices": 6, "Vertices": [45.2641, 5.3293, 17.9971, -3.0742, -27.035, 49.9269, 0.5249, 5.3293, 54.3556, 45.2641, 5.3293, 17.9971, 41.665, -27.035, 13.5684, -3.0742, -27.035, 49.9269], "Colors": [], "Normals": [-0.6211, 0.1736, -0.7643, -0.6211, 0.1736, -0.7643, -0.6211, 0.1736, -0.7643, -0.6211, 0.1736, -0.7643, -0.6211, 0.1736, -0.7643, -0.6211, 0.1736, -0.7643], "TextureCoords": [0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0], "NumTextureCoords": 6, "NumNormals": 6, "NumColors": 0}

public/templates/prerendered/zbuffer/scene.room

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"MusicFile": "templates/prerendered/music.mp3",
88
"MapFile": "templates/prerendered/zbuffer/scene.jsm",
99
"MapTextureFile": "templates/prerendered/zbuffer/scene.png",
10-
"MapZBufferFile": "templates/prerendered/zbuffer/scene-zbuffer.png",
10+
"MapZBufferFile": "templates/prerendered/zbuffer/scene-zbuffer.jsm",
11+
"MapZBufferTextureFile": "templates/prerendered/zbuffer/scene-zbuffer.png",
1112
"WalkmeshFile": "templates/prerendered/zbuffer/scene.jwm",
1213
"Camera": {
1314
"Matrix": [-7.762123107910156, 0.0, 6.306341648101807, 0.0, 1.0994617938995361, 9.847860336303711, 1.3532658815383911, 0.0, -6.209760665893555, 1.7436010837554932, -7.643247604370117, 0.0, -28.044504165649414, 2.4879045486450195, -26.32898712158203, 1.0],

src/boot_screen.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { TripleTriadScreen } from './templates/triple-triad/triple_triad_screen'
2222
import { VisualNovelScreen } from './templates/visual-novel/visual_novel_screen';
2323
import { PhysicsJoltScreen } from './templates/physics-jolt/physics_jolt_screen';
2424
import { PhysicsBox2DScreen } from './templates/physics-box2d/physics_box2d_screen';
25+
import { CarScreen } from './templates/car/car_screen';
2526
// ---------------------------------------------------------------------------------------
2627
import { CurveScreen } from './utils/curve/curve_screen';
2728
import { MenuRingScreen } from './utils/menu-ring/menu_ring_screen';
@@ -55,7 +56,7 @@ class BootScreen extends Screen {
5556
this.uiTemplates.add('2', '2D Fight');
5657
this.uiTemplates.add('3', '3D FPS');
5758
this.uiTemplates.add('4', '3D Isometric');
58-
this.uiTemplates.add('5', '3D Pre-rendered Paint');
59+
this.uiTemplates.add('5', '3D Pre-rendered (no z-buffer)');
5960
this.uiTemplates.add('6', '3D RPG');
6061
this.uiTemplates.add('7', '2D Shootem\'up');
6162
this.uiTemplates.add('8', '2D TCG');
@@ -68,7 +69,7 @@ class BootScreen extends Screen {
6869
this.uiTemplates.add('15', '2D Platformer');
6970
this.uiTemplates.add('16', '3D Physics With Jolt');
7071
this.uiTemplates.add('17', '2D Physics With Box2D');
71-
this.uiTemplates.add('18', '3D Pre-rendered ZBuffer');
72+
this.uiTemplates.add('18', '3D Pre-rendered (zbuffer)');
7273
this.uiTemplates.setVisible(false);
7374
uiManager.addWidget(this.uiTemplates, 'position:absolute; top:50%; left:50%; width:60%; transform:translate(-50%,-50%);');
7475

src/lib/gfx3/gfx3_debug_shader.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export const PIPELINE_DESC: any = {
2323
targets: [
2424
{ format: navigator.gpu.getPreferredCanvasFormat()},
2525
{ format: 'rgba16float' }, // normals
26-
{ format: 'rgba16float' } // ids
26+
{ format: 'rgba16float' }, // ids
27+
{ format: 'rgba16float' } // ch1
2728
]
2829
},
2930
primitive: {
@@ -38,7 +39,7 @@ export const PIPELINE_DESC: any = {
3839
}
3940
};
4041

41-
export const VERTEX_SHADER = /* wgsl */`
42+
export const VERTEX_SHADER = (data: any) => /* wgsl */`
4243
struct VertexOutput {
4344
@builtin(position) Position: vec4<f32>,
4445
@location(0) Color: vec3<f32>
@@ -57,11 +58,12 @@ fn main(
5758
return output;
5859
}`;
5960

60-
export const FRAGMENT_SHADER = /* wgsl */`
61+
export const FRAGMENT_SHADER = (data: any) => /* wgsl */`
6162
struct FragOutput {
6263
@location(0) Base: vec4f,
6364
@location(1) Normal: vec4f,
64-
@location(2) Id: vec4f
65+
@location(2) Id: vec4f,
66+
@location(3) Ch1: vec4f
6567
}
6668
6769
@fragment
@@ -72,5 +74,6 @@ fn main(
7274
output.Base = vec4(Color, 1);
7375
output.Normal = vec4(0.0, 0.0, 0.0, 0.0);
7476
output.Id = vec4(0.0, 0.0, 0.0, 0.0);
77+
output.Ch1 = vec4(0.0, 0.0, 0.0, 0.0);
7578
return output;
7679
}`;

src/lib/gfx3/gfx3_drawable.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ class Gfx3Drawable extends Gfx3Transformable implements Poolable<Gfx3Drawable> {
134134
* ■ dither: a = 4
135135
* ■ outline: a = 8
136136
* ■ shadow volume: a = 16
137-
* ■ second channel: a = 32
137+
* ■ channel 1: a = 32 (channel 1 is a rendering texture used by post process unit for many different things, it is invisible by default)
138138
*
139139
* @param {number} r - The pur identifier you can use for custom stuff.
140140
* @param {number} g - The decals group.
141141
* @param {number} b - The lights group.
142142
* @param {number} a - The flags value for specials effects.
143143
*/
144-
setId(r: number, g: number, b: number, a: number): void {
144+
setId(r: number, g: number = 0, b: number = 0, a: number = 0): void {
145145
this.id = [r, g, b, a];
146146
}
147147

@@ -203,6 +203,7 @@ class Gfx3Drawable extends Gfx3Transformable implements Poolable<Gfx3Drawable> {
203203
drawable.rotation = [this.rotation[0], this.rotation[1], this.rotation[2]];
204204
drawable.scale = [this.scale[0], this.scale[1], this.scale[2]];
205205
drawable.quaternion = new Quaternion(this.quaternion.w, this.quaternion.x, this.quaternion.y, this.quaternion.z);
206+
drawable.id = this.id;
206207
drawable.boundingBox = new Gfx3BoundingBox(this.boundingBox.min, this.boundingBox.max);
207208

208209
drawable.beginVertices(this.vertexCount);

src/lib/gfx3/gfx3_group.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ class Gfx3StaticGroup {
188188
getBindGroup(): GPUBindGroup {
189189
return this.bindGroup!;
190190
}
191+
192+
/**
193+
* Set a new pipeline.
194+
* Of course, you need to call allocate() after setPipeline().
195+
*
196+
* @param {GPURenderPipeline} pipeline - The new pipeline.
197+
*/
198+
setPipeline(pipeline: GPURenderPipeline): void {
199+
this.pipeline = pipeline;
200+
}
191201
}
192202

193203
/**
@@ -349,6 +359,16 @@ class Gfx3DynamicGroup {
349359
getSize(): number {
350360
return this.size;
351361
}
362+
363+
/**
364+
* Set a new pipeline.
365+
* Of course, you need to call allocate() after setPipeline().
366+
*
367+
* @param {GPURenderPipeline} pipeline - The new pipeline.
368+
*/
369+
setPipeline(pipeline: GPURenderPipeline): void {
370+
this.pipeline = pipeline;
371+
}
352372
}
353373

354374
export { Gfx3StaticGroup, Gfx3DynamicGroup };

src/lib/gfx3/gfx3_manager.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Gfx3Manager {
2727
normalsTexture: Gfx3RenderingTexture;
2828
idsTexture: Gfx3RenderingTexture;
2929
depthTexture: Gfx3RenderingTexture;
30+
channel1Texture: Gfx3RenderingTexture;
3031
commandEncoder: GPUCommandEncoder;
3132
passEncoder: GPURenderPassEncoder;
3233
pipelines: Map<string, GPURenderPipeline>;
@@ -51,6 +52,8 @@ class Gfx3Manager {
5152
this.normalsTexture = {} as Gfx3RenderingTexture;
5253
this.idsTexture = {} as Gfx3RenderingTexture;
5354
this.depthTexture = {} as Gfx3RenderingTexture;
55+
this.channel1Texture = {} as Gfx3RenderingTexture;
56+
5457
this.commandEncoder = {} as GPUCommandEncoder;
5558
this.passEncoder = {} as GPURenderPassEncoder;
5659
this.pipelines = new Map<string, GPURenderPipeline>();
@@ -109,6 +112,7 @@ class Gfx3Manager {
109112
this.normalsTexture = this.createRenderingTexture('rgba16float');
110113
this.idsTexture = this.createRenderingTexture('rgba16float');
111114
this.depthTexture = this.createRenderingTexture('depth24plus');
115+
this.channel1Texture = this.createRenderingTexture('rgba16float');
112116
this.vertexBuffer = this.device.createBuffer({ size: 0, usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST });
113117

114118
eventManager.subscribe(coreManager, 'E_RESIZE', this, this.#handleWindowResize);
@@ -185,6 +189,11 @@ class Gfx3Manager {
185189
clearValue: {r: 0.0, g: 0.0, b: 0.0, a: 1.0},
186190
loadOp: 'clear',
187191
storeOp: 'store'
192+
},{
193+
view: this.channel1Texture.gpuTextureView,
194+
clearValue: {r: 0.0, g: 0.0, b: 0.0, a: 1.0},
195+
loadOp: 'clear',
196+
storeOp: 'store'
188197
}],
189198
depthStencilAttachment: {
190199
view: this.depthTexture.gpuTextureView,
@@ -563,6 +572,13 @@ class Gfx3Manager {
563572
return this.depthTexture;
564573
}
565574

575+
/**
576+
* Returns the channel 1 texture.
577+
*/
578+
getChannel1Texture(): Gfx3RenderingTexture {
579+
return this.channel1Texture;
580+
}
581+
566582
/**
567583
* Returns the GPUCommandEncoder.
568584
*/

src/lib/gfx3/gfx3_renderer_abstract.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,34 @@ import { gfx3Manager } from '../gfx3/gfx3_manager';
55
*/
66
abstract class Gfx3RendererAbstract {
77
pipeline: GPURenderPipeline;
8+
pipelineId: string;
9+
data: any;
810

911
/**
1012
* @param {string} pipelineId - A unique identifier for the render pipeline.
11-
* @param {string} vertexShader - The code for the vertex shader.
12-
* @param {string} fragmentShader - The code for the fragment shader.
13+
* @param {Function} vertexShader - The code for the vertex shader.
14+
* @param {Function} fragmentShader - The code for the fragment shader.
1315
* @param {GPURenderPipelineDescriptor} pipelineDesc - The configuration of the pipeline, such as the vertex and fragment shaders, the color and depth formats, the primitive topology
16+
* @param {any} data - The custom data used by the shader template.
1417
*/
15-
constructor(pipelineId: string, vertexShader: string, fragmentShader: string, pipelineDesc: GPURenderPipelineDescriptor) {
16-
this.pipeline = gfx3Manager.loadPipeline(pipelineId, vertexShader, fragmentShader, pipelineDesc);
18+
constructor(pipelineId: string, vertexShader: (data: any) => string, fragmentShader: (data: any) => string, pipelineDesc: GPURenderPipelineDescriptor, data: any = {}) {
19+
this.pipeline = gfx3Manager.loadPipeline(pipelineId, vertexShader(data), fragmentShader(data), pipelineDesc);
20+
this.pipelineId = pipelineId;
21+
this.data = data;
22+
}
23+
24+
/**
25+
* Reload the pipeline.
26+
*
27+
* @param {Function} vertexShader - The code for the vertex shader.
28+
* @param {Function} fragmentShader - The code for the fragment shader.
29+
* @param {GPURenderPipelineDescriptor} pipelineDesc - The configuration of the pipeline, such as the vertex and fragment shaders, the color and depth formats, the primitive topology
30+
* @param {any} data - The custom data used by the shader template.
31+
*/
32+
reload(vertexShader: (options: any) => string, fragmentShader: (options: any) => string, pipelineDesc: GPURenderPipelineDescriptor, data: any = {}): void {
33+
gfx3Manager.deletePipeline(this.pipelineId);
34+
this.pipeline = gfx3Manager.loadPipeline(this.pipelineId, vertexShader(data), fragmentShader(data), pipelineDesc);
35+
this.data = data;
1736
}
1837
}
1938

src/lib/gfx3_flare/gfx3_flare_lens.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ class Gfx3FlareLens {
114114
setMaxDistanceBrightness(maxDistanceBrightness: number): void {
115115
this.maxDistanceBrightness = maxDistanceBrightness;
116116
}
117+
118+
/**
119+
* Replace default flares config by a custom one.
120+
*
121+
* @param {Array<Gfx3Flare>} flares - The flare list.
122+
*/
123+
setFlares(flares: Array<Gfx3Flare>): void {
124+
this.flares = flares;
125+
}
117126
}
118127

119128
export { Gfx3FlareLens };

0 commit comments

Comments
 (0)