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