webgl_multiple_elements_text.html 14 KB

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