-
Notifications
You must be signed in to change notification settings - Fork 2
/
_test-alphashadow.html
378 lines (302 loc) · 13 KB
/
_test-alphashadow.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
</style>
<script src="lib/jsmodplayer/src/modfile.js"></script>
<script src="lib/jsmodplayer/src/xmfile.js"></script>
<script src="lib/jsmodplayer/src/modplayer.js"></script>
<script src="lib/jsmodplayer/src/support.js"></script>
<script src="lib/cubicvr/CubicVR.js"></script>
<script src="lib/dsp.js"></script>
<script src="js/audio.js"></script>
<script src="js/bitFont.js" type="text/javascript" charset="utf-8"></script>
<script src="js/bitFont3D.js" type="text/javascript" charset="utf-8"></script>
<script>
var shaderList = [];
var fxChain;
var letters = [];
var scene;
document.addEventListener('DOMContentLoaded', function (e) {
var canvas = document.createElement('canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var gl = CubicVR.GLCore.init(canvas, 'lib/cubicvr/CubicVR_Core.vs', 'lib/cubicvr/CubicVR_Core.fs');
if (!gl) {
return;
}
// expose the alphaDepth buffer
shaderDepth = new CubicVR.PostProcessShader({
shader_vertex:"lib/cubicvr/post_shaders/fx_general.vs",
shader_fragment:"lib/cubicvr/post_shaders/alpha_depth.fs",
});
// simple color invert
shaderInvert = new CubicVR.PostProcessShader({
shader_vertex:"lib/cubicvr/post_shaders/fx_general.vs",
shader_fragment:"lib/cubicvr/post_shaders/invert.fs"
});
shaderDOF6 = new CubicVR.PostProcessShader({
shader_vertex:"lib/cubicvr/post_shaders/fx_general.vs",
shader_fragment:"lib/cubicvr/post_shaders/dof_6tap.fs",
init: function(shader) {
shader.addFloat("near_depth");
shader.addFloat("far_depth");
},
onupdate: function(shader) {
// linear depth
var d = CubicVR.vec3.length(scene.camera.position,scene.camera.target);
shader.setFloat("near_depth",(d-0.5-scene.camera.nearclip)/scene.camera.farclip);
shader.setFloat("far_depth",(d+4-scene.camera.nearclip)/scene.camera.farclip);
}
});
shaderSSAO = new CubicVR.PostProcessShader({
shader_vertex:"lib/cubicvr/post_shaders/fx_general.vs",
shader_fragment:"lib/cubicvr/post_shaders/ssao.fs",
});
// Bloom + 1/2 divisor + blend
shaderHalfBloom = new CubicVR.PostProcessShader({
shader_vertex:"lib/cubicvr/post_shaders/fx_general.vs",
shader_fragment:"lib/cubicvr/post_shaders/bloom_6tap.fs",
outputMode: CubicVR.enums.post.output.ADD,
outputDivisor: 2
});
// Bloom + 1/4 divisor + blend
shaderQuarterBloom = new CubicVR.PostProcessShader({
shader_vertex:"lib/cubicvr/post_shaders/fx_general.vs",
shader_fragment:"lib/cubicvr/post_shaders/bloom_6tap.fs",
outputMode: CubicVR.enums.post.output.ADD,
outputDivisor: 4
});
var i = 0;
var shaderActive = 0;
fxSelect = document.getElementById("shaderSelector");
fxSelect.options[i] = new Option("Depth Buffer View",i++);
fxSelect.options[i] = new Option("Invert Color",i++);
fxSelect.options[i] = new Option("Screen-Space Ambient Occlusion",i++);
fxSelect.options[shaderActive=i] = new Option("6-Tap Depth Of Field",i++);
fxSelect.options[i] = new Option("6-Tap Bloom, 1/2 blend",i++);
fxSelect.options[i] = new Option("6-Tap Bloom, 1/4 blend",i++);
// fxSelect.selectedIndex = fxSelect.options.length-1;
fxSelect.selectedIndex = shaderActive;
shaderList.push(shaderDepth);
shaderList.push(shaderInvert);
shaderList.push(shaderSSAO);
shaderList.push(shaderDOF6);
shaderList.push(shaderHalfBloom);
shaderList.push(shaderQuarterBloom);
fxChain = new CubicVR.PostProcessChain(canvas.width,canvas.height,true);
fxChain.setBlurOpacity(1);
fxChain.setBlurIntensity(0);
for (var i = 0; i < shaderList.length; i++)
{
fxChain.addShader(shaderList[i]);
shaderList[i].enabled = false;
}
shaderList[fxSelect.selectedIndex].enabled = true;
var fft;
var audioBuffer;
var signal = new Float32Array(4096);
var audioEngine = new AudioEngine(null,function (data) {
fft.forward(data);
audioBuffer = data;
for (var i=0, l=4096; i<l; ++i) {
if (fft.spectrum[i] > signal[i]) {
signal[i] = fft.spectrum[i];
}
else {
signal[i]*=0.45;
} //if
} //for
});
audioEngine.playMod('mod.mod');
audioEngine.audioObject.volume = 1;
fft = new FFT(4096, 44100);
var logoMaterial = new CubicVR.Material({
textures: {
color: new CubicVR.Texture("img/logo.jpg"),
alpha: new CubicVR.Texture("img/logo-alpha.jpg"),
},
});
var logoMesh = CubicVR.primitives.plane({
size: 1.0,
material: logoMaterial,
uvmapper: {
projectionMode: CubicVR.enums.uv.projection.PLANAR,
projectionAxis: CubicVR.enums.uv.axis.Z,
scale: [1, 1, 1]
}
});
logoMesh.triangulateQuads().compile().clean();
var tWidth = 200;
var tHalfWidth = tWidth/2;
var tEdge = tHalfWidth-10;
var TWO_PI = Math.PI*2;
var skip = 4;
var floorTextureColor = new CubicVR.CanvasTexture({
update: function (canvas, ctx) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.lineWidth = 5;
ctx.fillStyle = "rgb(40, 40, 40)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
if (audioBuffer) {
for (var i=0, l=signal.length; i<l && i<tEdge; i+=skip) {
var c = Math.min(255, Math.round(signal[i] * 1000) + 64);
ctx.strokeStyle = "rgb("+c+", "+c+", "+c+")";
ctx.beginPath();
ctx.arc(tHalfWidth, tHalfWidth, i, 0, TWO_PI, true);
ctx.stroke();
} //for
} //if
},
width: tWidth,
height: tWidth,
});
var floorTextureBump = new CubicVR.CanvasTexture({
update: function (canvas, ctx) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.lineWidth = 5;
if (audioBuffer) {
for (var i=0, l=signal.length; i<l && i<tEdge; i+=skip) {
var c = Math.min(255, Math.round(signal[i] * 1000) + 64);
ctx.strokeStyle = "rgb("+c+", "+c+", "+c+")";
ctx.beginPath();
ctx.arc(tHalfWidth, tHalfWidth, i, 0, TWO_PI, true);
ctx.stroke();
} //for
} //if
},
width: tWidth,
height: tWidth,
});
var floorTextureNormal = new CubicVR.NormalMapGen(floorTextureBump, 512, 512);
var floorMaterial = new CubicVR.Material({
textures: {
color: floorTextureColor,
bump: floorTextureBump,
normal: floorTextureNormal,
},
opacity: 1,
});
var floorMesh = CubicVR.primitives.plane({
size: 20.0,
material: floorMaterial,
uvmapper: {
projectionMode: CubicVR.enums.uv.projection.PLANAR,
projectionAxis: CubicVR.enums.uv.axis.Z,
scale: [20.5, 20.5, 1]
}
});
floorMesh.triangulateQuads().compile().clean();
scene = new CubicVR.Scene(canvas.width, canvas.height, 80, 0.01, 100);
var floorObject = new CubicVR.SceneObject(floorMesh);
var logoObject = new CubicVR.SceneObject(logoMesh);
floorObject.rotation[0] = 90;
floorObject.position[1] = -1;
// in this order because of depth buffer quirks
scene.bindSceneObject(floorObject);
scene.bindSceneObject(logoObject);
scene.camera.position = [1, 0.5, -1];
scene.camera.target = [0, 0, 0];
CubicVR.setGlobalDepthAlpha(true,scene.camera.nearclip,scene.camera.farclip);
/*
planeObject.motion = new CubicVR.Motion();
planeObject.motion.setKey(CubicVR.enums.motion.ROT, 1, 0, 0);
planeObject.motion.setKey(CubicVR.enums.motion.ROT, 1, 2.5, -180).tension = 1;
planeObject.motion.setKey(CubicVR.enums.motion.ROT, 1, 5, 360).tension = 1;
planeObject.motion.setKey(CubicVR.enums.motion.ROT, 1, 6, 360);
planeObject.motion.setBehavior(CubicVR.enums.motion.ROT, 1, CubicVR.enums.envelope.behavior.CONSTANT, CubicVR.enums.envelope.behavior.OFFSET);
*/
// var dirLight = new CubicVR.Light({
// type: CubicVR.enums.light.type.DIRECTIONAL,
// specular: [0.4,0.4,0.4],
// direction: CubicVR.vec3.normalize([0.5,-1,0.5])
// });
// scene.bindLight(dirLight);
//
// var pLight = new CubicVR.Light({
// type: CubicVR.enums.light.type.POINT,
// specular: [0.4, 0.4, 0.4],
// intensity: 1,
// distance: 20,
// position: [0, 1.5, 0],
// });
// scene.bindLight(pLight);
var spotLight = new CubicVR.Light({
type: CubicVR.enums.light.type.SPOT_SHADOW,
specular: [0.4,0.4,0.4],
diffuse: [1,1,1],
intensity: 6,
distance: 100,
cutoff: 50,
map_res: 2048,
position: [-7,10,-5]
});
scene.bindLight(spotLight);
var bf3d = new bitFont3D("box");
bf3d.loadFont();
var fontObj = new CubicVR.SceneObject({mesh:bf3d.chars[65], position:[0,0,0], scale:[1,1,1]});
// letters.push(fontObj);
// scene.bindSceneObject(fontObj);
window.addEventListener("keypress",function (e) {
// var key=(typeof event!='undefined')?window.event.keyCode:e.keyCode;
// fontObj.obj = bf3d.chars[e.charCode];
var newfontObj = new CubicVR.SceneObject({mesh:bf3d.chars[e.charCode], position:[(Math.random()-0.5)*10.0,(Math.random())*3.0,(Math.random()-0.5)*10.0], scale:[1,1,1]});
letters.push(newfontObj);
scene.bindSceneObject(newfontObj);
},true);
CubicVR.MainLoop(function(timer, gl) {
var seconds = timer.getSeconds();
spotLight.position[0] = 7.0*Math.sin(seconds/2.0);
spotLight.lookat([0,0,0]);
scene.updateShadows();
fxChain.begin();
// Clear the FX buffer
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
scene.render();
fxChain.end();
fxChain.render();
scene.evaluate(timer.getSeconds());
logoObject.rotation[1] = Math.sin(seconds)*180 + Math.sin(seconds/2)*540 + Math.sin(seconds/8)*1080;
floorTextureColor.update();
floorTextureBump.update();
floorTextureNormal.update();
// pLight.position[0] = Math.sin(seconds/10)*10;
// pLight.position[2] = Math.cos(seconds/10)*10;
scene.camera.position[0] = 2 * Math.sin(seconds/5) + Math.cos(seconds/2) * 1.5;
scene.camera.position[2] = 2 * Math.cos(seconds/5) + Math.cos(seconds/2) * 1.5;
});
document.body.appendChild(canvas);
//new CubicVR.MouseViewController(canvas, scene.camera);
}, false);
function selectShaderFX(elem)
{
fxSelect = document.getElementById("shaderSelector");
for (var i = 0; i < fxSelect.length; i++)
{
shaderList[i].enabled = (fxSelect.options[i].selected)?true:false;
}
}
function clearLetters() {
for (var i = 0, iMax = letters.length; i < iMax; i++) {
scene.removeSceneObject(letters[i]);
delete letters[i];
}
letters = [];
}
</script>
</head>
<body>
<div style='position:absolute; top:10px; left:10px; color:white; font-family:Arial; font-size:14px;'>
Change FX (multi): <a href="javascript:void(0)" onclick="document.getElementById('fxDiv').style.display=(document.getElementById('fxDiv').style.display=='none')?'':'none';" style='text-decoration:none'>( toggle )</a><div style='' id='fxDiv'><br /><select id="shaderSelector" onchange="selectShaderFX(this)" multiple size="15" style='vertical-align:top'></select><br/>
Blur opac: <input type='text' onchange="fxChain.setBlurOpacity(parseFloat(this.value))" size='4' value="1"> intens:<input type='text' onchange="fxChain.setBlurIntensity(parseFloat(this.value))" size='4' value="0"><br/><input type='button' value="Clear Letters" onclick='clearLetters()'></div>
</div>
</body>
</html>