webgl_materials_grass.html 3.1 KB

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