webgl_geometry_colors.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - vertex colors</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. color: #808080;
  9. font-family:Monospace;
  10. font-size:13px;
  11. text-align:center;
  12. background-color: #fff;
  13. margin: 0px;
  14. overflow: hidden;
  15. }
  16. #info {
  17. position: absolute;
  18. top: 0px; width: 100%;
  19. padding: 5px;
  20. }
  21. a {
  22. color: #0080ff;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="container"></div>
  28. <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - vertex colors - webgl</div>
  29. <script type="text/javascript" src="../build/Three.js"></script>
  30. <script type="text/javascript" src="js/Detector.js"></script>
  31. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  32. <script type="text/javascript" src="js/Stats.js"></script>
  33. <script type="text/javascript">
  34. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  35. var container, stats;
  36. var camera, scene, renderer;
  37. var mesh, mesh2, mesh3, 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.Camera( 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. light.position.normalize();
  51. scene.add( light );
  52. var shadowMaterial = new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture( 'textures/shadow.png' ) } );
  53. var shadowGeo = new THREE.PlaneGeometry( 300, 300, 1, 1 );
  54. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  55. mesh.position.y = - 250;
  56. mesh.rotation.x = - 90 * Math.PI / 180;
  57. scene.add( mesh );
  58. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  59. mesh.position.y = - 250;
  60. mesh.position.x = - 400;
  61. mesh.rotation.x = - 90 * Math.PI / 180;
  62. scene.add( mesh );
  63. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  64. mesh.position.y = - 250;
  65. mesh.position.x = 400;
  66. mesh.rotation.x = - 90 * Math.PI / 180;
  67. scene.add( mesh );
  68. var faceIndices = [ 'a', 'b', 'c', 'd' ];
  69. var color, f, f2, f3, p, n, vertexIndex,
  70. geometry = new THREE.IcosahedronGeometry( 1 ),
  71. geometry2 = new THREE.IcosahedronGeometry( 1 ),
  72. geometry3 = new THREE.IcosahedronGeometry( 1 );
  73. for ( var i = 0; i < geometry.faces.length; i++ ) {
  74. f = geometry.faces[ i ];
  75. f2 = geometry2.faces[ i ];
  76. f3 = geometry3.faces[ i ];
  77. n = ( f instanceof THREE.Face3 ) ? 3 : 4;
  78. for( var j = 0; j < n; j++ ) {
  79. vertexIndex = f[ faceIndices[ j ] ];
  80. p = geometry.vertices[ vertexIndex ].position;
  81. color = new THREE.Color( 0xffffff );
  82. color.setHSV( ( p.y + 1 ) / 2, 1.0, 1.0 );
  83. f.vertexColors[ j ] = color;
  84. color = new THREE.Color( 0xffffff );
  85. color.setHSV( 0.0, ( p.y + 1 ) / 2, 1.0 );
  86. f2.vertexColors[ j ] = color;
  87. color = new THREE.Color( 0xffffff );
  88. color.setHSV( 0.125 * vertexIndex/geometry.vertices.length, 1.0, 1.0 );
  89. f3.vertexColors[ j ] = color;
  90. }
  91. }
  92. var materials = [
  93. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
  94. new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true } )
  95. ];
  96. mesh = new THREE.Mesh( geometry, materials );
  97. mesh.position.x = -400;
  98. mesh.scale.x = mesh.scale.y = mesh.scale.z = 200;
  99. mesh.rotation.x = -1.87;
  100. scene.add( mesh );
  101. mesh2 = new THREE.Mesh( geometry2, materials );
  102. mesh2.position.x = 400;
  103. mesh2.rotation.x = 0;
  104. mesh2.scale = mesh.scale;
  105. scene.add( mesh2 );
  106. mesh3 = new THREE.Mesh( geometry3, materials );
  107. mesh3.position.x = 0;
  108. mesh3.rotation.x = 0;
  109. mesh3.scale = mesh.scale;
  110. scene.add( mesh3 );
  111. renderer = new THREE.WebGLRenderer( { antialias: true } );
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. container.appendChild( renderer.domElement );
  114. stats = new Stats();
  115. stats.domElement.style.position = 'absolute';
  116. stats.domElement.style.top = '0px';
  117. container.appendChild( stats.domElement );
  118. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  119. }
  120. function onDocumentMouseMove( event ) {
  121. mouseX = ( event.clientX - windowHalfX );
  122. mouseY = ( event.clientY - windowHalfY );
  123. }
  124. //
  125. function animate() {
  126. requestAnimationFrame( animate );
  127. render();
  128. stats.update();
  129. }
  130. function render() {
  131. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  132. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  133. renderer.render( scene, camera );
  134. }
  135. </script>
  136. </body>
  137. </html>