webgl_loader_ply.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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">three.js</a> -
  37. PLY loader test by <a href="https://github.com/menway">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.min.js"></script>
  40. <script src="js/loaders/PLYLoader.js"></script>
  41. <script src="js/Detector.js"></script>
  42. <script src="js/libs/stats.min.js"></script>
  43. <script>
  44. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  45. var container, stats;
  46. var camera, cameraTarget, scene, renderer;
  47. init();
  48. animate();
  49. function init() {
  50. container = document.createElement( 'div' );
  51. document.body.appendChild( container );
  52. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 15 );
  53. camera.position.set( 3, 0.15, 3 );
  54. cameraTarget = new THREE.Vector3( 0, -0.25, 0 );
  55. scene = new THREE.Scene();
  56. scene.fog = new THREE.Fog( 0x72645b, 2, 15 );
  57. // Ground
  58. var plane = new THREE.Mesh( new THREE.PlaneGeometry( 40, 40 ), new THREE.MeshPhongMaterial( { ambient: 0x999999, color: 0x999999, specular: 0x101010 } ) );
  59. plane.rotation.x = -Math.PI/2;
  60. plane.position.y = -0.5;
  61. scene.add( plane );
  62. plane.receiveShadow = true;
  63. // PLY file
  64. var loader = new THREE.PLYLoader();
  65. loader.addEventListener( 'load', function ( event ) {
  66. var geometry = event.content;
  67. var material = new THREE.MeshPhongMaterial( { ambient: 0x0055ff, color: 0x0055ff, specular: 0x111111, shininess: 200 } );
  68. var mesh = new THREE.Mesh( geometry, material );
  69. mesh.position.set( 0, - 0.25, 0 );
  70. mesh.rotation.set( 0, - Math.PI / 2, 0 );
  71. mesh.scale.set( 0.001, 0.001, 0.001 );
  72. mesh.castShadow = true;
  73. mesh.receiveShadow = true;
  74. scene.add( mesh );
  75. } );
  76. loader.load( './models/ply/ascii/dolphins.ply' );
  77. // Lights
  78. scene.add( new THREE.AmbientLight( 0x777777 ) );
  79. addShadowedLight( 1, 1, 1, 0xffffff, 1.35 );
  80. addShadowedLight( 0.5, 1, -1, 0xffaa00, 1 );
  81. // renderer
  82. renderer = new THREE.WebGLRenderer( { antialias: true } );
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. renderer.setClearColor( scene.fog.color, 1 );
  85. renderer.gammaInput = true;
  86. renderer.gammaOutput = true;
  87. renderer.shadowMapEnabled = true;
  88. renderer.shadowMapCullFace = THREE.CullFaceBack;
  89. container.appendChild( renderer.domElement );
  90. // stats
  91. stats = new Stats();
  92. stats.domElement.style.position = 'absolute';
  93. stats.domElement.style.top = '0px';
  94. container.appendChild( stats.domElement );
  95. // resize
  96. window.addEventListener( 'resize', onWindowResize, false );
  97. }
  98. function addShadowedLight( x, y, z, color, intensity ) {
  99. var directionalLight = new THREE.DirectionalLight( color, intensity );
  100. directionalLight.position.set( x, y, z )
  101. scene.add( directionalLight );
  102. directionalLight.castShadow = true;
  103. // directionalLight.shadowCameraVisible = true;
  104. var d = 1;
  105. directionalLight.shadowCameraLeft = -d;
  106. directionalLight.shadowCameraRight = d;
  107. directionalLight.shadowCameraTop = d;
  108. directionalLight.shadowCameraBottom = -d;
  109. directionalLight.shadowCameraNear = 1;
  110. directionalLight.shadowCameraFar = 4;
  111. directionalLight.shadowMapWidth = 1024;
  112. directionalLight.shadowMapHeight = 1024;
  113. directionalLight.shadowBias = -0.005;
  114. directionalLight.shadowDarkness = 0.15;
  115. }
  116. function onWindowResize() {
  117. camera.aspect = window.innerWidth / window.innerHeight;
  118. camera.updateProjectionMatrix();
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. }
  121. function animate() {
  122. requestAnimationFrame( animate );
  123. render();
  124. stats.update();
  125. }
  126. function render() {
  127. var timer = Date.now() * 0.0005;
  128. camera.position.x = Math.sin( timer ) * 3;
  129. camera.position.z = Math.cos( timer ) * 3;
  130. camera.lookAt( cameraTarget );
  131. renderer.render( scene, camera );
  132. }
  133. </script>
  134. </body>
  135. </html>