babylon.engine.js 48 KB

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