materials.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - geometry - materials</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. font-family: Monospace;
  9. background-color: #202020;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <script type="text/javascript" src="../build/Three.js"></script>
  17. <script type="text/javascript" src="../src/extras/primitives/Sphere.js"></script>
  18. <script type="text/javascript" src="../src/extras/primitives/Plane.js"></script>
  19. <script type="text/javascript" src="js/Stats.js"></script>
  20. <script type="text/javascript">
  21. var container, stats;
  22. var camera, scene, renderer, objects;
  23. var particleLight, pointLight;
  24. init();
  25. setInterval( loop, 1000 / 60 );
  26. function init() {
  27. container = document.createElement('div');
  28. document.body.appendChild(container);
  29. camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  30. camera.position.y = 200;
  31. camera.position.z = 800;
  32. scene = new THREE.Scene();
  33. // Grid
  34. var geometry = new THREE.Geometry();
  35. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - 500, 0, 0 ) ) );
  36. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 500, 0, 0 ) ) );
  37. for ( var i = 0; i <= 10; i ++ ) {
  38. var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.2 } ) );
  39. line.position.y = - 120;
  40. line.position.z = ( i * 100 ) - 500;
  41. scene.addObject( line );
  42. var line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.2 } ) );
  43. line.position.x = ( i * 100 ) - 500;
  44. line.position.y = - 120;
  45. line.rotation.y = 90 * Math.PI / 180;
  46. scene.addObject( line );
  47. }
  48. // Spheres
  49. var geometry = new Sphere( 100, 16, 8 );
  50. var materials = [];
  51. materials.push( new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, blending: THREE.AdditiveBlending } ) );
  52. materials.push( new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: true } ) );
  53. materials.push( new THREE.MeshLambertMaterial( { color: 0xffffff } ) );
  54. materials.push( new THREE.MeshDepthMaterial( { near: 1, far: 2000 } ) );
  55. materials.push( new THREE.MeshNormalMaterial( ) );
  56. materials.push( new THREE.MeshBasicMaterial( { map: new THREE.Texture( generateTexture() ) } ) );
  57. objects = [];
  58. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  59. var sphere = new THREE.Mesh( geometry, materials[ i ] );
  60. sphere.overdraw = true;
  61. sphere.position.x = ( i % 5 ) * 200 - 400;
  62. sphere.position.z = Math.floor( i / 5 ) * 200 - 200;
  63. sphere.rotation.x = Math.random() * 200 - 100;
  64. sphere.rotation.y = Math.random() * 200 - 100;
  65. sphere.rotation.z = Math.random() * 200 - 100;
  66. objects.push( sphere );
  67. scene.addObject( sphere );
  68. }
  69. particleLight = new THREE.Particle( new THREE.ParticleCircleMaterial( { color: 0xff0000 } ) );
  70. particleLight.scale.x = particleLight.scale.y = particleLight.scale.z = 5;
  71. scene.addObject( particleLight );
  72. // Lights
  73. scene.addLight( new THREE.AmbientLight( Math.random() * 0x202020 ) );
  74. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  75. directionalLight.position.x = Math.random() - 0.5;
  76. directionalLight.position.y = Math.random() - 0.5;
  77. directionalLight.position.z = Math.random() - 0.5;
  78. directionalLight.position.normalize();
  79. scene.addLight( directionalLight );
  80. pointLight = new THREE.PointLight( 0xff0000, 1 );
  81. scene.addLight( pointLight );
  82. renderer = new THREE.CanvasRenderer();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. container.appendChild( renderer.domElement );
  85. var debugCanvas = document.createElement( 'canvas' );
  86. debugCanvas.width = 512;
  87. debugCanvas.height = 512;
  88. debugCanvas.style.position = 'absolute';
  89. debugCanvas.style.top = '0px';
  90. debugCanvas.style.left = '0px';
  91. container.appendChild( debugCanvas );
  92. debugContext = debugCanvas.getContext( '2d' );
  93. debugContext.setTransform( 1, 0, 0, 1, 256, 256 );
  94. debugContext.strokeStyle = '#000000';
  95. stats = new Stats();
  96. stats.domElement.style.position = 'absolute';
  97. stats.domElement.style.top = '0px';
  98. container.appendChild(stats.domElement);
  99. }
  100. function generateTexture() {
  101. var canvas = document.createElement( 'canvas' );
  102. canvas.width = 256;
  103. canvas.height = 256;
  104. var context = canvas.getContext( '2d' );
  105. var image = context.getImageData( 0, 0, 256, 256 );
  106. var x = 0, y = 0;
  107. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  108. x = j % 256;
  109. y = x == 0 ? y + 1 : y;
  110. image.data[ i + 2 ] = Math.floor( x ^ y );
  111. image.data[ i + 3 ] = 255;
  112. }
  113. context.putImageData( image, 0, 0 );
  114. return canvas;
  115. }
  116. //
  117. function loop() {
  118. var timer = new Date().getTime() * 0.0001;
  119. camera.position.x = Math.cos( timer ) * 1000;
  120. camera.position.z = Math.sin( timer ) * 1000;
  121. for ( var i = 0, l = objects.length; i < l; i++ ) {
  122. var object = objects[ i ];
  123. object.rotation.x += 0.01;
  124. object.rotation.y += 0.005;
  125. }
  126. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  127. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  128. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  129. pointLight.position.x = particleLight.position.x;
  130. pointLight.position.y = particleLight.position.y;
  131. pointLight.position.z = particleLight.position.z;
  132. renderer.render( scene, camera );
  133. stats.update();
  134. }
  135. </script>
  136. </body>
  137. </html>