webgl_multiple_elements_text.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. }
  76. }
  77. </script>
  78. <script type="module">
  79. import * as THREE from 'three';
  80. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  81. const scenes = [];
  82. let views, t, canvas, renderer;
  83. window.onload = init;
  84. function init() {
  85. const balls = 20;
  86. const size = .25;
  87. const colors = [
  88. 'rgb(0,127,255)', 'rgb(255,0,0)', 'rgb(0,255,0)', 'rgb(0,255,255)',
  89. 'rgb(255,0,255)', 'rgb(255,0,127)', 'rgb(255,255,0)', 'rgb(0,255,127)'
  90. ];
  91. canvas = document.getElementById( 'c' );
  92. renderer = new THREE.WebGLRenderer( { canvas: canvas, antialias: true } );
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. views = document.querySelectorAll( '.view' );
  95. for ( let n = 0; n < views.length; n ++ ) {
  96. const scene = new THREE.Scene();
  97. scene.background = new THREE.Color( 0xffffff );
  98. const geometry0 = new THREE.BufferGeometry();
  99. const geometry1 = new THREE.BufferGeometry();
  100. const vertices = [];
  101. if ( views[ n ].lattice ) {
  102. const range = balls / 2;
  103. for ( let i = - range; i <= range; i ++ ) {
  104. for ( let j = - range; j <= range; j ++ ) {
  105. for ( let k = - range; k <= range; k ++ ) {
  106. vertices.push( i, j, k );
  107. }
  108. }
  109. }
  110. } else {
  111. for ( let m = 0; m < Math.pow( balls, 3 ); m ++ ) {
  112. const i = balls * Math.random() - balls / 2;
  113. const j = balls * Math.random() - balls / 2;
  114. const k = balls * Math.random() - balls / 2;
  115. vertices.push( i, j, k );
  116. }
  117. }
  118. geometry0.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  119. geometry1.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices.slice(), 3 ) );
  120. const index = Math.floor( colors.length * Math.random() );
  121. const canvas2 = document.createElement( 'canvas' );
  122. canvas2.width = 128;
  123. canvas2.height = 128;
  124. const context = canvas2.getContext( '2d' );
  125. context.arc( 64, 64, 64, 0, 2 * Math.PI );
  126. context.fillStyle = colors[ index ];
  127. context.fill();
  128. const texture = new THREE.CanvasTexture( canvas2 );
  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 ++;
  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. const 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 = true;
  233. </script>
  234. </div>
  235. <p>Here is a plane wave moving through a three-dimensional random distribution of molecules:</p>
  236. <div class="view">
  237. <script>
  238. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  239. parent.displacement = function ( x, y, z, t, target ) {
  240. return target.set( Math.sin( x - t ), 0, 0 );
  241. };
  242. parent.lattice = false;
  243. </script>
  244. </div>
  245. <p>Sound waves whose geometry is determined by two dimensions, cylindrical waves, obey the wave equation</p>
  246. <div class="math">
  247. <span class="math-frac">
  248. <span class="math-num">
  249. &part;<sup>2</sup><i>u</i>
  250. </span>
  251. <span class="math-denom">
  252. &part;<i>r</i><sup>2</sup>
  253. </span>
  254. </span>
  255. &plus;
  256. <span class="math-frac">
  257. <span class="math-num">
  258. 1
  259. </span>
  260. <span class="math-denom">
  261. <i>r</i>
  262. </span>
  263. </span>
  264. <span class="math-frac">
  265. <span class="math-num">
  266. &part;<i>u</i>
  267. </span>
  268. <span class="math-denom">
  269. &part;<i>r</i>
  270. </span>
  271. </span>
  272. &minus;
  273. <span class="math-frac">
  274. <span class="math-num">
  275. 1<sup></sup> <!-- sup for vertical alignment -->
  276. </span>
  277. <span class="math-denom">
  278. <i>c</i><sup>2</sup>
  279. </span>
  280. </span>
  281. <span class="math-frac">
  282. <span class="math-num">
  283. &part;<sup>2</sup><i>u</i>
  284. </span>
  285. <span class="math-denom">
  286. &part;<i>t</i><sup>2</sup>
  287. </span>
  288. </span>
  289. =&nbsp;0
  290. </div>
  291. <p>The monochromatic solution for cylindrical sound waves will be taken to be</p>
  292. <div class="math">
  293. <i>u</i>(<i>r</i>,<i>t</i>)&thinsp;=
  294. <span class="math-frac">
  295. <span class="math-num">
  296. sin(<i>k</i><i>r</i>&thinsp;&plusmn;&thinsp;&omega;<i>t</i>)
  297. </span>
  298. <span class="math-denom">
  299. <span class="math-sqrt">&radic;</span><span class="math-sqrt-stem"><i>r</i></span>
  300. </span>
  301. </span>
  302. </div>
  303. <p>Here is a cylindrical wave moving on a three-dimensional lattice of atoms:</p>
  304. <div class="view">
  305. <script>
  306. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  307. parent.displacement = function ( x, y, z, t, target ) {
  308. if ( x * x + y * y < 0.01 ) {
  309. return target.set( 0, 0, 0 );
  310. } else {
  311. const rho = Math.sqrt( x * x + y * y );
  312. const phi = Math.atan2( y, x );
  313. 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 );
  314. }
  315. };
  316. parent.lattice = true;
  317. </script>
  318. </div>
  319. <p>Here is a cylindrical wave moving through a three-dimensional random distribution of molecules:</p>
  320. <div class="view">
  321. <script>
  322. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  323. parent.displacement = function ( x, y, z, t, target ) {
  324. if ( x * x + y * y < 0.01 ) {
  325. return target.set( 0, 0, 0 );
  326. } else {
  327. const rho = Math.sqrt( x * x + y * y );
  328. const phi = Math.atan2( y, x );
  329. 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 );
  330. }
  331. };
  332. parent.lattice = false;
  333. </script>
  334. </div>
  335. <p>Sound waves whose geometry is determined by three dimensions, spherical waves, obey the wave equation</p>
  336. <div class="math">
  337. <span class="math-frac">
  338. <span class="math-num">
  339. &part;<sup>2</sup><i>u</i>
  340. </span>
  341. <span class="math-denom">
  342. &part;<i>r</i><sup>2</sup>
  343. </span>
  344. </span>
  345. &plus;
  346. <span class="math-frac">
  347. <span class="math-num">
  348. 2
  349. </span>
  350. <span class="math-denom">
  351. <i>r</i>
  352. </span>
  353. </span>
  354. <span class="math-frac">
  355. <span class="math-num">
  356. &part;<i>u</i>
  357. </span>
  358. <span class="math-denom">
  359. &part;<i>r</i>
  360. </span>
  361. </span>
  362. &minus;
  363. <span class="math-frac">
  364. <span class="math-num">
  365. 1<sup></sup> <!-- sup for vertical alignment -->
  366. </span>
  367. <span class="math-denom">
  368. <i>c</i><sup>2</sup>
  369. </span>
  370. </span>
  371. <span class="math-frac">
  372. <span class="math-num">
  373. &part;<sup>2</sup><i>u</i>
  374. </span>
  375. <span class="math-denom">
  376. &part;<i>t</i><sup>2</sup>
  377. </span>
  378. </span>
  379. =&nbsp;0
  380. </div>
  381. <p>The monochromatic solution for spherical sound waves will be taken to be</p>
  382. <div class="math">
  383. <i>u</i>(<i>r</i>,<i>t</i>)&thinsp;=
  384. <span class="math-frac">
  385. <span class="math-num">
  386. sin(<i>k</i><i>r</i>&thinsp;&plusmn;&thinsp;&omega;<i>t</i>)
  387. </span>
  388. <span class="math-denom">
  389. <i>r</i>
  390. </span>
  391. </span>
  392. </div>
  393. <p>Here is a spherical wave moving on a three-dimensional lattice of atoms:</p>
  394. <div class="view">
  395. <script>
  396. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  397. parent.displacement = function ( x, y, z, t, target ) {
  398. if ( x * x + y * y + z * z < 0.01 ) {
  399. return target.set( 0, 0, 0 );
  400. } else {
  401. const r = Math.sqrt( x * x + y * y + z * z );
  402. const theta = Math.acos( z / r );
  403. const phi = Math.atan2( y, x );
  404. 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 );
  405. }
  406. };
  407. parent.lattice = true;
  408. </script>
  409. </div>
  410. <p>Here is a spherical wave moving through a three-dimensional random distribution of molecules:</p>
  411. <div class="view">
  412. <script>
  413. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  414. parent.displacement = function ( x, y, z, t, target ) {
  415. if ( x * x + y * y + z * z < 0.01 ) {
  416. return target.set( 0, 0, 0 );
  417. } else {
  418. const r = Math.sqrt( x * x + y * y + z * z );
  419. const theta = Math.acos( z / r );
  420. const phi = Math.atan2( y, x );
  421. 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 );
  422. }
  423. };
  424. parent.lattice = false;
  425. </script>
  426. </div>
  427. <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>
  428. </body>
  429. </html>