webgl_loader_ply.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from 'three/addons/libs/stats.module.js';
  26. import { PLYLoader } from 'three/addons/loaders/PLYLoader.js';
  27. let container, stats;
  28. let camera, cameraTarget, scene, renderer;
  29. init();
  30. animate();
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
  35. camera.position.set( 3, 0.15, 3 );
  36. cameraTarget = new THREE.Vector3( 0, - 0.1, 0 );
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0x72645b );
  39. scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
  40. // Ground
  41. const plane = new THREE.Mesh(
  42. new THREE.PlaneGeometry( 40, 40 ),
  43. new THREE.MeshPhongMaterial( { color: 0xcbcbcb, specular: 0x474747 } )
  44. );
  45. plane.rotation.x = - Math.PI / 2;
  46. plane.position.y = - 0.5;
  47. scene.add( plane );
  48. plane.receiveShadow = true;
  49. // PLY file
  50. const loader = new PLYLoader();
  51. loader.load( './models/ply/ascii/dolphins.ply', function ( geometry ) {
  52. geometry.computeVertexNormals();
  53. const material = new THREE.MeshStandardMaterial( { color: 0x009cff, flatShading: true } );
  54. const mesh = new THREE.Mesh( geometry, material );
  55. mesh.position.y = - 0.2;
  56. mesh.position.z = 0.3;
  57. mesh.rotation.x = - Math.PI / 2;
  58. mesh.scale.multiplyScalar( 0.001 );
  59. mesh.castShadow = true;
  60. mesh.receiveShadow = true;
  61. scene.add( mesh );
  62. } );
  63. loader.load( './models/ply/binary/Lucy100k.ply', function ( geometry ) {
  64. geometry.computeVertexNormals();
  65. const material = new THREE.MeshStandardMaterial( { color: 0x009cff, flatShading: true } );
  66. const mesh = new THREE.Mesh( geometry, material );
  67. mesh.position.x = - 0.2;
  68. mesh.position.y = - 0.02;
  69. mesh.position.z = - 0.2;
  70. mesh.scale.multiplyScalar( 0.0006 );
  71. mesh.castShadow = true;
  72. mesh.receiveShadow = true;
  73. scene.add( mesh );
  74. } );
  75. // Lights
  76. scene.add( new THREE.HemisphereLight( 0x8d7c7c, 0x494966, 3 ) );
  77. addShadowedLight( 1, 1, 1, 0xffffff, 3.5 );
  78. addShadowedLight( 0.5, 1, - 1, 0xffd500, 3 );
  79. // renderer
  80. renderer = new THREE.WebGLRenderer( { antialias: true } );
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. renderer.shadowMap.enabled = true;
  84. container.appendChild( renderer.domElement );
  85. // stats
  86. stats = new Stats();
  87. container.appendChild( stats.dom );
  88. // resize
  89. window.addEventListener( 'resize', onWindowResize );
  90. }
  91. function addShadowedLight( x, y, z, color, intensity ) {
  92. const directionalLight = new THREE.DirectionalLight( color, intensity );
  93. directionalLight.position.set( x, y, z );
  94. scene.add( directionalLight );
  95. directionalLight.castShadow = true;
  96. const d = 1;
  97. directionalLight.shadow.camera.left = - d;
  98. directionalLight.shadow.camera.right = d;
  99. directionalLight.shadow.camera.top = d;
  100. directionalLight.shadow.camera.bottom = - d;
  101. directionalLight.shadow.camera.near = 1;
  102. directionalLight.shadow.camera.far = 4;
  103. directionalLight.shadow.mapSize.width = 1024;
  104. directionalLight.shadow.mapSize.height = 1024;
  105. directionalLight.shadow.bias = - 0.001;
  106. }
  107. function onWindowResize() {
  108. camera.aspect = window.innerWidth / window.innerHeight;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. }
  112. function animate() {
  113. requestAnimationFrame( animate );
  114. render();
  115. stats.update();
  116. }
  117. function render() {
  118. const timer = Date.now() * 0.0005;
  119. camera.position.x = Math.sin( timer ) * 2.5;
  120. camera.position.z = Math.cos( timer ) * 2.5;
  121. camera.lookAt( cameraTarget );
  122. renderer.render( scene, camera );
  123. }
  124. </script>
  125. </body>
  126. </html>