materials.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - 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, 14, 8 );
  50. var materials = [];
  51. materials.push( { material: new THREE.MeshBasicMaterial( { color: 0x00ffff, wireframe: true } ), overdraw: false, doubleSided: true } );
  52. materials.push( { material: new THREE.MeshBasicMaterial( { color: 0xff0000, blending: THREE.AdditiveBlending } ), overdraw: false, doubleSided: true } );
  53. materials.push( { material: new THREE.MeshLambertMaterial( { color: 0xffffff } ), overdraw: true, doubleSided: false } );
  54. materials.push( { material: new THREE.MeshPhongMaterial( { ambient: 0x030383, color: 0xf55555, specular: 0x66f6f6, shininess: 10 } ), overdraw: true, doubleSided: false } );
  55. materials.push( { material: new THREE.MeshDepthMaterial( { near: 1, far: 2000 } ), overdraw: true, doubleSided: false } );
  56. materials.push( { material: new THREE.MeshNormalMaterial(), overdraw: true, doubleSided: false } );
  57. materials.push( { material: new THREE.MeshBasicMaterial( { map: new THREE.Texture( generateTexture() ) } ), overdraw: true, doubleSided: false } );
  58. objects = [];
  59. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  60. var sphere = new THREE.Mesh( geometry, materials[ i ].material );
  61. sphere.overdraw = materials[ i ].overdraw;
  62. sphere.doubleSided = materials[ i ].doubleSided;
  63. sphere.position.x = ( i % 5 ) * 200 - 400;
  64. sphere.position.z = Math.floor( i / 5 ) * 200 - 200;
  65. sphere.rotation.x = Math.random() * 200 - 100;
  66. sphere.rotation.y = Math.random() * 200 - 100;
  67. sphere.rotation.z = Math.random() * 200 - 100;
  68. objects.push( sphere );
  69. scene.addObject( sphere );
  70. }
  71. particleLight = new THREE.Particle( new THREE.ParticleCircleMaterial( { color: 0xffffff } ) );
  72. particleLight.scale.x = particleLight.scale.y = particleLight.scale.z = 4;
  73. scene.addObject( particleLight );
  74. // Lights
  75. scene.addLight( new THREE.AmbientLight( Math.random() * 0x202020 ) );
  76. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  77. directionalLight.position.x = Math.random() - 0.5;
  78. directionalLight.position.y = Math.random() - 0.5;
  79. directionalLight.position.z = Math.random() - 0.5;
  80. directionalLight.position.normalize();
  81. scene.addLight( directionalLight );
  82. pointLight = new THREE.PointLight( 0xffffff, 1 );
  83. scene.addLight( pointLight );
  84. renderer = new THREE.CanvasRenderer();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. container.appendChild( renderer.domElement );
  87. var debugCanvas = document.createElement( 'canvas' );
  88. debugCanvas.width = 512;
  89. debugCanvas.height = 512;
  90. debugCanvas.style.position = 'absolute';
  91. debugCanvas.style.top = '0px';
  92. debugCanvas.style.left = '0px';
  93. container.appendChild( debugCanvas );
  94. debugContext = debugCanvas.getContext( '2d' );
  95. debugContext.setTransform( 1, 0, 0, 1, 256, 256 );
  96. debugContext.strokeStyle = '#000000';
  97. stats = new Stats();
  98. stats.domElement.style.position = 'absolute';
  99. stats.domElement.style.top = '0px';
  100. container.appendChild(stats.domElement);
  101. }
  102. function generateTexture() {
  103. var canvas = document.createElement( 'canvas' );
  104. canvas.width = 256;
  105. canvas.height = 256;
  106. var context = canvas.getContext( '2d' );
  107. var image = context.getImageData( 0, 0, 256, 256 );
  108. var x = 0, y = 0;
  109. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  110. x = j % 256;
  111. y = x == 0 ? y + 1 : y;
  112. image.data[ i + 2 ] = Math.floor( x ^ y );
  113. image.data[ i + 3 ] = 255;
  114. }
  115. context.putImageData( image, 0, 0 );
  116. return canvas;
  117. }
  118. //
  119. function loop() {
  120. var timer = new Date().getTime() * 0.0001;
  121. camera.position.x = Math.cos( timer ) * 1000;
  122. camera.position.z = Math.sin( timer ) * 1000;
  123. for ( var i = 0, l = objects.length; i < l; i++ ) {
  124. var object = objects[ i ];
  125. object.rotation.x += 0.01;
  126. object.rotation.y += 0.005;
  127. }
  128. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  129. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  130. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  131. pointLight.position.x = particleLight.position.x;
  132. pointLight.position.y = particleLight.position.y;
  133. pointLight.position.z = particleLight.position.z;
  134. renderer.render( scene, camera );
  135. stats.update();
  136. }
  137. </script>
  138. </body>
  139. </html>
粤ICP备19079148号