webgl_materials_grass.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - grass</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. </head>
  9. <body>
  10. <script type="module">
  11. import * as THREE from '../build/three.module.js';
  12. var camera, scene, renderer;
  13. init();
  14. animate();
  15. function init() {
  16. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  17. camera.position.set( 0, 75, 100 );
  18. scene = new THREE.Scene();
  19. scene.background = new THREE.Color( 0x003300 );
  20. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  21. var texture = new THREE.CanvasTexture( generateTexture() );
  22. for ( var i = 0; i < 15; i ++ ) {
  23. var material = new THREE.MeshBasicMaterial( {
  24. color: new THREE.Color().setHSL( 0.3, 0.75, ( i / 15 ) * 0.4 + 0.1 ),
  25. map: texture,
  26. depthTest: false,
  27. depthWrite: false,
  28. transparent: true
  29. } );
  30. var mesh = new THREE.Mesh( geometry, material );
  31. mesh.position.y = i * 0.25;
  32. mesh.rotation.x = - Math.PI / 2;
  33. scene.add( mesh );
  34. }
  35. scene.children.reverse();
  36. renderer = new THREE.WebGLRenderer();
  37. renderer.setPixelRatio( window.devicePixelRatio );
  38. renderer.setSize( window.innerWidth, window.innerHeight );
  39. document.body.appendChild( renderer.domElement );
  40. //
  41. window.addEventListener( 'resize', onWindowResize, false );
  42. }
  43. function onWindowResize() {
  44. camera.aspect = window.innerWidth / window.innerHeight;
  45. camera.updateProjectionMatrix();
  46. renderer.setSize( window.innerWidth, window.innerHeight );
  47. }
  48. function generateTexture() {
  49. var canvas = document.createElement( 'canvas' );
  50. canvas.width = 512;
  51. canvas.height = 512;
  52. var context = canvas.getContext( '2d' );
  53. for ( var i = 0; i < 20000; i ++ ) {
  54. context.fillStyle = 'hsl(0,0%,' + ( Math.random() * 50 + 50 ) + '%)';
  55. context.beginPath();
  56. context.arc( Math.random() * canvas.width, Math.random() * canvas.height, Math.random() + 0.15, 0, Math.PI * 2, true );
  57. context.fill();
  58. }
  59. context.globalAlpha = 0.075;
  60. context.globalCompositeOperation = 'lighter';
  61. return canvas;
  62. }
  63. //
  64. function animate() {
  65. requestAnimationFrame( animate );
  66. render();
  67. }
  68. function render() {
  69. var time = Date.now() / 6000;
  70. camera.position.x = 80 * Math.cos( time );
  71. camera.position.z = 80 * Math.sin( time );
  72. camera.lookAt( scene.position );
  73. for ( var i = 0, l = scene.children.length; i < l; i ++ ) {
  74. var mesh = scene.children[ i ];
  75. mesh.position.x = Math.sin( time * 4 ) * i * i * 0.005;
  76. mesh.position.z = Math.cos( time * 6 ) * i * i * 0.005;
  77. }
  78. renderer.render( scene, camera );
  79. }
  80. </script>
  81. </body>
  82. </html>