babylon.shadowGenerator.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var ShadowGenerator = (function () {
  4. function ShadowGenerator(mapSize, light) {
  5. var _this = this;
  6. // Members
  7. this.useVarianceShadowMap = true;
  8. this._darkness = 0;
  9. this._transparencyShadow = false;
  10. this._viewMatrix = BABYLON.Matrix.Zero();
  11. this._projectionMatrix = BABYLON.Matrix.Zero();
  12. this._transformMatrix = BABYLON.Matrix.Zero();
  13. this._worldViewProjection = BABYLON.Matrix.Zero();
  14. this._light = light;
  15. this._scene = light.getScene();
  16. light._shadowGenerator = this;
  17. // Render target
  18. this._shadowMap = new BABYLON.RenderTargetTexture(light.name + "_shadowMap", mapSize, this._scene, false);
  19. this._shadowMap.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  20. this._shadowMap.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  21. this._shadowMap.renderParticles = false;
  22. // Custom render function
  23. var renderSubMesh = function (subMesh) {
  24. var mesh = subMesh.getMesh();
  25. var world = mesh.getWorldMatrix();
  26. var engine = _this._scene.getEngine();
  27. if (_this.isReady(mesh)) {
  28. engine.enableEffect(_this._effect);
  29. // Bones
  30. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  31. _this._effect.setMatrix("world", world);
  32. _this._effect.setMatrix("viewProjection", _this.getTransformMatrix());
  33. _this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  34. } else {
  35. world.multiplyToRef(_this.getTransformMatrix(), _this._worldViewProjection);
  36. _this._effect.setMatrix("worldViewProjection", _this._worldViewProjection);
  37. }
  38. // Bind and draw
  39. mesh.bindAndDraw(subMesh, _this._effect, false);
  40. }
  41. };
  42. this._shadowMap.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes, transparentSubMeshes) {
  43. var index;
  44. for (index = 0; index < opaqueSubMeshes.length; index++) {
  45. renderSubMesh(opaqueSubMeshes.data[index]);
  46. }
  47. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  48. renderSubMesh(alphaTestSubMeshes.data[index]);
  49. }
  50. if (_this._transparencyShadow) {
  51. for (index = 0; index < transparentSubMeshes.length; index++) {
  52. renderSubMesh(transparentSubMeshes.data[index]);
  53. }
  54. }
  55. };
  56. }
  57. ShadowGenerator.prototype.isReady = function (mesh) {
  58. var defines = [];
  59. if (this.useVarianceShadowMap) {
  60. defines.push("#define VSM");
  61. }
  62. var attribs = [BABYLON.VertexBuffer.PositionKind];
  63. if (mesh.skeleton && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesIndicesKind) && mesh.isVerticesDataPresent(BABYLON.VertexBuffer.MatricesWeightsKind)) {
  64. attribs.push(BABYLON.VertexBuffer.MatricesIndicesKind);
  65. attribs.push(BABYLON.VertexBuffer.MatricesWeightsKind);
  66. defines.push("#define BONES");
  67. defines.push("#define BonesPerMesh " + mesh.skeleton.bones.length);
  68. }
  69. // Get correct effect
  70. var join = defines.join("\n");
  71. if (this._cachedDefines != join) {
  72. this._cachedDefines = join;
  73. this._effect = this._scene.getEngine().createEffect("shadowMap", attribs, ["world", "mBones", "viewProjection", "worldViewProjection"], [], join);
  74. }
  75. return this._effect.isReady();
  76. };
  77. ShadowGenerator.prototype.getShadowMap = function () {
  78. return this._shadowMap;
  79. };
  80. ShadowGenerator.prototype.getLight = function () {
  81. return this._light;
  82. };
  83. // Methods
  84. ShadowGenerator.prototype.getTransformMatrix = function () {
  85. var lightPosition = this._light.position;
  86. var lightDirection = this._light.direction;
  87. if (this._light._computeTransformedPosition()) {
  88. lightPosition = this._light._transformedPosition;
  89. }
  90. if (!this._cachedPosition || !this._cachedDirection || !lightPosition.equals(this._cachedPosition) || !lightDirection.equals(this._cachedDirection)) {
  91. this._cachedPosition = lightPosition.clone();
  92. this._cachedDirection = lightDirection.clone();
  93. var activeCamera = this._scene.activeCamera;
  94. BABYLON.Matrix.LookAtLHToRef(lightPosition, this._light.position.add(lightDirection), BABYLON.Vector3.Up(), this._viewMatrix);
  95. BABYLON.Matrix.PerspectiveFovLHToRef(Math.PI / 2.0, 1.0, activeCamera.minZ, activeCamera.maxZ, this._projectionMatrix);
  96. this._viewMatrix.multiplyToRef(this._projectionMatrix, this._transformMatrix);
  97. }
  98. return this._transformMatrix;
  99. };
  100. ShadowGenerator.prototype.getDarkness = function () {
  101. return this._darkness;
  102. };
  103. ShadowGenerator.prototype.setDarkness = function (darkness) {
  104. if (darkness >= 1.0)
  105. this._darkness = 1.0;
  106. else if (darkness <= 0.0)
  107. this._darkness = 0.0;
  108. else
  109. this._darkness = darkness;
  110. };
  111. ShadowGenerator.prototype.setTransparencyShadow = function (hasShadow) {
  112. this._transparencyShadow = hasShadow;
  113. };
  114. ShadowGenerator.prototype.dispose = function () {
  115. this._shadowMap.dispose();
  116. };
  117. return ShadowGenerator;
  118. })();
  119. BABYLON.ShadowGenerator = ShadowGenerator;
  120. })(BABYLON || (BABYLON = {}));
  121. //# sourceMappingURL=babylon.shadowGenerator.js.map