babylon.directionalLight.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. module BABYLON {
  2. export class DirectionalLight extends Light {
  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 setDirectionToTarget(target: Vector3): Vector3 {
  12. this.direction = BABYLON.Vector3.Normalize(target.subtract(this.position));
  13. return this.direction;
  14. }
  15. public _computeTransformedPosition(): boolean {
  16. if (this.parent && this.parent.getWorldMatrix) {
  17. if (!this._transformedPosition) {
  18. this._transformedPosition = BABYLON.Vector3.Zero();
  19. }
  20. BABYLON.Vector3.TransformCoordinatesToRef(this.position, this.parent.getWorldMatrix(), this._transformedPosition);
  21. return true;
  22. }
  23. return false;
  24. }
  25. public transferToEffect(effect: Effect, directionUniformName: string): void {
  26. if (this.parent && this.parent.getWorldMatrix) {
  27. if (!this._transformedDirection) {
  28. this._transformedDirection = BABYLON.Vector3.Zero();
  29. }
  30. BABYLON.Vector3.TransformNormalToRef(this.direction, this.parent.getWorldMatrix(), this._transformedDirection);
  31. effect.setFloat4(directionUniformName, this._transformedDirection.x, this._transformedDirection.y, this._transformedDirection.z, 1);
  32. return;
  33. }
  34. effect.setFloat4(directionUniformName, this.direction.x, this.direction.y, this.direction.z, 1);
  35. }
  36. public _getWorldMatrix(): Matrix {
  37. if (!this._worldMatrix) {
  38. this._worldMatrix = BABYLON.Matrix.Identity();
  39. }
  40. BABYLON.Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
  41. return this._worldMatrix;
  42. }
  43. }
  44. }