webgl_geometry_colors.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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" rel="noopener">three.js</a> webgl - vertex colors</div>
  30. <script src="../build/three.js"></script>
  31. <script src="js/WebGL.js"></script>
  32. <script src="js/libs/stats.min.js"></script>
  33. <script>
  34. if ( WEBGL.isWebGLAvailable() === false ) {
  35. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  36. }
  37. var container, stats;
  38. var camera, scene, renderer;
  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. scene.background = new THREE.Color( 0xffffff );
  50. var light = new THREE.DirectionalLight( 0xffffff );
  51. light.position.set( 0, 0, 1 );
  52. scene.add( light );
  53. // shadow
  54. var canvas = document.createElement( 'canvas' );
  55. canvas.width = 128;
  56. canvas.height = 128;
  57. var context = canvas.getContext( '2d' );
  58. var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  59. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  60. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  61. context.fillStyle = gradient;
  62. context.fillRect( 0, 0, canvas.width, canvas.height );
  63. var shadowTexture = new THREE.CanvasTexture( canvas );
  64. var shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture } );
  65. var shadowGeo = new THREE.PlaneBufferGeometry( 300, 300, 1, 1 );
  66. var shadowMesh;
  67. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  68. shadowMesh.position.y = - 250;
  69. shadowMesh.rotation.x = - Math.PI / 2;
  70. scene.add( shadowMesh );
  71. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  72. shadowMesh.position.y = - 250;
  73. shadowMesh.position.x = - 400;
  74. shadowMesh.rotation.x = - Math.PI / 2;
  75. scene.add( shadowMesh );
  76. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  77. shadowMesh.position.y = - 250;
  78. shadowMesh.position.x = 400;
  79. shadowMesh.rotation.x = - Math.PI / 2;
  80. scene.add( shadowMesh );
  81. var radius = 200;
  82. var geometry1 = new THREE.IcosahedronBufferGeometry( radius, 1 );
  83. var count = geometry1.attributes.position.count;
  84. geometry1.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( count * 3 ), 3 ) );
  85. var geometry2 = geometry1.clone();
  86. var geometry3 = geometry1.clone();
  87. var color = new THREE.Color();
  88. var positions1 = geometry1.attributes.position;
  89. var positions2 = geometry2.attributes.position;
  90. var positions3 = geometry3.attributes.position;
  91. var colors1 = geometry1.attributes.color;
  92. var colors2 = geometry2.attributes.color;
  93. var colors3 = geometry3.attributes.color;
  94. for ( var i = 0; i < count; i ++ ) {
  95. color.setHSL( ( positions1.getY( i ) / radius + 1 ) / 2, 1.0, 0.5 );
  96. colors1.setXYZ( i, color.r, color.g, color.b );
  97. color.setHSL( 0, ( positions2.getY( i ) / radius + 1 ) / 2, 0.5 );
  98. colors2.setXYZ( i, color.r, color.g, color.b );
  99. color.setRGB( 1, 0.8 - ( positions3.getY( i ) / radius + 1 ) / 2, 0 );
  100. colors3.setXYZ( i, color.r, color.g, color.b );
  101. }
  102. var material = new THREE.MeshPhongMaterial( {
  103. color: 0xffffff,
  104. flatShading: true,
  105. vertexColors: THREE.VertexColors,
  106. shininess: 0
  107. } );
  108. var wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  109. var mesh = new THREE.Mesh( geometry1, material );
  110. var wireframe = new THREE.Mesh( geometry1, wireframeMaterial );
  111. mesh.add( wireframe );
  112. mesh.position.x = - 400;
  113. mesh.rotation.x = - 1.87;
  114. scene.add( mesh );
  115. var mesh = new THREE.Mesh( geometry2, material );
  116. var wireframe = new THREE.Mesh( geometry2, wireframeMaterial );
  117. mesh.add( wireframe );
  118. mesh.position.x = 400;
  119. scene.add( mesh );
  120. var mesh = new THREE.Mesh( geometry3, material );
  121. var wireframe = new THREE.Mesh( geometry3, wireframeMaterial );
  122. mesh.add( wireframe );
  123. scene.add( mesh );
  124. renderer = new THREE.WebGLRenderer( { antialias: true } );
  125. renderer.setPixelRatio( window.devicePixelRatio );
  126. renderer.setSize( window.innerWidth, window.innerHeight );
  127. container.appendChild( renderer.domElement );
  128. stats = new Stats();
  129. container.appendChild( stats.dom );
  130. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  131. //
  132. window.addEventListener( 'resize', onWindowResize, false );
  133. }
  134. function onWindowResize() {
  135. windowHalfX = window.innerWidth / 2;
  136. windowHalfY = window.innerHeight / 2;
  137. camera.aspect = window.innerWidth / window.innerHeight;
  138. camera.updateProjectionMatrix();
  139. renderer.setSize( window.innerWidth, window.innerHeight );
  140. }
  141. function onDocumentMouseMove( event ) {
  142. mouseX = ( event.clientX - windowHalfX );
  143. mouseY = ( event.clientY - windowHalfY );
  144. }
  145. //
  146. function animate() {
  147. requestAnimationFrame( animate );
  148. render();
  149. stats.update();
  150. }
  151. function render() {
  152. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  153. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  154. camera.lookAt( scene.position );
  155. renderer.render( scene, camera );
  156. }
  157. </script>
  158. </body>
  159. </html>