webgl_geometry_colors.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #fff;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="container"></div>
  20. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - vertex colors</div>
  21. <script type="module">
  22. import {
  23. BufferAttribute,
  24. CanvasTexture,
  25. Color,
  26. DirectionalLight,
  27. IcosahedronBufferGeometry,
  28. Mesh,
  29. MeshBasicMaterial,
  30. MeshPhongMaterial,
  31. PerspectiveCamera,
  32. PlaneBufferGeometry,
  33. Scene,
  34. VertexColors,
  35. WebGLRenderer
  36. } from "../build/three.module.js";
  37. import Stats from './jsm/libs/stats.module.js';
  38. var container, stats;
  39. var camera, scene, renderer;
  40. var mouseX = 0, mouseY = 0;
  41. var windowHalfX = window.innerWidth / 2;
  42. var windowHalfY = window.innerHeight / 2;
  43. init();
  44. animate();
  45. function init() {
  46. container = document.getElementById( 'container' );
  47. camera = new PerspectiveCamera( 20, window.innerWidth / window.innerHeight, 1, 10000 );
  48. camera.position.z = 1800;
  49. scene = new Scene();
  50. scene.background = new Color( 0xffffff );
  51. var light = new DirectionalLight( 0xffffff );
  52. light.position.set( 0, 0, 1 );
  53. scene.add( light );
  54. // shadow
  55. var canvas = document.createElement( 'canvas' );
  56. canvas.width = 128;
  57. canvas.height = 128;
  58. var context = canvas.getContext( '2d' );
  59. var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  60. gradient.addColorStop( 0.1, 'rgba(210,210,210,1)' );
  61. gradient.addColorStop( 1, 'rgba(255,255,255,1)' );
  62. context.fillStyle = gradient;
  63. context.fillRect( 0, 0, canvas.width, canvas.height );
  64. var shadowTexture = new CanvasTexture( canvas );
  65. var shadowMaterial = new MeshBasicMaterial( { map: shadowTexture } );
  66. var shadowGeo = new PlaneBufferGeometry( 300, 300, 1, 1 );
  67. var shadowMesh;
  68. shadowMesh = new Mesh( shadowGeo, shadowMaterial );
  69. shadowMesh.position.y = - 250;
  70. shadowMesh.rotation.x = - Math.PI / 2;
  71. scene.add( shadowMesh );
  72. shadowMesh = new Mesh( shadowGeo, shadowMaterial );
  73. shadowMesh.position.y = - 250;
  74. shadowMesh.position.x = - 400;
  75. shadowMesh.rotation.x = - Math.PI / 2;
  76. scene.add( shadowMesh );
  77. shadowMesh = new Mesh( shadowGeo, shadowMaterial );
  78. shadowMesh.position.y = - 250;
  79. shadowMesh.position.x = 400;
  80. shadowMesh.rotation.x = - Math.PI / 2;
  81. scene.add( shadowMesh );
  82. var radius = 200;
  83. var geometry1 = new IcosahedronBufferGeometry( radius, 1 );
  84. var count = geometry1.attributes.position.count;
  85. geometry1.addAttribute( 'color', new BufferAttribute( new Float32Array( count * 3 ), 3 ) );
  86. var geometry2 = geometry1.clone();
  87. var geometry3 = geometry1.clone();
  88. var color = new Color();
  89. var positions1 = geometry1.attributes.position;
  90. var positions2 = geometry2.attributes.position;
  91. var positions3 = geometry3.attributes.position;
  92. var colors1 = geometry1.attributes.color;
  93. var colors2 = geometry2.attributes.color;
  94. var colors3 = geometry3.attributes.color;
  95. for ( var i = 0; i < count; i ++ ) {
  96. color.setHSL( ( positions1.getY( i ) / radius + 1 ) / 2, 1.0, 0.5 );
  97. colors1.setXYZ( i, color.r, color.g, color.b );
  98. color.setHSL( 0, ( positions2.getY( i ) / radius + 1 ) / 2, 0.5 );
  99. colors2.setXYZ( i, color.r, color.g, color.b );
  100. color.setRGB( 1, 0.8 - ( positions3.getY( i ) / radius + 1 ) / 2, 0 );
  101. colors3.setXYZ( i, color.r, color.g, color.b );
  102. }
  103. var material = new MeshPhongMaterial( {
  104. color: 0xffffff,
  105. flatShading: true,
  106. vertexColors: VertexColors,
  107. shininess: 0
  108. } );
  109. var wireframeMaterial = new MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  110. var mesh = new Mesh( geometry1, material );
  111. var wireframe = new Mesh( geometry1, wireframeMaterial );
  112. mesh.add( wireframe );
  113. mesh.position.x = - 400;
  114. mesh.rotation.x = - 1.87;
  115. scene.add( mesh );
  116. var mesh = new Mesh( geometry2, material );
  117. var wireframe = new Mesh( geometry2, wireframeMaterial );
  118. mesh.add( wireframe );
  119. mesh.position.x = 400;
  120. scene.add( mesh );
  121. var mesh = new Mesh( geometry3, material );
  122. var wireframe = new Mesh( geometry3, wireframeMaterial );
  123. mesh.add( wireframe );
  124. scene.add( mesh );
  125. renderer = new WebGLRenderer( { antialias: true } );
  126. renderer.setPixelRatio( window.devicePixelRatio );
  127. renderer.setSize( window.innerWidth, window.innerHeight );
  128. container.appendChild( renderer.domElement );
  129. stats = new Stats();
  130. container.appendChild( stats.dom );
  131. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  132. //
  133. window.addEventListener( 'resize', onWindowResize, false );
  134. }
  135. function onWindowResize() {
  136. windowHalfX = window.innerWidth / 2;
  137. windowHalfY = window.innerHeight / 2;
  138. camera.aspect = window.innerWidth / window.innerHeight;
  139. camera.updateProjectionMatrix();
  140. renderer.setSize( window.innerWidth, window.innerHeight );
  141. }
  142. function onDocumentMouseMove( event ) {
  143. mouseX = ( event.clientX - windowHalfX );
  144. mouseY = ( event.clientY - windowHalfY );
  145. }
  146. //
  147. function animate() {
  148. requestAnimationFrame( animate );
  149. render();
  150. stats.update();
  151. }
  152. function render() {
  153. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  154. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  155. camera.lookAt( scene.position );
  156. renderer.render( scene, camera );
  157. }
  158. </script>
  159. </body>
  160. </html>