babylon.engine.js 48 KB

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