babylon.spotLight.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. module BABYLON {
  2. export class SpotLight extends Light {
  3. private _transformedDirection: Vector3;
  4. private _transformedPosition: 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 setDirectionToTarget(target: Vector3): Vector3 {
  10. this.direction = BABYLON.Vector3.Normalize(target.subtract(this.position));
  11. return this.direction;
  12. }
  13. public transferToEffect(effect: Effect, positionUniformName: string, directionUniformName: string): void {
  14. var normalizeDirection;
  15. if (this.parent && this.parent.getWorldMatrix) {
  16. if (!this._transformedDirection) {
  17. this._transformedDirection = BABYLON.Vector3.Zero();
  18. }
  19. if (!this._transformedPosition) {
  20. this._transformedPosition = BABYLON.Vector3.Zero();
  21. }
  22. var parentWorldMatrix = this.parent.getWorldMatrix();
  23. BABYLON.Vector3.TransformCoordinatesToRef(this.position, parentWorldMatrix, this._transformedPosition);
  24. BABYLON.Vector3.TransformNormalToRef(this.direction, parentWorldMatrix, this._transformedDirection);
  25. effect.setFloat4(positionUniformName, this._transformedPosition.x, this._transformedPosition.y, this._transformedPosition.z, this.exponent);
  26. normalizeDirection = BABYLON.Vector3.Normalize(this._transformedDirection);
  27. } else {
  28. effect.setFloat4(positionUniformName, this.position.x, this.position.y, this.position.z, this.exponent);
  29. normalizeDirection = BABYLON.Vector3.Normalize(this.direction);
  30. }
  31. effect.setFloat4(directionUniformName, normalizeDirection.x, normalizeDirection.y, normalizeDirection.z, Math.cos(this.angle * 0.5));
  32. }
  33. public _getWorldMatrix(): Matrix {
  34. if (!this._worldMatrix) {
  35. this._worldMatrix = BABYLON.Matrix.Identity();
  36. }
  37. BABYLON.Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
  38. return this._worldMatrix;
  39. }
  40. }
  41. }