canvas_materials.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - materials</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #202020;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/Three.js"></script>
  18. <script src="js/Stats.js"></script>
  19. <script>
  20. var container, stats;
  21. var camera, scene, renderer, objects;
  22. var particleLight, pointLight;
  23. init();
  24. animate();
  25. function init() {
  26. container = document.createElement('div');
  27. document.body.appendChild(container);
  28. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  29. camera.position.set( 0, 200, 800 );
  30. scene = new THREE.Scene();
  31. scene.add( camera );
  32. // Grid
  33. var geometry = new THREE.Geometry();
  34. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - 500, 0, 0 ) ) );
  35. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 500, 0, 0 ) ) );
  36. var material = new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.2 } );
  37. for ( var i = 0; i <= 10; i ++ ) {
  38. var line = new THREE.Line( geometry, material );
  39. line.position.y = - 120;
  40. line.position.z = ( i * 100 ) - 500;
  41. scene.add( line );
  42. var line = new THREE.Line( geometry, material );
  43. line.position.x = ( i * 100 ) - 500;
  44. line.position.y = - 120;
  45. line.rotation.y = 90 * Math.PI / 180;
  46. scene.add( line );
  47. }
  48. // Spheres
  49. var geometry = new THREE.SphereGeometry( 100, 14, 7, false );
  50. var materials = [
  51. { material: new THREE.MeshBasicMaterial( { color: 0x00ffff, wireframe: true } ), doubleSided: true },
  52. { material: new THREE.MeshBasicMaterial( { color: 0xff0000, blending: THREE.AdditiveBlending } ), doubleSided: true },
  53. { material: new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, overdraw: true } ), doubleSided: false },
  54. { material: new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.SmoothShading, overdraw: true } ), doubleSided: false },
  55. { material: new THREE.MeshDepthMaterial( { overdraw: true } ), doubleSided: false },
  56. { material: new THREE.MeshNormalMaterial( { overdraw: true } ), doubleSided: false },
  57. { material: new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/land_ocean_ice_cloud_2048.jpg' ) } ), doubleSided: false },
  58. { material: new THREE.MeshLambertMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/land_ocean_ice_cloud_2048.jpg' ) } ), doubleSided: false },
  59. { material: new THREE.MeshBasicMaterial( { envMap: THREE.ImageUtils.loadTexture( 'textures/envmap.png', new THREE.SphericalReflectionMapping() ) } ), doubleSided: false }
  60. ];
  61. for ( var i = 0, l = geometry.faces.length; i < l; i ++ ) {
  62. var face = geometry.faces[ i ];
  63. if ( Math.random() > 0.7 ) face.material = [ materials[ Math.floor( Math.random() * materials.length ) ].material ];
  64. }
  65. materials.push( { material: new THREE.MeshFaceMaterial(), overdraw: false, doubleSided: true } );
  66. objects = [];
  67. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  68. var sphere = new THREE.Mesh( geometry, materials[ i ].material );
  69. sphere.doubleSided = materials[ i ].doubleSided;
  70. sphere.position.x = ( i % 5 ) * 200 - 400;
  71. sphere.position.z = Math.floor( i / 5 ) * 200 - 200;
  72. sphere.rotation.x = Math.random() * 200 - 100;
  73. sphere.rotation.y = Math.random() * 200 - 100;
  74. sphere.rotation.z = Math.random() * 200 - 100;
  75. objects.push( sphere );
  76. scene.add( sphere );
  77. }
  78. var PI2 = Math.PI * 2;
  79. var program = function ( context ) {
  80. context.beginPath();
  81. context.arc( 0, 0, 1, 0, PI2, true );
  82. context.closePath();
  83. context.fill();
  84. }
  85. particleLight = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: 0xffffff, program: program } ) );
  86. particleLight.scale.x = particleLight.scale.y = particleLight.scale.z = 4;
  87. scene.add( particleLight );
  88. // Lights
  89. scene.add( new THREE.AmbientLight( Math.random() * 0x202020 ) );
  90. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  91. directionalLight.position.x = Math.random() - 0.5;
  92. directionalLight.position.y = Math.random() - 0.5;
  93. directionalLight.position.z = Math.random() - 0.5;
  94. directionalLight.position.normalize();
  95. scene.add( directionalLight );
  96. pointLight = new THREE.PointLight( 0xffffff, 1 );
  97. scene.add( pointLight );
  98. renderer = new THREE.CanvasRenderer();
  99. // renderer = new THREE.WebGLRenderer();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. container.appendChild( renderer.domElement );
  102. var debugCanvas = document.createElement( 'canvas' );
  103. debugCanvas.width = 512;
  104. debugCanvas.height = 512;
  105. debugCanvas.style.position = 'absolute';
  106. debugCanvas.style.top = '0px';
  107. debugCanvas.style.left = '0px';
  108. container.appendChild( debugCanvas );
  109. debugContext = debugCanvas.getContext( '2d' );
  110. debugContext.setTransform( 1, 0, 0, 1, 256, 256 );
  111. debugContext.strokeStyle = '#000000';
  112. stats = new Stats();
  113. stats.domElement.style.position = 'absolute';
  114. stats.domElement.style.top = '0px';
  115. container.appendChild(stats.domElement);
  116. }
  117. function loadImage( path ) {
  118. var image = document.createElement( 'img' );
  119. var texture = new THREE.Texture( image, THREE.UVMapping )
  120. image.onload = function () { texture.needsUpdate = true; };
  121. image.src = path;
  122. return texture;
  123. }
  124. //
  125. function animate() {
  126. requestAnimationFrame( animate );
  127. render();
  128. stats.update();
  129. }
  130. function render() {
  131. var timer = Date.now() * 0.0001;
  132. camera.position.x = Math.cos( timer ) * 1000;
  133. camera.position.z = Math.sin( timer ) * 1000;
  134. camera.lookAt( scene.position );
  135. for ( var i = 0, l = objects.length; i < l; i++ ) {
  136. var object = objects[ i ];
  137. object.rotation.x += 0.01;
  138. object.rotation.y += 0.005;
  139. }
  140. particleLight.position.x = Math.sin( timer * 7 ) * 300;
  141. particleLight.position.y = Math.cos( timer * 5 ) * 400;
  142. particleLight.position.z = Math.cos( timer * 3 ) * 300;
  143. pointLight.position.x = particleLight.position.x;
  144. pointLight.position.y = particleLight.position.y;
  145. pointLight.position.z = particleLight.position.z;
  146. renderer.render( scene, camera );
  147. }
  148. </script>
  149. </body>
  150. </html>