webgl_multiple_elements_text.html 13 KB

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