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