forked from Asvarox/allkaraoke
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
289 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/Scenes/Game/Singing/GameOverlay/Drawing/Shaders/Shaders.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import GameState from 'Scenes/Game/Singing/GameState/GameState'; | ||
import { Curtains, Plane, PlaneParams } from 'curtainsjs'; | ||
|
||
export class Shaders { | ||
private curtains: Curtains; | ||
private plane: Plane; | ||
public constructor(private canvas: HTMLCanvasElement) { | ||
this.curtains = new Curtains({ | ||
container: 'canvas', | ||
}); | ||
const { gl } = this.curtains; | ||
// https://stackoverflow.com/a/39354174 | ||
gl.enable(gl.BLEND); | ||
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA); | ||
|
||
const planeElement = document.getElementById('plane')!; | ||
const planeDimensions = planeElement.getBoundingClientRect(); | ||
|
||
const playerUniforms: Record<string, any> = GameState.getPlayers().reduce( | ||
(uniforms, player) => ({ | ||
...uniforms, | ||
[`p${player.getNumber()}center`]: { | ||
name: `uP${player.getNumber()}center`, | ||
type: '2f', | ||
value: [0, 0], | ||
}, | ||
[`p${player.getNumber()}force`]: { | ||
name: `uP${player.getNumber()}force`, | ||
type: '1f', | ||
value: 0, | ||
}, | ||
}), | ||
{ | ||
resolution: { | ||
// resolution of our plane | ||
name: 'uResolution', | ||
type: '2f', | ||
value: [planeDimensions.width, planeDimensions.height], | ||
}, | ||
time: { | ||
name: 'uTime', // uniform name that will be passed to our shaders | ||
type: '1f', // this means our uniform is a float | ||
value: 0, | ||
}, | ||
}, | ||
); | ||
|
||
// set our initial parameters (basic uniforms) | ||
const params: PlaneParams = { | ||
vertexShaderID: 'plane-vs', // our vertex shader ID | ||
fragmentShaderID: 'plane-fs', // our fragment shader ID | ||
uniforms: playerUniforms, | ||
}; | ||
|
||
// create our plane using our curtains object, the HTML element and the parameters | ||
this.plane = new Plane(this.curtains, planeElement, params); | ||
|
||
this.plane.loadCanvas(this.canvas); | ||
|
||
this.plane.onRender(() => { | ||
// @ts-expect-error | ||
this.plane!.uniforms.time.value++; | ||
}); | ||
} | ||
|
||
public updatePlayerCenter = (playerNumber: number, x: number, y: number) => { | ||
this.plane.uniforms[`p${playerNumber}center`].value = [x / this.canvas.width, 1 - y / this.canvas.height]; | ||
}; | ||
|
||
public updatePlayerForce = (playerNumber: number, force: number) => { | ||
this.plane.uniforms[`p${playerNumber}force`].value = force; | ||
}; | ||
|
||
public cleanup = () => { | ||
this.plane?.remove(); | ||
this.curtains?.dispose(); | ||
}; | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Scenes/Game/Singing/GameOverlay/Drawing/Shaders/shader.frag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
precision mediump float; | ||
|
||
float MAX_FORCE = 300.0; | ||
// get our varyings | ||
varying vec3 vVertexPosition; | ||
varying vec2 vTextureCoord; | ||
// the uniform we declared inside our javascript | ||
uniform float uTime; | ||
uniform sampler2D planeTexture; | ||
uniform vec2 uResolution; | ||
|
||
uniform vec2 uP0center; | ||
uniform float uP0force; | ||
|
||
uniform vec2 uP1center; | ||
uniform float uP1force; | ||
|
||
float rnd(float n) { | ||
return fract(sin(n) * 43758.5453); | ||
} | ||
float random (vec2 st) { | ||
return rnd(dot(st.xy, vec2(12.9898, 78.233))); | ||
} | ||
|
||
void distort(vec2 center, float force, float zoom, float noise) { | ||
float actualForce = force * MAX_FORCE; | ||
vec2 centerAbs = center * uResolution; | ||
vec2 diff = (gl_FragCoord.xy - centerAbs.xy); | ||
float dist = length(diff); | ||
|
||
vec4 newColor = texture2D(planeTexture, (centerAbs + (diff * zoom) + (noise * 50.0 * (1.0 - pow(zoom, 12.0)))) / uResolution); | ||
gl_FragColor = newColor; | ||
|
||
} | ||
|
||
float calculateZoom(vec2 center, float force) { | ||
float actualForce = force * MAX_FORCE; | ||
vec2 centerAbs = center * uResolution; | ||
vec2 diff = (gl_FragCoord.xy - centerAbs.xy); | ||
float dist = length(diff); | ||
|
||
return pow(smoothstep(0.0, actualForce, dist), 1.0/6.0); | ||
} | ||
|
||
void main() { | ||
float p0zoom = calculateZoom(uP0center, uP0force); | ||
float p1zoom = calculateZoom(uP1center, uP1force); | ||
float noise = (random(gl_FragCoord.xy)) / 2.0 - 0.5; | ||
|
||
if (p0zoom < p1zoom) { | ||
distort(uP0center, uP0force, p0zoom, noise); | ||
} else { | ||
distort(uP1center, uP1force, p1zoom, noise); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Scenes/Game/Singing/GameOverlay/Drawing/Shaders/shader.vert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
precision mediump float; | ||
|
||
// default mandatory variables | ||
attribute vec3 aVertexPosition; | ||
attribute vec2 aTextureCoord; | ||
|
||
uniform mat4 uMVMatrix; | ||
uniform mat4 uPMatrix; | ||
|
||
uniform mat4 planeTextureMatrix; | ||
|
||
// custom varyings | ||
varying vec3 vVertexPosition; | ||
varying vec2 vTextureCoord; | ||
|
||
void main() { | ||
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); | ||
|
||
// varyings | ||
vVertexPosition = aVertexPosition; | ||
vTextureCoord = (planeTextureMatrix * vec4(aTextureCoord, 0.0, 1.0)).xy; | ||
} |
Oops, something went wrong.