babylon.engine.js 47 KB

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