webgl_geometry_convex.html 4.2 KB

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