webgl_multiple_elements_text.html 14 KB

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