babylon.engine.js 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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 isDDS = this.getCaps().s3tc && (url.substr(url.length - 4, 4).toLowerCase() === ".dds");
  647. scene._addPendingData(texture);
  648. texture.url = url;
  649. texture.noMipmap = noMipmap;
  650. texture.references = 1;
  651. this._loadedTexturesCache.push(texture);
  652. if (isDDS) {
  653. BABYLON.Tools.LoadFile(url, function (data) {
  654. var info = BABYLON.Internals.DDSTools.GetDDSInfo(data);
  655. var loadMipmap = info.mipmapCount > 1 && !noMipmap;
  656. prepareWebGLTexture(texture, _this._gl, scene, info.width, info.height, invertY, !loadMipmap, true, function () {
  657. BABYLON.Internals.DDSTools.UploadDDSLevels(_this._gl, _this.getCaps().s3tc, data, loadMipmap);
  658. });
  659. }, null, scene.database, true);
  660. } else {
  661. var onload = function (img) {
  662. prepareWebGLTexture(texture, _this._gl, scene, img.width, img.height, invertY, noMipmap, false, function (potWidth, potHeight) {
  663. var isPot = (img.width == potWidth && img.height == potHeight);
  664. if (!isPot) {
  665. _this._workingCanvas.width = potWidth;
  666. _this._workingCanvas.height = potHeight;
  667. _this._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, potWidth, potHeight);
  668. }
  669. _this._gl.texImage2D(_this._gl.TEXTURE_2D, 0, _this._gl.RGBA, _this._gl.RGBA, _this._gl.UNSIGNED_BYTE, isPot ? img : _this._workingCanvas);
  670. });
  671. };
  672. var onerror = function () {
  673. scene._removePendingData(texture);
  674. };
  675. BABYLON.Tools.LoadImage(url, onload, onerror, scene.database);
  676. }
  677. return texture;
  678. };
  679. Engine.prototype.createDynamicTexture = function (width, height, generateMipMaps) {
  680. var texture = this._gl.createTexture();
  681. width = getExponantOfTwo(width, this._caps.maxTextureSize);
  682. height = getExponantOfTwo(height, this._caps.maxTextureSize);
  683. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  684. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, this._gl.LINEAR);
  685. if (!generateMipMaps) {
  686. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR);
  687. } else {
  688. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR_MIPMAP_LINEAR);
  689. }
  690. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  691. this._activeTexturesCache = [];
  692. texture._baseWidth = width;
  693. texture._baseHeight = height;
  694. texture._width = width;
  695. texture._height = height;
  696. texture.isReady = false;
  697. texture.generateMipMaps = generateMipMaps;
  698. texture.references = 1;
  699. this._loadedTexturesCache.push(texture);
  700. return texture;
  701. };
  702. Engine.prototype.updateDynamicTexture = function (texture, canvas, invertY) {
  703. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  704. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 1 : 0);
  705. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, canvas);
  706. if (texture.generateMipMaps) {
  707. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  708. }
  709. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  710. this._activeTexturesCache = [];
  711. texture.isReady = true;
  712. };
  713. Engine.prototype.updateVideoTexture = function (texture, video, invertY) {
  714. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  715. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, invertY ? 0 : 1); // Video are upside down by default
  716. // Scale the video if it is a NPOT using the current working canvas
  717. if (video.videoWidth !== texture._width || video.videoHeight !== texture._height) {
  718. if (!texture._workingCanvas) {
  719. texture._workingCanvas = document.createElement("canvas");
  720. texture._workingContext = texture._workingCanvas.getContext("2d");
  721. texture._workingCanvas.width = texture._width;
  722. texture._workingCanvas.height = texture._height;
  723. }
  724. texture._workingContext.drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, texture._width, texture._height);
  725. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, texture._workingCanvas);
  726. } else {
  727. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, video);
  728. }
  729. if (texture.generateMipMaps) {
  730. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  731. }
  732. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  733. this._activeTexturesCache = [];
  734. texture.isReady = true;
  735. };
  736. Engine.prototype.createRenderTargetTexture = function (size, options) {
  737. // old version had a "generateMipMaps" arg instead of options.
  738. // if options.generateMipMaps is undefined, consider that options itself if the generateMipmaps value
  739. // in the same way, generateDepthBuffer is defaulted to true
  740. var generateMipMaps = false;
  741. var generateDepthBuffer = true;
  742. var samplingMode = BABYLON.Texture.TRILINEAR_SAMPLINGMODE;
  743. if (options !== undefined) {
  744. generateMipMaps = options.generateMipMaps === undefined ? options : options.generateMipmaps;
  745. generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  746. if (options.samplingMode !== undefined) {
  747. samplingMode = options.samplingMode;
  748. }
  749. }
  750. var gl = this._gl;
  751. var texture = gl.createTexture();
  752. gl.bindTexture(gl.TEXTURE_2D, texture);
  753. var width = size.width || size;
  754. var height = size.height || size;
  755. var magFilter = gl.NEAREST;
  756. var minFilter = gl.NEAREST;
  757. if (samplingMode === BABYLON.Texture.BILINEAR_SAMPLINGMODE) {
  758. magFilter = gl.LINEAR;
  759. if (generateMipMaps) {
  760. minFilter = gl.LINEAR_MIPMAP_NEAREST;
  761. } else {
  762. minFilter = gl.LINEAR;
  763. }
  764. } else if (samplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
  765. magFilter = gl.LINEAR;
  766. if (generateMipMaps) {
  767. minFilter = gl.LINEAR_MIPMAP_LINEAR;
  768. } else {
  769. minFilter = gl.LINEAR;
  770. }
  771. }
  772. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
  773. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
  774. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  775. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  776. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  777. var depthBuffer;
  778. // Create the depth buffer
  779. if (generateDepthBuffer) {
  780. depthBuffer = gl.createRenderbuffer();
  781. gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
  782. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, width, height);
  783. }
  784. // Create the framebuffer
  785. var framebuffer = gl.createFramebuffer();
  786. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  787. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
  788. if (generateDepthBuffer) {
  789. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
  790. }
  791. // Unbind
  792. gl.bindTexture(gl.TEXTURE_2D, null);
  793. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  794. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  795. texture._framebuffer = framebuffer;
  796. if (generateDepthBuffer) {
  797. texture._depthBuffer = depthBuffer;
  798. }
  799. texture._width = width;
  800. texture._height = height;
  801. texture.isReady = true;
  802. texture.generateMipMaps = generateMipMaps;
  803. texture.references = 1;
  804. this._activeTexturesCache = [];
  805. this._loadedTexturesCache.push(texture);
  806. return texture;
  807. };
  808. Engine.prototype.createCubeTexture = function (rootUrl, scene, extensions, noMipmap) {
  809. var _this = this;
  810. var gl = this._gl;
  811. var texture = gl.createTexture();
  812. texture.isCube = true;
  813. texture.url = rootUrl;
  814. texture.references = 1;
  815. this._loadedTexturesCache.push(texture);
  816. cascadeLoad(rootUrl, 0, [], scene, function (imgs) {
  817. var width = getExponantOfTwo(imgs[0].width, _this._caps.maxCubemapTextureSize);
  818. var height = width;
  819. _this._workingCanvas.width = width;
  820. _this._workingCanvas.height = height;
  821. var faces = [
  822. gl.TEXTURE_CUBE_MAP_POSITIVE_X, gl.TEXTURE_CUBE_MAP_POSITIVE_Y, gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
  823. gl.TEXTURE_CUBE_MAP_NEGATIVE_X, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
  824. ];
  825. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  826. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, 0);
  827. for (var index = 0; index < faces.length; index++) {
  828. _this._workingContext.drawImage(imgs[index], 0, 0, imgs[index].width, imgs[index].height, 0, 0, width, height);
  829. gl.texImage2D(faces[index], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, _this._workingCanvas);
  830. }
  831. if (!noMipmap) {
  832. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  833. }
  834. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  835. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, noMipmap ? gl.LINEAR : gl.LINEAR_MIPMAP_LINEAR);
  836. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  837. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  838. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  839. _this._activeTexturesCache = [];
  840. texture._width = width;
  841. texture._height = height;
  842. texture.isReady = true;
  843. }, extensions);
  844. return texture;
  845. };
  846. Engine.prototype._releaseTexture = function (texture) {
  847. var gl = this._gl;
  848. if (texture._framebuffer) {
  849. gl.deleteFramebuffer(texture._framebuffer);
  850. }
  851. if (texture._depthBuffer) {
  852. gl.deleteRenderbuffer(texture._depthBuffer);
  853. }
  854. gl.deleteTexture(texture);
  855. for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
  856. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  857. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  858. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  859. this._activeTexturesCache[channel] = null;
  860. }
  861. var index = this._loadedTexturesCache.indexOf(texture);
  862. if (index !== -1) {
  863. this._loadedTexturesCache.splice(index, 1);
  864. }
  865. };
  866. Engine.prototype.bindSamplers = function (effect) {
  867. this._gl.useProgram(effect.getProgram());
  868. var samplers = effect.getSamplers();
  869. for (var index = 0; index < samplers.length; index++) {
  870. var uniform = effect.getUniform(samplers[index]);
  871. this._gl.uniform1i(uniform, index);
  872. }
  873. this._currentEffect = null;
  874. };
  875. Engine.prototype._bindTexture = function (channel, texture) {
  876. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  877. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  878. this._activeTexturesCache[channel] = null;
  879. };
  880. Engine.prototype.setTextureFromPostProcess = function (channel, postProcess) {
  881. this._bindTexture(channel, postProcess._textures.data[postProcess._currentRenderTextureInd]);
  882. };
  883. Engine.prototype.setTexture = function (channel, texture) {
  884. if (channel < 0) {
  885. return;
  886. }
  887. // Not ready?
  888. if (!texture || !texture.isReady()) {
  889. if (this._activeTexturesCache[channel] != null) {
  890. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  891. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  892. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  893. this._activeTexturesCache[channel] = null;
  894. }
  895. return;
  896. }
  897. // Video
  898. if (texture instanceof BABYLON.VideoTexture) {
  899. if (texture.update()) {
  900. this._activeTexturesCache[channel] = null;
  901. }
  902. } else if (texture.delayLoadState == BABYLON.Engine.DELAYLOADSTATE_NOTLOADED) {
  903. texture.delayLoad();
  904. return;
  905. }
  906. if (this._activeTexturesCache[channel] == texture) {
  907. return;
  908. }
  909. this._activeTexturesCache[channel] = texture;
  910. var internalTexture = texture.getInternalTexture();
  911. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  912. if (internalTexture.isCube) {
  913. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, internalTexture);
  914. if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
  915. internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
  916. // CUBIC_MODE and SKYBOX_MODE both require CLAMP_TO_EDGE. All other modes use REPEAT.
  917. var textureWrapMode = (texture.coordinatesMode !== BABYLON.Texture.CUBIC_MODE && texture.coordinatesMode !== BABYLON.Texture.SKYBOX_MODE) ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE;
  918. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_S, textureWrapMode);
  919. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_T, textureWrapMode);
  920. }
  921. this._setAnisotropicLevel(this._gl.TEXTURE_CUBE_MAP, texture);
  922. } else {
  923. this._gl.bindTexture(this._gl.TEXTURE_2D, internalTexture);
  924. if (internalTexture._cachedWrapU !== texture.wrapU) {
  925. internalTexture._cachedWrapU = texture.wrapU;
  926. switch (texture.wrapU) {
  927. case BABYLON.Texture.WRAP_ADDRESSMODE:
  928. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.REPEAT);
  929. break;
  930. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  931. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
  932. break;
  933. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  934. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.MIRRORED_REPEAT);
  935. break;
  936. }
  937. }
  938. if (internalTexture._cachedWrapV !== texture.wrapV) {
  939. internalTexture._cachedWrapV = texture.wrapV;
  940. switch (texture.wrapV) {
  941. case BABYLON.Texture.WRAP_ADDRESSMODE:
  942. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.REPEAT);
  943. break;
  944. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  945. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
  946. break;
  947. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  948. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.MIRRORED_REPEAT);
  949. break;
  950. }
  951. }
  952. this._setAnisotropicLevel(this._gl.TEXTURE_2D, texture);
  953. }
  954. };
  955. Engine.prototype._setAnisotropicLevel = function (key, texture) {
  956. var anisotropicFilterExtension = this._caps.textureAnisotropicFilterExtension;
  957. if (anisotropicFilterExtension && texture._cachedAnisotropicFilteringLevel !== texture.anisotropicFilteringLevel) {
  958. this._gl.texParameterf(key, anisotropicFilterExtension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min(texture.anisotropicFilteringLevel, this._caps.maxAnisotropy));
  959. texture._cachedAnisotropicFilteringLevel = texture.anisotropicFilteringLevel;
  960. }
  961. };
  962. Engine.prototype.readPixels = function (x, y, width, height) {
  963. var data = new Uint8Array(height * width * 4);
  964. this._gl.readPixels(0, 0, width, height, this._gl.RGBA, this._gl.UNSIGNED_BYTE, data);
  965. return data;
  966. };
  967. // Dispose
  968. Engine.prototype.dispose = function () {
  969. this.stopRenderLoop();
  970. while (this.scenes.length) {
  971. this.scenes[0].dispose();
  972. }
  973. for (var name in this._compiledEffects) {
  974. this._gl.deleteProgram(this._compiledEffects[name]._program);
  975. }
  976. // Events
  977. window.removeEventListener("blur", this._onBlur);
  978. window.removeEventListener("focus", this._onFocus);
  979. document.removeEventListener("fullscreenchange", this._onFullscreenChange);
  980. document.removeEventListener("mozfullscreenchange", this._onFullscreenChange);
  981. document.removeEventListener("webkitfullscreenchange", this._onFullscreenChange);
  982. document.removeEventListener("msfullscreenchange", this._onFullscreenChange);
  983. document.removeEventListener("pointerlockchange", this._onPointerLockChange);
  984. document.removeEventListener("mspointerlockchange", this._onPointerLockChange);
  985. document.removeEventListener("mozpointerlockchange", this._onPointerLockChange);
  986. document.removeEventListener("webkitpointerlockchange", this._onPointerLockChange);
  987. };
  988. // Statics
  989. Engine.isSupported = function () {
  990. try {
  991. var tempcanvas = document.createElement("canvas");
  992. var gl = tempcanvas.getContext("webgl") || tempcanvas.getContext("experimental-webgl");
  993. return gl != null && !!window.WebGLRenderingContext;
  994. } catch (e) {
  995. return false;
  996. }
  997. };
  998. Engine._ALPHA_DISABLE = 0;
  999. Engine._ALPHA_ADD = 1;
  1000. Engine._ALPHA_COMBINE = 2;
  1001. Engine._DELAYLOADSTATE_NONE = 0;
  1002. Engine._DELAYLOADSTATE_LOADED = 1;
  1003. Engine._DELAYLOADSTATE_LOADING = 2;
  1004. Engine._DELAYLOADSTATE_NOTLOADED = 4;
  1005. Engine.Epsilon = 0.001;
  1006. Engine.CollisionsEpsilon = 0.001;
  1007. Engine.ShadersRepository = "Babylon/Shaders/";
  1008. return Engine;
  1009. })();
  1010. BABYLON.Engine = Engine;
  1011. })(BABYLON || (BABYLON = {}));
  1012. //# sourceMappingURL=babylon.engine.js.map