babylon.mirrorTexture.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. module BABYLON {
  2. export class MirrorTexture extends RenderTargetTexture {
  3. public mirrorPlane = new BABYLON.Plane(0, 1, 0, 1);
  4. private _transformMatrix = BABYLON.Matrix.Zero();
  5. private _mirrorMatrix = BABYLON.Matrix.Zero();
  6. private _savedViewMatrix: Matrix;
  7. constructor(name: string, size: number, scene: Scene, generateMipMaps?: boolean) {
  8. super(name, size, scene, generateMipMaps, true);
  9. this.onBeforeRender = () => {
  10. BABYLON.Matrix.ReflectionToRef(this.mirrorPlane, this._mirrorMatrix);
  11. this._savedViewMatrix = scene.getViewMatrix();
  12. this._mirrorMatrix.multiplyToRef(this._savedViewMatrix, this._transformMatrix);
  13. scene.setTransformMatrix(this._transformMatrix, scene.getProjectionMatrix());
  14. scene.clipPlane = this.mirrorPlane;
  15. scene.getEngine().cullBackFaces = false;
  16. }
  17. this.onAfterRender = () => {
  18. scene.setTransformMatrix(this._savedViewMatrix, scene.getProjectionMatrix());
  19. scene.getEngine().cullBackFaces = true;
  20. delete scene.clipPlane;
  21. }
  22. }
  23. public clone(): MirrorTexture {
  24. var textureSize = this.getSize();
  25. var newTexture = new BABYLON.MirrorTexture(this.name, textureSize.width, this.getScene(), this._generateMipMaps);
  26. // Base texture
  27. newTexture.hasAlpha = this.hasAlpha;
  28. newTexture.level = this.level;
  29. // Mirror Texture
  30. newTexture.mirrorPlane = this.mirrorPlane.clone();
  31. newTexture.renderList = this.renderList.slice(0);
  32. return newTexture;
  33. }
  34. }
  35. }