webgl_geometry_convex.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. <script type="module">
  12. import * as THREE from '../build/three.module.js';
  13. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  14. import { ConvexBufferGeometry } from './jsm/geometries/ConvexGeometry.js';
  15. var group, camera, scene, renderer;
  16. init();
  17. animate();
  18. function init() {
  19. scene = new THREE.Scene();
  20. renderer = new THREE.WebGLRenderer( { antialias: true } );
  21. renderer.setPixelRatio( window.devicePixelRatio );
  22. renderer.setSize( window.innerWidth, window.innerHeight );
  23. document.body.appendChild( renderer.domElement );
  24. // camera
  25. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  26. camera.position.set( 15, 20, 30 );
  27. scene.add( camera );
  28. // controls
  29. var controls = new OrbitControls( camera, renderer.domElement );
  30. controls.minDistance = 20;
  31. controls.maxDistance = 50;
  32. controls.maxPolarAngle = Math.PI / 2;
  33. scene.add( new THREE.AmbientLight( 0x222222 ) );
  34. // light
  35. var light = new THREE.PointLight( 0xffffff, 1 );
  36. camera.add( light );
  37. // helper
  38. scene.add( new THREE.AxesHelper( 20 ) );
  39. // textures
  40. var loader = new THREE.TextureLoader();
  41. var texture = loader.load( 'textures/sprites/disc.png' );
  42. group = new THREE.Group();
  43. scene.add( group );
  44. // points
  45. var vertices = new THREE.DodecahedronGeometry( 10 ).vertices;
  46. for ( var i = 0; i < vertices.length; i ++ ) {
  47. //vertices[ i ].add( randomPoint().multiplyScalar( 2 ) ); // wiggle the points
  48. }
  49. var pointsMaterial = new THREE.PointsMaterial( {
  50. color: 0x0080ff,
  51. map: texture,
  52. size: 1,
  53. alphaTest: 0.5
  54. } );
  55. var pointsGeometry = new THREE.BufferGeometry().setFromPoints( vertices );
  56. var points = new THREE.Points( pointsGeometry, pointsMaterial );
  57. group.add( points );
  58. // convex hull
  59. var meshMaterial = new THREE.MeshLambertMaterial( {
  60. color: 0xffffff,
  61. opacity: 0.5,
  62. transparent: true
  63. } );
  64. var meshGeometry = new ConvexBufferGeometry( vertices );
  65. var mesh = new THREE.Mesh( meshGeometry, meshMaterial );
  66. mesh.material.side = THREE.BackSide; // back faces
  67. mesh.renderOrder = 0;
  68. group.add( mesh );
  69. var mesh = new THREE.Mesh( meshGeometry, meshMaterial.clone() );
  70. mesh.material.side = THREE.FrontSide; // front faces
  71. mesh.renderOrder = 1;
  72. group.add( mesh );
  73. //
  74. window.addEventListener( 'resize', onWindowResize, false );
  75. }
  76. function onWindowResize() {
  77. camera.aspect = window.innerWidth / window.innerHeight;
  78. camera.updateProjectionMatrix();
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. }
  81. function animate() {
  82. requestAnimationFrame( animate );
  83. group.rotation.y += 0.005;
  84. render();
  85. }
  86. function render() {
  87. renderer.render( scene, camera );
  88. }
  89. </script>
  90. </body>
  91. </html>