webgl_geometry_convex.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - convex 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. </head>
  9. <body>
  10. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - convex geometry</div>
  11. <!-- Import maps polyfill -->
  12. <!-- Remove this when import maps will be widely supported -->
  13. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  24. import { ConvexGeometry } from './jsm/geometries/ConvexGeometry.js';
  25. import * as BufferGeometryUtils from './jsm/utils/BufferGeometryUtils.js';
  26. let group, camera, scene, renderer;
  27. init();
  28. animate();
  29. function init() {
  30. scene = new THREE.Scene();
  31. renderer = new THREE.WebGLRenderer( { antialias: true } );
  32. renderer.setPixelRatio( window.devicePixelRatio );
  33. renderer.setSize( window.innerWidth, window.innerHeight );
  34. document.body.appendChild( renderer.domElement );
  35. // camera
  36. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  37. camera.position.set( 15, 20, 30 );
  38. scene.add( camera );
  39. // controls
  40. const controls = new OrbitControls( camera, renderer.domElement );
  41. controls.minDistance = 20;
  42. controls.maxDistance = 50;
  43. controls.maxPolarAngle = Math.PI / 2;
  44. // ambient light
  45. scene.add( new THREE.AmbientLight( 0x222222 ) );
  46. // point light
  47. const light = new THREE.PointLight( 0xffffff, 1 );
  48. camera.add( light );
  49. // helper
  50. scene.add( new THREE.AxesHelper( 20 ) );
  51. // textures
  52. const loader = new THREE.TextureLoader();
  53. const texture = loader.load( 'textures/sprites/disc.png' );
  54. group = new THREE.Group();
  55. scene.add( group );
  56. // points
  57. let dodecahedronGeometry = new THREE.DodecahedronGeometry( 10 );
  58. // if normal and uv attributes are not removed, mergeVertices() can't consolidate indentical vertices with different normal/uv data
  59. dodecahedronGeometry.deleteAttribute( 'normal' );
  60. dodecahedronGeometry.deleteAttribute( 'uv' );
  61. dodecahedronGeometry = BufferGeometryUtils.mergeVertices( dodecahedronGeometry );
  62. const vertices = [];
  63. const positionAttribute = dodecahedronGeometry.getAttribute( 'position' );
  64. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  65. const vertex = new THREE.Vector3();
  66. vertex.fromBufferAttribute( positionAttribute, i );
  67. vertices.push( vertex );
  68. }
  69. const pointsMaterial = new THREE.PointsMaterial( {
  70. color: 0x0080ff,
  71. map: texture,
  72. size: 1,
  73. alphaTest: 0.5
  74. } );
  75. const pointsGeometry = new THREE.BufferGeometry().setFromPoints( vertices );
  76. const points = new THREE.Points( pointsGeometry, pointsMaterial );
  77. group.add( points );
  78. // convex hull
  79. const meshMaterial = new THREE.MeshLambertMaterial( {
  80. color: 0xffffff,
  81. opacity: 0.5,
  82. transparent: true
  83. } );
  84. const meshGeometry = new ConvexGeometry( vertices );
  85. const mesh1 = new THREE.Mesh( meshGeometry, meshMaterial );
  86. mesh1.material.side = THREE.BackSide; // back faces
  87. mesh1.renderOrder = 0;
  88. group.add( mesh1 );
  89. const mesh2 = new THREE.Mesh( meshGeometry, meshMaterial.clone() );
  90. mesh2.material.side = THREE.FrontSide; // front faces
  91. mesh2.renderOrder = 1;
  92. group.add( mesh2 );
  93. //
  94. window.addEventListener( 'resize', onWindowResize );
  95. }
  96. function onWindowResize() {
  97. camera.aspect = window.innerWidth / window.innerHeight;
  98. camera.updateProjectionMatrix();
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. }
  101. function animate() {
  102. requestAnimationFrame( animate );
  103. group.rotation.y += 0.005;
  104. render();
  105. }
  106. function render() {
  107. renderer.render( scene, camera );
  108. }
  109. </script>
  110. </body>
  111. </html>