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