babylon.mirrorTexture.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.MirrorTexture = function (name, size, scene, generateMipMaps) {
  4. this._scene = scene;
  5. this._scene.textures.push(this);
  6. this.name = name;
  7. this._generateMipMaps = generateMipMaps;
  8. this._texture = scene.getEngine().createRenderTargetTexture(size, generateMipMaps);
  9. // Render list
  10. this.renderList = [];
  11. // Rendering groups
  12. this._renderingManager = new BABYLON.RenderingManager(scene);
  13. // Internals
  14. this._transformMatrix = BABYLON.Matrix.Zero();
  15. this._mirrorMatrix = BABYLON.Matrix.Zero();
  16. };
  17. BABYLON.MirrorTexture.prototype = Object.create(BABYLON.RenderTargetTexture.prototype);
  18. // Members
  19. BABYLON.MirrorTexture.prototype.mirrorPlane = new BABYLON.Plane(0, 1, 0, 1);
  20. // Method
  21. BABYLON.MirrorTexture.prototype.onBeforeRender = function () {
  22. var scene = this._scene;
  23. BABYLON.Matrix.ReflectionToRef(this.mirrorPlane, this._mirrorMatrix);
  24. this._savedViewMatrix = scene.getViewMatrix();
  25. this._mirrorMatrix.multiplyToRef(this._savedViewMatrix, this._transformMatrix);
  26. scene.setTransformMatrix(this._transformMatrix, scene.getProjectionMatrix());
  27. BABYLON.clipPlane = this.mirrorPlane;
  28. scene.getEngine().cullBackFaces = false;
  29. };
  30. BABYLON.MirrorTexture.prototype.onAfterRender = function () {
  31. var scene = this._scene;
  32. scene.setTransformMatrix(this._savedViewMatrix, scene.getProjectionMatrix());
  33. scene.getEngine().cullBackFaces = true;
  34. delete BABYLON.clipPlane;
  35. };
  36. BABYLON.MirrorTexture.prototype.clone = function () {
  37. var textureSize = this.getSize();
  38. var newTexture = new BABYLON.DynamicTexture(this.name, textureSize.width, this._scene, this._generateMipMaps);
  39. // Base texture
  40. newTexture.hasAlpha = this.hasAlpha;
  41. newTexture.level = this.level;
  42. // Mirror Texture
  43. newTexture.mirrorPlane = this.mirrorPlane.clone();
  44. newTexture.renderList = this.renderList.slice(0);
  45. return newTexture;
  46. };
  47. })();