BlueGit

timfa frameworkv3

It begins

timfa Tim · pushed by timfa

Authored 2024-08-15 · 2 years ago

Hash
8e521cb1f12148b41d7a39f754e5ac3c6d1ad49d
Parent
a97cda44
10 files changed +385 −23
.gitignore +2 −0
@@ -1,2 +1,4 @@
1 1
2 2 js/framework\.js
3
4 framework/shaders.js
@@ -1,2 +1,4 @@
1
1
2js/framework\.js
2js/framework\.js
3
4framework/shaders.js
.gitlab-ci.yml +2 −0
@@ -10,6 +10,8 @@ build_job:
10 10 stage: build
11 11 script:
12 12 - apt-get update -qy
13 - chmod +x ./buildShaders.sh # Ensure the script is executable
14 - ./buildShaders.sh # Execute the script
13 15 - chmod +x ./glbuild.sh # Ensure the script is executable
14 16 - ./glbuild.sh # Execute the script
15 17 artifacts:
@@ -10,6 +10,8 @@ build_job:
10 stage: build
10 stage: build
11 script:
11 script:
12 - apt-get update -qy
12 - apt-get update -qy
13 - chmod +x ./buildShaders.sh # Ensure the script is executable
14 - ./buildShaders.sh # Execute the script
13 - chmod +x ./glbuild.sh # Ensure the script is executable
15 - chmod +x ./glbuild.sh # Ensure the script is executable
14 - ./glbuild.sh # Execute the script
16 - ./glbuild.sh # Execute the script
15 artifacts:
17 artifacts:
buildShaders.sh added +28 −0
@@ -0,0 +1,28 @@
1 #!/bin/bash
2
3 # Directory containing the shader files
4 SHADER_DIR="framework/shaders"
5 # Output file for the JavaScript shader object
6 OUTPUT_FILE="framework/shaders.js"
7
8 echo "const game_shaders_builtin = {" > "$OUTPUT_FILE"
9
10 for shader_file in "$SHADER_DIR"/*; do
11 base_name=$(basename "$shader_file")
12 name_without_ext="${base_name%.*}"
13
14 # Convert the filename to the desired key format
15 shader_name="${name_without_ext}_$(echo "$base_name" | awk -F. '{print $2}')"
16
17 # Strip comments, replace newlines with spaces, and escape double quotes
18 shader_content=$(sed -e 's/\/\/.*$//g' -e '/\/\*/,/\*\//d' "$shader_file" | tr '\n' ' ' | sed 's/"/\\"/g')
19
20 # Append the shader content to the output file
21 echo " \"$shader_name\": \"$shader_content\"," >> "$OUTPUT_FILE"
22 done
23
24 # Close the JSON object
25 echo "};" >> "$OUTPUT_FILE"
26 sed -i '$ s/,$//' "$OUTPUT_FILE"
27
28 echo "Shader object created in $OUTPUT_FILE"
@@ -0,0 +1,28 @@
1#!/bin/bash
2
3# Directory containing the shader files
4SHADER_DIR="framework/shaders"
5# Output file for the JavaScript shader object
6OUTPUT_FILE="framework/shaders.js"
7
8echo "const game_shaders_builtin = {" > "$OUTPUT_FILE"
9
10for shader_file in "$SHADER_DIR"/*; do
11 base_name=$(basename "$shader_file")
12 name_without_ext="${base_name%.*}"
13
14 # Convert the filename to the desired key format
15 shader_name="${name_without_ext}_$(echo "$base_name" | awk -F. '{print $2}')"
16
17 # Strip comments, replace newlines with spaces, and escape double quotes
18 shader_content=$(sed -e 's/\/\/.*$//g' -e '/\/\*/,/\*\//d' "$shader_file" | tr '\n' ' ' | sed 's/"/\\"/g')
19
20 # Append the shader content to the output file
21 echo " \"$shader_name\": \"$shader_content\"," >> "$OUTPUT_FILE"
22done
23
24# Close the JSON object
25echo "};" >> "$OUTPUT_FILE"
26sed -i '$ s/,$//' "$OUTPUT_FILE"
27
28echo "Shader object created in $OUTPUT_FILE"
framework/build.sh deleted +0 −23
@@ -1,23 +0,0 @@
1 BUILD_FILE="../js/framework.js"
2 SRC_DIR="../framework"
3
4 printf "Build started, output file: $BUILD_FILE\n"
5 unlink $BUILD_FILE
6
7 loop() {
8 shopt -s nullglob
9 shopt -s globstar
10
11 for file in "$@"/**
12 do
13 if [[ -f "$file" && "${file##*.}" = "js" ]]
14 then
15 printf "\n/*\n * Start File:\n * "$file"\n */ \n" >> "$BUILD_FILE";
16 printf "Building "$file"\n";
17 cat "$file" >> "$BUILD_FILE";
18 printf "\n/*\n * End File:\n * "$file"\n */ \n" >> "$BUILD_FILE";
19 fi
20 done
21 }
22 loop $SRC_DIR
23 printf "DONE"
@@ -1,23 +0,0 @@
1BUILD_FILE="../js/framework.js"
2SRC_DIR="../framework"
3
4printf "Build started, output file: $BUILD_FILE\n"
5unlink $BUILD_FILE
6
7loop() {
8 shopt -s nullglob
9 shopt -s globstar
10
11 for file in "$@"/**
12 do
13 if [[ -f "$file" && "${file##*.}" = "js" ]]
14 then
15 printf "\n/*\n * Start File:\n * "$file"\n */ \n" >> "$BUILD_FILE";
16 printf "Building "$file"\n";
17 cat "$file" >> "$BUILD_FILE";
18 printf "\n/*\n * End File:\n * "$file"\n */ \n" >> "$BUILD_FILE";
19 fi
20 done
21}
22loop $SRC_DIR
23printf "DONE"
framework/engine/TEMP_FOR_REFERENCE_main.js added +79 −0
@@ -0,0 +1,79 @@
1 // main.js
2 window.onload = init;
3
4 class Vector2
5 {
6 constructor (x, y)
7 {
8 this.x = x;
9 this.y = y;
10 }
11 }
12
13 const canvas = document.getElementById('canvas');
14 canvas.width = window.innerWidth;
15 canvas.height = window.innerHeight;
16
17 // Create the renderer (it will load default shaders if not provided)
18 const renderer = new Renderer(canvas, "bumpmap_vert", "bumpmap_frag");
19
20 const spriteImage = new Image();
21 spriteImage.src = 'sprite.png';
22
23 const normalImage = new Image();
24 normalImage.src = 'normal.png';
25
26 const position = new Vector2(200, 200);
27
28 const scale = new Vector2(0.4, 0.4);
29 var angle = 45;
30 var texture;
31 var normalTexture;
32
33 // TODO: See if we can use the old Framework way of loading images, better than this
34 spriteImage.onload = () =>
35 {
36 texture = renderer.loadTexture(spriteImage);
37 };
38
39 normalImage.onload = () =>
40 {
41 normalTexture = renderer.loadTexture(normalImage);
42 };
43
44 function init ()
45 {
46 setInterval(() =>
47 {
48 drawLoop();
49 }, 10);
50 }
51
52
53 function drawLoop ()
54 {
55 angle += 0.3;
56
57 // Update the light direction
58 const angleInRadians = (angle % 360) * (Math.PI / 180);
59 const lightDirection = [
60 Math.cos(angleInRadians),
61 Math.sin(angleInRadians),
62 0.2 // A default z-component for the light direction, adjust as needed
63 ];
64
65 renderer.bindTexture(normalTexture, renderer.gl.TEXTURE1);
66 // Render your sprite
67 renderer.render(
68 texture,
69 position,
70 scale,
71 0,
72 canvas.width,
73 canvas.height,
74 {
75 'u_normalMap': { unit: 1 }, // Example uniform
76 'u_lightDirection': [lightDirection[0], lightDirection[1], lightDirection[2]]
77 }
78 );
79 }
@@ -0,0 +1,79 @@
1// main.js
2window.onload = init;
3
4class Vector2
5{
6 constructor (x, y)
7 {
8 this.x = x;
9 this.y = y;
10 }
11}
12
13const canvas = document.getElementById('canvas');
14canvas.width = window.innerWidth;
15canvas.height = window.innerHeight;
16
17// Create the renderer (it will load default shaders if not provided)
18const renderer = new Renderer(canvas, "bumpmap_vert", "bumpmap_frag");
19
20const spriteImage = new Image();
21spriteImage.src = 'sprite.png';
22
23const normalImage = new Image();
24normalImage.src = 'normal.png';
25
26const position = new Vector2(200, 200);
27
28const scale = new Vector2(0.4, 0.4);
29var angle = 45;
30var texture;
31var normalTexture;
32
33// TODO: See if we can use the old Framework way of loading images, better than this
34spriteImage.onload = () =>
35{
36 texture = renderer.loadTexture(spriteImage);
37};
38
39normalImage.onload = () =>
40{
41 normalTexture = renderer.loadTexture(normalImage);
42};
43
44function init ()
45{
46 setInterval(() =>
47 {
48 drawLoop();
49 }, 10);
50}
51
52
53function drawLoop ()
54{
55 angle += 0.3;
56
57 // Update the light direction
58 const angleInRadians = (angle % 360) * (Math.PI / 180);
59 const lightDirection = [
60 Math.cos(angleInRadians),
61 Math.sin(angleInRadians),
62 0.2 // A default z-component for the light direction, adjust as needed
63 ];
64
65 renderer.bindTexture(normalTexture, renderer.gl.TEXTURE1);
66 // Render your sprite
67 renderer.render(
68 texture,
69 position,
70 scale,
71 0,
72 canvas.width,
73 canvas.height,
74 {
75 'u_normalMap': { unit: 1 }, // Example uniform
76 'u_lightDirection': [lightDirection[0], lightDirection[1], lightDirection[2]]
77 }
78 );
79}
framework/engine/objectComponents/glrenderer.js added +227 −0
@@ -0,0 +1,227 @@
1 class GLRenderer
2 {
3 constructor (canvas, vertexShaderSource = "default_vert", fragmentShaderSource = "default_frag")
4 {
5 this.canvas = canvas;
6 this.gl = canvas.getContext('webgl');
7
8 if (!this.gl)
9 {
10 console.error('WebGL not supported, falling back on experimental-webgl');
11 this.gl = canvas.getContext('experimental-webgl');
12 }
13
14 if (!this.gl)
15 {
16 throw new Error('Your browser does not support WebGL');
17 }
18
19 this.program = null;
20 this.positionLocation = null;
21 this.texCoordLocation = null;
22 this.matrixLocation = null;
23
24 this.uniformLocations = {};
25
26 // Create buffers for vertex positions and texture coordinates
27 this.positionBuffer = this.gl.createBuffer();
28 this.texCoordBuffer = this.gl.createBuffer();
29
30 // Default vertices for a quad
31 this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.positionBuffer);
32 const positions = [
33 -0.5, -0.5,
34 0.5, -0.5,
35 -0.5, 0.5,
36 0.5, 0.5,
37 ];
38 this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(positions), this.gl.STATIC_DRAW);
39
40 // Default texture coordinates
41 this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.texCoordBuffer);
42 const texCoords = [
43 0.0, 0.0,
44 1.0, 0.0,
45 0.0, 1.0,
46 1.0, 1.0,
47 ];
48 this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(texCoords), this.gl.STATIC_DRAW);
49
50 // Asynchronously load shaders
51 this.initShaders(vertexShaderSource, fragmentShaderSource);
52 }
53
54 async initShaders (vertexShaderSource, fragmentShaderSource)
55 {
56 // Once shaders are loaded, compile them and create the program
57 this.program = this.createProgram(game_shaders[vertexShaderSource], game_shaders[fragmentShaderSource]);
58
59 const numUniforms = this.gl.getProgramParameter(this.program, this.gl.ACTIVE_UNIFORMS);
60 for (let i = 0; i < numUniforms; i++)
61 {
62 const uniformInfo = this.gl.getActiveUniform(this.program, i);
63 if (uniformInfo)
64 {
65 const { name } = uniformInfo;
66 const location = this.gl.getUniformLocation(this.program, name);
67 if (location)
68 {
69 this.uniformLocations[name] = location;
70 }
71 }
72 }
73
74 // Get the locations of attributes and uniforms after the program is created
75 this.positionLocation = this.gl.getAttribLocation(this.program, 'a_position');
76 this.texCoordLocation = this.gl.getAttribLocation(this.program, 'a_texCoord');
77 this.matrixLocation = this.gl.getUniformLocation(this.program, 'u_matrix');
78 }
79
80 createShader (type, source)
81 {
82 const shader = this.gl.createShader(type);
83 this.gl.shaderSource(shader, source);
84 this.gl.compileShader(shader);
85 if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS))
86 {
87 console.error('An error occurred compiling the shaders:', this.gl.getShaderInfoLog(shader));
88 this.gl.deleteShader(shader);
89 return null;
90 }
91 return shader;
92 }
93
94 createProgram (vertexShaderSource, fragmentShaderSource)
95 {
96 const vertexShader = this.createShader(this.gl.VERTEX_SHADER, vertexShaderSource);
97 const fragmentShader = this.createShader(this.gl.FRAGMENT_SHADER, fragmentShaderSource);
98 const program = this.gl.createProgram();
99 this.gl.attachShader(program, vertexShader);
100 this.gl.attachShader(program, fragmentShader);
101 this.gl.linkProgram(program);
102 if (!this.gl.getProgramParameter(program, this.gl.LINK_STATUS))
103 {
104 console.error('Unable to initialize the shader program:', this.gl.getProgramInfoLog(program));
105 this.gl.deleteProgram(program);
106 return null;
107 }
108 return program;
109 }
110
111 loadTexture (image)
112 {
113 const texture = this.gl.createTexture();
114 this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
115 this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, image);
116 this.gl.generateMipmap(this.gl.TEXTURE_2D);
117 return texture;
118 }
119
120 bindTexture (texture, unit)
121 {
122 this.gl.activeTexture(unit);
123 this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
124 }
125
126 setUniform (name, value)
127 {
128 const location = this.uniformLocations[name];
129 if (location !== undefined)
130 {
131 if (value instanceof Float32Array)
132 {
133 this.gl.uniformMatrix4fv(location, false, value);
134 } else if (typeof value === 'number')
135 {
136 this.gl.uniform1f(location, value);
137 } else if (Array.isArray(value))
138 {
139 if (value.length === 2)
140 {
141 this.gl.uniform2fv(location, value);
142 } else if (value.length === 3)
143 {
144 this.gl.uniform3fv(location, value);
145 } else if (value.length === 4)
146 {
147 this.gl.uniform4fv(location, value);
148 }
149 } else if (value.unit)
150 {
151 // This is for textures, so we use a specific uniform location for texture unit
152 this.gl.uniform1i(location, value.unit);
153 }
154 }
155 else
156 {
157 console.warn(`Uniform location for ${name} not found`);
158 }
159 }
160
161 render (texture, position, scale, angle, canvasWidth, canvasHeight, uniforms = {})
162 {
163 if (!this.program)
164 {
165 console.warn('Shaders not initialized yet.');
166 return;
167 }
168
169 this.gl.clear(this.gl.COLOR_BUFFER_BIT);
170
171 this.gl.useProgram(this.program);
172
173 // Convert from world coordinates to WebGL normalized device coordinates
174 const normalizedPositionX = (position.x / canvasWidth) * 2 - 1;
175 const normalizedPositionY = 1 - (position.y / canvasHeight) * 2; // Flip Y-axis
176
177 // Bind buffers for vertices and texture coordinates
178 this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.positionBuffer);
179 this.gl.enableVertexAttribArray(this.positionLocation);
180 this.gl.vertexAttribPointer(this.positionLocation, 2, this.gl.FLOAT, false, 0, 0);
181
182 this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.texCoordBuffer);
183 this.gl.enableVertexAttribArray(this.texCoordLocation);
184 this.gl.vertexAttribPointer(this.texCoordLocation, 2, this.gl.FLOAT, false, 0, 0);
185
186 // Set the transformation matrix (using normalized position)
187 const matrix = this.createTransformationMatrix(
188 { x: normalizedPositionX, y: normalizedPositionY },
189 scale,
190 angle
191 );
192 this.setUniform('u_matrix', matrix);
193
194 // Bind the texture
195 if (texture)
196 {
197 this.bindTexture(texture, this.gl.TEXTURE0);
198 this.setUniform('u_texture', { unit: 0 });
199 }
200
201 // Set any additional uniforms
202 Object.entries(uniforms).forEach(([name, value]) =>
203 {
204 this.setUniform(name, value);
205 });
206
207 // Draw the sprite
208 this.gl.drawArrays(this.gl.TRIANGLE_STRIP, 0, 4);
209 }
210
211 createTransformationMatrix (position, scale, angle)
212 {
213 const rad = angle * Math.PI / 180;
214 const cos = Math.cos(rad);
215 const sin = Math.sin(rad);
216
217 // Apply rotation, scaling, and translation
218 const matrix = new Float32Array([
219 cos * scale.x, -sin * scale.y, 0, 0,
220 sin * scale.x, cos * scale.y, 0, 0,
221 0, 0, 1, 0,
222 position.x, position.y, 0, 1
223 ]);
224
225 return matrix;
226 }
227 }
@@ -0,0 +1,227 @@
1class GLRenderer
2{
3 constructor (canvas, vertexShaderSource = "default_vert", fragmentShaderSource = "default_frag")
4 {
5 this.canvas = canvas;
6 this.gl = canvas.getContext('webgl');
7
8 if (!this.gl)
9 {
10 console.error('WebGL not supported, falling back on experimental-webgl');
11 this.gl = canvas.getContext('experimental-webgl');
12 }
13
14 if (!this.gl)
15 {
16 throw new Error('Your browser does not support WebGL');
17 }
18
19 this.program = null;
20 this.positionLocation = null;
21 this.texCoordLocation = null;
22 this.matrixLocation = null;
23
24 this.uniformLocations = {};
25
26 // Create buffers for vertex positions and texture coordinates
27 this.positionBuffer = this.gl.createBuffer();
28 this.texCoordBuffer = this.gl.createBuffer();
29
30 // Default vertices for a quad
31 this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.positionBuffer);
32 const positions = [
33 -0.5, -0.5,
34 0.5, -0.5,
35 -0.5, 0.5,
36 0.5, 0.5,
37 ];
38 this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(positions), this.gl.STATIC_DRAW);
39
40 // Default texture coordinates
41 this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.texCoordBuffer);
42 const texCoords = [
43 0.0, 0.0,
44 1.0, 0.0,
45 0.0, 1.0,
46 1.0, 1.0,
47 ];
48 this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(texCoords), this.gl.STATIC_DRAW);
49
50 // Asynchronously load shaders
51 this.initShaders(vertexShaderSource, fragmentShaderSource);
52 }
53
54 async initShaders (vertexShaderSource, fragmentShaderSource)
55 {
56 // Once shaders are loaded, compile them and create the program
57 this.program = this.createProgram(game_shaders[vertexShaderSource], game_shaders[fragmentShaderSource]);
58
59 const numUniforms = this.gl.getProgramParameter(this.program, this.gl.ACTIVE_UNIFORMS);
60 for (let i = 0; i < numUniforms; i++)
61 {
62 const uniformInfo = this.gl.getActiveUniform(this.program, i);
63 if (uniformInfo)
64 {
65 const { name } = uniformInfo;
66 const location = this.gl.getUniformLocation(this.program, name);
67 if (location)
68 {
69 this.uniformLocations[name] = location;
70 }
71 }
72 }
73
74 // Get the locations of attributes and uniforms after the program is created
75 this.positionLocation = this.gl.getAttribLocation(this.program, 'a_position');
76 this.texCoordLocation = this.gl.getAttribLocation(this.program, 'a_texCoord');
77 this.matrixLocation = this.gl.getUniformLocation(this.program, 'u_matrix');
78 }
79
80 createShader (type, source)
81 {
82 const shader = this.gl.createShader(type);
83 this.gl.shaderSource(shader, source);
84 this.gl.compileShader(shader);
85 if (!this.gl.getShaderParameter(shader, this.gl.COMPILE_STATUS))
86 {
87 console.error('An error occurred compiling the shaders:', this.gl.getShaderInfoLog(shader));
88 this.gl.deleteShader(shader);
89 return null;
90 }
91 return shader;
92 }
93
94 createProgram (vertexShaderSource, fragmentShaderSource)
95 {
96 const vertexShader = this.createShader(this.gl.VERTEX_SHADER, vertexShaderSource);
97 const fragmentShader = this.createShader(this.gl.FRAGMENT_SHADER, fragmentShaderSource);
98 const program = this.gl.createProgram();
99 this.gl.attachShader(program, vertexShader);
100 this.gl.attachShader(program, fragmentShader);
101 this.gl.linkProgram(program);
102 if (!this.gl.getProgramParameter(program, this.gl.LINK_STATUS))
103 {
104 console.error('Unable to initialize the shader program:', this.gl.getProgramInfoLog(program));
105 this.gl.deleteProgram(program);
106 return null;
107 }
108 return program;
109 }
110
111 loadTexture (image)
112 {
113 const texture = this.gl.createTexture();
114 this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
115 this.gl.texImage2D(this.gl.TEXTURE_2D, 0, this.gl.RGBA, this.gl.RGBA, this.gl.UNSIGNED_BYTE, image);
116 this.gl.generateMipmap(this.gl.TEXTURE_2D);
117 return texture;
118 }
119
120 bindTexture (texture, unit)
121 {
122 this.gl.activeTexture(unit);
123 this.gl.bindTexture(this.gl.TEXTURE_2D, texture);
124 }
125
126 setUniform (name, value)
127 {
128 const location = this.uniformLocations[name];
129 if (location !== undefined)
130 {
131 if (value instanceof Float32Array)
132 {
133 this.gl.uniformMatrix4fv(location, false, value);
134 } else if (typeof value === 'number')
135 {
136 this.gl.uniform1f(location, value);
137 } else if (Array.isArray(value))
138 {
139 if (value.length === 2)
140 {
141 this.gl.uniform2fv(location, value);
142 } else if (value.length === 3)
143 {
144 this.gl.uniform3fv(location, value);
145 } else if (value.length === 4)
146 {
147 this.gl.uniform4fv(location, value);
148 }
149 } else if (value.unit)
150 {
151 // This is for textures, so we use a specific uniform location for texture unit
152 this.gl.uniform1i(location, value.unit);
153 }
154 }
155 else
156 {
157 console.warn(`Uniform location for ${name} not found`);
158 }
159 }
160
161 render (texture, position, scale, angle, canvasWidth, canvasHeight, uniforms = {})
162 {
163 if (!this.program)
164 {
165 console.warn('Shaders not initialized yet.');
166 return;
167 }
168
169 this.gl.clear(this.gl.COLOR_BUFFER_BIT);
170
171 this.gl.useProgram(this.program);
172
173 // Convert from world coordinates to WebGL normalized device coordinates
174 const normalizedPositionX = (position.x / canvasWidth) * 2 - 1;
175 const normalizedPositionY = 1 - (position.y / canvasHeight) * 2; // Flip Y-axis
176
177 // Bind buffers for vertices and texture coordinates
178 this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.positionBuffer);
179 this.gl.enableVertexAttribArray(this.positionLocation);
180 this.gl.vertexAttribPointer(this.positionLocation, 2, this.gl.FLOAT, false, 0, 0);
181
182 this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.texCoordBuffer);
183 this.gl.enableVertexAttribArray(this.texCoordLocation);
184 this.gl.vertexAttribPointer(this.texCoordLocation, 2, this.gl.FLOAT, false, 0, 0);
185
186 // Set the transformation matrix (using normalized position)
187 const matrix = this.createTransformationMatrix(
188 { x: normalizedPositionX, y: normalizedPositionY },
189 scale,
190 angle
191 );
192 this.setUniform('u_matrix', matrix);
193
194 // Bind the texture
195 if (texture)
196 {
197 this.bindTexture(texture, this.gl.TEXTURE0);
198 this.setUniform('u_texture', { unit: 0 });
199 }
200
201 // Set any additional uniforms
202 Object.entries(uniforms).forEach(([name, value]) =>
203 {
204 this.setUniform(name, value);
205 });
206
207 // Draw the sprite
208 this.gl.drawArrays(this.gl.TRIANGLE_STRIP, 0, 4);
209 }
210
211 createTransformationMatrix (position, scale, angle)
212 {
213 const rad = angle * Math.PI / 180;
214 const cos = Math.cos(rad);
215 const sin = Math.sin(rad);
216
217 // Apply rotation, scaling, and translation
218 const matrix = new Float32Array([
219 cos * scale.x, -sin * scale.y, 0, 0,
220 sin * scale.x, cos * scale.y, 0, 0,
221 0, 0, 1, 0,
222 position.x, position.y, 0, 1
223 ]);
224
225 return matrix;
226 }
227}
framework/shaders/bumpmap.frag added +15 −0
@@ -0,0 +1,15 @@
1 precision mediump float;
2
3 uniform sampler2D u_texture;
4 uniform sampler2D u_normalMap;
5 uniform vec3 u_lightDirection;
6
7 varying vec2 v_texCoord;
8
9 void main() {
10 vec4 color = texture2D(u_texture, v_texCoord);
11 vec3 normal = texture2D(u_normalMap, v_texCoord).rgb;
12 normal = normalize(normal * 2.0 - 1.0); // Convert from [0,1] to [-1,1]
13 float diffuse = max(dot(normal, u_lightDirection), 0.0);
14 gl_FragColor = color + diffuse * 0.6;
15 }
@@ -0,0 +1,15 @@
1precision mediump float;
2
3uniform sampler2D u_texture;
4uniform sampler2D u_normalMap;
5uniform vec3 u_lightDirection;
6
7varying vec2 v_texCoord;
8
9void main() {
10 vec4 color = texture2D(u_texture, v_texCoord);
11 vec3 normal = texture2D(u_normalMap, v_texCoord).rgb;
12 normal = normalize(normal * 2.0 - 1.0); // Convert from [0,1] to [-1,1]
13 float diffuse = max(dot(normal, u_lightDirection), 0.0);
14 gl_FragColor = color + diffuse * 0.6;
15}
framework/shaders/bumpmap.vert added +14 −0
@@ -0,0 +1,14 @@
1 attribute vec4 a_position;
2 attribute vec2 a_texCoord;
3 attribute vec2 a_normalTexCoord; // Normal map texture coordinates
4
5 uniform mat4 u_matrix;
6
7 varying vec2 v_texCoord;
8 varying vec2 v_normalTexCoord;
9
10 void main() {
11 gl_Position = u_matrix * a_position;
12 v_texCoord = a_texCoord;
13 v_normalTexCoord = a_normalTexCoord;
14 }
@@ -0,0 +1,14 @@
1attribute vec4 a_position;
2attribute vec2 a_texCoord;
3attribute vec2 a_normalTexCoord; // Normal map texture coordinates
4
5uniform mat4 u_matrix;
6
7varying vec2 v_texCoord;
8varying vec2 v_normalTexCoord;
9
10void main() {
11 gl_Position = u_matrix * a_position;
12 v_texCoord = a_texCoord;
13 v_normalTexCoord = a_normalTexCoord;
14}
framework/shaders/default.frag added +7 −0
@@ -0,0 +1,7 @@
1 precision mediump float;
2 uniform sampler2D u_texture;
3 varying vec2 v_texCoord;
4 void main() {
5 vec4 color = texture2D(u_texture, v_texCoord);
6 gl_FragColor = color;
7 }
@@ -0,0 +1,7 @@
1precision mediump float;
2uniform sampler2D u_texture;
3varying vec2 v_texCoord;
4void main() {
5 vec4 color = texture2D(u_texture, v_texCoord);
6 gl_FragColor = color;
7}
framework/shaders/default.vert added +11 −0
@@ -0,0 +1,11 @@
1 attribute vec4 a_position;
2 attribute vec2 a_texCoord;
3
4 uniform mat4 u_matrix;
5
6 varying vec2 v_texCoord;
7
8 void main() {
9 gl_Position = u_matrix * a_position;
10 v_texCoord = a_texCoord;
11 }
@@ -0,0 +1,11 @@
1attribute vec4 a_position;
2attribute vec2 a_texCoord;
3
4uniform mat4 u_matrix;
5
6varying vec2 v_texCoord;
7
8void main() {
9 gl_Position = u_matrix * a_position;
10 v_texCoord = a_texCoord;
11}