webgl_multiple_elements_text.html 14 KB

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