webgl_loader_ply.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. function init() {
  31. container = document.createElement( 'div' );
  32. document.body.appendChild( container );
  33. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
  34. camera.position.set( 3, 0.15, 3 );
  35. cameraTarget = new THREE.Vector3( 0, - 0.1, 0 );
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0x72645b );
  38. scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
  39. // Ground
  40. const plane = new THREE.Mesh(
  41. new THREE.PlaneGeometry( 40, 40 ),
  42. new THREE.MeshPhongMaterial( { color: 0xcbcbcb, specular: 0x474747 } )
  43. );
  44. plane.rotation.x = - Math.PI / 2;
  45. plane.position.y = - 0.5;
  46. scene.add( plane );
  47. plane.receiveShadow = true;
  48. // PLY file
  49. const loader = new PLYLoader();
  50. loader.load( './models/ply/ascii/dolphins.ply', function ( geometry ) {
  51. geometry.computeVertexNormals();
  52. const material = new THREE.MeshStandardMaterial( { color: 0x009cff, flatShading: true } );
  53. const mesh = new THREE.Mesh( geometry, material );
  54. mesh.position.y = - 0.2;
  55. mesh.position.z = 0.3;
  56. mesh.rotation.x = - Math.PI / 2;
  57. mesh.scale.multiplyScalar( 0.001 );
  58. mesh.castShadow = true;
  59. mesh.receiveShadow = true;
  60. scene.add( mesh );
  61. } );
  62. loader.load( './models/ply/binary/Lucy100k.ply', function ( geometry ) {
  63. geometry.computeVertexNormals();
  64. const material = new THREE.MeshStandardMaterial( { color: 0x009cff, flatShading: true } );
  65. const mesh = new THREE.Mesh( geometry, material );
  66. mesh.position.x = - 0.2;
  67. mesh.position.y = - 0.02;
  68. mesh.position.z = - 0.2;
  69. mesh.scale.multiplyScalar( 0.0006 );
  70. mesh.castShadow = true;
  71. mesh.receiveShadow = true;
  72. scene.add( mesh );
  73. } );
  74. // Lights
  75. scene.add( new THREE.HemisphereLight( 0x8d7c7c, 0x494966, 3 ) );
  76. addShadowedLight( 1, 1, 1, 0xffffff, 3.5 );
  77. addShadowedLight( 0.5, 1, - 1, 0xffd500, 3 );
  78. // renderer
  79. renderer = new THREE.WebGLRenderer( { antialias: true } );
  80. renderer.setPixelRatio( window.devicePixelRatio );
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. renderer.setAnimationLoop( animate );
  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. render();
  114. stats.update();
  115. }
  116. function render() {
  117. const timer = Date.now() * 0.0005;
  118. camera.position.x = Math.sin( timer ) * 2.5;
  119. camera.position.z = Math.cos( timer ) * 2.5;
  120. camera.lookAt( cameraTarget );
  121. renderer.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>