webgl_materials_grass.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - materials - grass - webgl</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" src="../src/extras/Detector.js"></script>
  22. <script type="text/javascript">
  23. if ( ! THREE.Detector.webgl ) THREE.Detector.addGetWebGLMessage();
  24. var camera, scene, renderer,
  25. mesh, levels = [];
  26. init();
  27. setInterval( loop, 1000 / 60 );
  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 Plane( 100, 100 );
  34. var texture = generateTextureBase();
  35. for ( var i = 0; i < 10; i ++ ) {
  36. mesh = levels[ i ] = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { map: new THREE.Texture( generateTextureLevel( texture ), new THREE.UVMapping(), THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping ) } ) );
  37. mesh.rotation.x = - 90 * ( Math.PI / 180 );
  38. mesh.position.y = i * 0.5;
  39. scene.addObject( mesh );
  40. }
  41. renderer = new THREE.WebGLRenderer();
  42. renderer.sortObjects = false;
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. document.body.appendChild( renderer.domElement );
  45. }
  46. function generateTextureBase() {
  47. var canvas = document.createElement( 'canvas' );
  48. canvas.loaded = true;
  49. canvas.width = 1024;
  50. canvas.height = 1024;
  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() * 3 + 1, 0, Math.PI * 2, true );
  56. context.closePath();
  57. context.fill();
  58. }
  59. context.globalAlpha = 0.1;
  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.loaded = true;
  67. canvas.width = texture.width;
  68. canvas.height = texture.height;
  69. canvas.getContext( '2d' ).drawImage( texture, 0, 0 );
  70. return canvas;
  71. }
  72. function loop() {
  73. var time = new Date().getTime() / 6000;
  74. camera.position.x = 100 * Math.cos( time );
  75. camera.position.z = 100 * Math.sin( time );
  76. for ( var i = 0, l = levels.length; i < l; i ++ ) {
  77. mesh = levels[ i ];
  78. mesh.position.x = Math.sin( time * 4 ) * i * i * 0.02;
  79. mesh.position.z = Math.cos( time * 6 ) * i * i * 0.02;
  80. }
  81. renderer.render( scene, camera );
  82. }
  83. </script>
  84. </body>
  85. </html>