babylon.engine.js 50 KB

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