webgl_multiple_elements_text.html 14 KB

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