webgl_multiple_elements_text.html 14 KB

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