babylon.engine.js 48 KB

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