webgl_geometry_colors.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/ThreeExtras.js"></script>
  30. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  31. <script type="text/javascript" src="js/Stats.js"></script>
  32. <script type="text/javascript">
  33. if ( ! THREE.Detector.webgl ) THREE.Detector.addGetWebGLMessage();
  34. var container, stats;
  35. var camera, scene, renderer;
  36. var mesh, mesh2, mesh3, light;
  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.Camera( 20, window.innerWidth / window.innerHeight, 1, 10000 );
  45. camera.position.z = 1800;
  46. scene = new THREE.Scene();
  47. light = new THREE.DirectionalLight( 0xffffff );
  48. light.position.set( 0, 0, 1 );
  49. light.position.normalize();
  50. scene.addLight( light );
  51. var shadowMaterial = new THREE.MeshBasicMaterial( { map: ImageUtils.loadTexture( 'textures/shadow.png' ) } );
  52. var shadowGeo = new Plane( 300, 300, 1, 1 );
  53. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  54. mesh.position.y = - 250;
  55. mesh.rotation.x = - 90 * Math.PI / 180;
  56. scene.addObject( mesh );
  57. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  58. mesh.position.y = - 250;
  59. mesh.position.x = - 400;
  60. mesh.rotation.x = - 90 * Math.PI / 180;
  61. scene.addObject( mesh );
  62. mesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  63. mesh.position.y = - 250;
  64. mesh.position.x = 400;
  65. mesh.rotation.x = - 90 * Math.PI / 180;
  66. scene.addObject( mesh );
  67. var color, p, colors = [], colors2 = [], colors3 = [],
  68. geometry = new Icosahedron( 1 ),
  69. geometry2 = new THREE.Geometry(),
  70. geometry3 = new THREE.Geometry();
  71. for( var i = 0; i < geometry.vertices.length; i++ ) {
  72. p = geometry.vertices[ i ].position;
  73. color = new THREE.Color( 0xffffff );
  74. color.setHSV( ( p.y + 1 ) / 2, 1.0, 1.0 );
  75. colors[ i ] = color;
  76. color = new THREE.Color( 0xffffff );
  77. color.setHSV( 0.0, ( p.y + 1 ) / 2, 1.0 );
  78. colors2[ i ] = color;
  79. color = new THREE.Color( 0xffffff );
  80. color.setHSV( 0.125 * i/geometry.vertices.length, 1.0, 1.0 );
  81. colors3[ i ] = color;
  82. }
  83. geometry3.vertices = geometry2.vertices = geometry.vertices;
  84. geometry3.faces = geometry2.faces = geometry.faces;
  85. geometry2.sortFacesByMaterial();
  86. geometry3.sortFacesByMaterial();
  87. geometry.colors = colors;
  88. geometry2.colors = colors2;
  89. geometry3.colors = colors3;
  90. var materials = [
  91. new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertex_colors: true } ),
  92. new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true } )
  93. ];
  94. mesh = new THREE.Mesh( geometry, materials );
  95. mesh.position.x = -400;
  96. mesh.scale.x = mesh.scale.y = mesh.scale.z = 200;
  97. mesh.rotation.x = -1.87;
  98. scene.addObject( mesh );
  99. mesh2 = new THREE.Mesh( geometry2, materials );
  100. mesh2.position.x = 400;
  101. mesh2.rotation.x = 0;
  102. mesh2.scale = mesh.scale;
  103. scene.addObject( mesh2 );
  104. mesh3 = new THREE.Mesh( geometry3, materials );
  105. mesh3.position.x = 0;
  106. mesh3.rotation.x = 0;
  107. mesh3.scale = mesh.scale;
  108. scene.addObject( mesh3 );
  109. renderer = new THREE.WebGLRenderer();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. container.appendChild( renderer.domElement );
  112. stats = new Stats();
  113. stats.domElement.style.position = 'absolute';
  114. stats.domElement.style.top = '0px';
  115. container.appendChild( stats.domElement );
  116. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  117. }
  118. function onDocumentMouseMove( event ) {
  119. mouseX = ( event.clientX - windowHalfX );
  120. mouseY = ( event.clientY - windowHalfY );
  121. }
  122. //
  123. function animate() {
  124. requestAnimationFrame( animate );
  125. render();
  126. stats.update();
  127. }
  128. function render() {
  129. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  130. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  131. renderer.render( scene, camera );
  132. }
  133. </script>
  134. </body>
  135. </html>