babylon.engine.js 48 KB

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