webgl_geometry_dynamic.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - dynamic</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. body {
  10. color: #666;
  11. }
  12. a {
  13. color: #08f;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="info">
  19. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - dynamic geometry<br />
  20. left click: forward, right click: backward
  21. </div>
  22. <script type="module">
  23. import * as THREE from '../build/three.module.js';
  24. import Stats from './jsm/libs/stats.module.js';
  25. import { FirstPersonControls } from './jsm/controls/FirstPersonControls.js';
  26. var camera, controls, scene, renderer, stats;
  27. var mesh, geometry, material, clock;
  28. var worldWidth = 128, worldDepth = 128;
  29. init();
  30. animate();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 20000 );
  33. camera.position.y = 200;
  34. clock = new THREE.Clock();
  35. scene = new THREE.Scene();
  36. scene.background = new THREE.Color( 0xaaccff );
  37. scene.fog = new THREE.FogExp2( 0xaaccff, 0.0007 );
  38. geometry = new THREE.PlaneBufferGeometry( 20000, 20000, worldWidth - 1, worldDepth - 1 );
  39. geometry.rotateX( - Math.PI / 2 );
  40. var position = geometry.attributes.position;
  41. position.usage = THREE.DynamicDrawUsage;
  42. for ( var i = 0; i < position.count; i ++ ) {
  43. var y = 35 * Math.sin( i / 2 );
  44. position.setY( i, y );
  45. }
  46. var texture = new THREE.TextureLoader().load( 'textures/water.jpg' );
  47. texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
  48. texture.repeat.set( 5, 5 );
  49. material = new THREE.MeshBasicMaterial( { color: 0x0044ff, map: texture } );
  50. mesh = new THREE.Mesh( geometry, material );
  51. scene.add( mesh );
  52. renderer = new THREE.WebGLRenderer( { antialias: true } );
  53. renderer.setPixelRatio( window.devicePixelRatio );
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. document.body.appendChild( renderer.domElement );
  56. controls = new FirstPersonControls( camera, renderer.domElement );
  57. controls.movementSpeed = 500;
  58. controls.lookSpeed = 0.1;
  59. stats = new Stats();
  60. document.body.appendChild( stats.dom );
  61. //
  62. window.addEventListener( 'resize', onWindowResize, false );
  63. }
  64. function onWindowResize() {
  65. camera.aspect = window.innerWidth / window.innerHeight;
  66. camera.updateProjectionMatrix();
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. controls.handleResize();
  69. }
  70. //
  71. function animate() {
  72. requestAnimationFrame( animate );
  73. render();
  74. stats.update();
  75. }
  76. function render() {
  77. var delta = clock.getDelta();
  78. var time = clock.getElapsedTime() * 10;
  79. var position = geometry.attributes.position;
  80. for ( var i = 0; i < position.count; i ++ ) {
  81. var y = 35 * Math.sin( i / 5 + ( time + i ) / 7 );
  82. position.setY( i, y );
  83. }
  84. position.needsUpdate = true;
  85. controls.update( delta );
  86. renderer.render( scene, camera );
  87. }
  88. </script>
  89. </body>
  90. </html>