You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
5.8 KiB

4 years ago
  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="js/three.min.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 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.PlaneBufferGeometry( 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' ];
  80. var color, f, f2, f3, p, 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. for( var j = 0; j < 3; j++ ) {
  90. vertexIndex = f[ faceIndices[ j ] ];
  91. p = geometry.vertices[ vertexIndex ];
  92. color = new THREE.Color( 0xffffff );
  93. color.setHSL( ( p.y / radius + 1 ) / 2, 1.0, 0.5 );
  94. f.vertexColors[ j ] = color;
  95. color = new THREE.Color( 0xffffff );
  96. color.setHSL( 0.0, ( p.y / radius + 1 ) / 2, 0.5 );
  97. f2.vertexColors[ j ] = color;
  98. color = new THREE.Color( 0xffffff );
  99. color.setHSL( 0.125 * vertexIndex/geometry.vertices.length, 1.0, 0.5 );
  100. f3.vertexColors[ j ] = color;
  101. }
  102. }
  103. var materials = [
  104. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  105. new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true, transparent: true } )
  106. ];
  107. group1 = THREE.SceneUtils.createMultiMaterialObject( geometry, materials );
  108. group1.position.x = -400;
  109. group1.rotation.x = -1.87;
  110. scene.add( group1 );
  111. group2 = THREE.SceneUtils.createMultiMaterialObject( geometry2, materials );
  112. group2.position.x = 400;
  113. group2.rotation.x = 0;
  114. scene.add( group2 );
  115. group3 = THREE.SceneUtils.createMultiMaterialObject( geometry3, materials );
  116. group3.position.x = 0;
  117. group3.rotation.x = 0;
  118. scene.add( group3 );
  119. renderer = new THREE.WebGLRenderer( { antialias: true } );
  120. renderer.setClearColor( 0xffffff );
  121. renderer.setPixelRatio( window.devicePixelRatio );
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. container.appendChild( renderer.domElement );
  124. stats = new Stats();
  125. stats.domElement.style.position = 'absolute';
  126. stats.domElement.style.top = '0px';
  127. container.appendChild( stats.domElement );
  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>