2
0

webvr_shadow.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - effects - cardboard</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <style>
  8. body {
  9. margin: 0px;
  10. overflow: hidden;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <script src="../build/three.js"></script>
  16. <script src="js/controls/VRControls.js"></script>
  17. <script src="js/effects/VREffect.js"></script>
  18. <script src="js/vr/WebVR.js"></script>
  19. <script src="js/Mirror.js"></script>
  20. <script>
  21. if ( WEBVR.isAvailable() === false ) {
  22. document.body.appendChild( WEBVR.getMessage() );
  23. }
  24. //
  25. var camera, scene, renderer;
  26. var effect, controls;
  27. var mirror;
  28. init();
  29. animate();
  30. function init() {
  31. scene = new THREE.Scene();
  32. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  33. scene.add( camera );
  34. var geometry = new THREE.TorusKnotGeometry( 0.4, 0.15, 150, 20 );
  35. var material = new THREE.MeshStandardMaterial( { roughness: 0.01, metalness: 0.2 } );
  36. var mesh = new THREE.Mesh( geometry, material );
  37. mesh.position.y = 0.75;
  38. mesh.position.z = - 2;
  39. mesh.castShadow = true;
  40. mesh.receiveShadow = true;
  41. scene.add( mesh );
  42. var geometry = new THREE.BoxGeometry( 1.5, 0.1, 1.5 );
  43. var material = new THREE.MeshStandardMaterial( { roughness: 1.0, metalness: 0.0 } );
  44. var mesh = new THREE.Mesh( geometry, material );
  45. mesh.position.y = - 0.2;
  46. mesh.position.z = - 2;
  47. mesh.castShadow = true;
  48. mesh.receiveShadow = true;
  49. scene.add( mesh );
  50. var light = new THREE.DirectionalLight( 0x8800ff );
  51. light.position.set( - 1, 1.5, - 1.5 );
  52. light.castShadow = true;
  53. light.shadow.camera.zoom = 4;
  54. scene.add( light );
  55. light.target.position.set( 0, 0, - 2 );
  56. scene.add( light.target );
  57. var helper = new THREE.CameraHelper( light.shadow.camera );
  58. // scene.add( helper );
  59. var light = new THREE.DirectionalLight( 0xff0000 );
  60. light.position.set( 1, 1.5, - 2.5 );
  61. light.castShadow = true;
  62. light.shadow.camera.zoom = 4;
  63. scene.add( light );
  64. light.target.position.set( 0, 0, - 2 );
  65. scene.add( light.target );
  66. var helper = new THREE.CameraHelper( light.shadow.camera );
  67. // scene.add( helper );
  68. //
  69. mirror = new THREE.Mirror( 1.4, 1.4, { textureWidth: window.innerWidth, textureHeight: window.innerHeight } );
  70. mirror.position.x = 1;
  71. mirror.position.y = 0.5;
  72. mirror.position.z = -3;
  73. mirror.rotation.y = - Math.PI / 4;
  74. scene.add( mirror );
  75. var geometry = new THREE.BoxGeometry( 1.5, 1.5, 0.1 );
  76. var material = new THREE.MeshStandardMaterial( { roughness: 1.0, metalness: 0.0 } );
  77. var mesh = new THREE.Mesh( geometry, material );
  78. mesh.position.z = - 0.07;
  79. mesh.castShadow = true;
  80. mesh.receiveShadow = true;
  81. mirror.add( mesh );
  82. //
  83. renderer = new THREE.WebGLRenderer( { antialias: true } );
  84. renderer.setClearColor( 0x101010 );
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. renderer.shadowMap.enabled = true;
  88. document.body.appendChild( renderer.domElement );
  89. //
  90. controls = new THREE.VRControls( camera );
  91. effect = new THREE.VREffect( renderer );
  92. if ( WEBVR.isAvailable() === true ) {
  93. document.body.appendChild( WEBVR.getButton( effect ) );
  94. }
  95. //
  96. window.addEventListener( 'resize', onWindowResize, false );
  97. }
  98. function onWindowResize() {
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. effect.setSize( window.innerWidth, window.innerHeight );
  102. }
  103. function animate() {
  104. effect.requestAnimationFrame( animate );
  105. render();
  106. }
  107. function render() {
  108. var time = performance.now() * 0.0002;
  109. var mesh = scene.children[ 1 ];
  110. mesh.rotation.x = time * 2;
  111. mesh.rotation.y = time * 5;
  112. controls.update();
  113. effect.render( scene, camera );
  114. }
  115. </script>
  116. </body>
  117. </html>