babylon.reflectionProbe.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. module BABYLON {
  2. export class ReflectionProbe{
  3. private _scene: Scene;
  4. private _renderTargetTexture: RenderTargetTexture;
  5. private _projectionMatrix: Matrix;
  6. private _viewMatrix = Matrix.Identity();
  7. private _target = Vector3.Zero();
  8. private _add = Vector3.Zero();
  9. private _attachedMesh: AbstractMesh;
  10. public invertYAxis = false;
  11. public position = Vector3.Zero();
  12. constructor(public name: string, size: number, scene: Scene, generateMipMaps = true) {
  13. this._scene = scene;
  14. this._scene.reflectionProbes.push(this);
  15. this._renderTargetTexture = new RenderTargetTexture(name, size, scene, generateMipMaps, true, Engine.TEXTURETYPE_UNSIGNED_INT, true);
  16. this._renderTargetTexture.onBeforeRenderObservable.add((faceIndex: number) => {
  17. switch (faceIndex) {
  18. case 0:
  19. this._add.copyFromFloats(1, 0, 0);
  20. break;
  21. case 1:
  22. this._add.copyFromFloats(-1, 0, 0);
  23. break;
  24. case 2:
  25. this._add.copyFromFloats(0, this.invertYAxis ? 1 : -1, 0);
  26. break;
  27. case 3:
  28. this._add.copyFromFloats(0, this.invertYAxis ? -1 : 1, 0);
  29. break;
  30. case 4:
  31. this._add.copyFromFloats(0, 0, 1);
  32. break;
  33. case 5:
  34. this._add.copyFromFloats(0, 0, -1);
  35. break;
  36. }
  37. if (this._attachedMesh) {
  38. this.position.copyFrom(this._attachedMesh.getAbsolutePosition());
  39. }
  40. this.position.addToRef(this._add, this._target);
  41. Matrix.LookAtLHToRef(this.position, this._target, Vector3.Up(), this._viewMatrix);
  42. scene.setTransformMatrix(this._viewMatrix, this._projectionMatrix);
  43. scene._forcedViewPosition = this.position;
  44. });
  45. this._renderTargetTexture.onAfterUnbindObservable.add(() => {
  46. scene._forcedViewPosition = null;
  47. scene.updateTransformMatrix(true);
  48. });
  49. if (scene.activeCamera) {
  50. this._projectionMatrix = Matrix.PerspectiveFovLH(Math.PI / 2, 1, scene.activeCamera.minZ, scene.activeCamera.maxZ);
  51. }
  52. }
  53. public get samples(): number {
  54. return this._renderTargetTexture.samples;
  55. }
  56. public set samples(value: number) {
  57. this._renderTargetTexture.samples = value;
  58. }
  59. public get refreshRate(): number {
  60. return this._renderTargetTexture.refreshRate;
  61. }
  62. public set refreshRate(value: number) {
  63. this._renderTargetTexture.refreshRate = value;
  64. }
  65. public getScene(): Scene {
  66. return this._scene;
  67. }
  68. public get cubeTexture(): RenderTargetTexture {
  69. return this._renderTargetTexture;
  70. }
  71. public get renderList(): Nullable<AbstractMesh[]> {
  72. return this._renderTargetTexture.renderList;
  73. }
  74. public attachToMesh(mesh: AbstractMesh): void {
  75. this._attachedMesh = mesh;
  76. }
  77. /**
  78. * Specifies whether or not the stencil and depth buffer are cleared between two rendering groups.
  79. *
  80. * @param renderingGroupId The rendering group id corresponding to its index
  81. * @param autoClearDepthStencil Automatically clears depth and stencil between groups if true.
  82. */
  83. public setRenderingAutoClearDepthStencil(renderingGroupId: number, autoClearDepthStencil: boolean): void {
  84. this._renderTargetTexture.setRenderingAutoClearDepthStencil(renderingGroupId, autoClearDepthStencil);
  85. }
  86. public dispose() {
  87. var index = this._scene.reflectionProbes.indexOf(this);
  88. if (index !== -1) {
  89. // Remove from the scene if found
  90. this._scene.reflectionProbes.splice(index, 1);
  91. }
  92. if (this._renderTargetTexture) {
  93. this._renderTargetTexture.dispose();
  94. (<any>this._renderTargetTexture) = null;
  95. }
  96. }
  97. }
  98. }