1
0

webgl_multiple_elements_text.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. </style>
  41. </head>
  42. <body>
  43. <canvas id="c"></canvas>
  44. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - multiple elements with text - webgl</div>
  45. <script type="importmap">
  46. {
  47. "imports": {
  48. "three": "../build/three.module.js",
  49. "three/addons/": "./jsm/"
  50. }
  51. }
  52. </script>
  53. <script type="module">
  54. import * as THREE from 'three';
  55. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  56. const scenes = [];
  57. const timer = new THREE.Timer();
  58. timer.connect( document );
  59. let views, t, canvas, renderer;
  60. window.onload = init;
  61. function init() {
  62. const balls = 20;
  63. const size = .25;
  64. const colors = [
  65. 'rgb(0,127,255)', 'rgb(255,0,0)', 'rgb(0,255,0)', 'rgb(0,255,255)',
  66. 'rgb(255,0,255)', 'rgb(255,0,127)', 'rgb(255,255,0)', 'rgb(0,255,127)'
  67. ];
  68. canvas = document.getElementById( 'c' );
  69. renderer = new THREE.WebGLRenderer( { canvas: canvas, antialias: true } );
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setAnimationLoop( animate );
  72. views = document.querySelectorAll( '.view' );
  73. for ( let n = 0; n < views.length; n ++ ) {
  74. const scene = new THREE.Scene();
  75. scene.background = new THREE.Color( 0xffffff );
  76. const geometry0 = new THREE.BufferGeometry();
  77. const geometry1 = new THREE.BufferGeometry();
  78. const vertices = [];
  79. if ( views[ n ].lattice ) {
  80. const range = balls / 2;
  81. for ( let i = - range; i <= range; i ++ ) {
  82. for ( let j = - range; j <= range; j ++ ) {
  83. for ( let k = - range; k <= range; k ++ ) {
  84. vertices.push( i, j, k );
  85. }
  86. }
  87. }
  88. } else {
  89. for ( let m = 0; m < Math.pow( balls, 3 ); m ++ ) {
  90. const i = balls * Math.random() - balls / 2;
  91. const j = balls * Math.random() - balls / 2;
  92. const k = balls * Math.random() - balls / 2;
  93. vertices.push( i, j, k );
  94. }
  95. }
  96. geometry0.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  97. geometry1.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices.slice(), 3 ) );
  98. const index = Math.floor( colors.length * Math.random() );
  99. const canvas2 = document.createElement( 'canvas' );
  100. canvas2.width = 128;
  101. canvas2.height = 128;
  102. const context = canvas2.getContext( '2d' );
  103. context.arc( 64, 64, 64, 0, 2 * Math.PI );
  104. context.fillStyle = colors[ index ];
  105. context.fill();
  106. const texture = new THREE.CanvasTexture( canvas2 );
  107. texture.colorSpace = THREE.SRGBColorSpace;
  108. const material = new THREE.PointsMaterial( { size: size, map: texture, transparent: true, alphaTest: 0.1 } );
  109. scene.add( new THREE.Points( geometry0, material ) );
  110. scene.userData.view = views[ n ];
  111. scene.userData.geometry1 = geometry1;
  112. const camera = new THREE.PerspectiveCamera( 75, 1, 0.1, 100 );
  113. camera.position.set( 0, 0, 1.2 * balls );
  114. scene.userData.camera = camera;
  115. const controls = new OrbitControls( camera, views[ n ] );
  116. scene.userData.controls = controls;
  117. scenes.push( scene );
  118. }
  119. t = 0;
  120. animate();
  121. }
  122. function updateSize() {
  123. const width = canvas.clientWidth;
  124. const height = canvas.clientHeight;
  125. if ( canvas.width !== width || canvas.height != height ) {
  126. renderer.setSize( width, height, false );
  127. }
  128. }
  129. function animate() {
  130. timer.update();
  131. updateSize();
  132. renderer.setClearColor( 0xffffff );
  133. renderer.setScissorTest( false );
  134. renderer.clear();
  135. renderer.setClearColor( 0x000000 );
  136. renderer.setScissorTest( true );
  137. scenes.forEach( function ( scene ) {
  138. const rect = scene.userData.view.getBoundingClientRect();
  139. // check if it's offscreen. If so skip it
  140. if ( rect.bottom < 0 || rect.top > renderer.domElement.clientHeight ||
  141. rect.right < 0 || rect.left > renderer.domElement.clientWidth ) {
  142. return; // it's off screen
  143. }
  144. // set the viewport
  145. const width = rect.right - rect.left;
  146. const height = rect.bottom - rect.top;
  147. const left = rect.left;
  148. const bottom = renderer.domElement.clientHeight - rect.bottom;
  149. renderer.setViewport( left, bottom, width, height );
  150. renderer.setScissor( left, bottom, width, height );
  151. renderer.render( scene, scene.userData.camera );
  152. const points = scene.children[ 0 ];
  153. const position = points.geometry.attributes.position;
  154. const point = new THREE.Vector3();
  155. const offset = new THREE.Vector3();
  156. for ( let i = 0; i < position.count; i ++ ) {
  157. point.fromBufferAttribute( scene.userData.geometry1.attributes.position, i );
  158. scene.userData.view.displacement( point.x, point.y, point.z, t / 5, offset );
  159. position.setXYZ( i, point.x + offset.x, point.y + offset.y, point.z + offset.z );
  160. }
  161. position.needsUpdate = true;
  162. } );
  163. t += timer.getDelta() * 60;
  164. }
  165. </script>
  166. <p>Sound waves whose geometry is determined by a single dimension, plane waves, obey the wave equation</p>
  167. <math display="block">
  168. <mfrac>
  169. <mrow>
  170. <msup>
  171. <mi>&part;</mi>
  172. <mn>2</mn>
  173. </msup>
  174. <mi>u</mi>
  175. </mrow>
  176. <mrow>
  177. <mi>&part;</mi>
  178. <msup>
  179. <mi>r</mi>
  180. <mn>2</mn>
  181. </msup>
  182. </mrow>
  183. </mfrac>
  184. <mo>&minus;</mo>
  185. <mfrac>
  186. <mn>1</mn>
  187. <msup>
  188. <mi>c</mi>
  189. <mn>2</mn>
  190. </msup>
  191. </mfrac>
  192. <mo>&sdot;</mo>
  193. <mfrac>
  194. <mrow>
  195. <msup>
  196. <mi>&part;</mi>
  197. <mn>2</mn>
  198. </msup>
  199. <mi>u</mi>
  200. </mrow>
  201. <mrow>
  202. <mi>&part;</mi>
  203. <msup>
  204. <mi>t</mi>
  205. <mn>2</mn>
  206. </msup>
  207. </mrow>
  208. </mfrac>
  209. <mo>=</mo>
  210. <mn>0</mn>
  211. </math>
  212. <p>where <math><mi>c</mi></math> designates the speed of sound in the medium. The monochromatic solution for plane waves will be taken to be</p>
  213. <math display="block">
  214. <mi>u</mi>
  215. <mo>(</mo>
  216. <mi>r</mi>
  217. <mo>,</mo>
  218. <mi>t</mi>
  219. <mo>)</mo>
  220. <mo>=</mo>
  221. <mi>sin</mi>
  222. <mo>(</mo>
  223. <mi>k</mi>
  224. <mi>r</mi>
  225. <mo>&plusmn;</mo>
  226. <mi>&omega;</mi>
  227. <mi>t</mi>
  228. <mo>)</mo>
  229. </math>
  230. <p>
  231. where <math><mi>&omega;</mi></math> is the frequency and
  232. <math>
  233. <mi>k</mi>
  234. <mo>=</mo>
  235. <mi>&omega;</mi>
  236. <mo>/</mo>
  237. <mi>c</mi>
  238. </math>
  239. is the wave number. The sign chosen in the argument determines the direction of movement of the waves.
  240. </p>
  241. <p>Here is a plane wave moving on a three-dimensional lattice of atoms:</p>
  242. <div class="view">
  243. <script>
  244. /* eslint-disable prefer-const*/
  245. let parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  246. parent.displacement = function ( x, y, z, t, target ) {
  247. return target.set( Math.sin( x - t ), 0, 0 );
  248. };
  249. parent.lattice = true;
  250. </script>
  251. </div>
  252. <p>Here is a plane wave moving through a three-dimensional random distribution of molecules:</p>
  253. <div class="view">
  254. <script>
  255. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  256. parent.displacement = function ( x, y, z, t, target ) {
  257. return target.set( Math.sin( x - t ), 0, 0 );
  258. };
  259. parent.lattice = false;
  260. </script>
  261. </div>
  262. <p>Sound waves whose geometry is determined by two dimensions, cylindrical waves, obey the wave equation</p>
  263. <math display="block">
  264. <mfrac>
  265. <mrow>
  266. <msup>
  267. <mi>&part;</mi>
  268. <mn>2</mn>
  269. </msup>
  270. <mi>u</mi>
  271. </mrow>
  272. <mrow>
  273. <mi>&part;</mi>
  274. <msup>
  275. <mi>r</mi>
  276. <mn>2</mn>
  277. </msup>
  278. </mrow>
  279. </mfrac>
  280. <mo>+</mo>
  281. <mfrac>
  282. <mrow>
  283. <mn>1</mn>
  284. </mrow>
  285. <mrow>
  286. <mi>r</mi>
  287. </mrow>
  288. </mfrac>
  289. <mo>&sdot;</mo>
  290. <mfrac>
  291. <mrow>
  292. <mi>&part;</mi>
  293. <mi>u</mi>
  294. </mrow>
  295. <mrow>
  296. <mi>&part;</mi>
  297. <mi>r</mi>
  298. </mrow>
  299. </mfrac>
  300. <mo>&minus;</mo>
  301. <mfrac>
  302. <mrow>
  303. <mn>1</mn>
  304. </mrow>
  305. <mrow>
  306. <msup>
  307. <mi>c</mi>
  308. <mn>2</mn>
  309. </msup>
  310. </mrow>
  311. </mfrac>
  312. <mo>&sdot;</mo>
  313. <mfrac>
  314. <mrow>
  315. <msup>
  316. <mi>&part;</mi>
  317. <mn>2</mn>
  318. </msup>
  319. <mi>u</mi>
  320. </mrow>
  321. <mrow>
  322. <mi>&part;</mi>
  323. <msup>
  324. <mi>t</mi>
  325. <mn>2</mn>
  326. </msup>
  327. </mrow>
  328. </mfrac>
  329. <mo>=</mo>
  330. <mn>0</mn>
  331. </math>
  332. <p>The monochromatic solution for cylindrical sound waves will be taken to be</p>
  333. <math display="block">
  334. <mi>u</mi>
  335. <mo stretchy="false">(</mo>
  336. <mi>r</mi>
  337. <mo>,</mo>
  338. <mi>t</mi>
  339. <mo stretchy="false">)</mo>
  340. <mo>=</mo>
  341. <mfrac>
  342. <mrow>
  343. <mi>sin</mi>
  344. <mo>(</mo>
  345. <mi>k</mi>
  346. <mi>r</mi>
  347. <mo>&plusmn;</mo>
  348. <mi>&omega;</mi>
  349. <mi>t</mi>
  350. <mo>)</mo>
  351. </mrow>
  352. <mrow>
  353. <msqrt>
  354. <mi>r</mi>
  355. </msqrt>
  356. </mrow>
  357. </mfrac>
  358. </math>
  359. <p>Here is a cylindrical wave moving on a three-dimensional lattice of atoms:</p>
  360. <div class="view">
  361. <script>
  362. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  363. parent.displacement = function ( x, y, z, t, target ) {
  364. if ( x * x + y * y < 0.01 ) {
  365. return target.set( 0, 0, 0 );
  366. } else {
  367. const rho = Math.sqrt( x * x + y * y );
  368. const phi = Math.atan2( y, x );
  369. 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 );
  370. }
  371. };
  372. parent.lattice = true;
  373. </script>
  374. </div>
  375. <p>Here is a cylindrical wave moving through a three-dimensional random distribution of molecules:</p>
  376. <div class="view">
  377. <script>
  378. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  379. parent.displacement = function ( x, y, z, t, target ) {
  380. if ( x * x + y * y < 0.01 ) {
  381. return target.set( 0, 0, 0 );
  382. } else {
  383. const rho = Math.sqrt( x * x + y * y );
  384. const phi = Math.atan2( y, x );
  385. 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 );
  386. }
  387. };
  388. parent.lattice = false;
  389. </script>
  390. </div>
  391. <p>Sound waves whose geometry is determined by three dimensions, spherical waves, obey the wave equation</p>
  392. <math display="block">
  393. <mfrac>
  394. <mrow>
  395. <msup>
  396. <mi>&part;</mi>
  397. <mn>2</mn>
  398. </msup>
  399. <mi>u</mi>
  400. </mrow>
  401. <mrow>
  402. <mi>&part;</mi>
  403. <msup>
  404. <mi>r</mi>
  405. <mn>2</mn>
  406. </msup>
  407. </mrow>
  408. </mfrac>
  409. <mo>+</mo>
  410. <mfrac>
  411. <mrow>
  412. <mn>2</mn>
  413. </mrow>
  414. <mrow>
  415. <mi>r</mi>
  416. </mrow>
  417. </mfrac>
  418. <mo>&sdot;</mo>
  419. <mfrac>
  420. <mrow>
  421. <mi>&part;</mi>
  422. <mi>u</mi>
  423. </mrow>
  424. <mrow>
  425. <mi>&part;</mi>
  426. <mi>r</mi>
  427. </mrow>
  428. </mfrac>
  429. <mo>&minus;</mo>
  430. <mfrac>
  431. <mrow>
  432. <mn>1</mn>
  433. </mrow>
  434. <mrow>
  435. <msup>
  436. <mi>c</mi>
  437. <mn>2</mn>
  438. </msup>
  439. </mrow>
  440. </mfrac>
  441. <mo>&sdot;</mo>
  442. <mfrac>
  443. <mrow>
  444. <msup>
  445. <mi>&part;</mi>
  446. <mn>2</mn>
  447. </msup>
  448. <mi>u</mi>
  449. </mrow>
  450. <mrow>
  451. <mi>&part;</mi>
  452. <msup>
  453. <mi>t</mi>
  454. <mn>2</mn>
  455. </msup>
  456. </mrow>
  457. </mfrac>
  458. <mo>=</mo>
  459. <mn>0</mn>
  460. </math>
  461. <p>The monochromatic solution for spherical sound waves will be taken to be</p>
  462. <math display="block">
  463. <mi>u</mi>
  464. <mo stretchy="false">(</mo>
  465. <mi>r</mi>
  466. <mo>,</mo>
  467. <mi>t</mi>
  468. <mo stretchy="false">)</mo>
  469. <mo>=</mo>
  470. <mfrac>
  471. <mrow>
  472. <mi>sin</mi>
  473. <mo>(</mo>
  474. <mi>k</mi>
  475. <mi>r</mi>
  476. <mo>&plusmn;</mo>
  477. <mi>&omega;</mi>
  478. <mi>t</mi>
  479. <mo>)</mo>
  480. </mrow>
  481. <mrow>
  482. <mi>r</mi>
  483. </mrow>
  484. </mfrac>
  485. </math>
  486. <p>Here is a spherical wave moving on a three-dimensional lattice of atoms:</p>
  487. <div class="view">
  488. <script>
  489. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  490. parent.displacement = function ( x, y, z, t, target ) {
  491. if ( x * x + y * y + z * z < 0.01 ) {
  492. return target.set( 0, 0, 0 );
  493. } else {
  494. const r = Math.sqrt( x * x + y * y + z * z );
  495. const theta = Math.acos( z / r );
  496. const phi = Math.atan2( y, x );
  497. 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 );
  498. }
  499. };
  500. parent.lattice = true;
  501. </script>
  502. </div>
  503. <p>Here is a spherical wave moving through a three-dimensional random distribution of molecules:</p>
  504. <div class="view">
  505. <script>
  506. parent = document.scripts[ document.scripts.length - 1 ].parentNode;
  507. parent.displacement = function ( x, y, z, t, target ) {
  508. if ( x * x + y * y + z * z < 0.01 ) {
  509. return target.set( 0, 0, 0 );
  510. } else {
  511. const r = Math.sqrt( x * x + y * y + z * z );
  512. const theta = Math.acos( z / r );
  513. const phi = Math.atan2( y, x );
  514. 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 );
  515. }
  516. };
  517. parent.lattice = false;
  518. </script>
  519. </div>
  520. <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>
  521. </body>
  522. </html>
粤ICP备19079148号