webgl_multiple_elements_text.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 { color: #0080ff; }
  42. .math {
  43. text-align: center;
  44. }
  45. .math-frac {
  46. display: inline-block;
  47. vertical-align: middle;
  48. }
  49. .math-num {
  50. display: block;
  51. }
  52. .math-denom {
  53. display: block;
  54. border-top: 1px solid;
  55. }
  56. .math-sqrt {
  57. display: inline-block;
  58. transform: scale(1, 1.3);
  59. }
  60. .math-sqrt-stem {
  61. display: inline-block;
  62. border-top: 1px solid;
  63. margin-top: 5px;
  64. }
  65. </style>
  66. </head>
  67. <body>
  68. <canvas id="c"></canvas>
  69. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - multiple elements with text - webgl</div>
  70. <script src="../build/three.js"></script>
  71. <script src="js/controls/OrbitControls.js"></script>
  72. <script src="js/WebGL.js"></script>
  73. <script>
  74. if ( WEBGL.isWebGLAvailable() === false ) {
  75. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  76. }
  77. var scenes = [], views, t, canvas, renderer;
  78. window.onload = init;
  79. function init() {
  80. var balls = 20;
  81. var size = .25;
  82. var colors = [
  83. 'rgb(0,127,255)', 'rgb(255,0,0)', 'rgb(0,255,0)', 'rgb(0,255,255)',
  84. 'rgb(255,0,255)', 'rgb(255,0,127)', 'rgb(255,255,0)', 'rgb(0,255,127)'
  85. ];
  86. canvas = document.getElementById( 'c' );
  87. renderer = new THREE.WebGLRenderer( { canvas: canvas, antialias: true } );
  88. renderer.setPixelRatio( window.devicePixelRatio );
  89. views = document.querySelectorAll( '.view' );
  90. for ( var n = 0; n < views.length; n ++ ) {
  91. var scene = new THREE.Scene();
  92. scene.background = new THREE.Color( 0xffffff );
  93. var geometry0 = new THREE.BufferGeometry();
  94. var geometry1 = new THREE.BufferGeometry();
  95. var vertices = [];
  96. if ( views[ n ].lattice ) {
  97. var range = balls / 2;
  98. for ( var i = - range; i <= range; i ++ ) {
  99. for ( var j = - range; j <= range; j ++ ) {
  100. for ( var k = - range; k <= range; k ++ ) {
  101. vertices.push( i, j, k );
  102. }
  103. }
  104. }
  105. } else {
  106. for ( var m = 0; m < Math.pow( balls, 3 ); m ++ ) {
  107. var i = balls * Math.random() - balls / 2;
  108. var j = balls * Math.random() - balls / 2;
  109. var k = balls * Math.random() - balls / 2;
  110. vertices.push( i, j, k );
  111. }
  112. }
  113. geometry0.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  114. geometry1.addAttribute( 'position', new THREE.Float32BufferAttribute( vertices.slice(), 3 ) );
  115. var index = Math.floor( colors.length * Math.random() );
  116. var canvas2 = document.createElement( 'canvas' );
  117. canvas2.width = 128;
  118. canvas2.height = 128;
  119. var context = canvas2.getContext( '2d' );
  120. context.arc( 64, 64, 64, 0, 2 * Math.PI );
  121. context.fillStyle = colors[ index ];
  122. context.fill();
  123. var texture = new THREE.CanvasTexture( canvas2 );
  124. var material = new THREE.PointsMaterial( { size: size, map: texture, transparent: true, alphaTest: 0.1 } );
  125. scene.add( new THREE.Points( geometry0, material ) );
  126. scene.userData.view = views[ n ];
  127. scene.userData.geometry1 = geometry1;
  128. var camera = new THREE.PerspectiveCamera( 75, 1, 0.1, 100 );
  129. camera.position.set( 0, 0, 1.2 * balls );
  130. scene.userData.camera = camera;
  131. var controls = new THREE.OrbitControls( camera, views[ n ] );
  132. scene.userData.controls = controls;
  133. scenes.push( scene );
  134. }
  135. t = 0;
  136. animate();
  137. }
  138. function updateSize() {
  139. var width = canvas.clientWidth;
  140. var height = canvas.clientHeight;
  141. if ( canvas.width !== width || canvas.height != height ) {
  142. renderer.setSize( width, height, false );
  143. }
  144. }
  145. function animate() {
  146. render();
  147. requestAnimationFrame( animate );
  148. }
  149. function render() {
  150. updateSize();
  151. renderer.setClearColor( 0xffffff );
  152. renderer.setScissorTest( false );
  153. renderer.clear();
  154. renderer.setClearColor( 0x000000 );
  155. renderer.setScissorTest( true );
  156. scenes.forEach( function ( scene ) {
  157. var rect = scene.userData.view.getBoundingClientRect();
  158. // check if it's offscreen. If so skip it
  159. if ( rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
  160. rect.right < 0 || rect.left > renderer.domElement.clientWidth ) {
  161. return; // it's off screen
  162. }
  163. // set the viewport
  164. var width = rect.right - rect.left;
  165. var height = rect.bottom - rect.top;
  166. var left = rect.left;
  167. var bottom = renderer.domElement.clientHeight - rect.bottom;
  168. renderer.setViewport( left, bottom, width, height );
  169. renderer.setScissor( left, bottom, width, height );
  170. renderer.render( scene, scene.userData.camera );
  171. var points = scene.children[ 0 ];
  172. var position = points.geometry.attributes.position;
  173. var point = new THREE.Vector3();
  174. var offset = new THREE.Vector3();
  175. for ( var i = 0; i < position.count; i ++ ) {
  176. point.fromBufferAttribute( scene.userData.geometry1.attributes.position, i );
  177. scene.userData.view.displacement( point.x, point.y, point.z, t / 5, offset );
  178. position.setXYZ( i, point.x + offset.x, point.y + offset.y, point.z + offset.z );
  179. }
  180. position.needsUpdate = true;
  181. } );
  182. t ++;
  183. }
  184. </script>
  185. <p>Sound waves whose geometry is determined by a single dimension, plane waves, obey the wave equation</p>
  186. <!-- css math formatting inspired by http://mathquill.com/mathquill/mathquill.css -->
  187. <div class="math">
  188. <span class="math-frac">
  189. <span class="math-num">
  190. &part;<sup>2</sup><i>u</i>
  191. </span>
  192. <span class="math-denom">
  193. &part;<i>r</i><sup>2</sup>
  194. </span>
  195. </span>
  196. &minus;
  197. <span class="math-frac">
  198. <span class="math-num">
  199. 1<sup></sup> <!-- sup for vertical alignment -->
  200. </span>
  201. <span class="math-denom">
  202. <i>c</i><sup>2</sup>
  203. </span>
  204. </span>
  205. <span class="math-frac">
  206. <span class="math-num">
  207. &part;<sup>2</sup><i>u</i>
  208. </span>
  209. <span class="math-denom">
  210. &part;<i>t</i><sup>2</sup>
  211. </span>
  212. </span>
  213. =&nbsp;0
  214. </div>
  215. <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>
  216. <div class="math">
  217. <i>u</i>(<i>r</i>,<i>t</i>)&thinsp;=&nbsp;sin(<i>k</i><i>r</i>&thinsp;&plusmn;&thinsp;&omega;<i>t</i>)
  218. </div>
  219. <p>where &omega; is the frequency and <i>k</i>=&omega;/<i>c</i> is the wave number. The sign chosen in the argument determines the direction of movement of the waves.</p>
  220. <p>Here is a plane wave moving on a three-dimensional lattice of atoms:</p>
  221. <div class="view">
  222. <script>
  223. var parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  224. parent.displacement = function ( x, y, z, t, target ) {
  225. return target.set( Math.sin( x - t ), 0, 0 );
  226. };
  227. parent.lattice = true;
  228. </script>
  229. </div>
  230. <p>Here is a plane wave moving through a three-dimensional random distribution of molecules:</p>
  231. <div class="view">
  232. <script>
  233. var parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  234. parent.displacement = function ( x, y, z, t, target ) {
  235. return target.set( Math.sin( x - t ), 0, 0 );
  236. };
  237. parent.lattice = false;
  238. </script>
  239. </div>
  240. <p>Sound waves whose geometry is determined by two dimensions, cylindrical waves, obey the wave equation</p>
  241. <div class="math">
  242. <span class="math-frac">
  243. <span class="math-num">
  244. &part;<sup>2</sup><i>u</i>
  245. </span>
  246. <span class="math-denom">
  247. &part;<i>r</i><sup>2</sup>
  248. </span>
  249. </span>
  250. &plus;
  251. <span class="math-frac">
  252. <span class="math-num">
  253. 1
  254. </span>
  255. <span class="math-denom">
  256. <i>r</i>
  257. </span>
  258. </span>
  259. <span class="math-frac">
  260. <span class="math-num">
  261. &part;<i>u</i>
  262. </span>
  263. <span class="math-denom">
  264. &part;<i>r</i>
  265. </span>
  266. </span>
  267. &minus;
  268. <span class="math-frac">
  269. <span class="math-num">
  270. 1<sup></sup> <!-- sup for vertical alignment -->
  271. </span>
  272. <span class="math-denom">
  273. <i>c</i><sup>2</sup>
  274. </span>
  275. </span>
  276. <span class="math-frac">
  277. <span class="math-num">
  278. &part;<sup>2</sup><i>u</i>
  279. </span>
  280. <span class="math-denom">
  281. &part;<i>t</i><sup>2</sup>
  282. </span>
  283. </span>
  284. =&nbsp;0
  285. </div>
  286. <p>The monochromatic solution for cylindrical sound waves will be taken to be</p>
  287. <div class="math">
  288. <i>u</i>(<i>r</i>,<i>t</i>)&thinsp;=
  289. <span class="math-frac">
  290. <span class="math-num">
  291. sin(<i>k</i><i>r</i>&thinsp;&plusmn;&thinsp;&omega;<i>t</i>)
  292. </span>
  293. <span class="math-denom">
  294. <span class="math-sqrt">&radic;</span><span class="math-sqrt-stem"><i>r</i></span>
  295. </span>
  296. </span>
  297. </div>
  298. <p>Here is a cylindrical wave moving on a three-dimensional lattice of atoms:</p>
  299. <div class="view">
  300. <script>
  301. var parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  302. parent.displacement = function ( x, y, z, t, target ) {
  303. if ( x * x + y * y < 0.01 ) {
  304. return target.set( 0, 0, 0 );
  305. } else {
  306. var rho = Math.sqrt( x * x + y * y );
  307. var phi = Math.atan2( y, x );
  308. return target.set( 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 );
  309. }
  310. };
  311. parent.lattice = true;
  312. </script>
  313. </div>
  314. <p>Here is a cylindrical wave moving through a three-dimensional random distribution of molecules:</p>
  315. <div class="view">
  316. <script>
  317. var parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  318. parent.displacement = function ( x, y, z, t, target ) {
  319. if ( x * x + y * y < 0.01 ) {
  320. return target.set( 0, 0, 0 );
  321. } else {
  322. var rho = Math.sqrt( x * x + y * y );
  323. var phi = Math.atan2( y, x );
  324. return target.set( 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 );
  325. }
  326. };
  327. parent.lattice = false;
  328. </script>
  329. </div>
  330. <p>Sound waves whose geometry is determined by three dimensions, spherical waves, obey the wave equation</p>
  331. <div class="math">
  332. <span class="math-frac">
  333. <span class="math-num">
  334. &part;<sup>2</sup><i>u</i>
  335. </span>
  336. <span class="math-denom">
  337. &part;<i>r</i><sup>2</sup>
  338. </span>
  339. </span>
  340. &plus;
  341. <span class="math-frac">
  342. <span class="math-num">
  343. 2
  344. </span>
  345. <span class="math-denom">
  346. <i>r</i>
  347. </span>
  348. </span>
  349. <span class="math-frac">
  350. <span class="math-num">
  351. &part;<i>u</i>
  352. </span>
  353. <span class="math-denom">
  354. &part;<i>r</i>
  355. </span>
  356. </span>
  357. &minus;
  358. <span class="math-frac">
  359. <span class="math-num">
  360. 1<sup></sup> <!-- sup for vertical alignment -->
  361. </span>
  362. <span class="math-denom">
  363. <i>c</i><sup>2</sup>
  364. </span>
  365. </span>
  366. <span class="math-frac">
  367. <span class="math-num">
  368. &part;<sup>2</sup><i>u</i>
  369. </span>
  370. <span class="math-denom">
  371. &part;<i>t</i><sup>2</sup>
  372. </span>
  373. </span>
  374. =&nbsp;0
  375. </div>
  376. <p>The monochromatic solution for spherical sound waves will be taken to be</p>
  377. <div class="math">
  378. <i>u</i>(<i>r</i>,<i>t</i>)&thinsp;=
  379. <span class="math-frac">
  380. <span class="math-num">
  381. sin(<i>k</i><i>r</i>&thinsp;&plusmn;&thinsp;&omega;<i>t</i>)
  382. </span>
  383. <span class="math-denom">
  384. <i>r</i>
  385. </span>
  386. </span>
  387. </div>
  388. <p>Here is a spherical wave moving on a three-dimensional lattice of atoms:</p>
  389. <div class="view">
  390. <script>
  391. var parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  392. parent.displacement = function ( x, y, z, t, target ) {
  393. if ( x * x + y * y + z * z < 0.01 ) {
  394. return target.set( 0, 0, 0 );
  395. } else {
  396. var r = Math.sqrt( x * x + y * y + z * z );
  397. var theta = Math.acos( z / r );
  398. var phi = Math.atan2( y, x );
  399. return target.set( 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 );
  400. }
  401. };
  402. parent.lattice = true;
  403. </script>
  404. </div>
  405. <p>Here is a spherical wave moving through a three-dimensional random distribution of molecules:</p>
  406. <div class="view">
  407. <script>
  408. var parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  409. parent.displacement = function ( x, y, z, t, target ) {
  410. if ( x * x + y * y + z * z < 0.01 ) {
  411. return target.set( 0, 0, 0 );
  412. } else {
  413. var r = Math.sqrt( x * x + y * y + z * z );
  414. var theta = Math.acos( z / r );
  415. var phi = Math.atan2( y, x );
  416. return target.set( 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 );
  417. }
  418. };
  419. parent.lattice = false;
  420. </script>
  421. </div>
  422. <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>
  423. </body>
  424. </html>