webgl_multiple_elements_text.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. </style>
  41. </head>
  42. <body>
  43. <canvas id="c"></canvas>
  44. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - multiple elements with text - webgl</div>
  45. <script type="importmap">
  46. {
  47. "imports": {
  48. "three": "../build/three.module.js",
  49. "three/addons/": "./jsm/"
  50. }
  51. }
  52. </script>
  53. <script type="module">
  54. import * as THREE from 'three';
  55. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  56. const scenes = [];
  57. const clock = new THREE.Clock();
  58. let views, t, canvas, renderer;
  59. window.onload = init;
  60. function init() {
  61. const balls = 20;
  62. const size = .25;
  63. const colors = [
  64. 'rgb(0,127,255)', 'rgb(255,0,0)', 'rgb(0,255,0)', 'rgb(0,255,255)',
  65. 'rgb(255,0,255)', 'rgb(255,0,127)', 'rgb(255,255,0)', 'rgb(0,255,127)'
  66. ];
  67. canvas = document.getElementById( 'c' );
  68. renderer = new THREE.WebGLRenderer( { canvas: canvas, antialias: true } );
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setAnimationLoop( animate );
  71. views = document.querySelectorAll( '.view' );
  72. for ( let n = 0; n < views.length; n ++ ) {
  73. const scene = new THREE.Scene();
  74. scene.background = new THREE.Color( 0xffffff );
  75. const geometry0 = new THREE.BufferGeometry();
  76. const geometry1 = new THREE.BufferGeometry();
  77. const vertices = [];
  78. if ( views[ n ].lattice ) {
  79. const range = balls / 2;
  80. for ( let i = - range; i <= range; i ++ ) {
  81. for ( let j = - range; j <= range; j ++ ) {
  82. for ( let k = - range; k <= range; k ++ ) {
  83. vertices.push( i, j, k );
  84. }
  85. }
  86. }
  87. } else {
  88. for ( let m = 0; m < Math.pow( balls, 3 ); m ++ ) {
  89. const i = balls * Math.random() - balls / 2;
  90. const j = balls * Math.random() - balls / 2;
  91. const k = balls * Math.random() - balls / 2;
  92. vertices.push( i, j, k );
  93. }
  94. }
  95. geometry0.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  96. geometry1.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices.slice(), 3 ) );
  97. const index = Math.floor( colors.length * Math.random() );
  98. const canvas2 = document.createElement( 'canvas' );
  99. canvas2.width = 128;
  100. canvas2.height = 128;
  101. const context = canvas2.getContext( '2d' );
  102. context.arc( 64, 64, 64, 0, 2 * Math.PI );
  103. context.fillStyle = colors[ index ];
  104. context.fill();
  105. const texture = new THREE.CanvasTexture( canvas2 );
  106. texture.colorSpace = THREE.SRGBColorSpace;
  107. const material = new THREE.PointsMaterial( { size: size, map: texture, transparent: true, alphaTest: 0.1 } );
  108. scene.add( new THREE.Points( geometry0, material ) );
  109. scene.userData.view = views[ n ];
  110. scene.userData.geometry1 = geometry1;
  111. const camera = new THREE.PerspectiveCamera( 75, 1, 0.1, 100 );
  112. camera.position.set( 0, 0, 1.2 * balls );
  113. scene.userData.camera = camera;
  114. const controls = new OrbitControls( camera, views[ n ] );
  115. scene.userData.controls = controls;
  116. scenes.push( scene );
  117. }
  118. t = 0;
  119. animate();
  120. }
  121. function updateSize() {
  122. const width = canvas.clientWidth;
  123. const height = canvas.clientHeight;
  124. if ( canvas.width !== width || canvas.height != height ) {
  125. renderer.setSize( width, height, false );
  126. }
  127. }
  128. function animate() {
  129. updateSize();
  130. renderer.setClearColor( 0xffffff );
  131. renderer.setScissorTest( false );
  132. renderer.clear();
  133. renderer.setClearColor( 0x000000 );
  134. renderer.setScissorTest( true );
  135. scenes.forEach( function ( scene ) {
  136. const rect = scene.userData.view.getBoundingClientRect();
  137. // check if it's offscreen. If so skip it
  138. if ( rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
  139. rect.right < 0 || rect.left > renderer.domElement.clientWidth ) {
  140. return; // it's off screen
  141. }
  142. // set the viewport
  143. const width = rect.right - rect.left;
  144. const height = rect.bottom - rect.top;
  145. const left = rect.left;
  146. const bottom = renderer.domElement.clientHeight - rect.bottom;
  147. renderer.setViewport( left, bottom, width, height );
  148. renderer.setScissor( left, bottom, width, height );
  149. renderer.render( scene, scene.userData.camera );
  150. const points = scene.children[ 0 ];
  151. const position = points.geometry.attributes.position;
  152. const point = new THREE.Vector3();
  153. const offset = new THREE.Vector3();
  154. for ( let i = 0; i < position.count; i ++ ) {
  155. point.fromBufferAttribute( scene.userData.geometry1.attributes.position, i );
  156. scene.userData.view.displacement( point.x, point.y, point.z, t / 5, offset );
  157. position.setXYZ( i, point.x + offset.x, point.y + offset.y, point.z + offset.z );
  158. }
  159. position.needsUpdate = true;
  160. } );
  161. t += clock.getDelta() * 60;
  162. }
  163. </script>
  164. <p>Sound waves whose geometry is determined by a single dimension, plane waves, obey the wave equation</p>
  165. <math display="block">
  166. <mfrac>
  167. <mrow>
  168. <msup>
  169. <mi>&part;</mi>
  170. <mn>2</mn>
  171. </msup>
  172. <mi>u</mi>
  173. </mrow>
  174. <mrow>
  175. <mi>&part;</mi>
  176. <msup>
  177. <mi>r</mi>
  178. <mn>2</mn>
  179. </msup>
  180. </mrow>
  181. </mfrac>
  182. <mo>&minus;</mo>
  183. <mfrac>
  184. <mn>1</mn>
  185. <msup>
  186. <mi>c</mi>
  187. <mn>2</mn>
  188. </msup>
  189. </mfrac>
  190. <mo>&sdot;</mo>
  191. <mfrac>
  192. <mrow>
  193. <msup>
  194. <mi>&part;</mi>
  195. <mn>2</mn>
  196. </msup>
  197. <mi>u</mi>
  198. </mrow>
  199. <mrow>
  200. <mi>&part;</mi>
  201. <msup>
  202. <mi>t</mi>
  203. <mn>2</mn>
  204. </msup>
  205. </mrow>
  206. </mfrac>
  207. <mo>=</mo>
  208. <mn>0</mn>
  209. </math>
  210. <p>where <math><mi>c</mi></math> designates the speed of sound in the medium. The monochromatic solution for plane waves will be taken to be</p>
  211. <math display="block">
  212. <mi>u</mi>
  213. <mo>(</mo>
  214. <mi>r</mi>
  215. <mo>,</mo>
  216. <mi>t</mi>
  217. <mo>)</mo>
  218. <mo>=</mo>
  219. <mi>sin</mi>
  220. <mo>(</mo>
  221. <mi>k</mi>
  222. <mi>r</mi>
  223. <mo>&plusmn;</mo>
  224. <mi>&omega;</mi>
  225. <mi>t</mi>
  226. <mo>)</mo>
  227. </math>
  228. <p>
  229. where <math><mi>&omega;</mi></math> is the frequency and
  230. <math>
  231. <mi>k</mi>
  232. <mo>=</mo>
  233. <mi>&omega;</mi>
  234. <mo>/</mo>
  235. <mi>c</mi>
  236. </math>
  237. is the wave number. The sign chosen in the argument determines the direction of movement of the waves.
  238. </p>
  239. <p>Here is a plane wave moving on a three-dimensional lattice of atoms:</p>
  240. <div class="view">
  241. <script>
  242. /* eslint-disable prefer-const*/
  243. let parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  244. parent.displacement = function ( x, y, z, t, target ) {
  245. return target.set( Math.sin( x - t ), 0, 0 );
  246. };
  247. parent.lattice = true;
  248. </script>
  249. </div>
  250. <p>Here is a plane wave moving through a three-dimensional random distribution of molecules:</p>
  251. <div class="view">
  252. <script>
  253. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  254. parent.displacement = function ( x, y, z, t, target ) {
  255. return target.set( Math.sin( x - t ), 0, 0 );
  256. };
  257. parent.lattice = false;
  258. </script>
  259. </div>
  260. <p>Sound waves whose geometry is determined by two dimensions, cylindrical waves, obey the wave equation</p>
  261. <math display="block">
  262. <mfrac>
  263. <mrow>
  264. <msup>
  265. <mi>&part;</mi>
  266. <mn>2</mn>
  267. </msup>
  268. <mi>u</mi>
  269. </mrow>
  270. <mrow>
  271. <mi>&part;</mi>
  272. <msup>
  273. <mi>r</mi>
  274. <mn>2</mn>
  275. </msup>
  276. </mrow>
  277. </mfrac>
  278. <mo>+</mo>
  279. <mfrac>
  280. <mrow>
  281. <mn>1</mn>
  282. </mrow>
  283. <mrow>
  284. <mi>r</mi>
  285. </mrow>
  286. </mfrac>
  287. <mo>&sdot;</mo>
  288. <mfrac>
  289. <mrow>
  290. <mi>&part;</mi>
  291. <mi>u</mi>
  292. </mrow>
  293. <mrow>
  294. <mi>&part;</mi>
  295. <mi>r</mi>
  296. </mrow>
  297. </mfrac>
  298. <mo>&minus;</mo>
  299. <mfrac>
  300. <mrow>
  301. <mn>1</mn>
  302. </mrow>
  303. <mrow>
  304. <msup>
  305. <mi>c</mi>
  306. <mn>2</mn>
  307. </msup>
  308. </mrow>
  309. </mfrac>
  310. <mo>&sdot;</mo>
  311. <mfrac>
  312. <mrow>
  313. <msup>
  314. <mi>&part;</mi>
  315. <mn>2</mn>
  316. </msup>
  317. <mi>u</mi>
  318. </mrow>
  319. <mrow>
  320. <mi>&part;</mi>
  321. <msup>
  322. <mi>t</mi>
  323. <mn>2</mn>
  324. </msup>
  325. </mrow>
  326. </mfrac>
  327. <mo>=</mo>
  328. <mn>0</mn>
  329. </math>
  330. <p>The monochromatic solution for cylindrical sound waves will be taken to be</p>
  331. <math display="block">
  332. <mi>u</mi>
  333. <mo stretchy="false">(</mo>
  334. <mi>r</mi>
  335. <mo>,</mo>
  336. <mi>t</mi>
  337. <mo stretchy="false">)</mo>
  338. <mo>=</mo>
  339. <mfrac>
  340. <mrow>
  341. <mi>sin</mi>
  342. <mo>(</mo>
  343. <mi>k</mi>
  344. <mi>r</mi>
  345. <mo>&plusmn;</mo>
  346. <mi>&omega;</mi>
  347. <mi>t</mi>
  348. <mo>)</mo>
  349. </mrow>
  350. <mrow>
  351. <msqrt>
  352. <mi>r</mi>
  353. </msqrt>
  354. </mrow>
  355. </mfrac>
  356. </math>
  357. <p>Here is a cylindrical wave moving on a three-dimensional lattice of atoms:</p>
  358. <div class="view">
  359. <script>
  360. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  361. parent.displacement = function ( x, y, z, t, target ) {
  362. if ( x * x + y * y < 0.01 ) {
  363. return target.set( 0, 0, 0 );
  364. } else {
  365. const rho = Math.sqrt( x * x + y * y );
  366. const phi = Math.atan2( y, x );
  367. 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 );
  368. }
  369. };
  370. parent.lattice = true;
  371. </script>
  372. </div>
  373. <p>Here is a cylindrical wave moving through a three-dimensional random distribution of molecules:</p>
  374. <div class="view">
  375. <script>
  376. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  377. parent.displacement = function ( x, y, z, t, target ) {
  378. if ( x * x + y * y < 0.01 ) {
  379. return target.set( 0, 0, 0 );
  380. } else {
  381. const rho = Math.sqrt( x * x + y * y );
  382. const phi = Math.atan2( y, x );
  383. 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 );
  384. }
  385. };
  386. parent.lattice = false;
  387. </script>
  388. </div>
  389. <p>Sound waves whose geometry is determined by three dimensions, spherical waves, obey the wave equation</p>
  390. <math display="block">
  391. <mfrac>
  392. <mrow>
  393. <msup>
  394. <mi>&part;</mi>
  395. <mn>2</mn>
  396. </msup>
  397. <mi>u</mi>
  398. </mrow>
  399. <mrow>
  400. <mi>&part;</mi>
  401. <msup>
  402. <mi>r</mi>
  403. <mn>2</mn>
  404. </msup>
  405. </mrow>
  406. </mfrac>
  407. <mo>+</mo>
  408. <mfrac>
  409. <mrow>
  410. <mn>2</mn>
  411. </mrow>
  412. <mrow>
  413. <mi>r</mi>
  414. </mrow>
  415. </mfrac>
  416. <mo>&sdot;</mo>
  417. <mfrac>
  418. <mrow>
  419. <mi>&part;</mi>
  420. <mi>u</mi>
  421. </mrow>
  422. <mrow>
  423. <mi>&part;</mi>
  424. <mi>r</mi>
  425. </mrow>
  426. </mfrac>
  427. <mo>&minus;</mo>
  428. <mfrac>
  429. <mrow>
  430. <mn>1</mn>
  431. </mrow>
  432. <mrow>
  433. <msup>
  434. <mi>c</mi>
  435. <mn>2</mn>
  436. </msup>
  437. </mrow>
  438. </mfrac>
  439. <mo>&sdot;</mo>
  440. <mfrac>
  441. <mrow>
  442. <msup>
  443. <mi>&part;</mi>
  444. <mn>2</mn>
  445. </msup>
  446. <mi>u</mi>
  447. </mrow>
  448. <mrow>
  449. <mi>&part;</mi>
  450. <msup>
  451. <mi>t</mi>
  452. <mn>2</mn>
  453. </msup>
  454. </mrow>
  455. </mfrac>
  456. <mo>=</mo>
  457. <mn>0</mn>
  458. </math>
  459. <p>The monochromatic solution for spherical sound waves will be taken to be</p>
  460. <math display="block">
  461. <mi>u</mi>
  462. <mo stretchy="false">(</mo>
  463. <mi>r</mi>
  464. <mo>,</mo>
  465. <mi>t</mi>
  466. <mo stretchy="false">)</mo>
  467. <mo>=</mo>
  468. <mfrac>
  469. <mrow>
  470. <mi>sin</mi>
  471. <mo>(</mo>
  472. <mi>k</mi>
  473. <mi>r</mi>
  474. <mo>&plusmn;</mo>
  475. <mi>&omega;</mi>
  476. <mi>t</mi>
  477. <mo>)</mo>
  478. </mrow>
  479. <mrow>
  480. <mi>r</mi>
  481. </mrow>
  482. </mfrac>
  483. </math>
  484. <p>Here is a spherical wave moving on a three-dimensional lattice of atoms:</p>
  485. <div class="view">
  486. <script>
  487. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  488. parent.displacement = function ( x, y, z, t, target ) {
  489. if ( x * x + y * y + z * z < 0.01 ) {
  490. return target.set( 0, 0, 0 );
  491. } else {
  492. const r = Math.sqrt( x * x + y * y + z * z );
  493. const theta = Math.acos( z / r );
  494. const phi = Math.atan2( y, x );
  495. 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 );
  496. }
  497. };
  498. parent.lattice = true;
  499. </script>
  500. </div>
  501. <p>Here is a spherical wave moving through a three-dimensional random distribution of molecules:</p>
  502. <div class="view">
  503. <script>
  504. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  505. parent.displacement = function ( x, y, z, t, target ) {
  506. if ( x * x + y * y + z * z < 0.01 ) {
  507. return target.set( 0, 0, 0 );
  508. } else {
  509. const r = Math.sqrt( x * x + y * y + z * z );
  510. const theta = Math.acos( z / r );
  511. const phi = Math.atan2( y, x );
  512. 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 );
  513. }
  514. };
  515. parent.lattice = false;
  516. </script>
  517. </div>
  518. <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>
  519. </body>
  520. </html>