webgl_multiple_elements_text.html 14 KB

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