123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- var BABYLON;
- (function (BABYLON) {
- var Effect = (function () {
- function Effect(baseName, attributesNames, uniformsNames, samplers, engine, defines, optionalDefines, onCompiled, onError) {
- var _this = this;
- this._isReady = false;
- this._compilationError = "";
- this._valueCache = [];
- this._engine = engine;
- this.name = baseName;
- this.defines = defines;
- this._uniformsNames = uniformsNames.concat(samplers);
- this._samplers = samplers;
- this._attributesNames = attributesNames;
- this.onError = onError;
- this.onCompiled = onCompiled;
- var vertexSource;
- var fragmentSource;
- if (baseName.vertexElement) {
- vertexSource = document.getElementById(baseName.vertexElement);
- } else {
- vertexSource = baseName.vertex || baseName;
- }
- if (baseName.fragmentElement) {
- fragmentSource = document.getElementById(baseName.fragmentElement);
- } else {
- fragmentSource = baseName.fragment || baseName;
- }
- this._loadVertexShader(vertexSource, function (vertexCode) {
- _this._loadFragmentShader(fragmentSource, function (fragmentCode) {
- _this._prepareEffect(vertexCode, fragmentCode, attributesNames, defines, optionalDefines);
- });
- });
- }
- // Properties
- Effect.prototype.isReady = function () {
- return this._isReady;
- };
- Effect.prototype.getProgram = function () {
- return this._program;
- };
- Effect.prototype.getAttributesNames = function () {
- return this._attributesNames;
- };
- Effect.prototype.getAttributeLocation = function (index) {
- return this._attributes[index];
- };
- Effect.prototype.getAttributeLocationByName = function (name) {
- var index = this._attributesNames.indexOf(name);
- return this._attributes[index];
- };
- Effect.prototype.getAttributesCount = function () {
- return this._attributes.length;
- };
- Effect.prototype.getUniformIndex = function (uniformName) {
- return this._uniformsNames.indexOf(uniformName);
- };
- Effect.prototype.getUniform = function (uniformName) {
- return this._uniforms[this._uniformsNames.indexOf(uniformName)];
- };
- Effect.prototype.getSamplers = function () {
- return this._samplers;
- };
- Effect.prototype.getCompilationError = function () {
- return this._compilationError;
- };
- // Methods
- Effect.prototype._loadVertexShader = function (vertex, callback) {
- // DOM element ?
- if (vertex instanceof HTMLElement) {
- var vertexCode = BABYLON.Tools.GetDOMTextContent(vertex);
- callback(vertexCode);
- return;
- }
- // Is in local store ?
- if (BABYLON.Effect.ShadersStore[vertex + "VertexShader"]) {
- callback(BABYLON.Effect.ShadersStore[vertex + "VertexShader"]);
- return;
- }
- var vertexShaderUrl;
- if (vertex[0] === ".") {
- vertexShaderUrl = vertex;
- } else {
- vertexShaderUrl = BABYLON.Engine.ShadersRepository + vertex;
- }
- // Vertex shader
- BABYLON.Tools.LoadFile(vertexShaderUrl + ".vertex.fx", callback);
- };
- Effect.prototype._loadFragmentShader = function (fragment, callback) {
- // DOM element ?
- if (fragment instanceof HTMLElement) {
- var fragmentCode = BABYLON.Tools.GetDOMTextContent(fragment);
- callback(fragmentCode);
- return;
- }
- // Is in local store ?
- if (BABYLON.Effect.ShadersStore[fragment + "PixelShader"]) {
- callback(BABYLON.Effect.ShadersStore[fragment + "PixelShader"]);
- return;
- }
- var fragmentShaderUrl;
- if (fragment[0] === ".") {
- fragmentShaderUrl = fragment;
- } else {
- fragmentShaderUrl = BABYLON.Engine.ShadersRepository + fragment;
- }
- // Fragment shader
- BABYLON.Tools.LoadFile(fragmentShaderUrl + ".fragment.fx", callback);
- };
- Effect.prototype._prepareEffect = function (vertexSourceCode, fragmentSourceCode, attributesNames, defines, optionalDefines, useFallback) {
- try {
- 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;
- if (this.onCompiled) {
- this.onCompiled(this);
- }
- } catch (e) {
- if (!useFallback && optionalDefines) {
- for (index = 0; index < optionalDefines.length; index++) {
- defines = defines.replace(optionalDefines[index], "");
- }
- this._prepareEffect(vertexSourceCode, fragmentSourceCode, attributesNames, defines, optionalDefines, true);
- } else {
- BABYLON.Tools.Error("Unable to compile effect: " + this.name);
- BABYLON.Tools.Error("Defines: " + defines);
- BABYLON.Tools.Error("Optional defines: " + optionalDefines);
- BABYLON.Tools.Error("Error: " + e.message);
- this._compilationError = e.message;
- if (this.onError) {
- this.onError(this, this._compilationError);
- }
- }
- }
- };
- Effect.prototype._bindTexture = function (channel, texture) {
- this._engine._bindTexture(this._samplers.indexOf(channel), texture);
- };
- Effect.prototype.setTexture = function (channel, texture) {
- this._engine.setTexture(this._samplers.indexOf(channel), texture);
- };
- Effect.prototype.setTextureFromPostProcess = function (channel, postProcess) {
- this._engine.setTextureFromPostProcess(this._samplers.indexOf(channel), postProcess);
- };
- //public _cacheMatrix(uniformName, matrix) {
- // if (!this._valueCache[uniformName]) {
- // this._valueCache[uniformName] = new BABYLON.Matrix();
- // }
- // for (var index = 0; index < 16; index++) {
- // this._valueCache[uniformName].m[index] = matrix.m[index];
- // }
- //};
- Effect.prototype._cacheFloat2 = function (uniformName, x, y) {
- if (!this._valueCache[uniformName]) {
- this._valueCache[uniformName] = [x, y];
- return;
- }
- this._valueCache[uniformName][0] = x;
- this._valueCache[uniformName][1] = y;
- };
- Effect.prototype._cacheFloat3 = function (uniformName, x, y, z) {
- if (!this._valueCache[uniformName]) {
- this._valueCache[uniformName] = [x, y, z];
- return;
- }
- this._valueCache[uniformName][0] = x;
- this._valueCache[uniformName][1] = y;
- this._valueCache[uniformName][2] = z;
- };
- Effect.prototype._cacheFloat4 = function (uniformName, x, y, z, w) {
- if (!this._valueCache[uniformName]) {
- this._valueCache[uniformName] = [x, y, z, w];
- return;
- }
- this._valueCache[uniformName][0] = x;
- this._valueCache[uniformName][1] = y;
- this._valueCache[uniformName][2] = z;
- this._valueCache[uniformName][3] = w;
- };
- Effect.prototype.setArray = function (uniformName, array) {
- this._engine.setArray(this.getUniform(uniformName), array);
- return this;
- };
- Effect.prototype.setMatrices = function (uniformName, matrices) {
- this._engine.setMatrices(this.getUniform(uniformName), matrices);
- return this;
- };
- Effect.prototype.setMatrix = function (uniformName, matrix) {
- //if (this._valueCache[uniformName] && this._valueCache[uniformName].equals(matrix))
- // return;
- //this._cacheMatrix(uniformName, matrix);
- this._engine.setMatrix(this.getUniform(uniformName), matrix);
- return this;
- };
- Effect.prototype.setFloat = function (uniformName, value) {
- if (this._valueCache[uniformName] && this._valueCache[uniformName] === value)
- return this;
- this._valueCache[uniformName] = value;
- this._engine.setFloat(this.getUniform(uniformName), value);
- return this;
- };
- Effect.prototype.setBool = function (uniformName, bool) {
- if (this._valueCache[uniformName] && this._valueCache[uniformName] === bool)
- return this;
- this._valueCache[uniformName] = bool;
- this._engine.setBool(this.getUniform(uniformName), bool ? 1 : 0);
- return this;
- };
- Effect.prototype.setVector2 = function (uniformName, vector2) {
- if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == vector2.x && this._valueCache[uniformName][1] == vector2.y)
- return this;
- this._cacheFloat2(uniformName, vector2.x, vector2.y);
- this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
- return this;
- };
- Effect.prototype.setFloat2 = function (uniformName, x, y) {
- if (this._valueCache[uniformName] && this._valueCache[uniformName][0] == x && this._valueCache[uniformName][1] == y)
- return this;
- this._cacheFloat2(uniformName, x, y);
- this._engine.setFloat2(this.getUniform(uniformName), x, y);
- return this;
- };
- 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;
- this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z);
- this._engine.setFloat3(this.getUniform(uniformName), vector3.x, vector3.y, vector3.z);
- return this;
- };
- 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;
- this._cacheFloat3(uniformName, x, y, z);
- this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
- return this;
- };
- 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;
- this._cacheFloat4(uniformName, x, y, z, w);
- this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
- return this;
- };
- 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;
- this._cacheFloat3(uniformName, color3.r, color3.g, color3.b);
- this._engine.setColor3(this.getUniform(uniformName), color3);
- return this;
- };
- 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;
- this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha);
- this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
- return this;
- };
- Effect.ShadersStore = {};
- return Effect;
- })();
- BABYLON.Effect = Effect;
- })(BABYLON || (BABYLON = {}));
- //# sourceMappingURL=babylon.effect.js.map
|