babylon.spotLight.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. module BABYLON {
  2. export class SpotLight extends Light implements IShadowLight {
  3. public transformedPosition: Vector3;
  4. private _transformedDirection: Vector3;
  5. private _worldMatrix: Matrix;
  6. constructor(name: string, public position: Vector3, public direction: Vector3, public angle: number, public exponent: number, scene: Scene) {
  7. super(name, scene);
  8. }
  9. public getAbsolutePosition(): Vector3 {
  10. return this.transformedPosition ? this.transformedPosition : this.position;
  11. }
  12. public setShadowProjectionMatrix(matrix: Matrix, viewMatrix: Matrix, renderList: Array<AbstractMesh>): void {
  13. var activeCamera = this.getScene().activeCamera;
  14. Matrix.PerspectiveFovLHToRef(this.angle, 1.0, activeCamera.minZ, activeCamera.maxZ, matrix);
  15. }
  16. public supportsVSM(): boolean {
  17. return true;
  18. }
  19. public needRefreshPerFrame(): boolean {
  20. return false;
  21. }
  22. public setDirectionToTarget(target: Vector3): Vector3 {
  23. this.direction = Vector3.Normalize(target.subtract(this.position));
  24. return this.direction;
  25. }
  26. public computeTransformedPosition(): boolean {
  27. if (this.parent && this.parent.getWorldMatrix) {
  28. if (!this.transformedPosition) {
  29. this.transformedPosition = Vector3.Zero();
  30. }
  31. Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this.transformedPosition);
  32. return true;
  33. }
  34. return false;
  35. }
  36. public transferToEffect(effect: Effect, positionUniformName: string, directionUniformName: string): void {
  37. var normalizeDirection;
  38. if (this.parent && this.parent.getWorldMatrix) {
  39. if (!this._transformedDirection) {
  40. this._transformedDirection = Vector3.Zero();
  41. }
  42. this.computeTransformedPosition();
  43. Vector3.TransformNormalToRef(this.direction, this.parent.getWorldMatrix(), this._transformedDirection);
  44. effect.setFloat4(positionUniformName, this.transformedPosition.x, this.transformedPosition.y, this.transformedPosition.z, this.exponent);
  45. normalizeDirection = Vector3.Normalize(this._transformedDirection);
  46. } else {
  47. effect.setFloat4(positionUniformName, this.position.x, this.position.y, this.position.z, this.exponent);
  48. normalizeDirection = Vector3.Normalize(this.direction);
  49. }
  50. effect.setFloat4(directionUniformName, normalizeDirection.x, normalizeDirection.y, normalizeDirection.z, Math.cos(this.angle * 0.5));
  51. }
  52. public _getWorldMatrix(): Matrix {
  53. if (!this._worldMatrix) {
  54. this._worldMatrix = Matrix.Identity();
  55. }
  56. Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
  57. return this._worldMatrix;
  58. }
  59. }
  60. }