123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859 |
- var BABYLON = BABYLON || {};
- (function () {
- BABYLON.Engine = function (canvas, antialias) {
- this._renderingCanvas = canvas;
- // GL
- try {
- this._gl = canvas.getContext("webgl", { antialias: antialias }) || canvas.getContext("experimental-webgl", { antialias: antialias });
- } catch (e) {
- throw new Error("WebGL not supported");
- }
- if (this._gl === undefined) {
- throw new Error("WebGL not supported");
- }
- // Options
- this.forceWireframe = false;
- this.cullBackFaces = true;
- // Scenes
- this.scenes = [];
- // Textures
- this._workingCanvas = document.createElement("canvas");
- this._workingContext = this._workingCanvas.getContext("2d");
- // Viewport
- this._hardwareScalingLevel = 1.0 / window.devicePixelRatio || 1.0
- this.resize();
- // Caps
- this._caps = {};
- this._caps.maxTexturesImageUnits = this._gl.getParameter(this._gl.MAX_TEXTURE_IMAGE_UNITS);
- this._caps.maxTextureSize = this._gl.getParameter(this._gl.MAX_TEXTURE_SIZE);
- this._caps.maxCubemapTextureSize = this._gl.getParameter(this._gl.MAX_CUBE_MAP_TEXTURE_SIZE);
- this._caps.maxRenderTextureSize = this._gl.getParameter(this._gl.MAX_RENDERBUFFER_SIZE);
- // Extensions
- var derivatives = this._gl.getExtension('OES_standard_derivatives');
- this._caps.standardDerivatives = (derivatives !== undefined);
- // Cache
- this._loadedTexturesCache = [];
- this._activeTexturesCache = [];
- this._buffersCache = {
- vertexBuffer: null,
- indexBuffer: null
- };
- this._currentEffect = null;
- this._currentState = {
- culling: null
- };
- this._compiledEffects = {};
- this._gl.enable(this._gl.DEPTH_TEST);
- this._gl.depthFunc(this._gl.LEQUAL);
- // Fullscreen
- this.isFullscreen = false;
- var that = this;
- document.addEventListener("fullscreenchange", function () {
- that.isFullscreen = document.fullscreen;
- }, false);
- document.addEventListener("mozfullscreenchange", function () {
- that.isFullscreen = document.mozFullScreen;
- }, false);
- document.addEventListener("webkitfullscreenchange", function () {
- that.isFullscreen = document.webkitIsFullScreen;
- }, false);
- document.addEventListener("msfullscreenchange", function () {
- that.isFullscreen = document.msIsFullScreen;
- }, false);
- };
- // Properties
- BABYLON.Engine.prototype.getAspectRatio = function () {
- return this._aspectRatio;
- };
- BABYLON.Engine.prototype.getRenderWidth = function () {
- return this._renderingCanvas.width;
- };
- BABYLON.Engine.prototype.getRenderHeight = function () {
- return this._renderingCanvas.height;
- };
- BABYLON.Engine.prototype.getRenderingCanvas = function () {
- return this._renderingCanvas;
- };
- BABYLON.Engine.prototype.setHardwareScalingLevel = function (level) {
- this._hardwareScalingLevel = level;
- this.resize();
- };
- BABYLON.Engine.prototype.getLoadedTexturesCache = function () {
- return this._loadedTexturesCache;
- };
- BABYLON.Engine.prototype.getCaps = function () {
- return this._caps;
- };
- // Methods
- BABYLON.Engine.prototype.stopRenderLoop = function () {
- this._runningLoop = false;
- };
- BABYLON.Engine.prototype.runRenderLoop = function (renderFunction) {
- this._runningLoop = true;
- var that = this;
- var loop = function () {
- // Start new frame
- that.beginFrame();
- renderFunction();
- // Present
- that.endFrame();
- if (that._runningLoop) {
- // Register new frame
- BABYLON.Tools.QueueNewFrame(loop);
- }
- };
- BABYLON.Tools.QueueNewFrame(loop);
- };
- BABYLON.Engine.prototype.switchFullscreen = function (element) {
- if (this.isFullscreen) {
- BABYLON.Tools.ExitFullscreen();
- } else {
- BABYLON.Tools.RequestFullscreen(element ? element : this._renderingCanvas);
- }
- };
- BABYLON.Engine.prototype.clear = function (color, backBuffer, depthStencil) {
- this._gl.clearColor(color.r, color.g, color.b, 1.0);
- this._gl.clearDepth(1.0);
- var mode = 0;
- if (backBuffer || this.forceWireframe)
- mode |= this._gl.COLOR_BUFFER_BIT;
- if (depthStencil)
- mode |= this._gl.DEPTH_BUFFER_BIT;
- this._gl.clear(mode);
- };
- BABYLON.Engine.prototype.beginFrame = function () {
- BABYLON.Tools._MeasureFps();
- this._gl.viewport(0, 0, this._renderingCanvas.width, this._renderingCanvas.height);
- };
- BABYLON.Engine.prototype.endFrame = function () {
- this.flushFramebuffer();
- };
- BABYLON.Engine.prototype.resize = function () {
- this._renderingCanvas.width = this._renderingCanvas.clientWidth / this._hardwareScalingLevel;
- this._renderingCanvas.height = this._renderingCanvas.clientHeight / this._hardwareScalingLevel;
- this._aspectRatio = this._renderingCanvas.width / this._renderingCanvas.height;
- };
- BABYLON.Engine.prototype.bindFramebuffer = function (texture) {
- var gl = this._gl;
- gl.bindFramebuffer(gl.FRAMEBUFFER, texture._framebuffer);
- gl.viewport(0.0, 0.0, texture._size, texture._size);
- this.wipeCaches();
- };
- BABYLON.Engine.prototype.unBindFramebuffer = function (texture) {
- if (texture.generateMipMaps) {
- var gl = this._gl;
- gl.bindTexture(gl.TEXTURE_2D, texture);
- gl.generateMipmap(gl.TEXTURE_2D);
- gl.bindTexture(gl.TEXTURE_2D, null);
- }
- };
- BABYLON.Engine.prototype.flushFramebuffer = function () {
- this._gl.flush();
- };
- BABYLON.Engine.prototype.restoreDefaultFramebuffer = function () {
- this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, null);
- this._gl.viewport(0, 0, this._renderingCanvas.width, this._renderingCanvas.height);
- this.wipeCaches();
- };
- // VBOs
- BABYLON.Engine.prototype.createVertexBuffer = function (vertices) {
- var vbo = this._gl.createBuffer();
- this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
- this._gl.bufferData(this._gl.ARRAY_BUFFER, new Float32Array(vertices), this._gl.STATIC_DRAW);
- this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
- this._buffersCache.vertexBuffer = null;
- vbo.references = 1;
- return vbo;
- };
- BABYLON.Engine.prototype.createDynamicVertexBuffer = function (capacity) {
- var vbo = this._gl.createBuffer();
- this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
- this._gl.bufferData(this._gl.ARRAY_BUFFER, capacity, this._gl.DYNAMIC_DRAW);
- this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
- this._buffersCache.vertexBuffer = null;
- vbo.references = 1;
- return vbo;
- };
- BABYLON.Engine.prototype.updateDynamicVertexBuffer = function (vertexBuffer, vertices) {
- this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
- this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, new Float32Array(vertices));
- this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
- };
- BABYLON.Engine.prototype.createIndexBuffer = function (indices, is32Bits) {
- var vbo = this._gl.createBuffer();
- this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, vbo);
- this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), this._gl.STATIC_DRAW);
- this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, null);
- this._buffersCache.indexBuffer = null;
- vbo.references = 1;
- vbo.is32Bits = is32Bits;
- return vbo;
- };
- BABYLON.Engine.prototype.bindBuffers = function (vertexBuffer, indexBuffer, vertexDeclaration, vertexStrideSize, effect) {
- if (this._buffersCache.vertexBuffer != vertexBuffer) {
- this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
- this._buffersCache.vertexBuffer = vertexBuffer;
- var offset = 0;
- for (var index = 0; index < vertexDeclaration.length; index++) {
- var order = effect.getAttribute(index);
- if (order >= 0) {
- this._gl.vertexAttribPointer(order, vertexDeclaration[index], this._gl.FLOAT, false, vertexStrideSize, offset);
- }
- offset += vertexDeclaration[index] * 4;
- }
- }
- if (this._buffersCache.indexBuffer != indexBuffer) {
- this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
- this._buffersCache.indexBuffer = indexBuffer;
- }
- };
- BABYLON.Engine.prototype._releaseBuffer = function (buffer) {
- buffer.references--;
- if (buffer.references === 0) {
- this._gl.deleteBuffer(buffer);
- }
- };
- BABYLON.Engine.prototype.draw = function (useTriangles, indexStart, indexCount) {
- this._gl.drawElements(useTriangles ? this._gl.TRIANGLES : this._gl.LINES, indexCount, this._gl.UNSIGNED_SHORT, indexStart * 2);
- };
- // Shaders
- BABYLON.Engine.prototype.createEffect = function (baseName, attributesNames, uniformsNames, samplers, defines) {
- var name = baseName + "@" + defines;
- if (this._compiledEffects[name]) {
- return this._compiledEffects[name];
- }
- var effect = new BABYLON.Effect(baseName, attributesNames, uniformsNames, samplers, this, defines);
- this._compiledEffects[name] = effect;
- return effect;
- };
- var compileShader = function (gl, source, type, defines) {
- var shader = gl.createShader(type === "vertex" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);
- gl.shaderSource(shader, (defines ? defines + "\n" : "") + source);
- gl.compileShader(shader);
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
- throw new Error(gl.getShaderInfoLog(shader));
- }
- return shader;
- };
- BABYLON.Engine.prototype.createShaderProgram = function (vertexCode, fragmentCode, defines) {
- var vertexShader = compileShader(this._gl, vertexCode, "vertex", defines);
- var fragmentShader = compileShader(this._gl, fragmentCode, "fragment", defines);
- var shaderProgram = this._gl.createProgram();
- this._gl.attachShader(shaderProgram, vertexShader);
- this._gl.attachShader(shaderProgram, fragmentShader);
- this._gl.linkProgram(shaderProgram);
- this._gl.deleteShader(vertexShader);
- this._gl.deleteShader(fragmentShader);
- return shaderProgram;
- };
- BABYLON.Engine.prototype.getUniforms = function (shaderProgram, uniformsNames) {
- var results = [];
- for (var index = 0; index < uniformsNames.length; index++) {
- results.push(this._gl.getUniformLocation(shaderProgram, uniformsNames[index]));
- }
- return results;
- };
- BABYLON.Engine.prototype.getAttributes = function (shaderProgram, attributesNames) {
- var results = [];
- for (var index = 0; index < attributesNames.length; index++) {
- try {
- results.push(this._gl.getAttribLocation(shaderProgram, attributesNames[index]));
- } catch (e) {
- results.push(-1);
- }
- }
- return results;
- };
- BABYLON.Engine.prototype.enableEffect = function (effect) {
- if (!effect || !effect.getAttributesCount() || this._currentEffect === effect) {
- return;
- }
- this._buffersCache.vertexBuffer = null;
- // Use program
- this._gl.useProgram(effect.getProgram());
- for (var index = 0; index < effect.getAttributesCount() ; index++) {
- // Attributes
- var order = effect.getAttribute(index);
- if (order >= 0) {
- this._gl.enableVertexAttribArray(effect.getAttribute(index));
- }
- }
- this._currentEffect = effect;
- };
- BABYLON.Engine.prototype.setMatrix = function (uniform, matrix) {
- if (!uniform)
- return;
- this._gl.uniformMatrix4fv(uniform, false, matrix.toArray());
- };
- BABYLON.Engine.prototype.setVector2 = function (uniform, x, y) {
- if (!uniform)
- return;
- this._gl.uniform2f(uniform, x, y);
- };
- BABYLON.Engine.prototype.setVector3 = function (uniform, vector3) {
- if (!uniform)
- return;
- this._gl.uniform3f(uniform, vector3.x, vector3.y, vector3.z);
- };
-
- BABYLON.Engine.prototype.setFloat2 = function (uniform, x, y) {
- if (!uniform)
- return;
- this._gl.uniform2f(uniform, x, y);
- };
-
- BABYLON.Engine.prototype.setFloat3 = function (uniform, x, y, z) {
- if (!uniform)
- return;
- this._gl.uniform3f(uniform, x, y, z);
- };
- BABYLON.Engine.prototype.setBool = function (uniform, bool) {
- if (!uniform)
- return;
- this._gl.uniform1i(uniform, bool);
- };
- BABYLON.Engine.prototype.setFloat4 = function (uniform, x, y, z, w) {
- if (!uniform)
- return;
- this._gl.uniform4f(uniform, x, y, z, w);
- };
- BABYLON.Engine.prototype.setColor3 = function (uniform, color3) {
- if (!uniform)
- return;
- this._gl.uniform3f(uniform, color3.r, color3.g, color3.b);
- };
- BABYLON.Engine.prototype.setColor4 = function (uniform, color3, alpha) {
- if (!uniform)
- return;
- this._gl.uniform4f(uniform, color3.r, color3.g, color3.b, alpha);
- };
- // States
- BABYLON.Engine.prototype.setState = function (culling) {
- // Culling
- if (this._currentState.culling !== culling) {
- if (culling) {
- this._gl.cullFace(this.cullBackFaces ? this._gl.BACK : this._gl.FRONT);
- this._gl.enable(this._gl.CULL_FACE);
- } else {
- this._gl.disable(this._gl.CULL_FACE);
- }
- this._currentState.culling = culling;
- }
- };
- BABYLON.Engine.prototype.setDepthBuffer = function (enable) {
- if (enable) {
- this._gl.enable(this._gl.DEPTH_TEST);
- } else {
- this._gl.disable(this._gl.DEPTH_TEST);
- }
- };
- BABYLON.Engine.prototype.setDepthWrite = function (enable) {
- this._gl.depthMask(enable);
- };
- BABYLON.Engine.prototype.setColorWrite = function (enable) {
- this._gl.colorMask(enable, enable, enable, enable);
- };
- BABYLON.Engine.prototype.setAlphaMode = function (mode) {
- switch (mode) {
- case BABYLON.Engine.ALPHA_DISABLE:
- this.setDepthWrite(true);
- this._gl.disable(this._gl.BLEND);
- break;
- case BABYLON.Engine.ALPHA_COMBINE:
- this.setDepthWrite(false);
- this._gl.blendFuncSeparate(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ZERO, this._gl.ONE);
- this._gl.enable(this._gl.BLEND);
- break;
- case BABYLON.Engine.ALPHA_ADD:
- this.setDepthWrite(false);
- this._gl.blendFuncSeparate(this._gl.ONE, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
- this._gl.enable(this._gl.BLEND);
- break;
- }
- };
- BABYLON.Engine.prototype.setAlphaTesting = function (enable) {
- this._alphaTest = enable;
- };
- BABYLON.Engine.prototype.getAlphaTesting = function () {
- return this._alphaTest;
- };
- // Textures
- BABYLON.Engine.prototype.wipeCaches = function () {
- this._activeTexturesCache = [];
- this._currentEffect = null;
- this._currentState = {
- culling: null
- };
- this._buffersCache = {
- vertexBuffer: null,
- indexBuffer: null
- };
- };
- var getExponantOfTwo = function (value, max) {
- var count = 1;
- do {
- count *= 2;
- } while (count < value);
- if (count > max)
- count = max;
- return count;
- };
- BABYLON.Engine.prototype.createTexture = function (url, noMipmap, invertY, scene) {
- var texture = this._gl.createTexture();
- var that = this;
- var img = new Image();
- img.onload = function () {
- that._workingCanvas.width = getExponantOfTwo(img.width, that._caps.maxTextureSize);
- that._workingCanvas.height = getExponantOfTwo(img.height, that._caps.maxTextureSize);
- that._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, that._workingCanvas.width, that._workingCanvas.height);
- that._gl.bindTexture(that._gl.TEXTURE_2D, texture);
- that._gl.pixelStorei(that._gl.UNPACK_FLIP_Y_WEBGL, invertY === undefined ? true : invertY);
- that._gl.texImage2D(that._gl.TEXTURE_2D, 0, that._gl.RGBA, that._gl.RGBA, that._gl.UNSIGNED_BYTE, that._workingCanvas);
- that._gl.texParameteri(that._gl.TEXTURE_2D, that._gl.TEXTURE_MAG_FILTER, that._gl.LINEAR);
- if (noMipmap) {
- that._gl.texParameteri(that._gl.TEXTURE_2D, that._gl.TEXTURE_MIN_FILTER, that._gl.LINEAR);
- } else {
- that._gl.texParameteri(that._gl.TEXTURE_2D, that._gl.TEXTURE_MIN_FILTER, that._gl.LINEAR_MIPMAP_LINEAR);
- that._gl.generateMipmap(that._gl.TEXTURE_2D);
- }
- that._gl.bindTexture(that._gl.TEXTURE_2D, null);
- that._activeTexturesCache = [];
- texture._baseWidth = img.width;
- texture._baseHeight = img.height;
- texture._width = that._workingCanvas.width;
- texture._height = that._workingCanvas.height;
- texture.isReady = true;
- scene._removePendingData(img);
- };
- img.onerror = function () {
- scene._removePendingData(img);
- };
- scene._addPendingData(img);
- img.src = url;
- texture.url = url;
- texture.noMipmap = noMipmap;
- texture.references = 1;
- this._loadedTexturesCache.push(texture);
- return texture;
- };
- BABYLON.Engine.prototype.createDynamicTexture = function (size, noMipmap) {
- var texture = this._gl.createTexture();
- var width = getExponantOfTwo(size, this._caps.maxTextureSize);
- var height = getExponantOfTwo(size, this._caps.maxTextureSize);
- this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
- this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, this._gl.LINEAR);
- if (noMipmap) {
- this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR);
- } else {
- this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR_MIPMAP_LINEAR);
- }
- this._gl.bindTexture(this._gl.TEXTURE_2D, null);
- this._activeTexturesCache = [];
- texture._baseWidth = width;
- texture._baseHeight = height;
- texture._width = width;
- texture._height = height;
- texture.isReady = false;
- texture.noMipmap = noMipmap;
- texture.references = 1;
- this._loadedTexturesCache.push(texture);
- return texture;
- };
- BABYLON.Engine.prototype.updateDynamicTexture = function (texture, canvas) {
- this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
- this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, true);
- this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, canvas);
- if (!texture.noMipmap) {
- this._gl.generateMipmap(this._gl.TEXTURE_2D);
- }
- this._gl.bindTexture(this._gl.TEXTURE_2D, null);
- this._activeTexturesCache = [];
- texture.isReady = true;
- };
- BABYLON.Engine.prototype.updateVideoTexture = function (texture, video) {
- this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
- this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, false);
- this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, video);
- if (!texture.noMipmap) {
- this._gl.generateMipmap(this._gl.TEXTURE_2D);
- }
- this._gl.bindTexture(this._gl.TEXTURE_2D, null);
- this._activeTexturesCache = [];
- texture.isReady = true;
- };
- BABYLON.Engine.prototype.createRenderTargetTexture = function (size, generateMipMaps) {
- var gl = this._gl;
- var texture = gl.createTexture();
- gl.bindTexture(gl.TEXTURE_2D, texture);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, generateMipMaps ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, size, size, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
- // Create the depth buffer
- var depthBuffer = gl.createRenderbuffer();
- gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
- gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, size, size);
- // Create the framebuffer
- var framebuffer = gl.createFramebuffer();
- gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
- gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
- gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
- // Unbind
- gl.bindTexture(gl.TEXTURE_2D, null);
- gl.bindRenderbuffer(gl.RENDERBUFFER, null);
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
- texture._framebuffer = framebuffer;
- texture._depthBuffer = depthBuffer;
- texture._size = size;
- texture.isReady = true;
- texture.generateMipMaps = generateMipMaps;
- texture.references = 1;
- this._activeTexturesCache = [];
- this._loadedTexturesCache.push(texture);
- return texture;
- };
- var extensions = ["_px.jpg", "_py.jpg", "_pz.jpg", "_nx.jpg", "_ny.jpg", "_nz.jpg"];
- var cascadeLoad = function (rootUrl, index, loadedImages, scene, onfinish) {
- var img = new Image();
- img.onload = function () {
- loadedImages.push(this);
- scene._removePendingData(img);
- if (index != extensions.length - 1) {
- cascadeLoad(rootUrl, index + 1, loadedImages, scene, onfinish);
- } else {
- onfinish(loadedImages);
- }
- };
- img.onerrror = function () {
- scene._removePendingData(img);
- };
- scene._addPendingData(img);
- img.src = rootUrl + extensions[index];
- };
- BABYLON.Engine.prototype.createCubeTexture = function (rootUrl, scene) {
- var gl = this._gl;
- var texture = gl.createTexture();
- texture.isCube = true;
- texture.url = rootUrl;
- texture.references = 1;
- this._loadedTexturesCache.push(texture);
- var that = this;
- cascadeLoad(rootUrl, 0, [], scene, function (imgs) {
- var width = getExponantOfTwo(imgs[0].width);
- var height = width;
- that._workingCanvas.width = width;
- that._workingCanvas.height = height;
- var faces = [
- gl.TEXTURE_CUBE_MAP_POSITIVE_X, gl.TEXTURE_CUBE_MAP_POSITIVE_Y, gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
- gl.TEXTURE_CUBE_MAP_NEGATIVE_X, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
- ];
- gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
- gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
- for (var index = 0; index < faces.length; index++) {
- that._workingContext.drawImage(imgs[index], 0, 0, imgs[index].width, imgs[index].height, 0, 0, width, height);
- gl.texImage2D(faces[index], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, that._workingCanvas);
- }
- gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
- gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
- gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
- gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
- gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
- gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
- that._activeTexturesCache = [];
- texture._width = width;
- texture._height = height;
- texture.isReady = true;
- });
- return texture;
- };
- BABYLON.Engine.prototype._releaseTexture = function (texture) {
- var gl = this._gl;
- if (texture._framebuffer) {
- gl.deleteFramebuffer(texture._framebuffer);
- }
- if (texture._depthBuffer) {
- gl.deleteRenderbuffer(texture._depthBuffer);
- }
- gl.deleteTexture(texture);
- // Unbind channels
- for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
- this._gl.activeTexture(this._gl["TEXTURE" + channel]);
- this._gl.bindTexture(this._gl.TEXTURE_2D, null);
- this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
- this._activeTexturesCache[channel] = null;
- }
- };
- BABYLON.Engine.prototype.bindSamplers = function (effect) {
- this._gl.useProgram(effect.getProgram());
- var samplers = effect.getSamplers();
- for (var index = 0; index < samplers.length; index++) {
- var uniform = effect.getUniform(samplers[index]);
- this._gl.uniform1i(uniform, index);
- }
- this._currentEffect = null;
- };
- BABYLON.Engine.prototype.setTexture = function (channel, texture) {
- if (!texture || !texture.isReady()) {
- if (this._activeTexturesCache[channel] != null) {
- this._gl.activeTexture(this._gl["TEXTURE" + channel]);
- this._gl.bindTexture(this._gl.TEXTURE_2D, null);
- this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
- this._activeTexturesCache[channel] = null;
- }
- return;
- }
- if (texture instanceof BABYLON.VideoTexture) {
- if (texture._update()) {
- this._activeTexturesCache[channel] = null;
- }
- }
- if (this._activeTexturesCache[channel] == texture) {
- return;
- }
- this._activeTexturesCache[channel] = texture;
- var internalTexture = texture.getInternalTexture();
- this._gl.activeTexture(this._gl["TEXTURE" + channel]);
- if (internalTexture.isCube) {
- this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, internalTexture);
- if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
- internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
- this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_S, texture.coordinatesMode !== BABYLON.CubeTexture.CUBIC_MODE ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE);
- this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_T, texture.coordinatesMode !== BABYLON.CubeTexture.CUBIC_MODE ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE);
- }
- } else {
- this._gl.bindTexture(this._gl.TEXTURE_2D, internalTexture);
- if (internalTexture._cachedWrapU !== texture.wrapU) {
- internalTexture._cachedWrapU = texture.wrapU;
- switch (texture.wrapU) {
- case BABYLON.Texture.WRAP_ADDRESSMODE:
- this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.REPEAT);
- break;
- case BABYLON.Texture.CLAMP_ADDRESSMODE:
- this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
- break;
- case BABYLON.Texture.MIRROR_ADDRESSMODE:
- this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.MIRRORED_REPEAT);
- break;
- }
- }
- if (internalTexture._cachedWrapV !== texture.wrapV) {
- internalTexture._cachedWrapV = texture.wrapV;
- switch (texture.wrapV) {
- case BABYLON.Texture.WRAP_ADDRESSMODE:
- this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.REPEAT);
- break;
- case BABYLON.Texture.CLAMP_ADDRESSMODE:
- this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
- break;
- case BABYLON.Texture.MIRROR_ADDRESSMODE:
- this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.MIRRORED_REPEAT);
- break;
- }
- }
- }
- };
- // Dispose
- BABYLON.Engine.prototype.dispose = function () {
- // Release scenes
- while (this.scenes.length) {
- this.scenes[0].dispose();
- }
- // Release effects
- for (var name in this._compiledEffects.length) {
- this._gl.deleteProgram(this._compiledEffects[name]._program);
- }
- };
- // Statics
- BABYLON.Engine.ShadersRepository = "Babylon/Shaders/";
- BABYLON.Engine.ALPHA_DISABLE = 0;
- BABYLON.Engine.ALPHA_ADD = 1;
- BABYLON.Engine.ALPHA_COMBINE = 2;
- BABYLON.Engine.epsilon = 0.001;
- BABYLON.Engine.collisionsEpsilon = 0.001;
- BABYLON.Engine.isSupported = function () {
- try {
- var tempcanvas = document.createElement("canvas");
- var gl = tempcanvas.getContext("webgl") || tempcanvas.getContext("experimental-webgl");
- return gl != null && !!window.WebGLRenderingContext;
- } catch (e) {
- return false;
- }
- };
- })();
|