-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-shader.js
104 lines (84 loc) · 2.62 KB
/
custom-shader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Color, Rapid, GLShader } from "../dist/rapid.js"
let rapid = new Rapid({
canvas: document.getElementById("game"),
backgroundColor: Color.fromHex("FFFFFF")
})
let costumUniformValue = 0
const gl = rapid.gl
const plane = await rapid.textures.textureFromUrl("./plane.png")
// Vertex Shader Source Code
const vertexShaderSource = `
precision mediump float;
// Used to display sprite textures, passed by the renderer
attribute vec2 aPosition;
attribute vec2 aRegion;
attribute float aTextureId;
attribute vec4 aColor;
uniform mat4 uProjectionMatrix;
uniform float uCustomUniform; // Custom data transmitted to uniform
varying vec2 vRegion;
varying float vTextureId;
varying vec4 vColor;
void main(void) {
vRegion = aRegion;
vTextureId = aTextureId;
vColor = aColor;
vec2 position = aPosition;
if (aRegion.y == 0.0) {
position.x += uCustomUniform * 20.0;
}
gl_Position = uProjectionMatrix * vec4(position, 0.0, 1.0);
vColor = vec4(0.5, aRegion.x + uCustomUniform, aRegion.y, 1.0);
gl_Position = uProjectionMatrix * vec4(position, 0.0, 1.0);
}
`;
// Fragment Shader Source Code
const fragmentShaderSource = `
precision mediump float;
uniform sampler2D uTextures[%TEXTURE_NUM%];
varying vec2 vRegion;
varying float vTextureId;
varying vec4 vColor;
void main(void) {
vec4 color;
%GET_COLOR%
gl_FragColor = color*vColor;
}
`;
// If you don't want to display the sprite's texture, then you don't need to add "uniform sampler2D uTextures[%TEXTURE_NUM%];" and "%GET_COLOR%"
// These two will be replaced by rapid for better performance
const customShader = new GLShader(rapid, vertexShaderSource, fragmentShaderSource)
const render = () => {
rapid.startRender()
rapid.renderSprite(plane, 100, 100, {
shader: customShader,
uniforms: {
// Set custom uniform (You can set mat3, vec2, and so on here)
uCustomUniform: Number(costumUniformValue)
// uVec2Uniform: [0,2] // recognized as vec2
// uMat3Uniform: [
// [0,0,0],
// [0,0,0],
// [0,0,0],
// ]
// recognized as mat3
}
});
rapid.endRender()
}
const uniformInputDom = document.getElementById("costumUniform")
uniformInputDom.value = costumUniformValue
uniformInputDom.addEventListener("input", (ev) => {
costumUniformValue = ev.target.value
})
// performance monitor
var stats = new Stats();
stats.showPanel(0);
document.body.appendChild(stats.dom);
function animate() {
stats.begin();
render()
stats.end();
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);