webgl_loader_ply.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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: 0xcbcbcb, specular: 0x474747 } )
  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: 0x009cff, 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: 0x009cff, 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( 0x8d7c7c, 0x494966 ) );
  80. addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
  81. addShadowedLight( 0.5, 1, - 1, 0xffd500, 1 );
  82. // renderer
  83. renderer = new THREE.WebGLRenderer( { antialias: true } );
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. renderer.shadowMap.enabled = true;
  87. container.appendChild( renderer.domElement );
  88. // stats
  89. stats = new Stats();
  90. container.appendChild( stats.dom );
  91. // resize
  92. window.addEventListener( 'resize', onWindowResize );
  93. }
  94. function addShadowedLight( x, y, z, color, intensity ) {
  95. const directionalLight = new THREE.DirectionalLight( color, intensity );
  96. directionalLight.position.set( x, y, z );
  97. scene.add( directionalLight );
  98. directionalLight.castShadow = true;
  99. const d = 1;
  100. directionalLight.shadow.camera.left = - d;
  101. directionalLight.shadow.camera.right = d;
  102. directionalLight.shadow.camera.top = d;
  103. directionalLight.shadow.camera.bottom = - d;
  104. directionalLight.shadow.camera.near = 1;
  105. directionalLight.shadow.camera.far = 4;
  106. directionalLight.shadow.mapSize.width = 1024;
  107. directionalLight.shadow.mapSize.height = 1024;
  108. directionalLight.shadow.bias = - 0.001;
  109. }
  110. function onWindowResize() {
  111. camera.aspect = window.innerWidth / window.innerHeight;
  112. camera.updateProjectionMatrix();
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. }
  115. function animate() {
  116. requestAnimationFrame( animate );
  117. render();
  118. stats.update();
  119. }
  120. function render() {
  121. const timer = Date.now() * 0.0005;
  122. camera.position.x = Math.sin( timer ) * 2.5;
  123. camera.position.z = Math.cos( timer ) * 2.5;
  124. camera.lookAt( cameraTarget );
  125. renderer.render( scene, camera );
  126. }
  127. </script>
  128. </body>
  129. </html>