webgl_geometry_colors.html 5.7 KB

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