geometry_colors.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - 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. #oldie {
  22. font-family:monospace;
  23. font-size:13px;
  24. text-align:center;
  25. background:#eee;
  26. color:#000;
  27. padding:1em;
  28. width:475px;
  29. margin:5em auto 0;
  30. display:none;
  31. }
  32. a {
  33. color: #0080ff;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div id="container"></div>
  39. <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - vertex colors - webgl</div>
  40. <center>
  41. <div id="oldie">
  42. Sorry, your browser doesn't support <a href="http://khronos.org/webgl/wiki/Getting_a_WebGL_Implementation">WebGL</a>
  43. <br/>
  44. Please try in
  45. <a href="http://www.chromium.org/getting-involved/dev-channel">Chrome 9+</a> /
  46. <a href="http://www.mozilla.com/en-US/firefox/all-beta.html">Firefox 4+</a> /
  47. <a href="http://nightly.webkit.org/">Safari OSX 10.6+</a>
  48. </div>
  49. </center>
  50. <script type="text/javascript" src="js/Stats.js"></script>
  51. <script type="text/javascript" src="../build/ThreeExtras.js"></script>
  52. <script type="text/javascript">
  53. if ( !is_browser_compatible() ) {
  54. document.getElementById( "oldie" ).style.display = "block";
  55. }
  56. var container, stats;
  57. var camera, scene, renderer;
  58. var mesh, mesh2, mesh3, light;
  59. var mouseX = 0, mouseY = 0;
  60. var windowHalfX = window.innerWidth / 2;
  61. var windowHalfY = window.innerHeight / 2;
  62. init();
  63. setInterval( loop, 1000 / 60 );
  64. function init() {
  65. container = document.getElementById( 'container' );
  66. camera = new THREE.Camera( 20, window.innerWidth / window.innerHeight, 1, 10000 );
  67. camera.position.z = 1800;
  68. scene = new THREE.Scene();
  69. light = new THREE.DirectionalLight( 0xffffff );
  70. light.position.set( 0, 0, 1 );
  71. light.position.normalize();
  72. scene.addLight( light );
  73. var shadowMaterial = new THREE.MeshBasicMaterial( { map: ImageUtils.loadTexture( 'textures/shadow.png' ) } );
  74. var shadowGeo = new Plane( 300, 300, 1, 1 );
  75. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  76. mesh.position.y = - 250;
  77. mesh.rotation.x = - 90 * Math.PI / 180;
  78. scene.addObject( mesh );
  79. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  80. mesh.position.y = - 250;
  81. mesh.position.x = - 400;
  82. mesh.rotation.x = - 90 * Math.PI / 180;
  83. scene.addObject( mesh );
  84. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  85. mesh.position.y = - 250;
  86. mesh.position.x = 400;
  87. mesh.rotation.x = - 90 * Math.PI / 180;
  88. scene.addObject( mesh );
  89. var color, p, colors = [], colors2 = [], colors3 = [],
  90. geometry = new Icosahedron( 1 ),
  91. geometry2 = new THREE.Geometry(),
  92. geometry3 = new THREE.Geometry();
  93. for( var i = 0; i < geometry.vertices.length; i++ ) {
  94. p = geometry.vertices[ i ].position;
  95. color = new THREE.Color( 0xffffff );
  96. color.setHSV( ( p.y + 1 ) / 2, 1.0, 1.0 );
  97. colors[ i ] = color;
  98. color = new THREE.Color( 0xffffff );
  99. color.setHSV( 0.0, ( p.y + 1 ) / 2, 1.0 );
  100. colors2[ i ] = color;
  101. color = new THREE.Color( 0xffffff );
  102. color.setHSV( 0.125 * i/geometry.vertices.length, 1.0, 1.0 );
  103. colors3[ i ] = color;
  104. }
  105. geometry3.vertices = geometry2.vertices = geometry.vertices;
  106. geometry3.faces = geometry2.faces = geometry.faces;
  107. geometry2.sortFacesByMaterial();
  108. geometry3.sortFacesByMaterial();
  109. geometry.colors = colors;
  110. geometry2.colors = colors2;
  111. geometry3.colors = colors3;
  112. var materials = [
  113. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertex_colors: true } ),
  114. new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true } )
  115. ];
  116. mesh = new THREE.Mesh( geometry, materials );
  117. mesh.position.x = -400;
  118. mesh.scale.x = mesh.scale.y = mesh.scale.z = 200;
  119. mesh.rotation.x = -1.87;
  120. scene.addObject( mesh );
  121. mesh2 = new THREE.Mesh( geometry2, materials );
  122. mesh2.position.x = 400;
  123. mesh2.rotation.x = 0;
  124. mesh2.scale = mesh.scale;
  125. scene.addObject( mesh2 );
  126. mesh3 = new THREE.Mesh( geometry3, materials );
  127. mesh3.position.x = 0;
  128. mesh3.rotation.x = 0;
  129. mesh3.scale = mesh.scale;
  130. scene.addObject( mesh3 );
  131. renderer = new THREE.WebGLRenderer();
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. container.appendChild( renderer.domElement );
  134. stats = new Stats();
  135. stats.domElement.style.position = 'absolute';
  136. stats.domElement.style.top = '0px';
  137. container.appendChild( stats.domElement );
  138. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  139. }
  140. function onDocumentMouseMove( event ) {
  141. mouseX = ( event.clientX - windowHalfX );
  142. mouseY = ( event.clientY - windowHalfY );
  143. }
  144. function loop() {
  145. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  146. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  147. renderer.render( scene, camera );
  148. stats.update();
  149. }
  150. function is_browser_compatible() {
  151. // WebGL support
  152. try { var test = new Float32Array(1); } catch(e) { return false; }
  153. return true;
  154. }
  155. </script>
  156. </body>
  157. </html>