2
0

webgl_multiple_elements_text.html 14 KB

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