summaryrefslogtreecommitdiffstats
path: root/o3d/samples/juggler.html
blob: 233e95889f0561ba4ddabd41b539e811f688c2c1 (plain)
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<!--
Copyright 2009, Google Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
    * Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->

<!--
O3D Juggler
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html style="width: 100%; height: 100%;">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
  Juggler Shader
</title>

<script type="text/javascript" src="o3djs/base.js"></script>

<script type="text/javascript" id="o3d">
o3djs.require('o3djs.util');
o3djs.require('o3djs.math');
o3djs.require('o3djs.rendergraph');
o3djs.require('o3djs.primitives');

// Events
// Run the init() function once the page has finished loading.
//         unload() when the page is unloaded.
window.onload = init;
window.onunload = unload;
// global variables
var g_o3d;
var g_math;
var g_client;
var g_o3dElement;
var g_viewInfo;
var g_pack;
var g_o3dWidth = -1;
var g_o3dHeight = -1;
var g_transform;
var g_clock = 0.0;
var g_timeMult = 1;  // amount to multiply elapsed time by.
                     // Used to make the animation run faster or slower.
var g_finished = false;  // for selenium testing
var g_thetaParam;
var g_numParam;
var g_numBalls;  // Must be either 3, 5, 7, or 9 for now.
var g_speedScale;  // Used to make higher numbers of balls animate faster.

/**
 * Creates the client area.
 */
function init() {
  o3djs.util.makeClients(initStep2);
}

/**
 * Initializes O3D, loads the effect, and creates the square.
 * @param {Array} clientElements Array of o3d object elements.
 */
function initStep2(clientElements) {
  // Initialize global variables and libraries.
  g_o3dElement = clientElements[0];
  g_o3d = g_o3dElement.o3d;
  g_math = o3djs.math;
  g_client = g_o3dElement.client;

  // Create a g_pack to manage our resources/assets
  g_pack = g_client.createPack();

  // Create the render graph for a view.
  g_viewInfo = o3djs.rendergraph.createBasicView(
      g_pack,
      g_client.root,
      g_client.renderGraphRoot,
      [0, 0, 0, 1]);

  var effect = g_pack.createObject('Effect');
  effect.loadFromFXString(document.getElementById('shader').value);

  // Create a Material for the effect.
  var myMaterial = g_pack.createObject('Material');

  // Apply our effect to this material.
  myMaterial.effect = effect;

  // Set the material's drawList for opaque objects.
  myMaterial.drawList = g_viewInfo.performanceDrawList;

  // Create the params the effect needs on the material.
  effect.createUniformParameters(myMaterial);

  // Create a square.
  var myShape = o3djs.primitives.createPlane(g_pack, myMaterial,
                                                  1, 1, 1, 1);

  // Set up the individual parameters in our effect file.
  g_thetaParam = myMaterial.getParam('theta');
  g_thetaParam.value = 0;
  g_numParam = myMaterial.getParam('num');
  updateNum();

  // Set the position of the camera.
  g_viewInfo.drawContext.view = g_math.matrix4.lookAt(
      [0, 1, 0],   //eye
      [0, 0, 0],   //target
      [0, 0, -1]); //up

  // Generate the projection matrix based
  // on the g_o3d plugin size by calling resize().
  resize();

  // Now attach the square to the root of the transform graph.
  g_client.root.addShape(myShape);

  toggleRenderCallback();

  g_finished = true;  // for selenium testing.
}

function updateNum() {
  var group = document.the_form.radio_group;
  for (var i = 0; i < group.length; ++i) {
    if (group[i].checked) {
      setNumBalls(parseInt(group[i].value));
    }
  }
}

function toggleRenderCallback() {
  var box = document.the_form.check_box;
  if (box.checked) {
    g_client.setRenderCallback(onrender);
  } else {
    g_client.clearRenderCallback();
  }
}

function setNumBalls(num) {
  g_numBalls = num;
  g_numParam.value = g_numBalls;
  g_speedScale = Math.sqrt(g_numBalls) * 5;
}

function onrender(render_event) {
  g_clock += render_event.elapsedTime * g_timeMult;
  g_thetaParam.value = g_clock * g_speedScale;

  // If we don't check the size of the client area every frame we don't get a
  // chance to adjust the perspective matrix fast enough to keep up with the
  // browser resizing us, so onrender must call resize.
  resize();
}


// Generates the projection matrix based on the size of the g_o3d plugin
// and calculates the view-projection matrix.
function resize() {
  var newWidth  = g_client.width;
  var newHeight = g_client.height;

  if (newWidth != g_o3dWidth || newHeight != g_o3dHeight) {
    g_o3dWidth = newWidth;
    g_o3dHeight = newHeight;

    // Determine what the size of the rendered square within the client should
    // be in pixels.
    var side = g_o3dWidth < g_o3dHeight ?
        g_o3dWidth : g_o3dHeight;

    // Convert to the region of world space that must be enclosed by the
    // orthographic projection.
    var worldSize = g_math.divVectorScalar([g_o3dWidth, g_o3dHeight], side);

    // Find a projection matrix to transform from world space to screen space.
    g_viewInfo.drawContext.projection = o3djs.math.matrix4.orthographic(
        -0.5 * worldSize[0], 0.5 * worldSize[0],
        -0.5 * worldSize[1], 0.5 * worldSize[1],
        0.5, 1.5);
  }
}

/**
 * Removes any callbacks so they don't get called after the page has unloaded.
 */
function unload() {
  if (g_client) {
    g_client.cleanup();
  }
}
</script>
</head>
<body style="width: 95%; height: 95%;">
<table style="width: 100%; height: 100%;">
  <tr>
    <td>
      <h1>Juggler</h1>
      <p>
        This sample displays a juggling pattern computed entirely in a shader.
        <form name="the_form">
          <input type="radio" name="radio_group" value="3"
              onclick=updateNum()>3 Balls
          <input type="radio" name="radio_group" value="5"
              onclick=updateNum()>5 Balls
          <input type="radio" name="radio_group" value="7"
              onclick=updateNum()>7 Balls
          <input type="radio" name="radio_group" value="9"
              onclick=updateNum()>9 Balls
          <input type="radio" name="radio_group" value="11" checked
              onclick=updateNum()>11 Balls
          <input type="radio" name="radio_group" value="13"
              onclick=updateNum()>13 Balls
          <input type="radio" name="radio_group" value="15"
              onclick=updateNum()>15 Balls
          <input type="radio" name="radio_group" value="17"
              onclick=updateNum()>17 Balls
          <input type="checkbox" name="check_box" checked
              onclick=toggleRenderCallback()>Animate
        </form>
      </p>
      <table id="container" style="width: 100%; height: 80%;">
        <tr>
          <td height="100%">
          <!-- Start of g_o3d plugin -->
          <div id="o3d" style="width: 100%; height: 100%;"></div>
          <!-- End of g_o3d plugin -->
          </td>
        </tr>
      </table>
      <!-- a simple way to get a multiline string -->
      <textarea id="shader" name="shader" cols="80" rows="20"
       style="display: none;">
// The 4x4 world view projection matrix.
float4x4 worldViewProjection : WORLDVIEWPROJECTION;

float theta;
float num;

// input parameters for our vertex shader
struct VertexShaderInput {
  float4 position : POSITION;
  float2 texCoord : TEXCOORD0;
};

// input parameters for our pixel shader
// also the output parameters for our vertex shader
struct PixelShaderInput {
  float4 position : POSITION;
  float2 texCoord : TEXCOORD0;
  float4 color : COLOR;
};

/**
 * vertexShaderMain - our vertex shader for the juggling texture
 */
PixelShaderInput vertexShaderMain(VertexShaderInput input) {
  PixelShaderInput output;

  output.position = mul(input.position, worldViewProjection);
  output.texCoord = 4.0 * (input.texCoord - float2(0.5, 0.5));
  output.color = float4(1, 1, 1, 1);

  return output;
}

float length_2(in float2 v) {
  return dot(v, v);
}

// Draw the balls in a single arc.
// Returns 1 if the pixel is within a ball, 0 if it isn't, and a value in
// between for pixels right on the edge, for antialiasing.
float drawBallsInArc(in float pi,
                     in float4 offset,
                     in float2 source_hand,
                     in float2 dest_hand,
                     in float height_factor,
                     in float baseline,
                     in float ball_radius_2,
                     in float hand_throw_offset,
                     in float2 Z,
                     in float threshold) {
  // Map theta from its current range of [0, 2 * num * pi) onto [0, (num - 1))
  // by scaling, adding offset, and modding, then map that to [0, 1) by scaling.
  // The first mapping tells us where in the repeating cycle we are, and the
  // second mapping simplifies the calculation of the parabola.

  // The vector offset is used to distinguish between balls in the same arc, but
  // out of phase.  At the beginning of this function, all the operations are
  // vectorized to save instructions; we get to calculate 4 ball positions for
  // the price of 1.

  // The reason for the (num - 1) in the expression below is that with num
  // balls, each ball spends (num - 1) beats in the air, then one in the hand.
  // So (num - 1) is the length of time a parabola takes.

  float4 time = fmod(theta / pi + offset, (num - 1)) / (num - 1);
  float dx = dest_hand.x - source_hand.x;
  float4 x = time * dx + source_hand.x - hand_throw_offset;
  float4 y = time * (1 - time);
  y = y * height_factor + baseline;
  float4 ZX = Z.x;
  float4 ZY = Z.y;
  float4 len_2 = (ZX - x) * (ZX - x) + (ZY - y) * (ZY - y);

  // This antialiasing fuzzes the balls just a bit larger than they would
  // otherwise be.
  float4 temp = clamp((len_2 - ball_radius_2) / threshold, 0, 1);

  // One minus the product of all entries in temp.
  temp.xy = temp.xy * temp.zw;
  return 1 - temp.x * temp.y;
}

float4 drawAirborneBalls(in float pi,
                         in float4 offset,
                         in float2 right_hand,
                         in float2 left_hand,
                         in float height_factor,
                         in float baseline,
                         in float ball_radius_2,
                         in float hand_swing_radius,
                         in float2 Z,
                         in float threshold) {
  float value =
      // balls going right to left
      (drawBallsInArc(pi, offset, right_hand, left_hand, height_factor,
          baseline, ball_radius_2, hand_swing_radius, Z, threshold) +
      // balls going left to right
      drawBallsInArc(pi, offset + 1, left_hand, right_hand, height_factor,
          baseline, ball_radius_2, -hand_swing_radius, Z, threshold));
  return float4(value, value, value, value);
}

/**
 * pixelShaderMain - pixel shader
 */

float4 pixelShaderMain(PixelShaderInput input) : COLOR {
  const float pi = 3.14159265;
  const float baseline = -1.4;
  const float2 right_hand = float2(0.8, baseline);
  const float2 left_hand = float2(-0.8, baseline);
  const float hand_swing_radius = 0.25;
  const float hand_radius = 0.15;
  const float hand_radius_2 = hand_radius * hand_radius;
  const float ball_radius = 0.08;
  const float ball_radius_2 = ball_radius * ball_radius;

  const float4 right_hand_color = float4(1, 0, 0, 1);
  const float4 left_hand_color = float4(0, 0, 1, 1);
  const float4 background_color = float4(0, 0, 0, 0);

  const float threshold = 0.002; // Used in clamp for antialiasing.

  float height_factor = num * 0.75;

  float2 Z = input.texCoord;

  // Coerce to the range [0, 2 * Pi * num].
  float2 r_h = hand_swing_radius * float2(-cos(theta), sin(theta)) + right_hand;
  float2 l_h = hand_swing_radius * float2(-cos(theta), -sin(theta)) + left_hand;

  // Initialize color of pixel to background_color.  Background color has an
  // alpha of 0.  Color of objects each have alpha 1, so multiplying by
  // (1-alpha) before adding the color ensures that nothing gets overdrawn.
  // It's kind of like a rudimentary z-buffer.
  float4 result = background_color;

  // Draw the hands.  The antialiasing here fuzzes the hands just a little bit
  // smaller than they would otherwise be.  That's the opposite of what we do
  // for the balls, just because it happens to be cheaper here to do smaller and
  // cheaper in drawBallsInArc to do larger.
  result +=
      clamp((hand_radius_2 - length_2(Z - r_h)) / threshold, 0, 1) *
          (Z.y < r_h.y) * right_hand_color +
      clamp((hand_radius_2 - length_2(Z - l_h)) / threshold, 0, 1) *
          (Z.y < l_h.y) * left_hand_color;

  // Draw the ball in the hand.  There is always a ball in exactly one hand, and
  // which hand that is alternates.
  float2 hand;
  if (fmod(floor(theta / pi), 2) > 0.5) {
    hand = r_h;
  } else {
    hand = l_h;
  }
  // The antialiasing here fuzzes the balls just a bit bigger than they would
  // otherwise be.  This is more work than making them smaller [the extra
  // subtraction in the "1 - clamp..." below], but inverting it in
  // drawBallsInArc would be more expensive, and they have to match so that the
  // balls are all the same size.
  result += (1 - result.a) *
      (1 - clamp((length_2(Z - hand) - ball_radius_2) / threshold, 0, 1));

  // Draw airborne balls.
  float4 offset = float4(0, 2, 4, 6);
  result += (1 - result.a) * drawAirborneBalls(pi,
                                               offset,
                                               right_hand,
                                               left_hand,
                                               height_factor,
                                               baseline,
                                               ball_radius_2,
                                               hand_swing_radius,
                                               Z,
                                               threshold);

  // For each up-to-4 pairs of balls you want to add, increment offset by
  // (8, 8, 8, 8) and call drawAirborneBalls again.
  offset += 8;
  result += (1 - result.a) * drawAirborneBalls(pi,
                                               offset,
                                               right_hand,
                                               left_hand,
                                               height_factor,
                                               baseline,
                                               ball_radius_2,
                                               hand_swing_radius,
                                               Z,
                                               threshold);

  return result;
}

// Here we tell our effect file *which* functions are
// our vertex and pixel shaders.

// #o3d VertexShaderEntryPoint vertexShaderMain
// #o3d PixelShaderEntryPoint pixelShaderMain
// #o3d MatrixLoadOrder RowMajor
      </textarea>
    </td>
  </tr>
</table>
</body>
</html>