canvas_materials.html 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.min.js"></script>
  18. <script src="js/libs/stats.min.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. // Grid
  32. var size = 500, step = 100;
  33. var geometry = new THREE.Geometry();
  34. for ( var i = - size; i <= size; i += step ) {
  35. geometry.vertices.push( new THREE.Vector3( - size, - 120, i ) );
  36. geometry.vertices.push( new THREE.Vector3( size, - 120, i ) );
  37. geometry.vertices.push( new THREE.Vector3( i, - 120, - size ) );
  38. geometry.vertices.push( new THREE.Vector3( i, - 120, size ) );
  39. }
  40. var material = new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.2 } );
  41. var line = new THREE.Line( geometry, material );
  42. line.type = THREE.LinePieces;
  43. scene.add( line );
  44. // Spheres
  45. var geometry = new THREE.SphereGeometry( 100, 14, 7 );
  46. var materials = [
  47. new THREE.MeshBasicMaterial( { color: 0x00ffff, wireframe: true, side: THREE.DoubleSide } ),
  48. new THREE.MeshBasicMaterial( { color: 0xff0000, blending: THREE.AdditiveBlending } ),
  49. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, overdraw: 0.5 } ),
  50. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.SmoothShading, overdraw: 0.5 } ),
  51. new THREE.MeshDepthMaterial( { overdraw: 0.5 } ),
  52. new THREE.MeshNormalMaterial( { overdraw: 0.5 } ),
  53. new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/land_ocean_ice_cloud_2048.jpg' ) } ),
  54. new THREE.MeshBasicMaterial( { envMap: THREE.ImageUtils.loadTexture( 'textures/envmap.png', new THREE.SphericalReflectionMapping() ), overdraw: 0.5 } ),
  55. new THREE.MeshBasicMaterial( { envMap: THREE.ImageUtils.loadTexture( 'textures/envmap.png', new THREE.SphericalRefractionMapping() ), overdraw: 0.5 } )
  56. ];
  57. for ( var i = 0, l = geometry.faces.length; i < l; i ++ ) {
  58. var face = geometry.faces[ i ];
  59. if ( Math.random() > 0.5 ) face.materialIndex = Math.floor( Math.random() * materials.length );
  60. }
  61. materials.push( new THREE.MeshFaceMaterial( materials ) );
  62. objects = [];
  63. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  64. var sphere = new THREE.Mesh( geometry, materials[ i ] );
  65. sphere.position.x = ( i % 5 ) * 200 - 400;
  66. sphere.position.z = Math.floor( i / 5 ) * 200 - 200;
  67. sphere.rotation.x = Math.random() * 200 - 100;
  68. sphere.rotation.y = Math.random() * 200 - 100;
  69. sphere.rotation.z = Math.random() * 200 - 100;
  70. objects.push( sphere );
  71. scene.add( sphere );
  72. }
  73. var PI2 = Math.PI * 2;
  74. var program = function ( context ) {
  75. context.beginPath();
  76. context.arc( 0, 0, 0.5, 0, PI2, true );
  77. context.fill();
  78. }
  79. particleLight = new THREE.Sprite( new THREE.SpriteCanvasMaterial( { color: 0xffffff, program: program } ) );
  80. particleLight.scale.x = particleLight.scale.y = 8;
  81. scene.add( particleLight );
  82. // Lights
  83. scene.add( new THREE.AmbientLight( Math.random() * 0x202020 ) );
  84. var directionalLight = new THREE.DirectionalLight( Math.random() * 0xffffff );
  85. directionalLight.position.x = Math.random() - 0.5;
  86. directionalLight.position.y = Math.random() - 0.5;
  87. directionalLight.position.z = Math.random() - 0.5;
  88. directionalLight.position.normalize();
  89. scene.add( directionalLight );
  90. pointLight = new THREE.PointLight( 0xffffff, 1 );
  91. scene.add( pointLight );
  92. renderer = new THREE.CanvasRenderer();
  93. renderer.setSize( window.innerWidth, window.innerHeight );
  94. container.appendChild( renderer.domElement );
  95. var debugCanvas = document.createElement( 'canvas' );
  96. debugCanvas.width = 512;
  97. debugCanvas.height = 512;
  98. debugCanvas.style.position = 'absolute';
  99. debugCanvas.style.top = '0px';
  100. debugCanvas.style.left = '0px';
  101. container.appendChild( debugCanvas );
  102. debugContext = debugCanvas.getContext( '2d' );
  103. debugContext.setTransform( 1, 0, 0, 1, 256, 256 );
  104. debugContext.strokeStyle = '#000000';
  105. stats = new Stats();
  106. stats.domElement.style.position = 'absolute';
  107. stats.domElement.style.top = '0px';
  108. container.appendChild(stats.domElement);
  109. //
  110. window.addEventListener( 'resize', onWindowResize, false );
  111. }
  112. function onWindowResize() {
  113. camera.aspect = window.innerWidth / window.innerHeight;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  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>