summaryrefslogtreecommitdiffstats
path: root/o3d/samples/o3d-webgl-samples/bubbles/bubble.html
blob: 80570f7ea0313e38097332b45b31cb7be3cadb83 (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
<!--
Copyright 2010, 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.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Bubbles Demo</title>

<link type="text/css" href="css/style.css" rel="stylesheet" />

<!-- Use jQuery UI for the sliders. -->
<link type="text/css"
  href="third_party/jquery/css/jquery-ui-1.8.4.custom.css" rel="stylesheet" />
<script type="text/javascript"
  src="third_party/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript"
  src="third_party/jquery/jquery-ui-1.8.4.custom.min.js"></script>

<!-- Our javascript code -->
<script type="text/javascript" src="../../o3d-webgl/base.js"></script>
<script type="text/javascript" src="../../o3djs/base.js"></script>
<script type="text/javascript" src="js/bubbledemo.js"></script>
<script type="text/javascript" src="js/environment.js"></script>
<script type="text/javascript" src="js/noise.js"></script>
<script type="text/javascript" src="js/iridescence.js"></script>
<script type="text/javascript" src="js/modulation.js"></script>
<script type="text/javascript" src="js/controls.js"></script>
<script type="text/javascript" src="js/main.js"></script>

<!--

This demo is based off of the bubble demo created for o3d command buffers:
http://code.google.com/p/o3d/source/browse/trunk/googleclient/o3d/?r=55#o3d/command_buffer/samples/bubble

 -->

</head>
<body>
  <!-- The o3d container. -->
  <div id="o3d"></div>

  <div id="floatControls">
    <b>Options:</b>
    <ul>
      <li>
        <label>
        Bubble Options?
        <input type="checkbox" onclick="toggle(0, this); void(0);" />
        </label>
      </li>
      <li>
        <label>
        Iridescence Texture?
        <input type="checkbox" onclick="toggle(1, this); void(0);" />
        </label>
      </li>
      <li>
        <label>
        Noise Texture?
        <input type="checkbox" onclick="toggle(2, this); void(0);" />
        </label>
      </li>
      <li>
        <label>Modulation Texture?
        <input type="checkbox" onclick="toggle(3, this); void(0);" />
        </label>
      </li>
      <li>
        <label>Environment Map?
        <input type="checkbox" onclick="toggle(4, this); void(0);" />
        </label>
      </li>
    </ul>
    <br />
    <b>Background Color (0-255, rgb):</b>
    <p></p>
      <form onSubmit="updateClearColor(); void(0); return false;">
        <input type="text" id="bg-red" size="3" maxLength="3" value="0" />
        <input type="text" id="bg-green" size="3" maxLength="3" value="0" />
        <input type="text" id="bg-blue" size="3" maxLength="3" value="0" />
        <input type="submit" value="Go!" />
      </form>
    <br />
    <b>Controls:</b>
    Drag to rotate. Mousewheel to zoom. 'q' to exit all options.
    <p id="loading">Loading...</p>
  </div>

  <!-- The option panels. -->
  <div id="controls">
    <div id="opt-bubble" class="option">
      <a href="#close" class="closer">close</a>
    </div>
    <div id="tex-iridescence" class="option">
      <a href="#close" class="closer">close</a>
    </div>
    <div id="tex-perlin" class="option">
      <a href="#close" class="closer">close</a>
    </div>
    <div id="tex-modulation" class="option">
      <a href="#close" class="closer">close</a>
    </div>
    <div id="tex-environment" class="option">
      <img src="cubemap/posx.png" />
      <img src="cubemap/negx.png" />
      <img src="cubemap/posy.png" />
      <img src="cubemap/negy.png" />
      <img src="cubemap/posz.png" />
      <img src="cubemap/negz.png" />
      <p>Using environment map:
        <a id="toggle-env" href="#toggle-env">On. Turn off?</a>
      </p>
      <a href="#close" class="closer">close</a>
    </div>
  </div>

  <!-- Define the vertex shader -->
  <textarea id="shader" name="shader" style="display: none;">
    #ifdef GL_ES
    precision highp float;
    #endif

    attribute vec3 position;
    attribute vec3 normal;
    attribute vec2 texCoord0;
    attribute float texCoord1;


    varying vec4 v_position;
    varying vec2 v_uv;
    varying vec2 v_params;
    varying vec3 v_reflected;

    uniform mat4 worldViewProjection;
    uniform mat4 world;
    uniform mat4 worldInverseTranspose;
    uniform vec3 eye;
    uniform vec4 thickness_params;
    uniform float timer;
    uniform float bubbleNumber;
    uniform float distortion;

    void main() {
      vec4 object_position = vec4(position.xyz, 1);
      vec4 object_normal = vec4(normal.xyz, 0);

      // texCoord1 is the sampled value from the modulation texture.
      float modulation_factor = sin(timer + bubbleNumber) *
          (distortion * texCoord1);
      vec4 unit_normal = normalize(object_normal);
      object_position.xyz += unit_normal.xyz * modulation_factor;

      vec3 tmp_normal = normalize((worldInverseTranspose * object_normal).xyz);
      vec4 tmp_position = world * object_position;
      vec4 center = world * vec4(0, 0, 0, 1);
      vec3 eye_vector = normalize(tmp_position.xyz - eye);

      // Absolute value for the back faces.
      float cos_i = abs(dot(tmp_normal, eye_vector));
      float thickness = exp(-(tmp_position.y - center.y) * thickness_params.x) *
          thickness_params.y / 2.0;

      gl_Position = worldViewProjection * object_position;
      v_position = world * object_position;
      v_uv = texCoord0;
      v_params = vec2(cos_i, thickness);
      v_reflected = reflect(eye_vector, tmp_normal);
    }

    // #o3d SplitMarker

    #ifdef GL_ES
    precision highp float;
    #endif

    uniform sampler2D noise_sampler;
    uniform sampler2D iridescence_sampler;
    uniform samplerCube env_sampler;
    uniform vec4 thickness_params;
    uniform bool useCubeMap;

    varying vec4 v_position;
    varying vec2 v_uv;
    varying vec2 v_params;
    varying vec3 v_reflected;

    void main() {
      // Noise: remap [0.. 1] to [-0.5, -.5]
      float noise = (texture2D(noise_sampler, v_uv).x - 0.5);
      float thickness = v_params.y - noise * thickness_params.z;
      float cos_i = v_params.x;

      // Modulate iridescence color by the environment looked up along the
      // reflected ray.
      vec4 color = texture2D(iridescence_sampler, vec2(cos_i, thickness));
      if (useCubeMap) {
        color.xyz *= textureCube(env_sampler, v_reflected).xyz;
      }

      // Modulate by per-face attenuation
      color.rgb *= thickness_params.w;
      gl_FragColor = color;
    }
  </textarea>
</body>
</html>