webgl_geometry_colors.html 5.9 KB

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