materials_grass.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - 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 type="text/javascript" src="../build/Three.js"></script>
  20. <script type="text/javascript" src="../src/extras/primitives/Plane.js"></script>
  21. <script type="text/javascript">
  22. var camera, scene, renderer,
  23. mesh, levels = [];
  24. init();
  25. setInterval( loop, 1000 / 60 );
  26. function init() {
  27. camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
  28. camera.position.y = 75;
  29. camera.position.z = 100;
  30. scene = new THREE.Scene();
  31. var geometry = new Plane( 100, 100 );
  32. var texture = generateTextureBase();
  33. for ( var i = 0; i < 10; i ++ ) {
  34. mesh = levels[ i ] = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: new THREE.Texture( generateTextureLevel( texture ), new THREE.UVMapping(), THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping ) } ) );
  35. mesh.rotation.x = - 90 * ( Math.PI / 180 );
  36. mesh.position.y = i * 0.5;
  37. scene.addObject( mesh );
  38. }
  39. renderer = new THREE.WebGLRenderer();
  40. renderer.sortObjects = false;
  41. renderer.setSize( window.innerWidth, window.innerHeight );
  42. document.body.appendChild( renderer.domElement );
  43. }
  44. function generateTextureBase() {
  45. var canvas = document.createElement( 'canvas' );
  46. canvas.loaded = true;
  47. canvas.width = 1024;
  48. canvas.height = 1024;
  49. var context = canvas.getContext( '2d' );
  50. for ( var i = 0; i < 20000; i ++ ) {
  51. context.fillStyle = 'rgba(0,' + Math.floor( Math.random() * 64 + 32 ) + ',16,1)';
  52. context.beginPath();
  53. context.arc( Math.random() * canvas.width, Math.random() * canvas.height, Math.random() * 3 + 1, 0, Math.PI * 2, true );
  54. context.closePath();
  55. context.fill();
  56. }
  57. context.globalAlpha = 0.1;
  58. context.globalCompositeOperation = 'lighter';
  59. return canvas;
  60. }
  61. function generateTextureLevel( texture ) {
  62. texture.getContext( '2d' ).drawImage( texture, 0, 0 );
  63. var canvas = document.createElement( 'canvas' );
  64. canvas.loaded = true;
  65. canvas.width = texture.width;
  66. canvas.height = texture.height;
  67. canvas.getContext( '2d' ).drawImage( texture, 0, 0 );
  68. return canvas;
  69. }
  70. function loop() {
  71. var time = new Date().getTime() / 6000;
  72. camera.position.x = 100 * Math.cos( time );
  73. camera.position.z = 100 * Math.sin( time );
  74. for ( var i = 0, l = levels.length; i < l; i ++ ) {
  75. mesh = levels[ i ];
  76. mesh.position.x = Math.sin( time * 4 ) * i * i * 0.02;
  77. mesh.position.z = Math.cos( time * 6 ) * i * i * 0.02;
  78. }
  79. renderer.render( scene, camera );
  80. }
  81. </script>
  82. </body>
  83. </html>