Mirror.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**
  2. * @author Slayvin / http://slayvin.net
  3. */
  4. THREE.ShaderLib['mirror'] = {
  5. uniforms: { "mirrorColor": { type: "c", value: new THREE.Color(0x7F7F7F) },
  6. "mirrorSampler": { type: "t", value: null },
  7. "textureMatrix" : { type: "m4", value: new THREE.Matrix4() }
  8. },
  9. vertexShader: [
  10. "uniform mat4 textureMatrix;",
  11. "varying vec4 mirrorCoord;",
  12. "void main() {",
  13. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  14. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  15. "mirrorCoord = textureMatrix * worldPosition;",
  16. "gl_Position = projectionMatrix * mvPosition;",
  17. "}"
  18. ].join("\n"),
  19. fragmentShader: [
  20. "uniform vec3 mirrorColor;",
  21. "uniform sampler2D mirrorSampler;",
  22. "varying vec4 mirrorCoord;",
  23. "float blendOverlay(float base, float blend) {",
  24. "return( base < 0.5 ? ( 2.0 * base * blend ) : (1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );",
  25. "}",
  26. "void main() {",
  27. "vec4 color = texture2DProj(mirrorSampler, mirrorCoord);",
  28. "color = vec4(blendOverlay(mirrorColor.r, color.r), blendOverlay(mirrorColor.g, color.g), blendOverlay(mirrorColor.b, color.b), 1.0);",
  29. "gl_FragColor = color;",
  30. "}"
  31. ].join("\n")
  32. };
  33. THREE.Mirror = function ( renderer, camera, options ) {
  34. THREE.Object3D.call( this );
  35. this.name = 'mirror_' + this.id;
  36. function isPowerOfTwo ( value ) {
  37. return ( value & ( value - 1 ) ) === 0;
  38. };
  39. options = options || {};
  40. this.matrixNeedsUpdate = true;
  41. var width = options.textureWidth !== undefined ? options.textureWidth : 512;
  42. var height = options.textureHeight !== undefined ? options.textureHeight : 512;
  43. this.clipBias = options.clipBias !== undefined ? options.clipBias : 0.0;
  44. var mirrorColor = options.color !== undefined ? new THREE.Color(options.color) : new THREE.Color(0x7F7F7F);
  45. this.renderer = renderer;
  46. this.mirrorPlane = new THREE.Plane();
  47. this.normal = new THREE.Vector3( 0, 0, 1 );
  48. this.mirrorWorldPosition = new THREE.Vector3();
  49. this.cameraWorldPosition = new THREE.Vector3();
  50. this.rotationMatrix = new THREE.Matrix4();
  51. this.lookAtPosition = new THREE.Vector3(0, 0, -1);
  52. this.clipPlane = new THREE.Vector4();
  53. // For debug only, show the normal and plane of the mirror
  54. var debugMode = options.debugMode !== undefined ? options.debugMode : false;
  55. if ( debugMode ) {
  56. var arrow = new THREE.ArrowHelper(new THREE.Vector3( 0, 0, 1 ), new THREE.Vector3( 0, 0, 0 ), 10, 0xffff80 );
  57. var planeGeometry = new THREE.Geometry();
  58. planeGeometry.vertices.push( new THREE.Vector3( -10, -10, 0 ) );
  59. planeGeometry.vertices.push( new THREE.Vector3( 10, -10, 0 ) );
  60. planeGeometry.vertices.push( new THREE.Vector3( 10, 10, 0 ) );
  61. planeGeometry.vertices.push( new THREE.Vector3( -10, 10, 0 ) );
  62. planeGeometry.vertices.push( planeGeometry.vertices[0] );
  63. var plane = new THREE.Line( planeGeometry, new THREE.LineBasicMaterial( { color: 0xffff80 } ) );
  64. this.add(arrow);
  65. this.add(plane);
  66. }
  67. if ( camera instanceof THREE.PerspectiveCamera ) {
  68. this.camera = camera;
  69. } else {
  70. this.camera = new THREE.PerspectiveCamera();
  71. console.log( this.name + ': camera is not a Perspective Camera!' );
  72. }
  73. this.textureMatrix = new THREE.Matrix4();
  74. this.mirrorCamera = this.camera.clone();
  75. this.texture = new THREE.WebGLRenderTarget( width, height );
  76. this.tempTexture = new THREE.WebGLRenderTarget( width, height );
  77. var mirrorShader = THREE.ShaderLib[ "mirror" ];
  78. var mirrorUniforms = THREE.UniformsUtils.clone( mirrorShader.uniforms );
  79. this.material = new THREE.ShaderMaterial( {
  80. fragmentShader: mirrorShader.fragmentShader,
  81. vertexShader: mirrorShader.vertexShader,
  82. uniforms: mirrorUniforms
  83. } );
  84. this.material.uniforms.mirrorSampler.value = this.texture;
  85. this.material.uniforms.mirrorColor.value = mirrorColor;
  86. this.material.uniforms.textureMatrix.value = this.textureMatrix;
  87. if ( !isPowerOfTwo(width) || !isPowerOfTwo( height ) ) {
  88. this.texture.generateMipmaps = false;
  89. this.tempTexture.generateMipmaps = false;
  90. }
  91. this.updateTextureMatrix();
  92. this.render();
  93. };
  94. THREE.Mirror.prototype = Object.create( THREE.Object3D.prototype );
  95. THREE.Mirror.prototype.renderWithMirror = function ( otherMirror ) {
  96. // update the mirror matrix to mirror the current view
  97. this.updateTextureMatrix();
  98. this.matrixNeedsUpdate = false;
  99. // set the camera of the other mirror so the mirrored view is the reference view
  100. var tempCamera = otherMirror.camera;
  101. otherMirror.camera = this.mirrorCamera;
  102. // render the other mirror in temp texture
  103. otherMirror.renderTemp();
  104. otherMirror.material.uniforms.mirrorSampler.value = otherMirror.tempTexture;
  105. // render the current mirror
  106. this.render();
  107. this.matrixNeedsUpdate = true;
  108. // restore material and camera of other mirror
  109. otherMirror.material.uniforms.mirrorSampler.value = otherMirror.texture;
  110. otherMirror.camera = tempCamera;
  111. // restore texture matrix of other mirror
  112. otherMirror.updateTextureMatrix();
  113. };
  114. THREE.Mirror.prototype.updateTextureMatrix = function () {
  115. var sign = THREE.Math.sign;
  116. this.updateMatrixWorld();
  117. this.camera.updateMatrixWorld();
  118. this.mirrorWorldPosition.getPositionFromMatrix( this.matrixWorld );
  119. this.cameraWorldPosition.getPositionFromMatrix( this.camera.matrixWorld );
  120. this.rotationMatrix.extractRotation( this.matrixWorld );
  121. this.normal.set( 0, 0, 1 );
  122. this.normal.applyMatrix4( this.rotationMatrix );
  123. var view = this.mirrorWorldPosition.clone().sub( this.cameraWorldPosition );
  124. var reflectView = view.reflect( this.normal );
  125. reflectView.add( this.mirrorWorldPosition );
  126. this.rotationMatrix.extractRotation( this.camera.matrixWorld );
  127. this.lookAtPosition.set(0, 0, -1);
  128. this.lookAtPosition.applyMatrix4( this.rotationMatrix );
  129. this.lookAtPosition.add( this.cameraWorldPosition );
  130. var target = this.mirrorWorldPosition.clone().sub( this.lookAtPosition );
  131. var reflectTarget = target.reflect( this.normal );
  132. reflectTarget.add( this.mirrorWorldPosition );
  133. this.up.set( 0, -1, 0 );
  134. this.up.applyMatrix4( this.rotationMatrix );
  135. var reflectUp = this.up.reflect( this.normal );
  136. this.mirrorCamera.position.copy(reflectView);
  137. this.mirrorCamera.up = reflectUp;
  138. this.mirrorCamera.lookAt( reflectTarget );
  139. this.mirrorCamera.updateProjectionMatrix();
  140. this.mirrorCamera.updateMatrixWorld();
  141. this.mirrorCamera.matrixWorldInverse.getInverse( this.mirrorCamera.matrixWorld );
  142. // Update the texture matrix
  143. this.textureMatrix.set( 0.5, 0.0, 0.0, 0.5,
  144. 0.0, 0.5, 0.0, 0.5,
  145. 0.0, 0.0, 0.5, 0.5,
  146. 0.0, 0.0, 0.0, 1.0 );
  147. this.textureMatrix.multiply( this.mirrorCamera.projectionMatrix );
  148. this.textureMatrix.multiply( this.mirrorCamera.matrixWorldInverse );
  149. // Now update projection matrix with new clip plane, implementing code from: http://www.terathon.com/code/oblique.html
  150. // Paper explaining this technique: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  151. this.mirrorPlane.setFromNormalAndCoplanarPoint( this.normal, this.mirrorWorldPosition );
  152. this.mirrorPlane.applyMatrix4( this.mirrorCamera.matrixWorldInverse );
  153. this.clipPlane.set( this.mirrorPlane.normal.x, this.mirrorPlane.normal.y, this.mirrorPlane.normal.z, this.mirrorPlane.constant );
  154. var q = new THREE.Vector4();
  155. var projectionMatrix = this.mirrorCamera.projectionMatrix;
  156. q.x = ( sign(this.clipPlane.x) + projectionMatrix.elements[8] ) / projectionMatrix.elements[0];
  157. q.y = ( sign(this.clipPlane.y) + projectionMatrix.elements[9] ) / projectionMatrix.elements[5];
  158. q.z = - 1.0;
  159. q.w = ( 1.0 + projectionMatrix.elements[10] ) / projectionMatrix.elements[14];
  160. // Calculate the scaled plane vector
  161. var c = new THREE.Vector4();
  162. c = this.clipPlane.multiplyScalar( 2.0 / this.clipPlane.dot(q) );
  163. // Replacing the third row of the projection matrix
  164. projectionMatrix.elements[2] = c.x;
  165. projectionMatrix.elements[6] = c.y;
  166. projectionMatrix.elements[10] = c.z + 1.0 - this.clipBias;
  167. projectionMatrix.elements[14] = c.w;
  168. };
  169. THREE.Mirror.prototype.render = function () {
  170. if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();
  171. this.matrixNeedsUpdate = true;
  172. // Render the mirrored view of the current scene into the target texture
  173. var scene = this;
  174. while ( scene.parent !== undefined ) {
  175. scene = scene.parent;
  176. }
  177. if ( scene !== undefined && scene instanceof THREE.Scene) {
  178. this.renderer.render( scene, this.mirrorCamera, this.texture, true );
  179. }
  180. };
  181. THREE.Mirror.prototype.renderTemp = function () {
  182. if ( this.matrixNeedsUpdate ) this.updateTextureMatrix();
  183. this.matrixNeedsUpdate = true;
  184. // Render the mirrored view of the current scene into the target texture
  185. var scene = this;
  186. while ( scene.parent !== undefined ) {
  187. scene = scene.parent;
  188. }
  189. if ( scene !== undefined && scene instanceof THREE.Scene) {
  190. this.renderer.render( scene, this.mirrorCamera, this.tempTexture, true );
  191. }
  192. };