webgl_geometry_colors.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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://github.com/mrdoob/three.js" target="_blank">three.js</a> - vertex colors - webgl</div>
  30. <script src="../build/Three.js"></script>
  31. <script src="js/Detector.js"></script>
  32. <script src="js/RequestAnimationFrame.js"></script>
  33. <script src="js/Stats.js"></script>
  34. <script>
  35. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  36. var container, stats;
  37. var camera, scene, renderer;
  38. var mesh, mesh2, mesh3, light;
  39. var mouseX = 0, mouseY = 0;
  40. var windowHalfX = window.innerWidth / 2;
  41. var windowHalfY = window.innerHeight / 2;
  42. init();
  43. animate();
  44. function init() {
  45. container = document.getElementById( 'container' );
  46. camera = new THREE.PerspectiveCamera( 20, window.innerWidth / window.innerHeight, 1, 10000 );
  47. camera.position.z = 1800;
  48. scene = new THREE.Scene();
  49. light = new THREE.DirectionalLight( 0xffffff );
  50. light.position.set( 0, 0, 1 );
  51. light.position.normalize();
  52. scene.add( light );
  53. var shadowMaterial = new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/shadow.png' ) } );
  54. var shadowGeo = new THREE.PlaneGeometry( 300, 300, 1, 1 );
  55. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  56. mesh.position.y = - 250;
  57. mesh.rotation.x = - 90 * Math.PI / 180;
  58. scene.add( mesh );
  59. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  60. mesh.position.y = - 250;
  61. mesh.position.x = - 400;
  62. mesh.rotation.x = - 90 * Math.PI / 180;
  63. scene.add( mesh );
  64. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  65. mesh.position.y = - 250;
  66. mesh.position.x = 400;
  67. mesh.rotation.x = - 90 * Math.PI / 180;
  68. scene.add( mesh );
  69. var faceIndices = [ 'a', 'b', 'c', 'd' ];
  70. var color, f, f2, f3, p, n, vertexIndex,
  71. geometry = new THREE.IcosahedronGeometry( 1 ),
  72. geometry2 = new THREE.IcosahedronGeometry( 1 ),
  73. geometry3 = new THREE.IcosahedronGeometry( 1 );
  74. for ( var i = 0; i < geometry.faces.length; i++ ) {
  75. f = geometry.faces[ i ];
  76. f2 = geometry2.faces[ i ];
  77. f3 = geometry3.faces[ i ];
  78. n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  79. for( var j = 0; j < n; j++ ) {
  80. vertexIndex = f[ faceIndices[ j ] ];
  81. p = geometry.vertices[ vertexIndex ].position;
  82. color = new THREE.Color( 0xffffff );
  83. color.setHSV( ( p.y + 1 ) / 2, 1.0, 1.0 );
  84. f.vertexColors[ j ] = color;
  85. color = new THREE.Color( 0xffffff );
  86. color.setHSV( 0.0, ( p.y + 1 ) / 2, 1.0 );
  87. f2.vertexColors[ j ] = color;
  88. color = new THREE.Color( 0xffffff );
  89. color.setHSV( 0.125 * vertexIndex/geometry.vertices.length, 1.0, 1.0 );
  90. f3.vertexColors[ j ] = color;
  91. }
  92. }
  93. var materials = [
  94. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  95. new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true } )
  96. ];
  97. mesh = new THREE.Mesh( geometry, materials );
  98. mesh.position.x = -400;
  99. mesh.scale.x = mesh.scale.y = mesh.scale.z = 200;
  100. mesh.rotation.x = -1.87;
  101. scene.add( mesh );
  102. mesh2 = new THREE.Mesh( geometry2, materials );
  103. mesh2.position.x = 400;
  104. mesh2.rotation.x = 0;
  105. mesh2.scale = mesh.scale;
  106. scene.add( mesh2 );
  107. mesh3 = new THREE.Mesh( geometry3, materials );
  108. mesh3.position.x = 0;
  109. mesh3.rotation.x = 0;
  110. mesh3.scale = mesh.scale;
  111. scene.add( mesh3 );
  112. renderer = new THREE.WebGLRenderer( { antialias: true } );
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. container.appendChild( renderer.domElement );
  115. stats = new Stats();
  116. stats.domElement.style.position = 'absolute';
  117. stats.domElement.style.top = '0px';
  118. container.appendChild( stats.domElement );
  119. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  120. }
  121. function onDocumentMouseMove( event ) {
  122. mouseX = ( event.clientX - windowHalfX );
  123. mouseY = ( event.clientY - windowHalfY );
  124. }
  125. //
  126. function animate() {
  127. requestAnimationFrame( animate );
  128. render();
  129. stats.update();
  130. }
  131. function render() {
  132. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  133. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  134. camera.lookAt( scene.position );
  135. renderer.render( scene, camera );
  136. }
  137. </script>
  138. </body>
  139. </html>