babylon.shadowGenerator.js 5.8 KB

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