webgl_loader_ply.html 4.5 KB

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