babylon.spotLight.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.SpotLight = function (name, position, direction, angle, exponent, scene) {
  5. BABYLON.Light.call(this, name, scene);
  6. this.position = position;
  7. this.direction = direction;
  8. this.angle = angle;
  9. this.exponent = exponent;
  10. this.diffuse = new BABYLON.Color3(1.0, 1.0, 1.0);
  11. this.specular = new BABYLON.Color3(1.0, 1.0, 1.0);
  12. };
  13. BABYLON.SpotLight.prototype = Object.create(BABYLON.Light.prototype);
  14. // Methods
  15. BABYLON.SpotLight.prototype.transferToEffect = function (effect, positionUniformName, directionUniformName) {
  16. var normalizeDirection;
  17. if (this.parent && this.parent.getWorldMatrix) {
  18. if (!this._transformedDirection) {
  19. this._transformedDirection = BABYLON.Vector3.Zero();
  20. }
  21. if (!this._transformedPosition) {
  22. this._transformedPosition = BABYLON.Vector3.Zero();
  23. }
  24. var parentWorldMatrix = this.parent.getWorldMatrix();
  25. BABYLON.Vector3.TransformCoordinatesToRef(this.position, parentWorldMatrix, this._transformedPosition);
  26. BABYLON.Vector3.TransformNormalToRef(this.direction, parentWorldMatrix, this._transformedDirection);
  27. effect.setFloat4(positionUniformName, this._transformedPosition.x, this._transformedPosition.y, this._transformedPosition.z, this.exponent);
  28. normalizeDirection = BABYLON.Vector3.Normalize(this._transformedDirection);
  29. } else {
  30. effect.setFloat4(positionUniformName, this.position.x, this.position.y, this.position.z, this.exponent);
  31. normalizeDirection = BABYLON.Vector3.Normalize(this.direction);
  32. }
  33. effect.setFloat4(directionUniformName, normalizeDirection.x, normalizeDirection.y, normalizeDirection.z, Math.cos(this.angle * 0.5));
  34. };
  35. BABYLON.SpotLight.prototype._getWorldMatrix = function () {
  36. if (!this._worldMatrix) {
  37. this._worldMatrix = BABYLON.Matrix.Identity();
  38. }
  39. BABYLON.Matrix.TranslationToRef(this.position.x, this.position.y, this.position.z, this._worldMatrix);
  40. return this._worldMatrix;
  41. };
  42. })();