webgl_loader_ply.html 4.7 KB

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