babylon.spotLight.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 setDirectionToTarget(target: Vector3): Vector3 {
  13. this.direction = Vector3.Normalize(target.subtract(this.position));
  14. return this.direction;
  15. }
  16. public computeTransformedPosition(): boolean {
  17. if (this.parent && this.parent.getWorldMatrix) {
  18. if (!this.transformedPosition) {
  19. this.transformedPosition = Vector3.Zero();
  20. }
  21. Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this.transformedPosition);
  22. return true;
  23. }
  24. return false;
  25. }
  26. public transferToEffect(effect: Effect, positionUniformName: string, directionUniformName: string): void {
  27. var normalizeDirection;
  28. if (this.parent && this.parent.getWorldMatrix) {
  29. if (!this._transformedDirection) {
  30. this._transformedDirection = Vector3.Zero();
  31. }
  32. this.computeTransformedPosition();
  33. Vector3.TransformNormalToRef(this.direction, this.parent.getWorldMatrix(), this._transformedDirection);
  34. effect.setFloat4(positionUniformName, this.transformedPosition.x, this.transformedPosition.y, this.transformedPosition.z, this.exponent);
  35. normalizeDirection = Vector3.Normalize(this._transformedDirection);
  36. } else {
  37. effect.setFloat4(positionUniformName, this.position.x, this.position.y, this.position.z, this.exponent);
  38. normalizeDirection = Vector3.Normalize(this.direction);
  39. }
  40. effect.setFloat4(directionUniformName, normalizeDirection.x, normalizeDirection.y, normalizeDirection.z, Math.cos(this.angle * 0.5));
  41. }
  42. public _getWorldMatrix(): Matrix {
  43. if (!this._worldMatrix) {
  44. this._worldMatrix = Matrix.Identity();
  45. }
  46. Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
  47. return this._worldMatrix;
  48. }
  49. }
  50. }