babylon.engine.js 44 KB

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