webxr_vr_sandbox.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - sandbox</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <script src="js/vr/HelioWebXRPolyfill.js"></script>
  11. <script type="module">
  12. import * as THREE from '../build/three.module.js';
  13. import { Lensflare, LensflareElement } from './jsm/objects/Lensflare.js';
  14. import { Reflector } from './jsm/objects/Reflector.js';
  15. import { VRButton } from './jsm/webxr/VRButton.js';
  16. var camera, scene, renderer;
  17. var reflector;
  18. init();
  19. animate();
  20. function init() {
  21. var background = new THREE.CubeTextureLoader()
  22. .setPath( 'textures/cube/MilkyWay/' )
  23. .load( [ 'dark-s_px.jpg', 'dark-s_nx.jpg', 'dark-s_py.jpg', 'dark-s_ny.jpg', 'dark-s_pz.jpg', 'dark-s_nz.jpg' ] );
  24. background.format = THREE.RGBFormat;
  25. scene = new THREE.Scene();
  26. scene.background = background;
  27. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  28. camera.position.set( 0, 1.6, 2 );
  29. var geometry = new THREE.TorusKnotBufferGeometry( 0.4, 0.15, 150, 20 );
  30. var material = new THREE.MeshStandardMaterial( { roughness: 0.01, metalness: 0.2, envMap: background } );
  31. var mesh = new THREE.Mesh( geometry, material );
  32. mesh.position.y = 0.75;
  33. mesh.position.z = - 2;
  34. mesh.castShadow = true;
  35. mesh.receiveShadow = true;
  36. scene.add( mesh );
  37. var geometry = new THREE.BoxBufferGeometry( 1.5, 0.1, 1.5 );
  38. var material = new THREE.MeshPhongMaterial();
  39. var mesh = new THREE.Mesh( geometry, material );
  40. mesh.position.y = - 0.2;
  41. mesh.position.z = - 2;
  42. mesh.castShadow = true;
  43. mesh.receiveShadow = true;
  44. scene.add( mesh );
  45. var light = new THREE.DirectionalLight( 0x8800ff );
  46. light.position.set( - 1, 1.5, - 1.5 );
  47. light.castShadow = true;
  48. light.shadow.camera.zoom = 4;
  49. scene.add( light );
  50. light.target.position.set( 0, 0, - 2 );
  51. scene.add( light.target );
  52. // var helper = new CameraHelper( light.shadow.camera );
  53. // scene.add( helper );
  54. var light = new THREE.DirectionalLight( 0xff0000 );
  55. light.position.set( 1, 1.5, - 2.5 );
  56. light.castShadow = true;
  57. light.shadow.camera.zoom = 4;
  58. scene.add( light );
  59. light.target.position.set( 0, 0, - 2 );
  60. scene.add( light.target );
  61. // var helper = new CameraHelper( light.shadow.camera );
  62. // scene.add( helper );
  63. // lensflare
  64. var loader = new THREE.TextureLoader();
  65. var texture0 = loader.load( "textures/lensflare/lensflare0.png" );
  66. var texture3 = loader.load( "textures/lensflare/lensflare3.png" );
  67. var lensflare = new Lensflare();
  68. lensflare.position.set( 0, 5, - 5 );
  69. lensflare.addElement( new LensflareElement( texture0, 700, 0 ) );
  70. lensflare.addElement( new LensflareElement( texture3, 60, 0.6 ) );
  71. lensflare.addElement( new LensflareElement( texture3, 70, 0.7 ) );
  72. lensflare.addElement( new LensflareElement( texture3, 120, 0.9 ) );
  73. lensflare.addElement( new LensflareElement( texture3, 70, 1 ) );
  74. scene.add( lensflare );
  75. //
  76. var geometry = new THREE.PlaneBufferGeometry( 1.4, 1.4 );
  77. reflector = new Reflector( geometry, {
  78. textureWidth: window.innerWidth * window.devicePixelRatio,
  79. textureHeight: window.innerHeight * window.devicePixelRatio
  80. } );
  81. reflector.position.x = 1;
  82. reflector.position.y = 0.5;
  83. reflector.position.z = - 3;
  84. reflector.rotation.y = - Math.PI / 4;
  85. scene.add( reflector );
  86. var geometry = new THREE.BoxBufferGeometry( 1.5, 1.5, 0.1 );
  87. var material = new THREE.MeshPhongMaterial();
  88. var mesh = new THREE.Mesh( geometry, material );
  89. mesh.position.z = - 0.07;
  90. mesh.castShadow = true;
  91. mesh.receiveShadow = true;
  92. reflector.add( mesh );
  93. //
  94. renderer = new THREE.WebGLRenderer( { antialias: true } );
  95. renderer.autoClear = false;
  96. renderer.setPixelRatio( window.devicePixelRatio );
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. renderer.shadowMap.enabled = true;
  99. renderer.xr.enabled = true;
  100. document.body.appendChild( renderer.domElement );
  101. document.body.appendChild( VRButton.createButton( renderer ) );
  102. //
  103. window.addEventListener( 'resize', onWindowResize, false );
  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. renderer.setAnimationLoop( render );
  112. }
  113. function render() {
  114. var time = performance.now() * 0.0002;
  115. var mesh = scene.children[ 0 ];
  116. mesh.rotation.x = time * 2;
  117. mesh.rotation.y = time * 5;
  118. renderer.render( scene, camera );
  119. }
  120. </script>
  121. </body>
  122. </html>