webgl_effects_peppersghost.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - effects - peppers ghost</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="https://threejs.org" target="_blank" rel="noopener">three.js</a> - <a href="https://en.wikipedia.org/wiki/Pepper%27s_ghost">peppers ghost effect</a> demo <br />
  12. <a href="http://www.instructables.com/id/Reflective-Prism/?ALLSTEPS" target="_blank" rel="noopener">how to build the reflective prism</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { PeppersGhostEffect } from './jsm/effects/PeppersGhostEffect.js';
  17. var container;
  18. var camera, scene, renderer, effect;
  19. var group;
  20. init();
  21. animate();
  22. function init() {
  23. container = document.createElement( 'div' );
  24. document.body.appendChild( container );
  25. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 100000 );
  26. scene = new THREE.Scene();
  27. group = new THREE.Group();
  28. scene.add( group );
  29. // Cube
  30. var geometry = new THREE.BoxBufferGeometry( 1, 1, 1 );
  31. geometry = geometry.toNonIndexed(); // ensure unique vertices for each triangle
  32. var position = geometry.attributes.position;
  33. var colors = [];
  34. var color = new THREE.Color();
  35. // generate for each side of the cube a different color
  36. for ( var i = 0; i < position.count; i += 6 ) {
  37. color.setHex( Math.random() * 0xffffff );
  38. // first face
  39. colors.push( color.r, color.g, color.b );
  40. colors.push( color.r, color.g, color.b );
  41. colors.push( color.r, color.g, color.b );
  42. // second face
  43. colors.push( color.r, color.g, color.b );
  44. colors.push( color.r, color.g, color.b );
  45. colors.push( color.r, color.g, color.b );
  46. }
  47. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  48. var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.VertexColors } );
  49. for ( var i = 0; i < 10; i ++ ) {
  50. var cube = new THREE.Mesh( geometry, material );
  51. cube.position.x = Math.random() * 2 - 1;
  52. cube.position.y = Math.random() * 2 - 1;
  53. cube.position.z = Math.random() * 2 - 1;
  54. cube.scale.multiplyScalar( Math.random() + 0.5 );
  55. group.add( cube );
  56. }
  57. renderer = new THREE.WebGLRenderer();
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. container.appendChild( renderer.domElement );
  60. effect = new PeppersGhostEffect( renderer );
  61. effect.setSize( window.innerWidth, window.innerHeight );
  62. effect.cameraDistance = 5;
  63. window.addEventListener( 'resize', onWindowResize, false );
  64. }
  65. function onWindowResize() {
  66. camera.aspect = window.innerWidth / window.innerHeight;
  67. camera.updateProjectionMatrix();
  68. effect.setSize( window.innerWidth, window.innerHeight );
  69. }
  70. function animate() {
  71. requestAnimationFrame( animate );
  72. group.rotation.y += 0.01;
  73. effect.render( scene, camera );
  74. }
  75. </script>
  76. </body>
  77. </html>