webgl_buffergeometry_constructed_from_geometry.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffer geometry constructed from geometry</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. a {
  10. color: #08f;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id="info">
  16. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry constructed from geometry<br/>
  17. by <a target="_blank" href="http://callum.com">Callum Prentice</a>
  18. </div>
  19. <script type="module">
  20. import * as THREE from '../build/three.module.js';
  21. import Stats from './jsm/libs/stats.module.js';
  22. import { TrackballControls } from './jsm/controls/TrackballControls.js';
  23. var camera, scene, renderer, controls, stats;
  24. init();
  25. animate();
  26. function init() {
  27. renderer = new THREE.WebGLRenderer( { antialias: true } );
  28. renderer.setPixelRatio( window.devicePixelRatio );
  29. renderer.setSize( window.innerWidth, window.innerHeight );
  30. document.body.appendChild( renderer.domElement );
  31. scene = new THREE.Scene();
  32. camera = new THREE.PerspectiveCamera( 45.0, window.innerWidth / window.innerHeight, 100, 1500.0 );
  33. camera.position.z = 480.0;
  34. scene.add( camera );
  35. controls = new TrackballControls( camera, renderer.domElement );
  36. controls.minDistance = 100.0;
  37. controls.maxDistance = 800.0;
  38. controls.dynamicDampingFactor = 0.1;
  39. scene.add( new THREE.AmbientLight( 0xffffff, 0.2 ) );
  40. var light = new THREE.PointLight( 0xffffff, 0.7 );
  41. camera.add( light );
  42. createScene();
  43. stats = new Stats();
  44. document.body.appendChild( stats.dom );
  45. window.addEventListener( 'resize', onWindowResize, false );
  46. }
  47. function createGeometry() {
  48. var heartShape = new THREE.Shape(); // From http://blog.burlock.org/html5/130-paths
  49. var x = 0, y = 0;
  50. heartShape.moveTo( x + 25, y + 25 );
  51. heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
  52. heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35, x - 30, y + 35 );
  53. heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
  54. heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
  55. heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
  56. heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
  57. var extrudeSettings = {
  58. depth: 16,
  59. bevelEnabled: true,
  60. bevelSegments: 1,
  61. steps: 2,
  62. bevelSize: 1,
  63. bevelThickness: 1
  64. };
  65. var geometry = new THREE.ExtrudeGeometry( heartShape, extrudeSettings );
  66. geometry.rotateX( Math.PI );
  67. geometry.scale( 0.4, 0.4, 0.4 );
  68. return geometry;
  69. }
  70. function createScene() {
  71. var bufferGeometry = new THREE.BufferGeometry();
  72. var radius = 125;
  73. var count = 80;
  74. var positions = [];
  75. var normals = [];
  76. var colors = [];
  77. var vector = new THREE.Vector3();
  78. var color = new THREE.Color( 0xffffff );
  79. var heartGeometry = createGeometry();
  80. var geometry = new THREE.Geometry();
  81. for ( var i = 1, l = count; i <= l; i ++ ) {
  82. var phi = Math.acos( - 1 + ( 2 * i ) / l );
  83. var theta = Math.sqrt( l * Math.PI ) * phi;
  84. vector.setFromSphericalCoords( radius, phi, theta );
  85. geometry.copy( heartGeometry );
  86. geometry.lookAt( vector );
  87. geometry.translate( vector.x, vector.y, vector.z );
  88. color.setHSL( ( i / l ), 1.0, 0.7 );
  89. geometry.faces.forEach( function ( face ) {
  90. positions.push( geometry.vertices[ face.a ].x );
  91. positions.push( geometry.vertices[ face.a ].y );
  92. positions.push( geometry.vertices[ face.a ].z );
  93. positions.push( geometry.vertices[ face.b ].x );
  94. positions.push( geometry.vertices[ face.b ].y );
  95. positions.push( geometry.vertices[ face.b ].z );
  96. positions.push( geometry.vertices[ face.c ].x );
  97. positions.push( geometry.vertices[ face.c ].y );
  98. positions.push( geometry.vertices[ face.c ].z );
  99. normals.push( face.normal.x );
  100. normals.push( face.normal.y );
  101. normals.push( face.normal.z );
  102. normals.push( face.normal.x );
  103. normals.push( face.normal.y );
  104. normals.push( face.normal.z );
  105. normals.push( face.normal.x );
  106. normals.push( face.normal.y );
  107. normals.push( face.normal.z );
  108. colors.push( color.r );
  109. colors.push( color.g );
  110. colors.push( color.b );
  111. colors.push( color.r );
  112. colors.push( color.g );
  113. colors.push( color.b );
  114. colors.push( color.r );
  115. colors.push( color.g );
  116. colors.push( color.b );
  117. } );
  118. }
  119. bufferGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  120. bufferGeometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  121. bufferGeometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  122. var material = new THREE.MeshPhongMaterial( { shininess: 80, vertexColors: THREE.VertexColors } );
  123. var mesh = new THREE.Mesh( bufferGeometry, material );
  124. scene.add( mesh );
  125. }
  126. function onWindowResize() {
  127. camera.aspect = window.innerWidth / window.innerHeight;
  128. camera.updateProjectionMatrix();
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. }
  131. function animate() {
  132. requestAnimationFrame( animate );
  133. controls.update();
  134. stats.update();
  135. renderer.render( scene, camera );
  136. }
  137. </script>
  138. </body>
  139. </html>