webgl_geometry_colors.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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" rel="noopener">three.js</a> webgl - vertex colors</div>
  30. <script src="../build/three.js"></script>
  31. <script src="js/Detector.js"></script>
  32. <script src="js/libs/stats.min.js"></script>
  33. <script>
  34. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  35. var container, stats;
  36. var camera, scene, renderer;
  37. var mouseX = 0, mouseY = 0;
  38. var windowHalfX = window.innerWidth / 2;
  39. var windowHalfY = window.innerHeight / 2;
  40. init();
  41. animate();
  42. function init() {
  43. container = document.getElementById( 'container' );
  44. camera = new THREE.PerspectiveCamera( 20, window.innerWidth / window.innerHeight, 1, 10000 );
  45. camera.position.z = 1800;
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0xffffff );
  48. var 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.PlaneBufferGeometry( 300, 300, 1, 1 );
  65. var shadowMesh;
  66. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  67. shadowMesh.position.y = - 250;
  68. shadowMesh.rotation.x = - Math.PI / 2;
  69. scene.add( shadowMesh );
  70. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  71. shadowMesh.position.y = - 250;
  72. shadowMesh.position.x = - 400;
  73. shadowMesh.rotation.x = - Math.PI / 2;
  74. scene.add( shadowMesh );
  75. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  76. shadowMesh.position.y = - 250;
  77. shadowMesh.position.x = 400;
  78. shadowMesh.rotation.x = - Math.PI / 2;
  79. scene.add( shadowMesh );
  80. var faceIndices = [ 'a', 'b', 'c' ];
  81. var color, f, f2, f3, p, vertexIndex,
  82. radius = 200,
  83. geometry = new THREE.IcosahedronGeometry( radius, 1 ),
  84. geometry2 = new THREE.IcosahedronGeometry( radius, 1 ),
  85. geometry3 = new THREE.IcosahedronGeometry( radius, 1 );
  86. for ( var i = 0; i < geometry.faces.length; i ++ ) {
  87. f = geometry.faces[ i ];
  88. f2 = geometry2.faces[ i ];
  89. f3 = geometry3.faces[ i ];
  90. for ( var j = 0; j < 3; j ++ ) {
  91. vertexIndex = f[ faceIndices[ j ] ];
  92. p = geometry.vertices[ vertexIndex ];
  93. color = new THREE.Color( 0xffffff );
  94. color.setHSL( ( p.y / radius + 1 ) / 2, 1.0, 0.5 );
  95. f.vertexColors[ j ] = color;
  96. color = new THREE.Color( 0xffffff );
  97. color.setHSL( 0.0, ( p.y / radius + 1 ) / 2, 0.5 );
  98. f2.vertexColors[ j ] = color;
  99. color = new THREE.Color( 0xffffff );
  100. color.setHSL( 0.125 * vertexIndex / geometry.vertices.length, 1.0, 0.5 );
  101. f3.vertexColors[ j ] = color;
  102. }
  103. }
  104. var mesh, wireframe;
  105. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true, vertexColors: THREE.VertexColors, shininess: 0 } );
  106. var wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  107. mesh = new THREE.Mesh( geometry, material );
  108. wireframe = new THREE.Mesh( geometry, wireframeMaterial );
  109. mesh.add( wireframe );
  110. mesh.position.x = - 400;
  111. mesh.rotation.x = - 1.87;
  112. scene.add( mesh );
  113. mesh = new THREE.Mesh( geometry2, material );
  114. wireframe = new THREE.Mesh( geometry2, wireframeMaterial );
  115. mesh.add( wireframe );
  116. mesh.position.x = 400;
  117. scene.add( mesh );
  118. mesh = new THREE.Mesh( geometry3, material );
  119. wireframe = new THREE.Mesh( geometry3, wireframeMaterial );
  120. mesh.add( wireframe );
  121. scene.add( mesh );
  122. renderer = new THREE.WebGLRenderer( { antialias: true } );
  123. renderer.setPixelRatio( window.devicePixelRatio );
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. container.appendChild( renderer.domElement );
  126. stats = new Stats();
  127. container.appendChild( stats.dom );
  128. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  129. //
  130. window.addEventListener( 'resize', onWindowResize, false );
  131. }
  132. function onWindowResize() {
  133. windowHalfX = window.innerWidth / 2;
  134. windowHalfY = window.innerHeight / 2;
  135. camera.aspect = window.innerWidth / window.innerHeight;
  136. camera.updateProjectionMatrix();
  137. renderer.setSize( window.innerWidth, window.innerHeight );
  138. }
  139. function onDocumentMouseMove( event ) {
  140. mouseX = ( event.clientX - windowHalfX );
  141. mouseY = ( event.clientY - windowHalfY );
  142. }
  143. //
  144. function animate() {
  145. requestAnimationFrame( animate );
  146. render();
  147. stats.update();
  148. }
  149. function render() {
  150. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  151. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  152. camera.lookAt( scene.position );
  153. renderer.render( scene, camera );
  154. }
  155. </script>
  156. </body>
  157. </html>