ParallaxBarrierEffect.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author marklundin / http://mark-lundin.com/
  4. * @author alteredq / http://alteredqualia.com/
  5. */
  6. THREE.ParallaxBarrierEffect = function ( renderer ) {
  7. var _camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  8. var _scene = new THREE.Scene();
  9. var _stereo = new THREE.StereoCamera();
  10. var _params = { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBAFormat };
  11. var _renderTargetL = new THREE.WebGLRenderTarget( 512, 512, _params );
  12. var _renderTargetR = new THREE.WebGLRenderTarget( 512, 512, _params );
  13. var _material = new THREE.ShaderMaterial( {
  14. uniforms: {
  15. "mapLeft": { value: _renderTargetL.texture },
  16. "mapRight": { value: _renderTargetR.texture }
  17. },
  18. vertexShader: [
  19. "varying vec2 vUv;",
  20. "void main() {",
  21. " vUv = vec2( uv.x, uv.y );",
  22. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  23. "}"
  24. ].join( "\n" ),
  25. fragmentShader: [
  26. "uniform sampler2D mapLeft;",
  27. "uniform sampler2D mapRight;",
  28. "varying vec2 vUv;",
  29. "void main() {",
  30. " vec2 uv = vUv;",
  31. " if ( ( mod( gl_FragCoord.y, 2.0 ) ) > 1.00 ) {",
  32. " gl_FragColor = texture2D( mapLeft, uv );",
  33. " } else {",
  34. " gl_FragColor = texture2D( mapRight, uv );",
  35. " }",
  36. "}"
  37. ].join( "\n" )
  38. } );
  39. var mesh = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), _material );
  40. _scene.add( mesh );
  41. this.setSize = function ( width, height ) {
  42. renderer.setSize( width, height );
  43. var pixelRatio = renderer.getPixelRatio();
  44. _renderTargetL.setSize( width * pixelRatio, height * pixelRatio );
  45. _renderTargetR.setSize( width * pixelRatio, height * pixelRatio );
  46. };
  47. this.render = function ( scene, camera ) {
  48. scene.updateMatrixWorld();
  49. if ( camera.parent === null ) camera.updateMatrixWorld();
  50. _stereo.update( camera );
  51. renderer.render( scene, _stereo.cameraL, _renderTargetL, true );
  52. renderer.render( scene, _stereo.cameraR, _renderTargetR, true );
  53. renderer.render( _scene, _camera );
  54. };
  55. };