babylon.engine.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.Engine = function (canvas, antialias, options) {
  5. var that = this;
  6. this._renderingCanvas = canvas;
  7. options = options || {};
  8. options.antialias = antialias;
  9. // GL
  10. try {
  11. this._gl = canvas.getContext("webgl", options) || canvas.getContext("experimental-webgl", options);
  12. } catch (e) {
  13. throw new Error("WebGL not supported");
  14. }
  15. if (!this._gl) {
  16. throw new Error("WebGL not supported");
  17. }
  18. this._windowIsBackground = false;
  19. window.addEventListener("blur", function () {
  20. that._windowIsBackground = true;
  21. });
  22. window.addEventListener("focus", function () {
  23. that._windowIsBackground = false;
  24. if (that._runningLoop) {
  25. // Register new frame
  26. BABYLON.Tools.QueueNewFrame(function () {
  27. that._renderLoop();
  28. });
  29. }
  30. });
  31. // Options
  32. this.forceWireframe = false;
  33. this.cullBackFaces = true;
  34. this.renderEvenInBackground = true;
  35. // Scenes
  36. this.scenes = [];
  37. // Textures
  38. this._workingCanvas = document.createElement("canvas");
  39. this._workingContext = this._workingCanvas.getContext("2d");
  40. // Viewport
  41. this._hardwareScalingLevel = 1.0 / (window.devicePixelRatio || 1.0);
  42. this.resize();
  43. // Caps
  44. this._caps = {};
  45. this._caps.maxTexturesImageUnits = this._gl.getParameter(this._gl.MAX_TEXTURE_IMAGE_UNITS);
  46. this._caps.maxTextureSize = this._gl.getParameter(this._gl.MAX_TEXTURE_SIZE);
  47. this._caps.maxCubemapTextureSize = this._gl.getParameter(this._gl.MAX_CUBE_MAP_TEXTURE_SIZE);
  48. this._caps.maxRenderTextureSize = this._gl.getParameter(this._gl.MAX_RENDERBUFFER_SIZE);
  49. // Extensions
  50. this._caps.standardDerivatives = (this._gl.getExtension('OES_standard_derivatives') !== null);
  51. this._caps.s3tc = this._gl.getExtension('WEBGL_compressed_texture_s3tc');
  52. this._caps.textureFloat = (this._gl.getExtension('OES_texture_float') !== null);
  53. this._caps.textureAnisotropicFilterExtension = this._gl.getExtension('EXT_texture_filter_anisotropic') || this._gl.getExtension('WEBKIT_EXT_texture_filter_anisotropic') || this._gl.getExtension('MOZ_EXT_texture_filter_anisotropic');
  54. this._caps.maxAnisotropy = this._caps.textureAnisotropicFilterExtension ? this._gl.getParameter(this._caps.textureAnisotropicFilterExtension.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 0;
  55. // Cache
  56. this._loadedTexturesCache = [];
  57. this._activeTexturesCache = [];
  58. this._currentEffect = null;
  59. this._currentState = {
  60. culling: null
  61. };
  62. this._compiledEffects = {};
  63. this._gl.enable(this._gl.DEPTH_TEST);
  64. this._gl.depthFunc(this._gl.LEQUAL);
  65. // Fullscreen
  66. this.isFullscreen = false;
  67. var onFullscreenChange = function () {
  68. if (document.fullscreen !== undefined) {
  69. that.isFullscreen = document.fullscreen;
  70. } else if (document.mozFullScreen !== undefined) {
  71. that.isFullscreen = document.mozFullScreen;
  72. } else if (document.webkitIsFullScreen !== undefined) {
  73. that.isFullscreen = document.webkitIsFullScreen;
  74. } else if (document.msIsFullScreen !== undefined) {
  75. that.isFullscreen = document.msIsFullScreen;
  76. }
  77. // Pointer lock
  78. if (that.isFullscreen && that._pointerLockRequested) {
  79. canvas.requestPointerLock = canvas.requestPointerLock ||
  80. canvas.msRequestPointerLock ||
  81. canvas.mozRequestPointerLock ||
  82. canvas.webkitRequestPointerLock;
  83. if (canvas.requestPointerLock) {
  84. canvas.requestPointerLock();
  85. }
  86. }
  87. };
  88. document.addEventListener("fullscreenchange", onFullscreenChange, false);
  89. document.addEventListener("mozfullscreenchange", onFullscreenChange, false);
  90. document.addEventListener("webkitfullscreenchange", onFullscreenChange, false);
  91. document.addEventListener("msfullscreenchange", onFullscreenChange, false);
  92. // Pointer lock
  93. this.isPointerLock = false;
  94. var onPointerLockChange = function () {
  95. that.isPointerLock = (document.mozPointerLockElement === canvas ||
  96. document.webkitPointerLockElement === canvas ||
  97. document.msPointerLockElement === canvas ||
  98. document.pointerLockElement === canvas
  99. );
  100. };
  101. document.addEventListener("pointerlockchange", onPointerLockChange, false);
  102. document.addEventListener("mspointerlockchange", onPointerLockChange, false);
  103. document.addEventListener("mozpointerlockchange", onPointerLockChange, false);
  104. document.addEventListener("webkitpointerlockchange", onPointerLockChange, false);
  105. };
  106. // Properties
  107. BABYLON.Engine.prototype.getAspectRatio = function (camera) {
  108. var viewport = camera.viewport;
  109. return (this._renderingCanvas.width * viewport.width) / (this._renderingCanvas.height * viewport.height);
  110. };
  111. BABYLON.Engine.prototype.getRenderWidth = function () {
  112. return this._renderingCanvas.width;
  113. };
  114. BABYLON.Engine.prototype.getRenderHeight = function () {
  115. return this._renderingCanvas.height;
  116. };
  117. BABYLON.Engine.prototype.getRenderingCanvas = function () {
  118. return this._renderingCanvas;
  119. };
  120. BABYLON.Engine.prototype.setHardwareScalingLevel = function (level) {
  121. this._hardwareScalingLevel = level;
  122. this.resize();
  123. };
  124. BABYLON.Engine.prototype.getHardwareScalingLevel = function () {
  125. return this._hardwareScalingLevel;
  126. };
  127. BABYLON.Engine.prototype.getLoadedTexturesCache = function () {
  128. return this._loadedTexturesCache;
  129. };
  130. BABYLON.Engine.prototype.getCaps = function () {
  131. return this._caps;
  132. };
  133. // Methods
  134. BABYLON.Engine.prototype.stopRenderLoop = function () {
  135. this._renderFunction = null;
  136. this._runningLoop = false;
  137. };
  138. BABYLON.Engine.prototype._renderLoop = function () {
  139. if (!this.renderEvenInBackground && this._windowIsBackground) {
  140. return;
  141. }
  142. // Start new frame
  143. this.beginFrame();
  144. if (this._renderFunction) {
  145. this._renderFunction();
  146. }
  147. // Present
  148. this.endFrame();
  149. if (this._runningLoop) {
  150. // Register new frame
  151. var that = this;
  152. BABYLON.Tools.QueueNewFrame(function () {
  153. that._renderLoop();
  154. });
  155. }
  156. };
  157. BABYLON.Engine.prototype.runRenderLoop = function (renderFunction) {
  158. this._runningLoop = true;
  159. this._renderFunction = renderFunction;
  160. var that = this;
  161. BABYLON.Tools.QueueNewFrame(function () {
  162. that._renderLoop();
  163. });
  164. };
  165. BABYLON.Engine.prototype.switchFullscreen = function (requestPointerLock) {
  166. if (this.isFullscreen) {
  167. BABYLON.Tools.ExitFullscreen();
  168. } else {
  169. this._pointerLockRequested = requestPointerLock;
  170. BABYLON.Tools.RequestFullscreen(this._renderingCanvas);
  171. }
  172. };
  173. BABYLON.Engine.prototype.clear = function (color, backBuffer, depthStencil) {
  174. this._gl.clearColor(color.r, color.g, color.b, color.a !== undefined ? color.a : 1.0);
  175. this._gl.clearDepth(1.0);
  176. var mode = 0;
  177. if (backBuffer)
  178. mode |= this._gl.COLOR_BUFFER_BIT;
  179. if (depthStencil)
  180. mode |= this._gl.DEPTH_BUFFER_BIT;
  181. this._gl.clear(mode);
  182. };
  183. BABYLON.Engine.prototype.setViewport = function (viewport, requiredWidth, requiredHeight) {
  184. var width = requiredWidth || this._renderingCanvas.width;
  185. var height = requiredHeight || this._renderingCanvas.height;
  186. var x = viewport.x || 0;
  187. var y = viewport.y || 0;
  188. this._cachedViewport = viewport;
  189. this._gl.viewport(x * width, y * height, width * viewport.width, height * viewport.height);
  190. };
  191. BABYLON.Engine.prototype.setDirectViewport = function (x, y, width, height) {
  192. this._cachedViewport = null;
  193. this._gl.viewport(x, y, width, height);
  194. };
  195. BABYLON.Engine.prototype.beginFrame = function () {
  196. BABYLON.Tools._MeasureFps();
  197. };
  198. BABYLON.Engine.prototype.endFrame = function () {
  199. this.flushFramebuffer();
  200. };
  201. BABYLON.Engine.prototype.resize = function () {
  202. this._renderingCanvas.width = this._renderingCanvas.clientWidth / this._hardwareScalingLevel;
  203. this._renderingCanvas.height = this._renderingCanvas.clientHeight / this._hardwareScalingLevel;
  204. };
  205. BABYLON.Engine.prototype.bindFramebuffer = function (texture) {
  206. var gl = this._gl;
  207. gl.bindFramebuffer(gl.FRAMEBUFFER, texture._framebuffer);
  208. this._gl.viewport(0, 0, texture._width, texture._height);
  209. this.wipeCaches();
  210. };
  211. BABYLON.Engine.prototype.unBindFramebuffer = function (texture) {
  212. if (texture.generateMipMaps) {
  213. var gl = this._gl;
  214. gl.bindTexture(gl.TEXTURE_2D, texture);
  215. gl.generateMipmap(gl.TEXTURE_2D);
  216. gl.bindTexture(gl.TEXTURE_2D, null);
  217. }
  218. };
  219. BABYLON.Engine.prototype.flushFramebuffer = function () {
  220. this._gl.flush();
  221. };
  222. BABYLON.Engine.prototype.restoreDefaultFramebuffer = function () {
  223. this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, null);
  224. this.setViewport(this._cachedViewport);
  225. this.wipeCaches();
  226. };
  227. // VBOs
  228. BABYLON.Engine.prototype.createVertexBuffer = function (vertices) {
  229. var vbo = this._gl.createBuffer();
  230. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
  231. this._gl.bufferData(this._gl.ARRAY_BUFFER, new Float32Array(vertices), this._gl.STATIC_DRAW);
  232. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  233. vbo.references = 1;
  234. return vbo;
  235. };
  236. BABYLON.Engine.prototype.createDynamicVertexBuffer = function (capacity) {
  237. var vbo = this._gl.createBuffer();
  238. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
  239. this._gl.bufferData(this._gl.ARRAY_BUFFER, capacity, this._gl.DYNAMIC_DRAW);
  240. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  241. vbo.references = 1;
  242. return vbo;
  243. };
  244. BABYLON.Engine.prototype.updateDynamicVertexBuffer = function (vertexBuffer, vertices, length) {
  245. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  246. // Should be (vertices instanceof Float32Array ? vertices : new Float32Array(vertices)) but Chrome raises an Exception in this case :(
  247. if (length) {
  248. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, new Float32Array(vertices, 0, length));
  249. } else {
  250. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, new Float32Array(vertices));
  251. }
  252. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  253. };
  254. BABYLON.Engine.prototype.createIndexBuffer = function (indices) {
  255. var vbo = this._gl.createBuffer();
  256. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, vbo);
  257. this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), this._gl.STATIC_DRAW);
  258. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, null);
  259. vbo.references = 1;
  260. return vbo;
  261. };
  262. BABYLON.Engine.prototype.bindBuffers = function (vertexBuffer, indexBuffer, vertexDeclaration, vertexStrideSize, effect) {
  263. if (this._cachedVertexBuffers !== vertexBuffer || this._cachedEffectForVertexBuffers !== effect) {
  264. this._cachedVertexBuffers = vertexBuffer;
  265. this._cachedEffectForVertexBuffers = effect;
  266. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  267. var offset = 0;
  268. for (var index = 0; index < vertexDeclaration.length; index++) {
  269. var order = effect.getAttribute(index);
  270. if (order >= 0) {
  271. this._gl.vertexAttribPointer(order, vertexDeclaration[index], this._gl.FLOAT, false, vertexStrideSize, offset);
  272. }
  273. offset += vertexDeclaration[index] * 4;
  274. }
  275. }
  276. if (this._cachedIndexBuffer !== indexBuffer) {
  277. this._cachedIndexBuffer = indexBuffer;
  278. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  279. }
  280. };
  281. BABYLON.Engine.prototype.bindMultiBuffers = function (vertexBuffers, indexBuffer, effect) {
  282. if (this._cachedVertexBuffers !== vertexBuffers || this._cachedEffectForVertexBuffers !== effect) {
  283. this._cachedVertexBuffers = vertexBuffers;
  284. this._cachedEffectForVertexBuffers = effect;
  285. var attributes = effect.getAttributesNames();
  286. for (var index = 0; index < attributes.length; index++) {
  287. var order = effect.getAttribute(index);
  288. if (order >= 0) {
  289. var vertexBuffer = vertexBuffers[attributes[index]];
  290. var stride = vertexBuffer.getStrideSize();
  291. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer._buffer);
  292. this._gl.vertexAttribPointer(order, stride, this._gl.FLOAT, false, stride * 4, 0);
  293. }
  294. }
  295. }
  296. if (this._cachedIndexBuffer !== indexBuffer) {
  297. this._cachedIndexBuffer = indexBuffer;
  298. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  299. }
  300. };
  301. BABYLON.Engine.prototype._releaseBuffer = function (buffer) {
  302. buffer.references--;
  303. if (buffer.references === 0) {
  304. this._gl.deleteBuffer(buffer);
  305. }
  306. };
  307. BABYLON.Engine.prototype.draw = function (useTriangles, indexStart, indexCount) {
  308. this._gl.drawElements(useTriangles ? this._gl.TRIANGLES : this._gl.LINES, indexCount, this._gl.UNSIGNED_SHORT, indexStart * 2);
  309. };
  310. // Shaders
  311. BABYLON.Engine.prototype.createEffect = function (baseName, attributesNames, uniformsNames, samplers, defines, optionalDefines) {
  312. var vertex = baseName.vertexElement || baseName.vertex || baseName;
  313. var fragment = baseName.fragmentElement || baseName.fragment || baseName;
  314. var name = vertex + "+" + fragment + "@" + defines;
  315. if (this._compiledEffects[name]) {
  316. return this._compiledEffects[name];
  317. }
  318. var effect = new BABYLON.Effect(baseName, attributesNames, uniformsNames, samplers, this, defines, optionalDefines);
  319. this._compiledEffects[name] = effect;
  320. return effect;
  321. };
  322. var compileShader = function (gl, source, type, defines) {
  323. var shader = gl.createShader(type === "vertex" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);
  324. gl.shaderSource(shader, (defines ? defines + "\n" : "") + source);
  325. gl.compileShader(shader);
  326. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  327. throw new Error(gl.getShaderInfoLog(shader));
  328. }
  329. return shader;
  330. };
  331. BABYLON.Engine.prototype.createShaderProgram = function (vertexCode, fragmentCode, defines) {
  332. var vertexShader = compileShader(this._gl, vertexCode, "vertex", defines);
  333. var fragmentShader = compileShader(this._gl, fragmentCode, "fragment", defines);
  334. var shaderProgram = this._gl.createProgram();
  335. this._gl.attachShader(shaderProgram, vertexShader);
  336. this._gl.attachShader(shaderProgram, fragmentShader);
  337. this._gl.linkProgram(shaderProgram);
  338. var error = this._gl.getProgramInfoLog(shaderProgram);
  339. if (error) {
  340. throw new Error(error);
  341. }
  342. this._gl.deleteShader(vertexShader);
  343. this._gl.deleteShader(fragmentShader);
  344. return shaderProgram;
  345. };
  346. BABYLON.Engine.prototype.getUniforms = function (shaderProgram, uniformsNames) {
  347. var results = [];
  348. for (var index = 0; index < uniformsNames.length; index++) {
  349. results.push(this._gl.getUniformLocation(shaderProgram, uniformsNames[index]));
  350. }
  351. return results;
  352. };
  353. BABYLON.Engine.prototype.getAttributes = function (shaderProgram, attributesNames) {
  354. var results = [];
  355. for (var index = 0; index < attributesNames.length; index++) {
  356. try {
  357. results.push(this._gl.getAttribLocation(shaderProgram, attributesNames[index]));
  358. } catch (e) {
  359. results.push(-1);
  360. }
  361. }
  362. return results;
  363. };
  364. BABYLON.Engine.prototype.enableEffect = function (effect) {
  365. if (!effect || !effect.getAttributesCount() || this._currentEffect === effect) {
  366. return;
  367. }
  368. // Use program
  369. this._gl.useProgram(effect.getProgram());
  370. for (var index = 0; index < effect.getAttributesCount() ; index++) {
  371. // Attributes
  372. var order = effect.getAttribute(index);
  373. if (order >= 0) {
  374. this._gl.enableVertexAttribArray(effect.getAttribute(index));
  375. }
  376. }
  377. this._currentEffect = effect;
  378. };
  379. BABYLON.Engine.prototype.setArray = function (uniform, array) {
  380. if (!uniform)
  381. return;
  382. this._gl.uniform1fv(uniform, array);
  383. };
  384. BABYLON.Engine.prototype.setMatrices = function (uniform, matrices) {
  385. if (!uniform)
  386. return;
  387. this._gl.uniformMatrix4fv(uniform, false, matrices);
  388. };
  389. BABYLON.Engine.prototype.setMatrix = function (uniform, matrix) {
  390. if (!uniform)
  391. return;
  392. this._gl.uniformMatrix4fv(uniform, false, matrix.toArray());
  393. };
  394. BABYLON.Engine.prototype.setFloat = function (uniform, value) {
  395. if (!uniform)
  396. return;
  397. this._gl.uniform1f(uniform, value);
  398. };
  399. BABYLON.Engine.prototype.setFloat2 = function (uniform, x, y) {
  400. if (!uniform)
  401. return;
  402. this._gl.uniform2f(uniform, x, y);
  403. };
  404. BABYLON.Engine.prototype.setFloat3 = function (uniform, x, y, z) {
  405. if (!uniform)
  406. return;
  407. this._gl.uniform3f(uniform, x, y, z);
  408. };
  409. BABYLON.Engine.prototype.setBool = function (uniform, bool) {
  410. if (!uniform)
  411. return;
  412. this._gl.uniform1i(uniform, bool);
  413. };
  414. BABYLON.Engine.prototype.setFloat4 = function (uniform, x, y, z, w) {
  415. if (!uniform)
  416. return;
  417. this._gl.uniform4f(uniform, x, y, z, w);
  418. };
  419. BABYLON.Engine.prototype.setColor3 = function (uniform, color3) {
  420. if (!uniform)
  421. return;
  422. this._gl.uniform3f(uniform, color3.r, color3.g, color3.b);
  423. };
  424. BABYLON.Engine.prototype.setColor4 = function (uniform, color3, alpha) {
  425. if (!uniform)
  426. return;
  427. this._gl.uniform4f(uniform, color3.r, color3.g, color3.b, alpha);
  428. };
  429. // States
  430. BABYLON.Engine.prototype.setState = function (culling) {
  431. // Culling
  432. if (this._currentState.culling !== culling) {
  433. if (culling) {
  434. this._gl.cullFace(this.cullBackFaces ? this._gl.BACK : this._gl.FRONT);
  435. this._gl.enable(this._gl.CULL_FACE);
  436. } else {
  437. this._gl.disable(this._gl.CULL_FACE);
  438. }
  439. this._currentState.culling = culling;
  440. }
  441. };
  442. BABYLON.Engine.prototype.setDepthBuffer = function (enable) {
  443. if (enable) {
  444. this._gl.enable(this._gl.DEPTH_TEST);
  445. } else {
  446. this._gl.disable(this._gl.DEPTH_TEST);
  447. }
  448. };
  449. BABYLON.Engine.prototype.setDepthWrite = function (enable) {
  450. this._gl.depthMask(enable);
  451. };
  452. BABYLON.Engine.prototype.setColorWrite = function (enable) {
  453. this._gl.colorMask(enable, enable, enable, enable);
  454. };
  455. BABYLON.Engine.prototype.setAlphaMode = function (mode) {
  456. switch (mode) {
  457. case BABYLON.Engine.ALPHA_DISABLE:
  458. this.setDepthWrite(true);
  459. this._gl.disable(this._gl.BLEND);
  460. break;
  461. case BABYLON.Engine.ALPHA_COMBINE:
  462. this.setDepthWrite(false);
  463. this._gl.blendFuncSeparate(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE);
  464. this._gl.enable(this._gl.BLEND);
  465. break;
  466. case BABYLON.Engine.ALPHA_ADD:
  467. this.setDepthWrite(false);
  468. this._gl.blendFuncSeparate(this._gl.ONE, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
  469. this._gl.enable(this._gl.BLEND);
  470. break;
  471. }
  472. };
  473. BABYLON.Engine.prototype.setAlphaTesting = function (enable) {
  474. this._alphaTest = enable;
  475. };
  476. BABYLON.Engine.prototype.getAlphaTesting = function () {
  477. return this._alphaTest;
  478. };
  479. // Textures
  480. BABYLON.Engine.prototype.wipeCaches = function () {
  481. this._activeTexturesCache = [];
  482. this._currentEffect = null;
  483. this._currentState = {
  484. culling: null
  485. };
  486. this._cachedVertexBuffers = null;
  487. this._cachedVertexBuffers = null;
  488. this._cachedEffectForVertexBuffers = null;
  489. };
  490. var getExponantOfTwo = function (value, max) {
  491. var count = 1;
  492. do {
  493. count *= 2;
  494. } while (count < value);
  495. if (count > max)
  496. count = max;
  497. return count;
  498. };
  499. var prepareWebGLTexture = function (texture, scene, width, height, invertY, noMipmap, processFunction) {
  500. var engine = scene.getEngine();
  501. var potWidth = getExponantOfTwo(width, engine._caps.maxTextureSize);
  502. var potHeight = getExponantOfTwo(height, engine._caps.maxTextureSize);
  503. engine._gl.bindTexture(engine._gl.TEXTURE_2D, texture);
  504. engine._gl.pixelStorei(engine._gl.UNPACK_FLIP_Y_WEBGL, invertY === undefined ? true : invertY);
  505. processFunction(potWidth, potHeight);
  506. engine._gl.texParameteri(engine._gl.TEXTURE_2D, engine._gl.TEXTURE_MAG_FILTER, engine._gl.LINEAR);
  507. if (noMipmap) {
  508. engine._gl.texParameteri(engine._gl.TEXTURE_2D, engine._gl.TEXTURE_MIN_FILTER, engine._gl.LINEAR);
  509. } else {
  510. engine._gl.texParameteri(engine._gl.TEXTURE_2D, engine._gl.TEXTURE_MIN_FILTER, engine._gl.LINEAR_MIPMAP_LINEAR);
  511. engine._gl.generateMipmap(engine._gl.TEXTURE_2D);
  512. }
  513. engine._gl.bindTexture(engine._gl.TEXTURE_2D, null);
  514. engine._activeTexturesCache = [];
  515. texture._baseWidth = width;
  516. texture._baseHeight = height;
  517. texture._width = potWidth;
  518. texture._height = potHeight;
  519. texture.isReady = true;
  520. scene._removePendingData(texture);
  521. };
  522. BABYLON.Engine.prototype.createTexture = function (url, noMipmap, invertY, scene) {
  523. var texture = this._gl.createTexture();
  524. var that = this;
  525. var isDDS = this.getCaps().s3tc && (url.substr(url.length - 4, 4).toLowerCase() === ".dds");
  526. scene._addPendingData(texture);
  527. texture.url = url;
  528. texture.noMipmap = noMipmap;
  529. texture.references = 1;
  530. this._loadedTexturesCache.push(texture);
  531. if (isDDS) {
  532. BABYLON.Tools.LoadFile(url, function (data) {
  533. var info = BABYLON.Tools.GetDDSInfo(data);
  534. var loadMipmap = info.mipmapCount > 1 && !noMipmap;
  535. prepareWebGLTexture(texture, scene, info.width, info.height, invertY, !loadMipmap, function (potWidth, potHeight) {
  536. BABYLON.Tools.UploadDDSLevels(that._gl, that.getCaps().s3tc, data, loadMipmap);
  537. });
  538. }, null, scene.database, true);
  539. } else {
  540. var onload = function(img) {
  541. prepareWebGLTexture(texture, scene, img.width, img.height, invertY, noMipmap, function (potWidth, potHeight) {
  542. var isPot = (img.width == potWidth && img.height == potHeight);
  543. if (!isPot) {
  544. that._workingCanvas.width = potWidth;
  545. that._workingCanvas.height = potHeight;
  546. that._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, potWidth, potHeight);
  547. }
  548. that._gl.texImage2D(that._gl.TEXTURE_2D, 0, that._gl.RGBA, that._gl.RGBA, that._gl.UNSIGNED_BYTE, isPot ? img : that._workingCanvas);
  549. });
  550. };
  551. var onerror = function() {
  552. scene._removePendingData(texture);
  553. };
  554. BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
  555. }
  556. return texture;
  557. };
  558. BABYLON.Engine.prototype.createDynamicTexture = function (width, height, generateMipMaps) {
  559. var texture = this._gl.createTexture();
  560. width = getExponantOfTwo(width, this._caps.maxTextureSize);
  561. height = getExponantOfTwo(height, this._caps.maxTextureSize);
  562. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  563. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, this._gl.LINEAR);
  564. if (!generateMipMaps) {
  565. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR);
  566. } else {
  567. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR_MIPMAP_LINEAR);
  568. }
  569. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  570. this._activeTexturesCache = [];
  571. texture._baseWidth = width;
  572. texture._baseHeight = height;
  573. texture._width = width;
  574. texture._height = height;
  575. texture.isReady = false;
  576. texture.generateMipMaps = generateMipMaps;
  577. texture.references = 1;
  578. this._loadedTexturesCache.push(texture);
  579. return texture;
  580. };
  581. BABYLON.Engine.prototype.updateDynamicTexture = function (texture, canvas, invertY) {
  582. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  583. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY);
  584. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, canvas);
  585. if (texture.generateMipMaps) {
  586. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  587. }
  588. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  589. this._activeTexturesCache = [];
  590. texture.isReady = true;
  591. };
  592. BABYLON.Engine.prototype.updateVideoTexture = function (texture, video, invertY) {
  593. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  594. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? false : true); // Video are upside down by default
  595. // Scale the video if it is a NPOT
  596. if (video.videoWidth !== texture._width || video.videoHeight !== texture._height) {
  597. if (!texture._workingCanvas) {
  598. texture._workingCanvas = document.createElement("canvas");
  599. texture._workingContext = texture._workingCanvas.getContext("2d");
  600. texture._workingCanvas.width = texture._width;
  601. texture._workingCanvas.height = texture._height;
  602. }
  603. texture._workingContext.drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, texture._width, texture._height);
  604. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, texture._workingCanvas);
  605. } else {
  606. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, video);
  607. }
  608. if (texture.generateMipMaps) {
  609. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  610. }
  611. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  612. this._activeTexturesCache = [];
  613. texture.isReady = true;
  614. };
  615. BABYLON.Engine.prototype.createRenderTargetTexture = function (size, options) {
  616. // old version had a "generateMipMaps" arg instead of options.
  617. // if options.generateMipMaps is undefined, consider that options itself if the generateMipmaps value
  618. // in the same way, generateDepthBuffer is defaulted to true
  619. var generateMipMaps = false;
  620. var generateDepthBuffer = true;
  621. var samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  622. if (options !== undefined) {
  623. generateMipMaps = options.generateMipMaps === undefined ? options : options.generateMipmaps;
  624. generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  625. if (options.samplingMode !== undefined) {
  626. samplingMode = options.samplingMode;
  627. }
  628. }
  629. var gl = this._gl;
  630. var texture = gl.createTexture();
  631. gl.bindTexture(gl.TEXTURE_2D, texture);
  632. var width = size.width || size;
  633. var height = size.height || size;
  634. var magFilter = gl.NEAREST;
  635. var minFilter = gl.NEAREST;
  636. if (samplingMode === BABYLON.Texture.BILINEAR_SAMPLINGMODE) {
  637. magFilter = gl.LINEAR;
  638. if (generateMipMaps) {
  639. minFilter = gl.LINEAR_MIPMAP_NEAREST;
  640. } else {
  641. minFilter = gl.LINEAR;
  642. }
  643. } else if (samplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
  644. magFilter = gl.LINEAR;
  645. if (generateMipMaps) {
  646. minFilter = gl.LINEAR_MIPMAP_LINEAR;
  647. } else {
  648. minFilter = gl.LINEAR;
  649. }
  650. }
  651. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
  652. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
  653. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  654. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  655. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  656. var depthBuffer;
  657. // Create the depth buffer
  658. if (generateDepthBuffer) {
  659. depthBuffer = gl.createRenderbuffer();
  660. gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
  661. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
  662. }
  663. // Create the framebuffer
  664. var framebuffer = gl.createFramebuffer();
  665. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  666. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
  667. if (generateDepthBuffer) {
  668. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
  669. }
  670. // Unbind
  671. gl.bindTexture(gl.TEXTURE_2D, null);
  672. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  673. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  674. texture._framebuffer = framebuffer;
  675. if (generateDepthBuffer) {
  676. texture._depthBuffer = depthBuffer;
  677. }
  678. texture._width = width;
  679. texture._height = height;
  680. texture.isReady = true;
  681. texture.generateMipMaps = generateMipMaps;
  682. texture.references = 1;
  683. this._activeTexturesCache = [];
  684. this._loadedTexturesCache.push(texture);
  685. return texture;
  686. };
  687. var cascadeLoad = function (rootUrl, index, loadedImages, scene, onfinish, extensions) {
  688. var img;
  689. var onload = function () {
  690. loadedImages.push(img);
  691. scene._removePendingData(img);
  692. if (index != extensions.length - 1) {
  693. cascadeLoad(rootUrl, index + 1, loadedImages, scene, onfinish, extensions);
  694. } else {
  695. onfinish(loadedImages);
  696. }
  697. };
  698. var onerror = function () {
  699. scene._removePendingData(img);
  700. };
  701. img = BABYLON.Tools.LoadImage(rootUrl + extensions[index], onload, onerror, scene.database);
  702. scene._addPendingData(img);
  703. };
  704. BABYLON.Engine.prototype.createCubeTexture = function (rootUrl, scene, extensions, noMipmap) {
  705. var gl = this._gl;
  706. var texture = gl.createTexture();
  707. texture.isCube = true;
  708. texture.url = rootUrl;
  709. texture.references = 1;
  710. this._loadedTexturesCache.push(texture);
  711. var that = this;
  712. cascadeLoad(rootUrl, 0, [], scene, function (imgs) {
  713. var width = getExponantOfTwo(imgs[0].width, that._caps.maxCubemapTextureSize);
  714. var height = width;
  715. that._workingCanvas.width = width;
  716. that._workingCanvas.height = height;
  717. var faces = [
  718. gl.TEXTURE_CUBE_MAP_POSITIVE_X, gl.TEXTURE_CUBE_MAP_POSITIVE_Y, gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
  719. gl.TEXTURE_CUBE_MAP_NEGATIVE_X, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
  720. ];
  721. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  722. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
  723. for (var index = 0; index < faces.length; index++) {
  724. that._workingContext.drawImage(imgs[index], 0, 0, imgs[index].width, imgs[index].height, 0, 0, width, height);
  725. gl.texImage2D(faces[index], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, that._workingCanvas);
  726. }
  727. if (!noMipmap) {
  728. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  729. }
  730. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  731. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, noMipmap ? gl.LINEAR : gl.LINEAR_MIPMAP_LINEAR);
  732. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  733. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  734. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  735. that._activeTexturesCache = [];
  736. texture._width = width;
  737. texture._height = height;
  738. texture.isReady = true;
  739. }, extensions);
  740. return texture;
  741. };
  742. BABYLON.Engine.prototype._releaseTexture = function (texture) {
  743. var gl = this._gl;
  744. if (texture._framebuffer) {
  745. gl.deleteFramebuffer(texture._framebuffer);
  746. }
  747. if (texture._depthBuffer) {
  748. gl.deleteRenderbuffer(texture._depthBuffer);
  749. }
  750. gl.deleteTexture(texture);
  751. // Unbind channels
  752. for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
  753. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  754. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  755. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  756. this._activeTexturesCache[channel] = null;
  757. }
  758. var index = this._loadedTexturesCache.indexOf(texture);
  759. if (index !== -1) {
  760. this._loadedTexturesCache.splice(index, 1);
  761. }
  762. };
  763. BABYLON.Engine.prototype.bindSamplers = function (effect) {
  764. this._gl.useProgram(effect.getProgram());
  765. var samplers = effect.getSamplers();
  766. for (var index = 0; index < samplers.length; index++) {
  767. var uniform = effect.getUniform(samplers[index]);
  768. this._gl.uniform1i(uniform, index);
  769. }
  770. this._currentEffect = null;
  771. };
  772. BABYLON.Engine.prototype._bindTexture = function (channel, texture) {
  773. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  774. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  775. this._activeTexturesCache[channel] = null;
  776. };
  777. BABYLON.Engine.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  778. this._bindTexture(channel, postProcess._textures.data[postProcess._currentRenderTextureInd]);
  779. };
  780. BABYLON.Engine.prototype.setTexture = function (channel, texture) {
  781. if (channel < 0) {
  782. return;
  783. }
  784. // Not ready?
  785. if (!texture || !texture.isReady()) {
  786. if (this._activeTexturesCache[channel] != null) {
  787. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  788. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  789. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  790. this._activeTexturesCache[channel] = null;
  791. }
  792. return;
  793. }
  794. // Video
  795. if (texture instanceof BABYLON.VideoTexture) {
  796. if (texture._update()) {
  797. this._activeTexturesCache[channel] = null;
  798. }
  799. } else if (texture.delayLoadState == BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) { // Delay loading
  800. texture.delayLoad();
  801. return;
  802. }
  803. if (this._activeTexturesCache[channel] == texture) {
  804. return;
  805. }
  806. this._activeTexturesCache[channel] = texture;
  807. var internalTexture = texture.getInternalTexture();
  808. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  809. if (internalTexture.isCube) {
  810. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, internalTexture);
  811. if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
  812. internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
  813. 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);
  814. 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);
  815. }
  816. this._setAnisotropicLevel(this._gl.TEXTURE_CUBE_MAP, texture);
  817. } else {
  818. this._gl.bindTexture(this._gl.TEXTURE_2D, internalTexture);
  819. if (internalTexture._cachedWrapU !== texture.wrapU) {
  820. internalTexture._cachedWrapU = texture.wrapU;
  821. switch (texture.wrapU) {
  822. case BABYLON.Texture.WRAP_ADDRESSMODE:
  823. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.REPEAT);
  824. break;
  825. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  826. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
  827. break;
  828. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  829. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.MIRRORED_REPEAT);
  830. break;
  831. }
  832. }
  833. if (internalTexture._cachedWrapV !== texture.wrapV) {
  834. internalTexture._cachedWrapV = texture.wrapV;
  835. switch (texture.wrapV) {
  836. case BABYLON.Texture.WRAP_ADDRESSMODE:
  837. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.REPEAT);
  838. break;
  839. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  840. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
  841. break;
  842. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  843. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.MIRRORED_REPEAT);
  844. break;
  845. }
  846. }
  847. this._setAnisotropicLevel(this._gl.TEXTURE_2D, texture);
  848. }
  849. };
  850. BABYLON.Engine.prototype._setAnisotropicLevel = function (key, texture) {
  851. var anisotropicFilterExtension = this._caps.textureAnisotropicFilterExtension;
  852. if (anisotropicFilterExtension && texture._cachedAnisotropicFilteringLevel !== texture.anisotropicFilteringLevel) {
  853. this._gl.texParameterf(key, anisotropicFilterExtension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(texture.anisotropicFilteringLevel, this._caps.maxAnisotropy));
  854. texture._cachedAnisotropicFilteringLevel = texture.anisotropicFilteringLevel;
  855. }
  856. };
  857. // Dispose
  858. BABYLON.Engine.prototype.dispose = function () {
  859. // Release scenes
  860. while (this.scenes.length) {
  861. this.scenes[0].dispose();
  862. }
  863. // Release effects
  864. for (var name in this._compiledEffects.length) {
  865. this._gl.deleteProgram(this._compiledEffects[name]._program);
  866. }
  867. };
  868. // Statics
  869. BABYLON.Engine.ShadersRepository = "Babylon/Shaders/";
  870. BABYLON.Engine.ALPHA_DISABLE = 0;
  871. BABYLON.Engine.ALPHA_ADD = 1;
  872. BABYLON.Engine.ALPHA_COMBINE = 2;
  873. // Statics
  874. BABYLON.Engine.DELAYLOADSTATE_NONE = 0;
  875. BABYLON.Engine.DELAYLOADSTATE_LOADED = 1;
  876. BABYLON.Engine.DELAYLOADSTATE_LOADING = 2;
  877. BABYLON.Engine.DELAYLOADSTATE_NOTLOADED = 4;
  878. BABYLON.Engine.epsilon = 0.001;
  879. BABYLON.Engine.collisionsEpsilon = 0.001;
  880. BABYLON.Engine.isSupported = function () {
  881. try {
  882. var tempcanvas = document.createElement("canvas");
  883. var gl = tempcanvas.getContext("webgl") || tempcanvas.getContext("experimental-webgl");
  884. return gl != null && !!window.WebGLRenderingContext;
  885. } catch (e) {
  886. return false;
  887. }
  888. };
  889. })();