webgl_geometry_colors.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - vertex colors</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. color: #808080;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #fff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. a {
  23. color: #0080ff;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="container"></div>
  29. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> webgl - vertex colors</div>
  30. <script src="../build/three.min.js"></script>
  31. <script src="js/Detector.js"></script>
  32. <script src="js/Stats.js"></script>
  33. <script>
  34. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  35. var container, stats;
  36. var camera, scene, renderer;
  37. var mesh, group1, group2, group3, light;
  38. var mouseX = 0, mouseY = 0;
  39. var windowHalfX = window.innerWidth / 2;
  40. var windowHalfY = window.innerHeight / 2;
  41. init();
  42. animate();
  43. function init() {
  44. container = document.getElementById( 'container' );
  45. camera = new THREE.PerspectiveCamera( 20, window.innerWidth / window.innerHeight, 1, 10000 );
  46. camera.position.z = 1800;
  47. scene = new THREE.Scene();
  48. light = new THREE.DirectionalLight( 0xffffff );
  49. light.position.set( 0, 0, 1 );
  50. scene.add( light );
  51. // shadow
  52. var canvas = document.createElement( 'canvas' );
  53. canvas.width = 128;
  54. canvas.height = 128;
  55. var context = canvas.getContext( '2d' );
  56. var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  57. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  58. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  59. context.fillStyle = gradient;
  60. context.fillRect( 0, 0, canvas.width, canvas.height );
  61. var shadowTexture = new THREE.Texture( canvas );
  62. shadowTexture.needsUpdate = true;
  63. var shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
  64. var shadowGeo = new THREE.PlaneGeometry( 300, 300, 1, 1 );
  65. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  66. mesh.position.y = - 250;
  67. mesh.rotation.x = - Math.PI / 2;
  68. scene.add( mesh );
  69. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  70. mesh.position.y = - 250;
  71. mesh.position.x = - 400;
  72. mesh.rotation.x = - Math.PI / 2;
  73. scene.add( mesh );
  74. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  75. mesh.position.y = - 250;
  76. mesh.position.x = 400;
  77. mesh.rotation.x = - Math.PI / 2;
  78. scene.add( mesh );
  79. var faceIndices = [ 'a', 'b', 'c', 'd' ];
  80. var color, f, f2, f3, p, n, vertexIndex,
  81. radius = 200,
  82. geometry = new THREE.IcosahedronGeometry( radius, 1 ),
  83. geometry2 = new THREE.IcosahedronGeometry( radius, 1 ),
  84. geometry3 = new THREE.IcosahedronGeometry( radius, 1 );
  85. for ( var i = 0; i < geometry.faces.length; i ++ ) {
  86. f = geometry.faces[ i ];
  87. f2 = geometry2.faces[ i ];
  88. f3 = geometry3.faces[ i ];
  89. n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  90. for( var j = 0; j < n; j++ ) {
  91. vertexIndex = f[ faceIndices[ j ] ];
  92. p = geometry.vertices[ vertexIndex ];
  93. color = new THREE.Color( 0xffffff );
  94. color.setHSV( ( p.y / radius + 1 ) / 2, 1.0, 1.0 );
  95. f.vertexColors[ j ] = color;
  96. color = new THREE.Color( 0xffffff );
  97. color.setHSV( 0.0, ( p.y / radius + 1 ) / 2, 1.0 );
  98. f2.vertexColors[ j ] = color;
  99. color = new THREE.Color( 0xffffff );
  100. color.setHSV( 0.125 * vertexIndex/geometry.vertices.length, 1.0, 1.0 );
  101. f3.vertexColors[ j ] = color;
  102. }
  103. }
  104. var materials = [
  105. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  106. new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true, transparent: true } )
  107. ];
  108. group1 = THREE.SceneUtils.createMultiMaterialObject( geometry, materials );
  109. group1.position.x = -400;
  110. group1.rotation.x = -1.87;
  111. scene.add( group1 );
  112. group2 = THREE.SceneUtils.createMultiMaterialObject( geometry2, materials );
  113. group2.position.x = 400;
  114. group2.rotation.x = 0;
  115. scene.add( group2 );
  116. group3 = THREE.SceneUtils.createMultiMaterialObject( geometry3, materials );
  117. group3.position.x = 0;
  118. group3.rotation.x = 0;
  119. scene.add( group3 );
  120. renderer = new THREE.WebGLRenderer( { antialias: true } );
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. container.appendChild( renderer.domElement );
  123. stats = new Stats();
  124. stats.domElement.style.position = 'absolute';
  125. stats.domElement.style.top = '0px';
  126. container.appendChild( stats.domElement );
  127. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  128. //
  129. window.addEventListener( 'resize', onWindowResize, false );
  130. }
  131. function onWindowResize() {
  132. windowHalfX = window.innerWidth / 2;
  133. windowHalfY = window.innerHeight / 2;
  134. camera.aspect = window.innerWidth / window.innerHeight;
  135. camera.updateProjectionMatrix();
  136. renderer.setSize( window.innerWidth, window.innerHeight );
  137. }
  138. function onDocumentMouseMove( event ) {
  139. mouseX = ( event.clientX - windowHalfX );
  140. mouseY = ( event.clientY - windowHalfY );
  141. }
  142. //
  143. function animate() {
  144. requestAnimationFrame( animate );
  145. render();
  146. stats.update();
  147. }
  148. function render() {
  149. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  150. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  151. camera.lookAt( scene.position );
  152. renderer.render( scene, camera );
  153. }
  154. </script>
  155. </body>
  156. </html>