webgl_multiple_elements_text.html 14 KB

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