babylon.effect.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.Effect = function (baseName, attributesNames, uniformsNames, samplers, engine, defines) {
  4. this._engine = engine;
  5. this.name = baseName;
  6. this.defines = defines;
  7. this._uniformsNames = uniformsNames.concat(samplers);
  8. this._samplers = samplers;
  9. this._isReady = false;
  10. var that = this;
  11. // Is in local store ?
  12. if (BABYLON.Effect.ShadersStore[baseName + "VertexShader"]) {
  13. this._prepareEffect(BABYLON.Effect.ShadersStore[baseName + "VertexShader"], BABYLON.Effect.ShadersStore[baseName + "PixelShader"], attributesNames, defines);
  14. } else {
  15. var shaderUrl = BABYLON.Engine.ShadersRepository + baseName;
  16. // Vertex shader
  17. BABYLON.Tools.LoadFile(shaderUrl + ".vertex.fx",
  18. function(vertexSourceCode) {
  19. // Fragment shader
  20. BABYLON.Tools.LoadFile(shaderUrl + ".fragment.fx",
  21. function(fragmentSourceCode) {
  22. that._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines);
  23. });
  24. }
  25. );
  26. }
  27. // Cache
  28. this._valueCache = [];
  29. };
  30. // Properties
  31. BABYLON.Effect.prototype.isReady = function () {
  32. return this._isReady;
  33. };
  34. BABYLON.Effect.prototype.getProgram = function() {
  35. return this._program;
  36. };
  37. BABYLON.Effect.prototype.getAttribute = function (index) {
  38. return this._attributes[index];
  39. };
  40. BABYLON.Effect.prototype.getAttributesCount = function () {
  41. return this._attributes.length;
  42. };
  43. BABYLON.Effect.prototype.getUniformIndex = function (uniformName) {
  44. return this._uniformsNames.indexOf(uniformName);
  45. };
  46. BABYLON.Effect.prototype.getUniform = function (uniformName) {
  47. return this._uniforms[this._uniformsNames.indexOf(uniformName)];
  48. };
  49. BABYLON.Effect.prototype.getSamplers = function () {
  50. return this._samplers;
  51. };
  52. // Methods
  53. BABYLON.Effect.prototype._prepareEffect = function (vertexSourceCode, fragmentSourceCode, attributesNames, defines) {
  54. var engine = this._engine;
  55. this._program = engine.createShaderProgram(vertexSourceCode, fragmentSourceCode, defines);
  56. this._uniforms = engine.getUniforms(this._program, this._uniformsNames);
  57. this._attributes = engine.getAttributes(this._program, attributesNames);
  58. for (var index = 0; index < this._samplers.length; index++) {
  59. var sampler = this.getUniform(this._samplers[index]);
  60. if (sampler == null) {
  61. this._samplers.splice(index, 1);
  62. index--;
  63. }
  64. }
  65. engine.bindSamplers(this);
  66. this._isReady = true;
  67. };
  68. BABYLON.Effect.prototype.setTexture = function (channel, texture) {
  69. this._engine.setTexture(this._samplers.indexOf(channel), texture);
  70. };
  71. BABYLON.Effect.prototype.setMatrix = function (uniformName, matrix) {
  72. if (this._valueCache[uniformName] && this._valueCache[uniformName].equals(matrix))
  73. return;
  74. this._valueCache[uniformName] = matrix;
  75. this._engine.setMatrix(this.getUniform(uniformName), matrix);
  76. };
  77. BABYLON.Effect.prototype.setBool = function (uniformName, bool) {
  78. if (this._valueCache[uniformName] && this._valueCache[uniformName] === bool)
  79. return;
  80. this._valueCache[uniformName] = bool;
  81. this._engine.setBool(this.getUniform(uniformName), bool);
  82. };
  83. BABYLON.Effect.prototype.setVector2 = function (uniformName, x, y) {
  84. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y)
  85. return;
  86. this._valueCache[uniformName] = [x, y];
  87. this._engine.setVector2(this.getUniform(uniformName), x, y);
  88. };
  89. BABYLON.Effect.prototype.setVector3 = function (uniformName, vector3) {
  90. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector3.x && this._valueCache[uniformName][1] == vector3.y && this._valueCache[uniformName][2] == vector3.z)
  91. return;
  92. this._valueCache[uniformName] = [vector3.x, vector3.y, vector3.z];
  93. this._engine.setVector3(this.getUniform(uniformName), vector3);
  94. };
  95. BABYLON.Effect.prototype.setFloat2 = function (uniformName, x, y) {
  96. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y)
  97. return;
  98. this._valueCache[uniformName] = [x, y];
  99. this._engine.setFloat2(this.getUniform(uniformName), x, y);
  100. };
  101. BABYLON.Effect.prototype.setFloat3 = function (uniformName, x, y, z) {
  102. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y && this._valueCache[uniformName][2] == z)
  103. return;
  104. this._valueCache[uniformName] = [x, y, z];
  105. this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
  106. };
  107. BABYLON.Effect.prototype.setFloat4 = function (uniformName, x, y, z, w) {
  108. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y && this._valueCache[uniformName][2] == z && this._valueCache[uniformName][3] == w)
  109. return;
  110. this._valueCache[uniformName] = [x, y, z, w];
  111. this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
  112. };
  113. BABYLON.Effect.prototype.setColor3 = function (uniformName, color3) {
  114. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == color3.r && this._valueCache[uniformName][1] == color3.g && this._valueCache[uniformName][2] == color3.b)
  115. return;
  116. this._valueCache[uniformName] = [color3.r, color3.g, color3.b];
  117. this._engine.setColor3(this.getUniform(uniformName), color3);
  118. };
  119. BABYLON.Effect.prototype.setColor4 = function (uniformName, color3, alpha) {
  120. if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == color3.r && this._valueCache[uniformName][1] == color3.g && this._valueCache[uniformName][2] == color3.b && this._valueCache[uniformName][3] == alpha)
  121. return;
  122. this._valueCache[uniformName] = [color3.r, color3.g, color3.b, alpha];
  123. this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
  124. };
  125. // Statics
  126. BABYLON.Effect.ShadersStore = {};
  127. })();