webgl_multiple_elements_with_text.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - multiple elements with text</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. * {
  9. box-sizing: border-box;
  10. -moz-box-sizing: border-box;
  11. }
  12. body {
  13. color: #000;
  14. font-family: Monospace;
  15. font-size: 13px;
  16. background-color: #fff;
  17. margin: auto;
  18. padding: .5in;
  19. max-width: 7in;
  20. text-align: justify;
  21. }
  22. .view {
  23. width: 5in;
  24. height: 5in;
  25. margin: auto;
  26. }
  27. #c {
  28. position: fixed;
  29. left: 0px; top: 0px;
  30. width: 100%;
  31. height: 100%;
  32. background-color: #fff;
  33. z-index: -1;
  34. }
  35. #info {
  36. position: absolute;
  37. top: 0px; width: 6.5in;
  38. padding: 0px;
  39. text-align: center;
  40. }
  41. a {
  42. color: #0080ff;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <canvas id="c"></canvas>
  48. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - multiple elements with text - webgl</div>
  49. <script src="../build/three.min.js"></script>
  50. <script src="../examples/js/controls/OrbitControls.js"></script>
  51. <script src="js/Detector.js"></script>
  52. <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"></script>
  53. <script>
  54. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  55. var scenes = [], views, t, canvas, renderer;
  56. window.onload = init;
  57. function init() {
  58. var balls = 20;
  59. var size = .25;
  60. var colors = [ 'rgb(0,127,255)', 'rgb(255,0,0)', 'rgb(0,255,0)', 'rgb(0,255,255)',
  61. 'rgb(255,0,255)', 'rgb(255,0,127)', 'rgb(255,255,0)', 'rgb(0,255,127)' ];
  62. canvas = document.getElementById( 'c' );
  63. renderer = new THREE.WebGLRenderer( { canvas: canvas, antialias: true } );
  64. renderer.setClearColor( 0xffffff, 1 );
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. views = document.querySelectorAll( '.view' );
  67. for ( var n = 0 ; n < views.length ; n++ ) {
  68. var scene = new THREE.Scene();
  69. var geometry = new THREE.Geometry();
  70. var geometry0 = new THREE.Geometry();
  71. if ( views[n].lattice ) {
  72. var range = balls / 2;
  73. for ( var i = -range ; i <= range ; i++ ) {
  74. for ( var j = -range ; j <= range ; j++ ) {
  75. for ( var k = -range ; k <= range ; k++ ) {
  76. geometry.vertices.push( new THREE.Vector3( i, j, k ) );
  77. geometry0.vertices.push( new THREE.Vector3( i, j, k ) );
  78. }
  79. }
  80. }
  81. } else {
  82. for ( var m = 0 ; m < Math.pow( balls, 3 ) ; m++ ) {
  83. var i = balls * Math.random() - balls / 2;
  84. var j = balls * Math.random() - balls / 2;
  85. var k = balls * Math.random() - balls / 2;
  86. geometry.vertices.push( new THREE.Vector3( i, j, k ) );
  87. geometry0.vertices.push( new THREE.Vector3( i, j, k ) );
  88. }
  89. }
  90. var index = Math.floor( colors.length * Math.random() );
  91. var canvas2 = document.createElement( 'canvas' );
  92. canvas2.width = 128;
  93. canvas2.height = 128;
  94. var context = canvas2.getContext( '2d' );
  95. context.arc( 64, 64, 64, 0, 2 * Math.PI );
  96. context.fillStyle = colors[ index ];
  97. context.fill();
  98. var texture = new THREE.Texture( canvas2 );
  99. texture.needsUpdate = true;
  100. var material = new THREE.PointsMaterial( { size: size, map: texture, transparent: true, alphaTest: .1 } );
  101. scene.add( new THREE.Points( geometry, material ) );
  102. scene.userData.view = views[n];
  103. scene.userData.geometry0 = geometry0;
  104. var camera = new THREE.PerspectiveCamera( 75, 1, .1, 100 );
  105. camera.position.set( 0, 0, 1.2*balls );
  106. scene.userData.camera = camera;
  107. var controls = new THREE.OrbitControls( camera, views[n] );
  108. scene.userData.controls = controls;
  109. scenes.push( scene );
  110. }
  111. t = 0;
  112. animate();
  113. }
  114. function updateSize() {
  115. var width = canvas.clientWidth;
  116. var height = canvas.clientHeight;
  117. if ( canvas.width !== width || canvas.height != height ) {
  118. renderer.setSize( width, height, false );
  119. }
  120. }
  121. function animate() {
  122. render();
  123. requestAnimationFrame( animate );
  124. }
  125. function render() {
  126. updateSize();
  127. renderer.setClearColor( 0xffffff );
  128. renderer.setViewport( 0, 0, canvas.clientWidth, canvas.clientHeight );
  129. renderer.clear();
  130. renderer.setClearColor( 0x000000 );
  131. renderer.setScissorTest( true );
  132. scenes.forEach( function( scene ) {
  133. var rect = scene.userData.view.getBoundingClientRect();
  134. // check if it's offscreen. If so skip it
  135. if ( rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
  136. rect.right < 0 || rect.left > renderer.domElement.clientWidth ) {
  137. return; // it's off screen
  138. }
  139. // set the viewport
  140. var width = rect.right - rect.left;
  141. var height = rect.bottom - rect.top;
  142. var left = rect.left;
  143. var bottom = renderer.domElement.clientHeight - rect.bottom;
  144. renderer.setViewport( left, bottom, width, height );
  145. renderer.setScissor( left, bottom, width, height );
  146. renderer.render( scene, scene.userData.camera );
  147. scene.userData.controls.update();
  148. for ( var i = 0 ; i < scene.children[0].geometry.vertices.length ; i++ ) {
  149. var v0 = scene.userData.geometry0.vertices[i];
  150. var v = scene.userData.view.displacement( v0.x, v0.y, v0.z, t/5 );
  151. scene.children[0].geometry.vertices[i].set( v.x + v0.x, v.y + v0.y, v.z + v0.z );
  152. }
  153. scene.children[0].geometry.verticesNeedUpdate = true;
  154. } );
  155. renderer.setScissorTest( false );
  156. t++;
  157. }
  158. </script>
  159. <p>Sound waves whose geometry is determined by a single dimension, plane waves, obey the wave equation</p>
  160. \[ \frac{ \partial^2 u }{ \partial r^2 } - \frac{ 1 }{ c^2 } \frac{ \partial^2 u }{ \partial t^2 } = 0 \]
  161. <p>where <i>c</i> designates the speed of sound in the medium. The monochromatic solution for plane waves will be taken to be</p>
  162. \[ u(r,t) = \sin( k r \pm &omega; t ) \]
  163. <p>where &omega; is the frequency and \( k = &omega; / c \) is the wave number. The sign chosen in the argument determines the direction of movement of the waves.</p>
  164. <p>Here is a plane wave moving on a three-dimensional lattice of atoms:</p>
  165. <div class="view">
  166. <script>
  167. var parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  168. parent.displacement = function( x, y, z, t ) {
  169. return new THREE.Vector3( Math.sin( x - t ), 0, 0);
  170. };
  171. parent.lattice = true;
  172. </script>
  173. </div>
  174. <p>Here is a plane wave moving through a three-dimensional random distribution of molecules:</p>
  175. <div class="view">
  176. <script>
  177. var parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  178. parent.displacement = function( x, y, z, t ) {
  179. return new THREE.Vector3( Math.sin( x - t ), 0, 0);
  180. };
  181. parent.lattice = false;
  182. </script>
  183. </div>
  184. <p>Sound waves whose geometry is determined by two dimensions, cylindrical waves, obey the wave equation</p>
  185. \[ \frac{ \partial^2 u }{ \partial r^2 } + \frac{ 1 }{ r } \frac{ \partial u }{ \partial r } - \frac{ 1 }{ c^2 } \frac{ \partial^2 u }{ \partial t^2 } = 0 \]
  186. <p>The monochromatic solution for cylindrical sound waves will be taken to be</p>
  187. \[ u(r,t) = \frac{ \sin( k r \pm &omega; t ) }{ \sqrt{ r } } \]
  188. <p>Here is a cylindrical wave moving on a three-dimensional lattice of atoms:</p>
  189. <div class="view">
  190. <script>
  191. var parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  192. parent.displacement = function( x, y, z, t ) {
  193. if ( x * x + y * y < 0.01 ) {
  194. return new THREE.Vector3( 0, 0, 0);
  195. } else {
  196. var rho = Math.sqrt( x * x + y * y );
  197. var phi = Math.atan2( y, x );
  198. return new THREE.Vector3( 1.5 * Math.cos( phi ) * Math.sin( rho - t ) / Math.sqrt( rho ), 1.5 * Math.sin( phi ) * Math.sin( rho - t ) / Math.sqrt( rho ), 0);
  199. }
  200. }
  201. parent.lattice = true;
  202. </script>
  203. </div>
  204. <p>Here is a cylindrical wave moving through a three-dimensional random distribution of molecules:</p>
  205. <div class="view">
  206. <script>
  207. var parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  208. parent.displacement = function( x, y, z, t ) {
  209. if ( x * x + y * y < 0.01 ) {
  210. return new THREE.Vector3( 0, 0, 0);
  211. } else {
  212. var rho = Math.sqrt( x * x + y * y );
  213. var phi = Math.atan2( y, x );
  214. return new THREE.Vector3( 1.5 * Math.cos( phi ) * Math.sin( rho - t ) / Math.sqrt( rho ), 1.5 * Math.sin( phi ) * Math.sin( rho - t ) / Math.sqrt( rho ), 0);
  215. }
  216. }
  217. parent.lattice = false;
  218. </script>
  219. </div>
  220. <p>Sound waves whose geometry is determined by three dimensions, spherical waves, obey the wave equation</p>
  221. \[ \frac{ \partial^2 u }{ \partial r^2 } + \frac{ 2 }{ r } \frac{ \partial u }{ \partial r } - \frac{ 1 }{ c^2 } \frac{ \partial^2 u }{ \partial t^2 } = 0 \]
  222. <p>The monochromatic solution for spherical sound waves will be taken to be</p>
  223. \[ u(r,t) = \frac{ \sin( k r \pm &omega; t ) }{ r } \]
  224. <p>Here is a spherical wave moving on a three-dimensional lattice of atoms:</p>
  225. <div class="view">
  226. <script>
  227. var parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  228. parent.displacement = function( x, y, z, t ) {
  229. if ( x * x + y * y + z * z < 0.01 ) {
  230. return new THREE.Vector3( 0, 0, 0);
  231. } else {
  232. var r = Math.sqrt( x * x + y * y + z * z );
  233. var theta = Math.acos( z / r );
  234. var phi = Math.atan2( y, x );
  235. return new THREE.Vector3( 3 * Math.cos( phi ) * Math.sin( theta ) * Math.sin( r - t ) / r , 3 * Math.sin( phi ) * Math.sin( theta ) * Math.sin( r - t ) / r , 3 * Math.cos( theta ) * Math.sin( r - t ) / r );
  236. }
  237. }
  238. parent.lattice = true;
  239. </script>
  240. </div>
  241. <p>Here is a spherical wave moving through a three-dimensional random distribution of molecules:</p>
  242. <div class="view">
  243. <script>
  244. var parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  245. parent.displacement = function( x, y, z, t ) {
  246. if ( x * x + y * y + z * z < 0.01 ) {
  247. return new THREE.Vector3( 0, 0, 0);
  248. } else {
  249. var r = Math.sqrt( x * x + y * y + z * z );
  250. var theta = Math.acos( z / r );
  251. var phi = Math.atan2( y, x );
  252. return new THREE.Vector3( 3 * Math.cos( phi ) * Math.sin( theta ) * Math.sin( r - t ) / r , 3 * Math.sin( phi ) * Math.sin( theta ) * Math.sin( r - t ) / r , 3 * Math.cos( theta ) * Math.sin( r - t ) / r );
  253. }
  254. }
  255. parent.lattice = false;
  256. </script>
  257. </div>
  258. <p>The mathematical description of sound waves can be carried to higher dimensions, but one needs to wait for Four.js and its higher-dimensional successors to attempt visualizations.</p>
  259. </body>
  260. </html>