babylon.shadowGenerator.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.ShadowGenerator = function (mapSize, light) {
  4. this._light = light;
  5. this._scene = light.getScene();
  6. light._shadowGenerator = this;
  7. // Render target
  8. this._shadowMap = new BABYLON.RenderTargetTexture(light.name + "_shadowMap", mapSize, this._scene, false);
  9. this._shadowMap.wrapU = BABYLON.Texture.CLAMP_ADDRESSMODE;
  10. this._shadowMap.wrapV = BABYLON.Texture.CLAMP_ADDRESSMODE;
  11. // Effect
  12. this._effect = this._scene.getEngine().createEffect("shadowMap",
  13. ["position"],
  14. ["worldViewProjection"],
  15. [], "");
  16. this._effectVSM = this._scene.getEngine().createEffect("shadowMap",
  17. ["position"],
  18. ["worldViewProjection"],
  19. [], "#define VSM");
  20. // Custom render function
  21. var that = this;
  22. var renderSubMesh = function (subMesh, effect) {
  23. var mesh = subMesh.getMesh();
  24. var world = mesh.getWorldMatrix();
  25. effect.setMatrix("worldViewProjection", world.multiply(that.getTransformMatrix()));
  26. // Bind and draw
  27. mesh.bindAndDraw(subMesh, effect, false);
  28. };
  29. this._shadowMap.customRenderFunction = function (opaqueSubMeshes, alphaTestSubMeshes, transparentSubMeshes, activeMeshes) {
  30. var engine = that._scene.getEngine();
  31. var index;
  32. var effect = that.useVarianceShadowMap ? that._effectVSM : that._effect;
  33. engine.enableEffect(effect);
  34. for (index = 0; index < opaqueSubMeshes.length; index++) {
  35. renderSubMesh(opaqueSubMeshes[index], effect);
  36. }
  37. for (index = 0; index < alphaTestSubMeshes.length; index++) {
  38. renderSubMesh(alphaTestSubMeshes[index], effect);
  39. }
  40. };
  41. };
  42. // Members
  43. BABYLON.ShadowGenerator.prototype.useVarianceShadowMap = true;
  44. // Properties
  45. BABYLON.ShadowGenerator.prototype.isReady = function () {
  46. return this._effect.isReady() && this._effectVSM.isReady();
  47. };
  48. BABYLON.ShadowGenerator.prototype.getShadowMap = function () {
  49. return this._shadowMap;
  50. };
  51. BABYLON.ShadowGenerator.prototype.getLight = function () {
  52. return this._light;
  53. };
  54. // Methods
  55. BABYLON.ShadowGenerator.prototype.getTransformMatrix = function () {
  56. if (!this._cachedPosition || !this._cachedDirection || !this._light.position.equals(this._cachedPosition) || !this._light.direction.equals(this._cachedDirection)) {
  57. this._cachedPosition = this._light.position.clone();
  58. this._cachedDirection = this._light.direction.clone();
  59. var activeCamera = this._scene.activeCamera;
  60. var view = new BABYLON.Matrix.LookAtLH(this._light.position, this._light.position.add(this._light.direction), BABYLON.Vector3.Up());
  61. var projection = new BABYLON.Matrix.PerspectiveFovLH(Math.PI / 2.0, 1.0, activeCamera.minZ, activeCamera.maxZ);
  62. this._transformMatrix = view.multiply(projection);
  63. }
  64. return this._transformMatrix;
  65. };
  66. BABYLON.ShadowGenerator.prototype.dispose = function() {
  67. this._shadowMap.dispose();
  68. };
  69. })();