-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.html
168 lines (136 loc) · 5.42 KB
/
index.html
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<!DOCTYPE html>
<html>
<head>
<title>WebGL Shader Editor</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,500,700" rel="stylesheet">
<!-- Libraries -->
<script src="./node_modules/@webcomponents/webcomponentsjs/webcomponents-lite.js"></script>
<script src="./node_modules/ace-builds/src-min/ace.js"></script>
<script src="./node_modules/three/build/three.js"></script>
<script src="./node_modules/three/examples/js/controls/OrbitControls.js"></script>
<script src="./node_modules/@gkjohnson/javascript-utils/Debouncer.js"></script>
<script src="./node_modules/@gkjohnson/javascript-utils/Animator.js"></script>
<script src="./node_modules/@gkjohnson/javascript-utils/Mixin.js" ></script>
<!-- Scripts -->
<script src="./scripts/DebugShaders.js"></script>
<!-- Library Elements -->
<link rel="import" href="./node_modules/@polymer/polymer/polymer-element.html" />
<link rel="import" href="./node_modules/@polymer/polymer/lib/elements/dom-if.html"/>
<link rel="import" href="./node_modules/@polymer/polymer/lib/elements/dom-repeat.html"/>
<!-- Elements -->
<link rel="import" href="./elements/ace-editor.html" />
<link rel="import" href="./elements/zoomable-element.html" />
<link rel="import" href="./elements/zoomable-image.html" />
<link rel="import" href="./elements/zoomable-canvas.html" />
<link rel="import" href="./elements/image-magnifier.html" />
<link rel="import" href="./elements/shader-editor.html" />
<link rel="import" href="./elements/shader-preview.html" />
<style type="text/css">
html, body {
margin: 0;
padding: 0;
height: 100%;
font-weight: 100;
font-family: Roboto, sans-serif;
}
webgl-shader {
display: none;
}
::-webkit-scrollbar
{
width: 5px;
height: 5px;
position: absolute;
}
::-webkit-scrollbar-thumb
{
background-color: rgba(255,255,255,0.2);
}
</style>
<webgl-shader vertex>
// Lighting
struct DirLight {
vec3 color;
vec3 direction;
};
uniform float time;
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform DirLight directionalLights[NUM_DIR_LIGHTS];
uniform vec3 ambientLightColor;
varying vec3 lighting;
varying vec2 texcoord;
void main()
{
vec3 worldNorm = (modelViewMatrix * vec4(normal, 0)).xyz;
texcoord = uv;
lighting = ambientLightColor;
for(int i = 0; i < NUM_DIR_LIGHTS; i ++) {
DirLight dl = directionalLights[i];
lighting += clamp(dot(worldNorm, dl.direction), 0.0, 1.0) * dl.color;
}
vec3 sample = texture2D(texture1, uv).xyz;
vec3 pos = position + normal * sample * sin(time * 0.001);
gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0);
}
</webgl-shader>
<webgl-shader fragment>
varying vec3 lighting;
varying vec2 texcoord;
uniform sampler2D texture0; // checkerboard
uniform sampler2D texture1; // clouds
uniform sampler2D texture2; // random noise
uniform vec4 screenSize;
void main() {
vec3 rgb = texture2D(texture1, texcoord).xyz;
vec2 st;
st.x = gl_FragCoord.x / 2.0;
st.x -= floor(st.x);
st.y = gl_FragCoord.y / 2.0;
st.y -= floor(st.y);
if (rgb.r < 0.5) {
float offset = 0.5;
offset += floor(st.y + 0.5) == 0.0 ? -1.0 : 0.0;
if (floor(st.x + offset) == 0.0) discard;
}
gl_FragColor = vec4(rgb, 1) * vec4(rgb * lighting, 1);
}
</webgl-shader>
</head>
<body>
<shader-editor></shader-editor>
<script type="text/javascript">
// dedents the shader based on the indentation of the
// first line with words
const dedent = sh => {
sh = sh.replace(/^[\s\n\r]*(\n|\r)/m, '');
const tabMatches = sh.match(/^\s+/);
const tab = tabMatches ? tabMatches[0] : '';
sh = sh.replace(new RegExp(`^${tab}`, 'gm'), '');
return sh;
};
// used in console
const resetShaders = () => {
se.vertexShader = dedent(vs);
se.fragmentShader = dedent(fs);
};
const se = document.querySelector('shader-editor');
const vs = document.querySelector('webgl-shader[vertex]').textContent;
const fs = document.querySelector('webgl-shader[fragment]').textContent;
window.onunload = () => {
localStorage.setItem('vertexShader', se.vertexShader);
localStorage.setItem('fragmentShader', se.fragmentShader);
};
const path = /\.bundle\.html$/.test(location.href) ? '..' : '.';
se.vertexShader = localStorage.getItem('vertexShader') || dedent(vs);
se.fragmentShader = localStorage.getItem('fragmentShader') || dedent(fs);
se.textures = {
'texture0': `${path}/textures/checkerboard.png`,
'texture1': `${path}/textures/clouds.png`,
'texture2': `${path}/textures/random.png`
};
</script>
</body>
</html>