babylon.engine.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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. };
  393. // Shaders
  394. Engine.prototype._releaseEffect = function (effect) {
  395. if (this._compiledEffects[effect._key]) {
  396. delete this._compiledEffects[effect._key];
  397. if (effect.getProgram()) {
  398. this._gl.deleteProgram(effect.getProgram());
  399. }
  400. }
  401. };
  402. Engine.prototype.createEffect = function (baseName, attributesNames, uniformsNames, samplers, defines, optionalDefines, onCompiled, onError) {
  403. var vertex = baseName.vertexElement || baseName.vertex || baseName;
  404. var fragment = baseName.fragmentElement || baseName.fragment || baseName;
  405. var name = vertex + "+" + fragment + "@" + defines;
  406. if (this._compiledEffects[name]) {
  407. return this._compiledEffects[name];
  408. }
  409. var effect = new BABYLON.Effect(baseName, attributesNames, uniformsNames, samplers, this, defines, optionalDefines, onCompiled, onError);
  410. effect._key = name;
  411. this._compiledEffects[name] = effect;
  412. return effect;
  413. };
  414. Engine.prototype.createShaderProgram = function (vertexCode, fragmentCode, defines) {
  415. var vertexShader = compileShader(this._gl, vertexCode, "vertex", defines);
  416. var fragmentShader = compileShader(this._gl, fragmentCode, "fragment", defines);
  417. var shaderProgram = this._gl.createProgram();
  418. this._gl.attachShader(shaderProgram, vertexShader);
  419. this._gl.attachShader(shaderProgram, fragmentShader);
  420. var linked = this._gl.linkProgram(shaderProgram);
  421. if (!linked) {
  422. var error = this._gl.getProgramInfoLog(shaderProgram);
  423. if (error) {
  424. throw new Error(error);
  425. }
  426. }
  427. this._gl.deleteShader(vertexShader);
  428. this._gl.deleteShader(fragmentShader);
  429. return shaderProgram;
  430. };
  431. Engine.prototype.getUniforms = function (shaderProgram, uniformsNames) {
  432. var results = [];
  433. for (var index = 0; index < uniformsNames.length; index++) {
  434. results.push(this._gl.getUniformLocation(shaderProgram, uniformsNames[index]));
  435. }
  436. return results;
  437. };
  438. Engine.prototype.getAttributes = function (shaderProgram, attributesNames) {
  439. var results = [];
  440. for (var index = 0; index < attributesNames.length; index++) {
  441. try {
  442. results.push(this._gl.getAttribLocation(shaderProgram, attributesNames[index]));
  443. } catch (e) {
  444. results.push(-1);
  445. }
  446. }
  447. return results;
  448. };
  449. Engine.prototype.enableEffect = function (effect) {
  450. if (!effect || !effect.getAttributesCount() || this._currentEffect === effect) {
  451. return;
  452. }
  453. // Use program
  454. this._gl.useProgram(effect.getProgram());
  455. var currentCount = effect.getAttributesCount();
  456. var maxIndex = 0;
  457. for (var index = 0; index < currentCount; index++) {
  458. // Attributes
  459. var order = effect.getAttribute(index);
  460. if (order >= 0) {
  461. this._gl.enableVertexAttribArray(effect.getAttribute(index));
  462. maxIndex = Math.max(maxIndex, order);
  463. }
  464. }
  465. for (index = maxIndex + 1; index <= this._lastVertexAttribIndex; index++) {
  466. this._gl.disableVertexAttribArray(index);
  467. }
  468. this._lastVertexAttribIndex = maxIndex;
  469. this._currentEffect = effect;
  470. };
  471. Engine.prototype.setArray = function (uniform, array) {
  472. if (!uniform)
  473. return;
  474. this._gl.uniform1fv(uniform, array);
  475. };
  476. Engine.prototype.setMatrices = function (uniform, matrices) {
  477. if (!uniform)
  478. return;
  479. this._gl.uniformMatrix4fv(uniform, false, matrices);
  480. };
  481. Engine.prototype.setMatrix = function (uniform, matrix) {
  482. if (!uniform)
  483. return;
  484. this._gl.uniformMatrix4fv(uniform, false, matrix.toArray());
  485. };
  486. Engine.prototype.setFloat = function (uniform, value) {
  487. if (!uniform)
  488. return;
  489. this._gl.uniform1f(uniform, value);
  490. };
  491. Engine.prototype.setFloat2 = function (uniform, x, y) {
  492. if (!uniform)
  493. return;
  494. this._gl.uniform2f(uniform, x, y);
  495. };
  496. Engine.prototype.setFloat3 = function (uniform, x, y, z) {
  497. if (!uniform)
  498. return;
  499. this._gl.uniform3f(uniform, x, y, z);
  500. };
  501. Engine.prototype.setBool = function (uniform, bool) {
  502. if (!uniform)
  503. return;
  504. this._gl.uniform1i(uniform, bool);
  505. };
  506. Engine.prototype.setFloat4 = function (uniform, x, y, z, w) {
  507. if (!uniform)
  508. return;
  509. this._gl.uniform4f(uniform, x, y, z, w);
  510. };
  511. Engine.prototype.setColor3 = function (uniform, color3) {
  512. if (!uniform)
  513. return;
  514. this._gl.uniform3f(uniform, color3.r, color3.g, color3.b);
  515. };
  516. Engine.prototype.setColor4 = function (uniform, color3, alpha) {
  517. if (!uniform)
  518. return;
  519. this._gl.uniform4f(uniform, color3.r, color3.g, color3.b, alpha);
  520. };
  521. // States
  522. Engine.prototype.setState = function (culling) {
  523. // Culling
  524. if (this._cullingState !== culling) {
  525. if (culling) {
  526. this._gl.cullFace(this.cullBackFaces ? this._gl.BACK : this._gl.FRONT);
  527. this._gl.enable(this._gl.CULL_FACE);
  528. } else {
  529. this._gl.disable(this._gl.CULL_FACE);
  530. }
  531. this._cullingState = culling;
  532. }
  533. };
  534. Engine.prototype.setDepthBuffer = function (enable) {
  535. if (enable) {
  536. this._gl.enable(this._gl.DEPTH_TEST);
  537. } else {
  538. this._gl.disable(this._gl.DEPTH_TEST);
  539. }
  540. };
  541. Engine.prototype.setDepthWrite = function (enable) {
  542. this._gl.depthMask(enable);
  543. this._depthMask = enable;
  544. };
  545. Engine.prototype.setColorWrite = function (enable) {
  546. this._gl.colorMask(enable, enable, enable, enable);
  547. };
  548. Engine.prototype.setAlphaMode = function (mode) {
  549. switch (mode) {
  550. case BABYLON.Engine.ALPHA_DISABLE:
  551. this.setDepthWrite(true);
  552. this._gl.disable(this._gl.BLEND);
  553. break;
  554. case BABYLON.Engine.ALPHA_COMBINE:
  555. this.setDepthWrite(false);
  556. this._gl.blendFuncSeparate(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ONE, this._gl.ONE);
  557. this._gl.enable(this._gl.BLEND);
  558. break;
  559. case BABYLON.Engine.ALPHA_ADD:
  560. this.setDepthWrite(false);
  561. this._gl.blendFuncSeparate(this._gl.ONE, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
  562. this._gl.enable(this._gl.BLEND);
  563. break;
  564. }
  565. };
  566. Engine.prototype.setAlphaTesting = function (enable) {
  567. this._alphaTest = enable;
  568. };
  569. Engine.prototype.getAlphaTesting = function () {
  570. return this._alphaTest;
  571. };
  572. // Textures
  573. Engine.prototype.wipeCaches = function () {
  574. this._activeTexturesCache = [];
  575. this._currentEffect = null;
  576. this._cullingState = null;
  577. this._cachedVertexBuffers = null;
  578. this._cachedIndexBuffer = null;
  579. this._cachedEffectForVertexBuffers = null;
  580. };
  581. Engine.prototype.createTexture = function (url, noMipmap, invertY, scene) {
  582. var _this = this;
  583. var texture = this._gl.createTexture();
  584. var isDDS = this.getCaps().s3tc && (url.substr(url.length - 4, 4).toLowerCase() === ".dds");
  585. scene._addPendingData(texture);
  586. texture.url = url;
  587. texture.noMipmap = noMipmap;
  588. texture.references = 1;
  589. this._loadedTexturesCache.push(texture);
  590. if (isDDS) {
  591. BABYLON.Tools.LoadFile(url, function (data) {
  592. var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
  593. var loadMipmap = info.mipmapCount > 1 && !noMipmap;
  594. prepareWebGLTexture(texture, _this._gl, scene, info.width, info.height, invertY, !loadMipmap, true, function () {
  595. BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, loadMipmap);
  596. });
  597. }, null, scene.database, true);
  598. } else {
  599. var onload = function (img) {
  600. prepareWebGLTexture(texture, _this._gl, scene, img.width, img.height, invertY, noMipmap, false, function (potWidth, potHeight) {
  601. var isPot = (img.width == potWidth && img.height == potHeight);
  602. if (!isPot) {
  603. _this._workingCanvas.width = potWidth;
  604. _this._workingCanvas.height = potHeight;
  605. _this._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, potWidth, potHeight);
  606. }
  607. _this._gl.texImage2D(_this._gl.TEXTURE_2D, 0, _this._gl.RGBA, _this._gl.RGBA, _this._gl.UNSIGNED_BYTE, isPot ? img : _this._workingCanvas);
  608. });
  609. };
  610. var onerror = function () {
  611. scene._removePendingData(texture);
  612. };
  613. BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
  614. }
  615. return texture;
  616. };
  617. Engine.prototype.createDynamicTexture = function (width, height, generateMipMaps) {
  618. var texture = this._gl.createTexture();
  619. width = getExponantOfTwo(width, this._caps.maxTextureSize);
  620. height = getExponantOfTwo(height, this._caps.maxTextureSize);
  621. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  622. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, this._gl.LINEAR);
  623. if (!generateMipMaps) {
  624. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR);
  625. } else {
  626. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR_MIPMAP_LINEAR);
  627. }
  628. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  629. this._activeTexturesCache = [];
  630. texture._baseWidth = width;
  631. texture._baseHeight = height;
  632. texture._width = width;
  633. texture._height = height;
  634. texture.isReady = false;
  635. texture.generateMipMaps = generateMipMaps;
  636. texture.references = 1;
  637. this._loadedTexturesCache.push(texture);
  638. return texture;
  639. };
  640. Engine.prototype.updateDynamicTexture = function (texture, canvas, invertY) {
  641. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  642. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 1 : 0);
  643. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, canvas);
  644. if (texture.generateMipMaps) {
  645. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  646. }
  647. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  648. this._activeTexturesCache = [];
  649. texture.isReady = true;
  650. };
  651. Engine.prototype.updateVideoTexture = function (texture, video, invertY) {
  652. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  653. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 0 : 1); // Video are upside down by default
  654. // Scale the video if it is a NPOT using the current working canvas
  655. if (video.videoWidth !== texture._width || video.videoHeight !== texture._height) {
  656. if (!texture._workingCanvas) {
  657. texture._workingCanvas = document.createElement("canvas");
  658. texture._workingContext = texture._workingCanvas.getContext("2d");
  659. texture._workingCanvas.width = texture._width;
  660. texture._workingCanvas.height = texture._height;
  661. }
  662. texture._workingContext.drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, texture._width, texture._height);
  663. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, texture._workingCanvas);
  664. } else {
  665. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, video);
  666. }
  667. if (texture.generateMipMaps) {
  668. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  669. }
  670. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  671. this._activeTexturesCache = [];
  672. texture.isReady = true;
  673. };
  674. Engine.prototype.createRenderTargetTexture = function (size, options) {
  675. // old version had a "generateMipMaps" arg instead of options.
  676. // if options.generateMipMaps is undefined, consider that options itself if the generateMipmaps value
  677. // in the same way, generateDepthBuffer is defaulted to true
  678. var generateMipMaps = false;
  679. var generateDepthBuffer = true;
  680. var samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  681. if (options !== undefined) {
  682. generateMipMaps = options.generateMipMaps === undefined ? options : options.generateMipmaps;
  683. generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  684. if (options.samplingMode !== undefined) {
  685. samplingMode = options.samplingMode;
  686. }
  687. }
  688. var gl = this._gl;
  689. var texture = gl.createTexture();
  690. gl.bindTexture(gl.TEXTURE_2D, texture);
  691. var width = size.width || size;
  692. var height = size.height || size;
  693. var magFilter = gl.NEAREST;
  694. var minFilter = gl.NEAREST;
  695. if (samplingMode === BABYLON.Texture.BILINEAR_SAMPLINGMODE) {
  696. magFilter = gl.LINEAR;
  697. if (generateMipMaps) {
  698. minFilter = gl.LINEAR_MIPMAP_NEAREST;
  699. } else {
  700. minFilter = gl.LINEAR;
  701. }
  702. } else if (samplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
  703. magFilter = gl.LINEAR;
  704. if (generateMipMaps) {
  705. minFilter = gl.LINEAR_MIPMAP_LINEAR;
  706. } else {
  707. minFilter = gl.LINEAR;
  708. }
  709. }
  710. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
  711. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
  712. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  713. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  714. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  715. var depthBuffer;
  716. // Create the depth buffer
  717. if (generateDepthBuffer) {
  718. depthBuffer = gl.createRenderbuffer();
  719. gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
  720. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
  721. }
  722. // Create the framebuffer
  723. var framebuffer = gl.createFramebuffer();
  724. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  725. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
  726. if (generateDepthBuffer) {
  727. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
  728. }
  729. // Unbind
  730. gl.bindTexture(gl.TEXTURE_2D, null);
  731. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  732. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  733. texture._framebuffer = framebuffer;
  734. if (generateDepthBuffer) {
  735. texture._depthBuffer = depthBuffer;
  736. }
  737. texture._width = width;
  738. texture._height = height;
  739. texture.isReady = true;
  740. texture.generateMipMaps = generateMipMaps;
  741. texture.references = 1;
  742. this._activeTexturesCache = [];
  743. this._loadedTexturesCache.push(texture);
  744. return texture;
  745. };
  746. Engine.prototype.createCubeTexture = function (rootUrl, scene, extensions, noMipmap) {
  747. var _this = this;
  748. var gl = this._gl;
  749. var texture = gl.createTexture();
  750. texture.isCube = true;
  751. texture.url = rootUrl;
  752. texture.references = 1;
  753. this._loadedTexturesCache.push(texture);
  754. cascadeLoad(rootUrl, 0, [], scene, function (imgs) {
  755. var width = getExponantOfTwo(imgs[0].width, _this._caps.maxCubemapTextureSize);
  756. var height = width;
  757. _this._workingCanvas.width = width;
  758. _this._workingCanvas.height = height;
  759. var faces = [
  760. gl.TEXTURE_CUBE_MAP_POSITIVE_X, gl.TEXTURE_CUBE_MAP_POSITIVE_Y, gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
  761. gl.TEXTURE_CUBE_MAP_NEGATIVE_X, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
  762. ];
  763. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  764. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
  765. for (var index = 0; index < faces.length; index++) {
  766. _this._workingContext.drawImage(imgs[index], 0, 0, imgs[index].width, imgs[index].height, 0, 0, width, height);
  767. gl.texImage2D(faces[index], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, _this._workingCanvas);
  768. }
  769. if (!noMipmap) {
  770. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  771. }
  772. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  773. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, noMipmap ? gl.LINEAR : gl.LINEAR_MIPMAP_LINEAR);
  774. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  775. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  776. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  777. _this._activeTexturesCache = [];
  778. texture._width = width;
  779. texture._height = height;
  780. texture.isReady = true;
  781. }, extensions);
  782. return texture;
  783. };
  784. Engine.prototype._releaseTexture = function (texture) {
  785. var gl = this._gl;
  786. if (texture._framebuffer) {
  787. gl.deleteFramebuffer(texture._framebuffer);
  788. }
  789. if (texture._depthBuffer) {
  790. gl.deleteRenderbuffer(texture._depthBuffer);
  791. }
  792. gl.deleteTexture(texture);
  793. for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
  794. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  795. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  796. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  797. this._activeTexturesCache[channel] = null;
  798. }
  799. var index = this._loadedTexturesCache.indexOf(texture);
  800. if (index !== -1) {
  801. this._loadedTexturesCache.splice(index, 1);
  802. }
  803. };
  804. Engine.prototype.bindSamplers = function (effect) {
  805. this._gl.useProgram(effect.getProgram());
  806. var samplers = effect.getSamplers();
  807. for (var index = 0; index < samplers.length; index++) {
  808. var uniform = effect.getUniform(samplers[index]);
  809. this._gl.uniform1i(uniform, index);
  810. }
  811. this._currentEffect = null;
  812. };
  813. Engine.prototype._bindTexture = function (channel, texture) {
  814. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  815. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  816. this._activeTexturesCache[channel] = null;
  817. };
  818. Engine.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  819. this._bindTexture(channel, postProcess._textures.data[postProcess._currentRenderTextureInd]);
  820. };
  821. Engine.prototype.setTexture = function (channel, texture) {
  822. if (channel < 0) {
  823. return;
  824. }
  825. // Not ready?
  826. if (!texture || !texture.isReady()) {
  827. if (this._activeTexturesCache[channel] != null) {
  828. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  829. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  830. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  831. this._activeTexturesCache[channel] = null;
  832. }
  833. return;
  834. }
  835. // Video
  836. if (texture instanceof BABYLON.VideoTexture) {
  837. if (texture.update()) {
  838. this._activeTexturesCache[channel] = null;
  839. }
  840. } else if (texture.delayLoadState == BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  841. texture.delayLoad();
  842. return;
  843. }
  844. if (this._activeTexturesCache[channel] == texture) {
  845. return;
  846. }
  847. this._activeTexturesCache[channel] = texture;
  848. var internalTexture = texture.getInternalTexture();
  849. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  850. if (internalTexture.isCube) {
  851. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, internalTexture);
  852. if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
  853. internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
  854. // CUBIC_MODE and SKYBOX_MODE both require CLAMP_TO_EDGE. All other modes use REPEAT.
  855. var textureWrapMode = (texture.coordinatesMode !== BABYLON.Texture.CUBIC_MODE && texture.coordinatesMode !== BABYLON.Texture.SKYBOX_MODE) ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE;
  856. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_S, textureWrapMode);
  857. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_T, textureWrapMode);
  858. }
  859. this._setAnisotropicLevel(this._gl.TEXTURE_CUBE_MAP, texture);
  860. } else {
  861. this._gl.bindTexture(this._gl.TEXTURE_2D, internalTexture);
  862. if (internalTexture._cachedWrapU !== texture.wrapU) {
  863. internalTexture._cachedWrapU = texture.wrapU;
  864. switch (texture.wrapU) {
  865. case BABYLON.Texture.WRAP_ADDRESSMODE:
  866. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.REPEAT);
  867. break;
  868. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  869. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
  870. break;
  871. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  872. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.MIRRORED_REPEAT);
  873. break;
  874. }
  875. }
  876. if (internalTexture._cachedWrapV !== texture.wrapV) {
  877. internalTexture._cachedWrapV = texture.wrapV;
  878. switch (texture.wrapV) {
  879. case BABYLON.Texture.WRAP_ADDRESSMODE:
  880. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.REPEAT);
  881. break;
  882. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  883. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
  884. break;
  885. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  886. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.MIRRORED_REPEAT);
  887. break;
  888. }
  889. }
  890. this._setAnisotropicLevel(this._gl.TEXTURE_2D, texture);
  891. }
  892. };
  893. Engine.prototype._setAnisotropicLevel = function (key, texture) {
  894. var anisotropicFilterExtension = this._caps.textureAnisotropicFilterExtension;
  895. if (anisotropicFilterExtension && texture._cachedAnisotropicFilteringLevel !== texture.anisotropicFilteringLevel) {
  896. this._gl.texParameterf(key, anisotropicFilterExtension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(texture.anisotropicFilteringLevel, this._caps.maxAnisotropy));
  897. texture._cachedAnisotropicFilteringLevel = texture.anisotropicFilteringLevel;
  898. }
  899. };
  900. Engine.prototype.readPixels = function (x, y, width, height) {
  901. var data = new Uint8Array(height * width * 4);
  902. this._gl.readPixels(0, 0, width, height, this._gl.RGBA, this._gl.UNSIGNED_BYTE, data);
  903. return data;
  904. };
  905. // Dispose
  906. Engine.prototype.dispose = function () {
  907. this.stopRenderLoop();
  908. while (this.scenes.length) {
  909. this.scenes[0].dispose();
  910. }
  911. for (var name in this._compiledEffects) {
  912. this._gl.deleteProgram(this._compiledEffects[name]._program);
  913. }
  914. // Events
  915. window.removeEventListener("blur", this._onBlur);
  916. window.removeEventListener("focus", this._onFocus);
  917. document.removeEventListener("fullscreenchange", this._onFullscreenChange);
  918. document.removeEventListener("mozfullscreenchange", this._onFullscreenChange);
  919. document.removeEventListener("webkitfullscreenchange", this._onFullscreenChange);
  920. document.removeEventListener("msfullscreenchange", this._onFullscreenChange);
  921. document.removeEventListener("pointerlockchange", this._onPointerLockChange);
  922. document.removeEventListener("mspointerlockchange", this._onPointerLockChange);
  923. document.removeEventListener("mozpointerlockchange", this._onPointerLockChange);
  924. document.removeEventListener("webkitpointerlockchange", this._onPointerLockChange);
  925. };
  926. // Statics
  927. Engine.isSupported = function () {
  928. try {
  929. var tempcanvas = document.createElement("canvas");
  930. var gl = tempcanvas.getContext("webgl") || tempcanvas.getContext("experimental-webgl");
  931. return gl != null && !!window.WebGLRenderingContext;
  932. } catch (e) {
  933. return false;
  934. }
  935. };
  936. Engine.ShadersRepository = "Babylon/Shaders/";
  937. Engine.ALPHA_DISABLE = 0;
  938. Engine.ALPHA_ADD = 1;
  939. Engine.ALPHA_COMBINE = 2;
  940. Engine.DELAYLOADSTATE_NONE = 0;
  941. Engine.DELAYLOADSTATE_LOADED = 1;
  942. Engine.DELAYLOADSTATE_LOADING = 2;
  943. Engine.DELAYLOADSTATE_NOTLOADED = 4;
  944. Engine.Epsilon = 0.001;
  945. Engine.CollisionsEpsilon = 0.001;
  946. return Engine;
  947. })();
  948. BABYLON.Engine = Engine;
  949. })(BABYLON || (BABYLON = {}));
  950. //# sourceMappingURL=babylon.engine.js.map