123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- var BABYLON = BABYLON || {};
- (function () {
- BABYLON.Effect = function (baseName, attributesNames, uniformsNames, samplers, engine, defines) {
- this._engine = engine;
- this.name = baseName;
- this.defines = defines;
- this._uniformsNames = uniformsNames.concat(samplers);
- this._samplers = samplers;
- this._isReady = false;
- var that = this;
- // Is in local store ?
- if (BABYLON.Effect.ShadersStore[baseName + "VertexShader"]) {
- this._prepareEffect(BABYLON.Effect.ShadersStore[baseName + "VertexShader"], BABYLON.Effect.ShadersStore[baseName + "PixelShader"], attributesNames, defines);
- } else {
- var shaderUrl = BABYLON.Engine.ShadersRepository + baseName;
- // Vertex shader
- BABYLON.Tools.LoadFile(shaderUrl + ".vertex.fx",
- function(vertexSourceCode) {
- // Fragment shader
- BABYLON.Tools.LoadFile(shaderUrl + ".fragment.fx",
- function(fragmentSourceCode) {
- that._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines);
- });
- }
- );
- }
-
- // Cache
- this._valueCache = [];
- };
-
- // Properties
- BABYLON.Effect.prototype.isReady = function () {
- return this._isReady;
- };
-
- BABYLON.Effect.prototype.getProgram = function() {
- return this._program;
- };
-
- BABYLON.Effect.prototype.getAttribute = function (index) {
- return this._attributes[index];
- };
-
- BABYLON.Effect.prototype.getAttributesCount = function () {
- return this._attributes.length;
- };
-
- BABYLON.Effect.prototype.getUniformIndex = function (uniformName) {
- return this._uniformsNames.indexOf(uniformName);
- };
-
- BABYLON.Effect.prototype.getUniform = function (uniformName) {
- return this._uniforms[this._uniformsNames.indexOf(uniformName)];
- };
-
- BABYLON.Effect.prototype.getSamplers = function () {
- return this._samplers;
- };
-
- // Methods
- BABYLON.Effect.prototype._prepareEffect = function (vertexSourceCode, fragmentSourceCode, attributesNames, defines) {
- var engine = this._engine;
- this._program = engine.createShaderProgram(vertexSourceCode, fragmentSourceCode, defines);
- this._uniforms = engine.getUniforms(this._program, this._uniformsNames);
- this._attributes = engine.getAttributes(this._program, attributesNames);
- for (var index = 0; index < this._samplers.length; index++) {
- var sampler = this.getUniform(this._samplers[index]);
- if (sampler == null) {
- this._samplers.splice(index, 1);
- index--;
- }
- }
- engine.bindSamplers(this);
- this._isReady = true;
- };
- BABYLON.Effect.prototype.setTexture = function (channel, texture) {
- this._engine.setTexture(this._samplers.indexOf(channel), texture);
- };
-
- BABYLON.Effect.prototype.setMatrix = function (uniformName, matrix) {
- if (this._valueCache[uniformName] && this._valueCache[uniformName].equals(matrix))
- return;
- this._valueCache[uniformName] = matrix;
- this._engine.setMatrix(this.getUniform(uniformName), matrix);
- };
-
- BABYLON.Effect.prototype.setBool = function (uniformName, bool) {
- if (this._valueCache[uniformName] && this._valueCache[uniformName] === bool)
- return;
- this._valueCache[uniformName] = bool;
- this._engine.setBool(this.getUniform(uniformName), bool);
- };
-
- BABYLON.Effect.prototype.setVector2 = function (uniformName, x, y) {
- if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y)
- return;
- this._valueCache[uniformName] = [x, y];
- this._engine.setVector2(this.getUniform(uniformName), x, y);
- };
-
- BABYLON.Effect.prototype.setVector3 = function (uniformName, vector3) {
- if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector3.x && this._valueCache[uniformName][1] == vector3.y && this._valueCache[uniformName][2] == vector3.z)
- return;
- this._valueCache[uniformName] = [vector3.x, vector3.y, vector3.z];
-
- this._engine.setVector3(this.getUniform(uniformName), vector3);
- };
-
- BABYLON.Effect.prototype.setFloat2 = function (uniformName, x, y) {
- if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y)
- return;
- this._valueCache[uniformName] = [x, y];
- this._engine.setFloat2(this.getUniform(uniformName), x, y);
- };
-
- BABYLON.Effect.prototype.setFloat3 = function (uniformName, x, y, z) {
- if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y && this._valueCache[uniformName][2] == z)
- return;
- this._valueCache[uniformName] = [x, y, z];
- this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
- };
-
- BABYLON.Effect.prototype.setFloat4 = function (uniformName, x, y, z, w) {
- if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y && this._valueCache[uniformName][2] == z && this._valueCache[uniformName][3] == w)
- return;
- this._valueCache[uniformName] = [x, y, z, w];
- this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
- };
-
- BABYLON.Effect.prototype.setColor3 = function (uniformName, color3) {
- if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == color3.r && this._valueCache[uniformName][1] == color3.g && this._valueCache[uniformName][2] == color3.b)
- return;
- this._valueCache[uniformName] = [color3.r, color3.g, color3.b];
- this._engine.setColor3(this.getUniform(uniformName), color3);
- };
-
- BABYLON.Effect.prototype.setColor4 = function (uniformName, color3, alpha) {
- 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)
- return;
- this._valueCache[uniformName] = [color3.r, color3.g, color3.b, alpha];
- this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
- };
-
- // Statics
- BABYLON.Effect.ShadersStore = {};
- })();
|