canvas_materials.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/ImageUtils.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. // loop();
  26. setInterval( loop, 1000 / 60 );
  27. function init() {
  28. container = document.createElement('div');
  29. document.body.appendChild(container);
  30. camera = new THREE.Camera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  31. camera.position.y = 200;
  32. camera.position.z = 800;
  33. scene = new THREE.Scene();
  34. // Grid
  35. var geometry = new THREE.Geometry();
  36. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - 500, 0, 0 ) ) );
  37. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 500, 0, 0 ) ) );
  38. var material = new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.2 } );
  39. for ( var i = 0; i <= 10; i ++ ) {
  40. var line = new THREE.Line( geometry, material );
  41. line.position.y = - 120;
  42. line.position.z = ( i * 100 ) - 500;
  43. scene.addObject( line );
  44. var line = new THREE.Line( geometry, material );
  45. line.position.x = ( i * 100 ) - 500;
  46. line.position.y = - 120;
  47. line.rotation.y = 90 * Math.PI / 180;
  48. scene.addObject( line );
  49. }
  50. // Spheres
  51. var geometry = new Sphere( 100, 14, 7, false );
  52. var materials = [
  53. { material: new THREE.MeshBasicMaterial( { color: 0x00ffff, wireframe: true } ), overdraw: false, doubleSided: true },
  54. { material: new THREE.MeshBasicMaterial( { color: 0xff0000, blending: THREE.AdditiveBlending } ), overdraw: false, doubleSided: true },
  55. { material: new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading } ), overdraw: true, doubleSided: false },
  56. { material: new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.SmoothShading } ), overdraw: true, doubleSided: false },
  57. { material: new THREE.MeshDepthMaterial(), overdraw: true, doubleSided: false },
  58. { material: new THREE.MeshNormalMaterial(), overdraw: true, doubleSided: false },
  59. { material: new THREE.MeshBasicMaterial( { map: ImageUtils.loadTexture( 'textures/land_ocean_ice_cloud_2048.jpg' ) } ), overdraw: false, doubleSided: false },
  60. { material: new THREE.MeshLambertMaterial( { map: ImageUtils.loadTexture( 'textures/land_ocean_ice_cloud_2048.jpg' ) } ), overdraw: false, doubleSided: false },
  61. { material: new THREE.MeshBasicMaterial( { env_map: ImageUtils.loadTexture( 'textures/envmap.png', new THREE.SphericalReflectionMapping() ) } ), overdraw: false, doubleSided: false }
  62. ];
  63. for ( var i = 0, l = geometry.faces.length; i < l; i ++ ) {
  64. var face = geometry.faces[ i ];
  65. if ( Math.random() > 0.7 ) face.material = [ materials[ Math.floor( Math.random() * materials.length ) ].material ];
  66. }
  67. materials.push( { material: new THREE.MeshFaceMaterial(), overdraw: false, doubleSided: true } );
  68. objects = [];
  69. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  70. var sphere = new THREE.Mesh( geometry, materials[ i ].material );
  71. sphere.overdraw = materials[ i ].overdraw;
  72. sphere.doubleSided = materials[ i ].doubleSided;
  73. sphere.position.x = ( i % 5 ) * 200 - 400;
  74. sphere.position.z = Math.floor( i / 5 ) * 200 - 200;
  75. sphere.rotation.x = Math.random() * 200 - 100;
  76. sphere.rotation.y = Math.random() * 200 - 100;
  77. sphere.rotation.z = Math.random() * 200 - 100;
  78. objects.push( sphere );
  79. scene.addObject( sphere );
  80. }
  81. particleLight = new THREE.Particle( new THREE.ParticleCircleMaterial( { color: 0xffffff } ) );
  82. particleLight.scale.x = particleLight.scale.y = particleLight.scale.z = 4;
  83. scene.addObject( particleLight );
  84. // Lights
  85. scene.addLight( new THREE.AmbientLight( Math.random() * 0x202020 ) );
  86. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  87. directionalLight.position.x = Math.random() - 0.5;
  88. directionalLight.position.y = Math.random() - 0.5;
  89. directionalLight.position.z = Math.random() - 0.5;
  90. directionalLight.position.normalize();
  91. scene.addLight( directionalLight );
  92. pointLight = new THREE.PointLight( 0xffffff, 1 );
  93. scene.addLight( pointLight );
  94. renderer = new THREE.CanvasRenderer();
  95. // renderer = new THREE.WebGLRenderer();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. container.appendChild( renderer.domElement );
  98. var debugCanvas = document.createElement( 'canvas' );
  99. debugCanvas.width = 512;
  100. debugCanvas.height = 512;
  101. debugCanvas.style.position = 'absolute';
  102. debugCanvas.style.top = '0px';
  103. debugCanvas.style.left = '0px';
  104. container.appendChild( debugCanvas );
  105. debugContext = debugCanvas.getContext( '2d' );
  106. debugContext.setTransform( 1, 0, 0, 1, 256, 256 );
  107. debugContext.strokeStyle = '#000000';
  108. stats = new Stats();
  109. stats.domElement.style.position = 'absolute';
  110. stats.domElement.style.top = '0px';
  111. container.appendChild(stats.domElement);
  112. }
  113. function loadImage( path ) {
  114. var image = document.createElement( 'img' );
  115. var texture = new THREE.Texture( image, THREE.UVMapping )
  116. image.onload = function () { texture.loaded = true; };
  117. image.src = path;
  118. return texture;
  119. }
  120. //
  121. function loop() {
  122. var timer = new Date().getTime() * 0.0001;
  123. camera.position.x = Math.cos( timer ) * 1000;
  124. camera.position.z = Math.sin( timer ) * 1000;
  125. for ( var i = 0, l = objects.length; i < l; i++ ) {
  126. var object = objects[ i ];
  127. object.rotation.x += 0.01;
  128. object.rotation.y += 0.005;
  129. }
  130. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  131. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  132. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  133. pointLight.position.x = particleLight.position.x;
  134. pointLight.position.y = particleLight.position.y;
  135. pointLight.position.z = particleLight.position.z;
  136. renderer.render( scene, camera );
  137. stats.update();
  138. }
  139. </script>
  140. </body>
  141. </html>