webgl_loader_ply.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - PLY</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">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> -
  12. PLY loader test by <a href="https://github.com/menway" target="_blank" rel="noopener">Wei Meng</a>.<br/>
  13. Image from <a href="http://people.sc.fsu.edu/~jburkardt/data/ply/ply.html">John Burkardt</a>
  14. </div>
  15. <!-- Import maps polyfill -->
  16. <!-- Remove this when import maps will be widely supported -->
  17. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  18. <script type="importmap">
  19. {
  20. "imports": {
  21. "three": "../build/three.module.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { PLYLoader } from 'three/addons/loaders/PLYLoader.js';
  30. let container, stats;
  31. let camera, cameraTarget, scene, renderer;
  32. init();
  33. animate();
  34. function init() {
  35. container = document.createElement( 'div' );
  36. document.body.appendChild( container );
  37. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
  38. camera.position.set( 3, 0.15, 3 );
  39. cameraTarget = new THREE.Vector3( 0, - 0.1, 0 );
  40. scene = new THREE.Scene();
  41. scene.background = new THREE.Color( 0x72645b );
  42. scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
  43. // Ground
  44. const plane = new THREE.Mesh(
  45. new THREE.PlaneGeometry( 40, 40 ),
  46. new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
  47. );
  48. plane.rotation.x = - Math.PI / 2;
  49. plane.position.y = - 0.5;
  50. scene.add( plane );
  51. plane.receiveShadow = true;
  52. // PLY file
  53. const loader = new PLYLoader();
  54. loader.load( './models/ply/ascii/dolphins.ply', function ( geometry ) {
  55. geometry.computeVertexNormals();
  56. const material = new THREE.MeshStandardMaterial( { color: 0x0055ff, flatShading: true } );
  57. const mesh = new THREE.Mesh( geometry, material );
  58. mesh.position.y = - 0.2;
  59. mesh.position.z = 0.3;
  60. mesh.rotation.x = - Math.PI / 2;
  61. mesh.scale.multiplyScalar( 0.001 );
  62. mesh.castShadow = true;
  63. mesh.receiveShadow = true;
  64. scene.add( mesh );
  65. } );
  66. loader.load( './models/ply/binary/Lucy100k.ply', function ( geometry ) {
  67. geometry.computeVertexNormals();
  68. const material = new THREE.MeshStandardMaterial( { color: 0x0055ff, flatShading: true } );
  69. const mesh = new THREE.Mesh( geometry, material );
  70. mesh.position.x = - 0.2;
  71. mesh.position.y = - 0.02;
  72. mesh.position.z = - 0.2;
  73. mesh.scale.multiplyScalar( 0.0006 );
  74. mesh.castShadow = true;
  75. mesh.receiveShadow = true;
  76. scene.add( mesh );
  77. } );
  78. // Lights
  79. scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) );
  80. addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
  81. addShadowedLight( 0.5, 1, - 1, 0xffaa00, 1 );
  82. // renderer
  83. renderer = new THREE.WebGLRenderer( { antialias: true } );
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. renderer.outputEncoding = THREE.sRGBEncoding;
  87. renderer.shadowMap.enabled = true;
  88. container.appendChild( renderer.domElement );
  89. // stats
  90. stats = new Stats();
  91. container.appendChild( stats.dom );
  92. // resize
  93. window.addEventListener( 'resize', onWindowResize );
  94. }
  95. function addShadowedLight( x, y, z, color, intensity ) {
  96. const directionalLight = new THREE.DirectionalLight( color, intensity );
  97. directionalLight.position.set( x, y, z );
  98. scene.add( directionalLight );
  99. directionalLight.castShadow = true;
  100. const d = 1;
  101. directionalLight.shadow.camera.left = - d;
  102. directionalLight.shadow.camera.right = d;
  103. directionalLight.shadow.camera.top = d;
  104. directionalLight.shadow.camera.bottom = - d;
  105. directionalLight.shadow.camera.near = 1;
  106. directionalLight.shadow.camera.far = 4;
  107. directionalLight.shadow.mapSize.width = 1024;
  108. directionalLight.shadow.mapSize.height = 1024;
  109. directionalLight.shadow.bias = - 0.001;
  110. }
  111. function onWindowResize() {
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. }
  116. function animate() {
  117. requestAnimationFrame( animate );
  118. render();
  119. stats.update();
  120. }
  121. function render() {
  122. const timer = Date.now() * 0.0005;
  123. camera.position.x = Math.sin( timer ) * 2.5;
  124. camera.position.z = Math.cos( timer ) * 2.5;
  125. camera.lookAt( cameraTarget );
  126. renderer.render( scene, camera );
  127. }
  128. </script>
  129. </body>
  130. </html>