webgl_loader_ply.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. color: #fff;
  16. position: absolute;
  17. top: 10px;
  18. width: 100%;
  19. text-align: center;
  20. z-index: 100;
  21. display:block;
  22. }
  23. a { color: skyblue }
  24. .button { background:#999; color:#eee; padding:0.2em 0.5em; cursor:pointer }
  25. .highlight { background:orange; color:#fff; }
  26. span {
  27. display: inline-block;
  28. width: 60px;
  29. float: left;
  30. text-align: center;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div id="info">
  36. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> -
  37. PLY loader test by <a href="https://github.com/menway" target="_blank" rel="noopener">Wei Meng</a>. Image from <a href="http://people.sc.fsu.edu/~jburkardt/data/ply/ply.html">John Burkardt</a>
  38. </div>
  39. <script src="../build/three.js"></script>
  40. <script src="js/loaders/PLYLoader.js"></script>
  41. <script src="js/WebGL.js"></script>
  42. <script src="js/libs/stats.min.js"></script>
  43. <script>
  44. if ( WEBGL.isWebGLAvailable() === false ) {
  45. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  46. }
  47. var container, stats;
  48. var camera, cameraTarget, scene, renderer;
  49. init();
  50. animate();
  51. function init() {
  52. container = document.createElement( 'div' );
  53. document.body.appendChild( container );
  54. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
  55. camera.position.set( 3, 0.15, 3 );
  56. cameraTarget = new THREE.Vector3( 0, -0.1, 0 );
  57. scene = new THREE.Scene();
  58. scene.background = new THREE.Color( 0x72645b );
  59. scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
  60. // Ground
  61. var plane = new THREE.Mesh(
  62. new THREE.PlaneBufferGeometry( 40, 40 ),
  63. new THREE.MeshPhongMaterial( { color: 0x999999, specular: 0x101010 } )
  64. );
  65. plane.rotation.x = -Math.PI/2;
  66. plane.position.y = -0.5;
  67. scene.add( plane );
  68. plane.receiveShadow = true;
  69. // PLY file
  70. var loader = new THREE.PLYLoader();
  71. loader.load( './models/ply/ascii/dolphins.ply', function ( geometry ) {
  72. geometry.computeVertexNormals();
  73. var material = new THREE.MeshStandardMaterial( { color: 0x0055ff, flatShading: true } );
  74. var mesh = new THREE.Mesh( geometry, material );
  75. mesh.position.y = - 0.2;
  76. mesh.position.z = 0.3;
  77. mesh.rotation.x = - Math.PI / 2;
  78. mesh.scale.multiplyScalar( 0.001 );
  79. mesh.castShadow = true;
  80. mesh.receiveShadow = true;
  81. scene.add( mesh );
  82. } );
  83. loader.load( './models/ply/binary/Lucy100k.ply', function ( geometry ) {
  84. geometry.computeVertexNormals();
  85. var material = new THREE.MeshStandardMaterial( { color: 0x0055ff, flatShading: true } );
  86. var mesh = new THREE.Mesh( geometry, material );
  87. mesh.position.x = - 0.2;
  88. mesh.position.y = - 0.02;
  89. mesh.position.z = - 0.2;
  90. mesh.scale.multiplyScalar( 0.0006 );
  91. mesh.castShadow = true;
  92. mesh.receiveShadow = true;
  93. scene.add( mesh );
  94. } );
  95. // Lights
  96. scene.add( new THREE.HemisphereLight( 0x443333, 0x111122 ) );
  97. addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
  98. addShadowedLight( 0.5, 1, -1, 0xffaa00, 1 );
  99. // renderer
  100. renderer = new THREE.WebGLRenderer( { antialias: true } );
  101. renderer.setPixelRatio( window.devicePixelRatio );
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. renderer.gammaInput = true;
  104. renderer.gammaOutput = true;
  105. renderer.shadowMap.enabled = true;
  106. container.appendChild( renderer.domElement );
  107. // stats
  108. stats = new Stats();
  109. container.appendChild( stats.dom );
  110. // resize
  111. window.addEventListener( 'resize', onWindowResize, false );
  112. }
  113. function addShadowedLight( x, y, z, color, intensity ) {
  114. var directionalLight = new THREE.DirectionalLight( color, intensity );
  115. directionalLight.position.set( x, y, z );
  116. scene.add( directionalLight );
  117. directionalLight.castShadow = true;
  118. var d = 1;
  119. directionalLight.shadow.camera.left = -d;
  120. directionalLight.shadow.camera.right = d;
  121. directionalLight.shadow.camera.top = d;
  122. directionalLight.shadow.camera.bottom = -d;
  123. directionalLight.shadow.camera.near = 1;
  124. directionalLight.shadow.camera.far = 4;
  125. directionalLight.shadow.mapSize.width = 1024;
  126. directionalLight.shadow.mapSize.height = 1024;
  127. directionalLight.shadow.bias = -0.001;
  128. }
  129. function onWindowResize() {
  130. camera.aspect = window.innerWidth / window.innerHeight;
  131. camera.updateProjectionMatrix();
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. }
  134. function animate() {
  135. requestAnimationFrame( animate );
  136. render();
  137. stats.update();
  138. }
  139. function render() {
  140. var timer = Date.now() * 0.0005;
  141. camera.position.x = Math.sin( timer ) * 2.5;
  142. camera.position.z = Math.cos( timer ) * 2.5;
  143. camera.lookAt( cameraTarget );
  144. renderer.render( scene, camera );
  145. }
  146. </script>
  147. </body>
  148. </html>