webgl_materials_grass.html 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - materials - grass</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. background:#030;
  9. color:#fff;
  10. padding:0;
  11. margin:0;
  12. overflow:hidden;
  13. font-family:georgia;
  14. text-align:center;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <script src="../build/Three.js"></script>
  20. <script src="js/Detector.js"></script>
  21. <script src="js/RequestAnimationFrame.js"></script>
  22. <script>
  23. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  24. var camera, scene, renderer,
  25. mesh, levels = [];
  26. init();
  27. animate();
  28. function init() {
  29. camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  30. camera.position.y = 75;
  31. camera.position.z = 100;
  32. scene = new THREE.Scene();
  33. var geometry = new THREE.PlaneGeometry( 100, 100 );
  34. var bitmap = generateTextureBase();
  35. for ( var i = 0; i < 15; i ++ ) {
  36. mesh = levels[ i ] = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: new THREE.Texture( generateTextureLevel( bitmap ) ), transparent: true, depthTest: false } ) );
  37. mesh.materials[0].map.needsUpdate = true;
  38. mesh.rotation.x = - 90 * ( Math.PI / 180 );
  39. mesh.position.y = i * 0.25;
  40. scene.add( mesh );
  41. }
  42. renderer = new THREE.WebGLRenderer();
  43. renderer.sortObjects = false;
  44. renderer.setSize( window.innerWidth, window.innerHeight );
  45. document.body.appendChild( renderer.domElement );
  46. }
  47. function generateTextureBase() {
  48. var canvas = document.createElement( 'canvas' );
  49. canvas.width = 512;
  50. canvas.height = 512;
  51. var context = canvas.getContext( '2d' );
  52. for ( var i = 0; i < 20000; i ++ ) {
  53. context.fillStyle = 'rgba(0,' + Math.floor( Math.random() * 64 + 32 ) + ',16,1)';
  54. context.beginPath();
  55. context.arc( Math.random() * canvas.width, Math.random() * canvas.height, Math.random() * 1 + 0.5, 0, Math.PI * 2, true );
  56. context.closePath();
  57. context.fill();
  58. }
  59. context.globalAlpha = 0.075;
  60. context.globalCompositeOperation = 'lighter';
  61. return canvas;
  62. }
  63. function generateTextureLevel( texture ) {
  64. texture.getContext( '2d' ).drawImage( texture, 0, 0 );
  65. var canvas = document.createElement( 'canvas' );
  66. canvas.width = texture.width;
  67. canvas.height = texture.height;
  68. canvas.getContext( '2d' ).drawImage( texture, 0, 0 );
  69. return canvas;
  70. }
  71. //
  72. function animate() {
  73. requestAnimationFrame( animate );
  74. render();
  75. }
  76. function render() {
  77. var time = new Date().getTime() / 6000;
  78. camera.position.x = 80 * Math.cos( time );
  79. camera.position.z = 80 * Math.sin( time );
  80. for ( var i = 0, l = levels.length; i < l; i ++ ) {
  81. mesh = levels[ i ];
  82. mesh.position.x = Math.sin( time * 4 ) * i * i * 0.005;
  83. mesh.position.z = Math.cos( time * 6 ) * i * i * 0.005;
  84. }
  85. renderer.render( scene, camera );
  86. }
  87. </script>
  88. </body>
  89. </html>