babylon.directionalLight.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. module BABYLON {
  2. export class DirectionalLight extends Light implements IShadowLight {
  3. public position: Vector3;
  4. private _transformedDirection: Vector3;
  5. public transformedPosition: Vector3;
  6. private _worldMatrix: Matrix;
  7. constructor(name: string, public direction: Vector3, scene: Scene) {
  8. super(name, scene);
  9. this.position = direction.scale(-1);
  10. }
  11. public getAbsolutePosition(): Vector3 {
  12. return this.transformedPosition ? this.transformedPosition : this.position;
  13. }
  14. public setDirectionToTarget(target: Vector3): Vector3 {
  15. this.direction = Vector3.Normalize(target.subtract(this.position));
  16. return this.direction;
  17. }
  18. public computeTransformedPosition(): boolean {
  19. if (this.parent && this.parent.getWorldMatrix) {
  20. if (!this.transformedPosition) {
  21. this.transformedPosition = Vector3.Zero();
  22. }
  23. Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this.transformedPosition);
  24. return true;
  25. }
  26. return false;
  27. }
  28. public transferToEffect(effect: Effect, directionUniformName: string): void {
  29. if (this.parent && this.parent.getWorldMatrix) {
  30. if (!this._transformedDirection) {
  31. this._transformedDirection = Vector3.Zero();
  32. }
  33. Vector3.TransformNormalToRef(this.direction, this.parent.getWorldMatrix(), this._transformedDirection);
  34. effect.setFloat4(directionUniformName, this._transformedDirection.x, this._transformedDirection.y, this._transformedDirection.z, 1);
  35. return;
  36. }
  37. effect.setFloat4(directionUniformName, this.direction.x, this.direction.y, this.direction.z, 1);
  38. }
  39. public _getWorldMatrix(): Matrix {
  40. if (!this._worldMatrix) {
  41. this._worldMatrix = Matrix.Identity();
  42. }
  43. Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
  44. return this._worldMatrix;
  45. }
  46. }
  47. }