babylon.engine.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.Engine = function (canvas, antialias) {
  4. this._renderingCanvas = canvas;
  5. // GL
  6. try {
  7. this._gl = canvas.getContext("webgl", { antialias: antialias }) || canvas.getContext("experimental-webgl", { antialias: antialias });
  8. } catch (e) {
  9. throw new Error("WebGL not supported");
  10. }
  11. if (this._gl === undefined) {
  12. throw new Error("WebGL not supported");
  13. }
  14. // Options
  15. this.forceWireframe = false;
  16. this.cullBackFaces = true;
  17. // Scenes
  18. this.scenes = [];
  19. // Textures
  20. this._workingCanvas = document.createElement("canvas");
  21. this._workingContext = this._workingCanvas.getContext("2d");
  22. // Viewport
  23. this._hardwareScalingLevel = 1.0 / window.devicePixelRatio || 1.0;
  24. this.resize();
  25. // Caps
  26. this._caps = {};
  27. this._caps.maxTexturesImageUnits = this._gl.getParameter(this._gl.MAX_TEXTURE_IMAGE_UNITS);
  28. this._caps.maxTextureSize = this._gl.getParameter(this._gl.MAX_TEXTURE_SIZE);
  29. this._caps.maxCubemapTextureSize = this._gl.getParameter(this._gl.MAX_CUBE_MAP_TEXTURE_SIZE);
  30. this._caps.maxRenderTextureSize = this._gl.getParameter(this._gl.MAX_RENDERBUFFER_SIZE);
  31. // Extensions
  32. var derivatives = this._gl.getExtension('OES_standard_derivatives');
  33. this._caps.standardDerivatives = (derivatives !== undefined);
  34. // Cache
  35. this._loadedTexturesCache = [];
  36. this._activeTexturesCache = [];
  37. this._buffersCache = {
  38. vertexBuffer: null,
  39. indexBuffer: null
  40. };
  41. this._currentEffect = null;
  42. this._currentState = {
  43. culling: null
  44. };
  45. this._compiledEffects = {};
  46. this._gl.enable(this._gl.DEPTH_TEST);
  47. this._gl.depthFunc(this._gl.LEQUAL);
  48. // Fullscreen
  49. this.isFullscreen = false;
  50. var that = this;
  51. document.addEventListener("fullscreenchange", function () {
  52. that.isFullscreen = document.fullscreen;
  53. }, false);
  54. document.addEventListener("mozfullscreenchange", function () {
  55. that.isFullscreen = document.mozFullScreen;
  56. }, false);
  57. document.addEventListener("webkitfullscreenchange", function () {
  58. that.isFullscreen = document.webkitIsFullScreen;
  59. }, false);
  60. document.addEventListener("msfullscreenchange", function () {
  61. that.isFullscreen = document.msIsFullScreen;
  62. }, false);
  63. };
  64. // Properties
  65. BABYLON.Engine.prototype.getAspectRatio = function () {
  66. return this._aspectRatio;
  67. };
  68. BABYLON.Engine.prototype.getRenderWidth = function () {
  69. return this._renderingCanvas.width;
  70. };
  71. BABYLON.Engine.prototype.getRenderHeight = function () {
  72. return this._renderingCanvas.height;
  73. };
  74. BABYLON.Engine.prototype.getRenderingCanvas = function () {
  75. return this._renderingCanvas;
  76. };
  77. BABYLON.Engine.prototype.setHardwareScalingLevel = function (level) {
  78. this._hardwareScalingLevel = level;
  79. this.resize();
  80. };
  81. BABYLON.Engine.prototype.getHardwareScalingLevel = function () {
  82. return this._hardwareScalingLevel;
  83. };
  84. BABYLON.Engine.prototype.getLoadedTexturesCache = function () {
  85. return this._loadedTexturesCache;
  86. };
  87. BABYLON.Engine.prototype.getCaps = function () {
  88. return this._caps;
  89. };
  90. // Methods
  91. BABYLON.Engine.prototype.stopRenderLoop = function () {
  92. this._runningLoop = false;
  93. };
  94. BABYLON.Engine.prototype.runRenderLoop = function (renderFunction) {
  95. this._runningLoop = true;
  96. var that = this;
  97. var loop = function () {
  98. // Start new frame
  99. that.beginFrame();
  100. renderFunction();
  101. // Present
  102. that.endFrame();
  103. if (that._runningLoop) {
  104. // Register new frame
  105. BABYLON.Tools.QueueNewFrame(loop);
  106. }
  107. };
  108. BABYLON.Tools.QueueNewFrame(loop);
  109. };
  110. BABYLON.Engine.prototype.switchFullscreen = function (element) {
  111. if (this.isFullscreen) {
  112. BABYLON.Tools.ExitFullscreen();
  113. } else {
  114. BABYLON.Tools.RequestFullscreen(element ? element : this._renderingCanvas);
  115. }
  116. };
  117. BABYLON.Engine.prototype.clear = function (color, backBuffer, depthStencil) {
  118. this._gl.clearColor(color.r, color.g, color.b, 1.0);
  119. this._gl.clearDepth(1.0);
  120. var mode = 0;
  121. if (backBuffer || this.forceWireframe)
  122. mode |= this._gl.COLOR_BUFFER_BIT;
  123. if (depthStencil)
  124. mode |= this._gl.DEPTH_BUFFER_BIT;
  125. this._gl.clear(mode);
  126. };
  127. BABYLON.Engine.prototype.beginFrame = function () {
  128. BABYLON.Tools._MeasureFps();
  129. this._gl.viewport(0, 0, this._renderingCanvas.width, this._renderingCanvas.height);
  130. };
  131. BABYLON.Engine.prototype.endFrame = function () {
  132. this.flushFramebuffer();
  133. };
  134. BABYLON.Engine.prototype.resize = function () {
  135. this._renderingCanvas.width = this._renderingCanvas.clientWidth / this._hardwareScalingLevel;
  136. this._renderingCanvas.height = this._renderingCanvas.clientHeight / this._hardwareScalingLevel;
  137. this._aspectRatio = this._renderingCanvas.width / this._renderingCanvas.height;
  138. };
  139. BABYLON.Engine.prototype.bindFramebuffer = function (texture) {
  140. var gl = this._gl;
  141. gl.bindFramebuffer(gl.FRAMEBUFFER, texture._framebuffer);
  142. gl.viewport(0.0, 0.0, texture._size, texture._size);
  143. this.wipeCaches();
  144. };
  145. BABYLON.Engine.prototype.unBindFramebuffer = function (texture) {
  146. if (texture.generateMipMaps) {
  147. var gl = this._gl;
  148. gl.bindTexture(gl.TEXTURE_2D, texture);
  149. gl.generateMipmap(gl.TEXTURE_2D);
  150. gl.bindTexture(gl.TEXTURE_2D, null);
  151. }
  152. };
  153. BABYLON.Engine.prototype.flushFramebuffer = function () {
  154. this._gl.flush();
  155. };
  156. BABYLON.Engine.prototype.restoreDefaultFramebuffer = function () {
  157. this._gl.bindFramebuffer(this._gl.FRAMEBUFFER, null);
  158. this._gl.viewport(0, 0, this._renderingCanvas.width, this._renderingCanvas.height);
  159. this.wipeCaches();
  160. };
  161. // VBOs
  162. BABYLON.Engine.prototype.createVertexBuffer = function (vertices) {
  163. var vbo = this._gl.createBuffer();
  164. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
  165. this._gl.bufferData(this._gl.ARRAY_BUFFER, new Float32Array(vertices), this._gl.STATIC_DRAW);
  166. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  167. this._buffersCache.vertexBuffer = null;
  168. vbo.references = 1;
  169. return vbo;
  170. };
  171. BABYLON.Engine.prototype.createDynamicVertexBuffer = function (capacity) {
  172. var vbo = this._gl.createBuffer();
  173. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vbo);
  174. this._gl.bufferData(this._gl.ARRAY_BUFFER, capacity, this._gl.DYNAMIC_DRAW);
  175. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  176. this._buffersCache.vertexBuffer = null;
  177. vbo.references = 1;
  178. return vbo;
  179. };
  180. BABYLON.Engine.prototype.updateDynamicVertexBuffer = function (vertexBuffer, vertices) {
  181. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  182. this._gl.bufferSubData(this._gl.ARRAY_BUFFER, 0, new Float32Array(vertices));
  183. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, null);
  184. };
  185. BABYLON.Engine.prototype.createIndexBuffer = function (indices, is32Bits) {
  186. var vbo = this._gl.createBuffer();
  187. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, vbo);
  188. this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), this._gl.STATIC_DRAW);
  189. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, null);
  190. this._buffersCache.indexBuffer = null;
  191. vbo.references = 1;
  192. vbo.is32Bits = is32Bits;
  193. return vbo;
  194. };
  195. BABYLON.Engine.prototype.bindBuffers = function (vertexBuffer, indexBuffer, vertexDeclaration, vertexStrideSize, effect) {
  196. if (this._buffersCache.vertexBuffer != vertexBuffer) {
  197. this._gl.bindBuffer(this._gl.ARRAY_BUFFER, vertexBuffer);
  198. this._buffersCache.vertexBuffer = vertexBuffer;
  199. var offset = 0;
  200. for (var index = 0; index < vertexDeclaration.length; index++) {
  201. var order = effect.getAttribute(index);
  202. if (order >= 0) {
  203. this._gl.vertexAttribPointer(order, vertexDeclaration[index], this._gl.FLOAT, false, vertexStrideSize, offset);
  204. }
  205. offset += vertexDeclaration[index] * 4;
  206. }
  207. }
  208. if (this._buffersCache.indexBuffer != indexBuffer) {
  209. this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  210. this._buffersCache.indexBuffer = indexBuffer;
  211. }
  212. };
  213. BABYLON.Engine.prototype._releaseBuffer = function (buffer) {
  214. buffer.references--;
  215. if (buffer.references === 0) {
  216. this._gl.deleteBuffer(buffer);
  217. }
  218. };
  219. BABYLON.Engine.prototype.draw = function (useTriangles, indexStart, indexCount) {
  220. this._gl.drawElements(useTriangles ? this._gl.TRIANGLES : this._gl.LINES, indexCount, this._gl.UNSIGNED_SHORT, indexStart * 2);
  221. };
  222. // Shaders
  223. BABYLON.Engine.prototype.createEffect = function (baseName, attributesNames, uniformsNames, samplers, defines) {
  224. var name = baseName + "@" + defines;
  225. if (this._compiledEffects[name]) {
  226. return this._compiledEffects[name];
  227. }
  228. var effect = new BABYLON.Effect(baseName, attributesNames, uniformsNames, samplers, this, defines);
  229. this._compiledEffects[name] = effect;
  230. return effect;
  231. };
  232. var compileShader = function (gl, source, type, defines) {
  233. var shader = gl.createShader(type === "vertex" ? gl.VERTEX_SHADER : gl.FRAGMENT_SHADER);
  234. gl.shaderSource(shader, (defines ? defines + "\n" : "") + source);
  235. gl.compileShader(shader);
  236. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  237. throw new Error(gl.getShaderInfoLog(shader));
  238. }
  239. return shader;
  240. };
  241. BABYLON.Engine.prototype.createShaderProgram = function (vertexCode, fragmentCode, defines) {
  242. var vertexShader = compileShader(this._gl, vertexCode, "vertex", defines);
  243. var fragmentShader = compileShader(this._gl, fragmentCode, "fragment", defines);
  244. var shaderProgram = this._gl.createProgram();
  245. this._gl.attachShader(shaderProgram, vertexShader);
  246. this._gl.attachShader(shaderProgram, fragmentShader);
  247. this._gl.linkProgram(shaderProgram);
  248. this._gl.deleteShader(vertexShader);
  249. this._gl.deleteShader(fragmentShader);
  250. return shaderProgram;
  251. };
  252. BABYLON.Engine.prototype.getUniforms = function (shaderProgram, uniformsNames) {
  253. var results = [];
  254. for (var index = 0; index < uniformsNames.length; index++) {
  255. results.push(this._gl.getUniformLocation(shaderProgram, uniformsNames[index]));
  256. }
  257. return results;
  258. };
  259. BABYLON.Engine.prototype.getAttributes = function (shaderProgram, attributesNames) {
  260. var results = [];
  261. for (var index = 0; index < attributesNames.length; index++) {
  262. try {
  263. results.push(this._gl.getAttribLocation(shaderProgram, attributesNames[index]));
  264. } catch (e) {
  265. results.push(-1);
  266. }
  267. }
  268. return results;
  269. };
  270. BABYLON.Engine.prototype.enableEffect = function (effect) {
  271. if (!effect || !effect.getAttributesCount() || this._currentEffect === effect) {
  272. return;
  273. }
  274. this._buffersCache.vertexBuffer = null;
  275. // Use program
  276. this._gl.useProgram(effect.getProgram());
  277. for (var index = 0; index < effect.getAttributesCount() ; index++) {
  278. // Attributes
  279. var order = effect.getAttribute(index);
  280. if (order >= 0) {
  281. this._gl.enableVertexAttribArray(effect.getAttribute(index));
  282. }
  283. }
  284. this._currentEffect = effect;
  285. };
  286. BABYLON.Engine.prototype.setMatrix = function (uniform, matrix) {
  287. if (!uniform)
  288. return;
  289. this._gl.uniformMatrix4fv(uniform, false, matrix.toArray());
  290. };
  291. BABYLON.Engine.prototype.setVector2 = function (uniform, x, y) {
  292. if (!uniform)
  293. return;
  294. this._gl.uniform2f(uniform, x, y);
  295. };
  296. BABYLON.Engine.prototype.setVector3 = function (uniform, vector3) {
  297. if (!uniform)
  298. return;
  299. this._gl.uniform3f(uniform, vector3.x, vector3.y, vector3.z);
  300. };
  301. BABYLON.Engine.prototype.setFloat2 = function (uniform, x, y) {
  302. if (!uniform)
  303. return;
  304. this._gl.uniform2f(uniform, x, y);
  305. };
  306. BABYLON.Engine.prototype.setFloat3 = function (uniform, x, y, z) {
  307. if (!uniform)
  308. return;
  309. this._gl.uniform3f(uniform, x, y, z);
  310. };
  311. BABYLON.Engine.prototype.setBool = function (uniform, bool) {
  312. if (!uniform)
  313. return;
  314. this._gl.uniform1i(uniform, bool);
  315. };
  316. BABYLON.Engine.prototype.setFloat4 = function (uniform, x, y, z, w) {
  317. if (!uniform)
  318. return;
  319. this._gl.uniform4f(uniform, x, y, z, w);
  320. };
  321. BABYLON.Engine.prototype.setColor3 = function (uniform, color3) {
  322. if (!uniform)
  323. return;
  324. this._gl.uniform3f(uniform, color3.r, color3.g, color3.b);
  325. };
  326. BABYLON.Engine.prototype.setColor4 = function (uniform, color3, alpha) {
  327. if (!uniform)
  328. return;
  329. this._gl.uniform4f(uniform, color3.r, color3.g, color3.b, alpha);
  330. };
  331. // States
  332. BABYLON.Engine.prototype.setState = function (culling) {
  333. // Culling
  334. if (this._currentState.culling !== culling) {
  335. if (culling) {
  336. this._gl.cullFace(this.cullBackFaces ? this._gl.BACK : this._gl.FRONT);
  337. this._gl.enable(this._gl.CULL_FACE);
  338. } else {
  339. this._gl.disable(this._gl.CULL_FACE);
  340. }
  341. this._currentState.culling = culling;
  342. }
  343. };
  344. BABYLON.Engine.prototype.setDepthBuffer = function (enable) {
  345. if (enable) {
  346. this._gl.enable(this._gl.DEPTH_TEST);
  347. } else {
  348. this._gl.disable(this._gl.DEPTH_TEST);
  349. }
  350. };
  351. BABYLON.Engine.prototype.setDepthWrite = function (enable) {
  352. this._gl.depthMask(enable);
  353. };
  354. BABYLON.Engine.prototype.setColorWrite = function (enable) {
  355. this._gl.colorMask(enable, enable, enable, enable);
  356. };
  357. BABYLON.Engine.prototype.setAlphaMode = function (mode) {
  358. switch (mode) {
  359. case BABYLON.Engine.ALPHA_DISABLE:
  360. this.setDepthWrite(true);
  361. this._gl.disable(this._gl.BLEND);
  362. break;
  363. case BABYLON.Engine.ALPHA_COMBINE:
  364. this.setDepthWrite(false);
  365. this._gl.blendFuncSeparate(this._gl.SRC_ALPHA, this._gl.ONE_MINUS_SRC_ALPHA, this._gl.ZERO, this._gl.ONE);
  366. this._gl.enable(this._gl.BLEND);
  367. break;
  368. case BABYLON.Engine.ALPHA_ADD:
  369. this.setDepthWrite(false);
  370. this._gl.blendFuncSeparate(this._gl.ONE, this._gl.ONE, this._gl.ZERO, this._gl.ONE);
  371. this._gl.enable(this._gl.BLEND);
  372. break;
  373. }
  374. };
  375. BABYLON.Engine.prototype.setAlphaTesting = function (enable) {
  376. this._alphaTest = enable;
  377. };
  378. BABYLON.Engine.prototype.getAlphaTesting = function () {
  379. return this._alphaTest;
  380. };
  381. // Textures
  382. BABYLON.Engine.prototype.wipeCaches = function () {
  383. this._activeTexturesCache = [];
  384. this._currentEffect = null;
  385. this._currentState = {
  386. culling: null
  387. };
  388. this._buffersCache = {
  389. vertexBuffer: null,
  390. indexBuffer: null
  391. };
  392. };
  393. var getExponantOfTwo = function (value, max) {
  394. var count = 1;
  395. do {
  396. count *= 2;
  397. } while (count < value);
  398. if (count > max)
  399. count = max;
  400. return count;
  401. };
  402. BABYLON.Engine.prototype.createTexture = function (url, noMipmap, invertY, scene) {
  403. var texture = this._gl.createTexture();
  404. var that = this;
  405. var img = new Image();
  406. img.onload = function () {
  407. that._workingCanvas.width = getExponantOfTwo(img.width, that._caps.maxTextureSize);
  408. that._workingCanvas.height = getExponantOfTwo(img.height, that._caps.maxTextureSize);
  409. that._workingContext.drawImage(img, 0, 0, img.width, img.height, 0, 0, that._workingCanvas.width, that._workingCanvas.height);
  410. that._gl.bindTexture(that._gl.TEXTURE_2D, texture);
  411. that._gl.pixelStorei(that._gl.UNPACK_FLIP_Y_WEBGL, invertY === undefined ? true : invertY);
  412. that._gl.texImage2D(that._gl.TEXTURE_2D, 0, that._gl.RGBA, that._gl.RGBA, that._gl.UNSIGNED_BYTE, that._workingCanvas);
  413. that._gl.texParameteri(that._gl.TEXTURE_2D, that._gl.TEXTURE_MAG_FILTER, that._gl.LINEAR);
  414. if (noMipmap) {
  415. that._gl.texParameteri(that._gl.TEXTURE_2D, that._gl.TEXTURE_MIN_FILTER, that._gl.LINEAR);
  416. } else {
  417. that._gl.texParameteri(that._gl.TEXTURE_2D, that._gl.TEXTURE_MIN_FILTER, that._gl.LINEAR_MIPMAP_LINEAR);
  418. that._gl.generateMipmap(that._gl.TEXTURE_2D);
  419. }
  420. that._gl.bindTexture(that._gl.TEXTURE_2D, null);
  421. that._activeTexturesCache = [];
  422. texture._baseWidth = img.width;
  423. texture._baseHeight = img.height;
  424. texture._width = that._workingCanvas.width;
  425. texture._height = that._workingCanvas.height;
  426. texture.isReady = true;
  427. scene._removePendingData(img);
  428. };
  429. img.onerror = function () {
  430. scene._removePendingData(img);
  431. };
  432. scene._addPendingData(img);
  433. img.src = url;
  434. texture.url = url;
  435. texture.noMipmap = noMipmap;
  436. texture.references = 1;
  437. this._loadedTexturesCache.push(texture);
  438. return texture;
  439. };
  440. BABYLON.Engine.prototype.createDynamicTexture = function (size, noMipmap) {
  441. var texture = this._gl.createTexture();
  442. var width = getExponantOfTwo(size, this._caps.maxTextureSize);
  443. var height = getExponantOfTwo(size, this._caps.maxTextureSize);
  444. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  445. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MAG_FILTER, this._gl.LINEAR);
  446. if (noMipmap) {
  447. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR);
  448. } else {
  449. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_MIN_FILTER, this._gl.LINEAR_MIPMAP_LINEAR);
  450. }
  451. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  452. this._activeTexturesCache = [];
  453. texture._baseWidth = width;
  454. texture._baseHeight = height;
  455. texture._width = width;
  456. texture._height = height;
  457. texture.isReady = false;
  458. texture.noMipmap = noMipmap;
  459. texture.references = 1;
  460. this._loadedTexturesCache.push(texture);
  461. return texture;
  462. };
  463. BABYLON.Engine.prototype.updateDynamicTexture = function (texture, canvas) {
  464. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  465. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, true);
  466. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, canvas);
  467. if (!texture.noMipmap) {
  468. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  469. }
  470. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  471. this._activeTexturesCache = [];
  472. texture.isReady = true;
  473. };
  474. BABYLON.Engine.prototype.updateVideoTexture = function (texture, video) {
  475. this._gl.bindTexture(this._gl.TEXTURE_2D, texture);
  476. this._gl.pixelStorei(this._gl.UNPACK_FLIP_Y_WEBGL, false);
  477. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, this._gl.RGBA, this._gl.RGBA, this._gl.UNSIGNED_BYTE, video);
  478. if (!texture.noMipmap) {
  479. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  480. }
  481. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  482. this._activeTexturesCache = [];
  483. texture.isReady = true;
  484. };
  485. BABYLON.Engine.prototype.createRenderTargetTexture = function (size, generateMipMaps) {
  486. var gl = this._gl;
  487. var texture = gl.createTexture();
  488. gl.bindTexture(gl.TEXTURE_2D, texture);
  489. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  490. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, generateMipMaps ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);
  491. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  492. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  493. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, size, size, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
  494. // Create the depth buffer
  495. var depthBuffer = gl.createRenderbuffer();
  496. gl.bindRenderbuffer(gl.RENDERBUFFER, depthBuffer);
  497. gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_COMPONENT16, size, size);
  498. // Create the framebuffer
  499. var framebuffer = gl.createFramebuffer();
  500. gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
  501. gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, texture, 0);
  502. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.RENDERBUFFER, depthBuffer);
  503. // Unbind
  504. gl.bindTexture(gl.TEXTURE_2D, null);
  505. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  506. gl.bindFramebuffer(gl.FRAMEBUFFER, null);
  507. texture._framebuffer = framebuffer;
  508. texture._depthBuffer = depthBuffer;
  509. texture._size = size;
  510. texture.isReady = true;
  511. texture.generateMipMaps = generateMipMaps;
  512. texture.references = 1;
  513. this._activeTexturesCache = [];
  514. this._loadedTexturesCache.push(texture);
  515. return texture;
  516. };
  517. var extensions = ["_px.jpg", "_py.jpg", "_pz.jpg", "_nx.jpg", "_ny.jpg", "_nz.jpg"];
  518. var cascadeLoad = function (rootUrl, index, loadedImages, scene, onfinish) {
  519. var img = new Image();
  520. img.onload = function () {
  521. loadedImages.push(this);
  522. scene._removePendingData(img);
  523. if (index != extensions.length - 1) {
  524. cascadeLoad(rootUrl, index + 1, loadedImages, scene, onfinish);
  525. } else {
  526. onfinish(loadedImages);
  527. }
  528. };
  529. img.onerrror = function () {
  530. scene._removePendingData(img);
  531. };
  532. scene._addPendingData(img);
  533. img.src = rootUrl + extensions[index];
  534. };
  535. BABYLON.Engine.prototype.createCubeTexture = function (rootUrl, scene) {
  536. var gl = this._gl;
  537. var texture = gl.createTexture();
  538. texture.isCube = true;
  539. texture.url = rootUrl;
  540. texture.references = 1;
  541. this._loadedTexturesCache.push(texture);
  542. var that = this;
  543. cascadeLoad(rootUrl, 0, [], scene, function (imgs) {
  544. var width = getExponantOfTwo(imgs[0].width);
  545. var height = width;
  546. that._workingCanvas.width = width;
  547. that._workingCanvas.height = height;
  548. var faces = [
  549. gl.TEXTURE_CUBE_MAP_POSITIVE_X, gl.TEXTURE_CUBE_MAP_POSITIVE_Y, gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
  550. gl.TEXTURE_CUBE_MAP_NEGATIVE_X, gl.TEXTURE_CUBE_MAP_NEGATIVE_Y, gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
  551. ];
  552. gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture);
  553. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
  554. for (var index = 0; index < faces.length; index++) {
  555. that._workingContext.drawImage(imgs[index], 0, 0, imgs[index].width, imgs[index].height, 0, 0, width, height);
  556. gl.texImage2D(faces[index], 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, that._workingCanvas);
  557. }
  558. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  559. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  560. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);
  561. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  562. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  563. gl.bindTexture(gl.TEXTURE_CUBE_MAP, null);
  564. that._activeTexturesCache = [];
  565. texture._width = width;
  566. texture._height = height;
  567. texture.isReady = true;
  568. });
  569. return texture;
  570. };
  571. BABYLON.Engine.prototype._releaseTexture = function (texture) {
  572. var gl = this._gl;
  573. if (texture._framebuffer) {
  574. gl.deleteFramebuffer(texture._framebuffer);
  575. }
  576. if (texture._depthBuffer) {
  577. gl.deleteRenderbuffer(texture._depthBuffer);
  578. }
  579. gl.deleteTexture(texture);
  580. // Unbind channels
  581. for (var channel = 0; channel < this._caps.maxTexturesImageUnits; channel++) {
  582. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  583. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  584. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  585. this._activeTexturesCache[channel] = null;
  586. }
  587. };
  588. BABYLON.Engine.prototype.bindSamplers = function (effect) {
  589. this._gl.useProgram(effect.getProgram());
  590. var samplers = effect.getSamplers();
  591. for (var index = 0; index < samplers.length; index++) {
  592. var uniform = effect.getUniform(samplers[index]);
  593. this._gl.uniform1i(uniform, index);
  594. }
  595. this._currentEffect = null;
  596. };
  597. BABYLON.Engine.prototype.setTexture = function (channel, texture) {
  598. if (!texture || !texture.isReady()) {
  599. if (this._activeTexturesCache[channel] != null) {
  600. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  601. this._gl.bindTexture(this._gl.TEXTURE_2D, null);
  602. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, null);
  603. this._activeTexturesCache[channel] = null;
  604. }
  605. return;
  606. }
  607. if (texture instanceof BABYLON.VideoTexture) {
  608. if (texture._update()) {
  609. this._activeTexturesCache[channel] = null;
  610. }
  611. }
  612. if (this._activeTexturesCache[channel] == texture) {
  613. return;
  614. }
  615. this._activeTexturesCache[channel] = texture;
  616. var internalTexture = texture.getInternalTexture();
  617. this._gl.activeTexture(this._gl["TEXTURE" + channel]);
  618. if (internalTexture.isCube) {
  619. this._gl.bindTexture(this._gl.TEXTURE_CUBE_MAP, internalTexture);
  620. if (internalTexture._cachedCoordinatesMode !== texture.coordinatesMode) {
  621. internalTexture._cachedCoordinatesMode = texture.coordinatesMode;
  622. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_S, texture.coordinatesMode !== BABYLON.CubeTexture.CUBIC_MODE ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE);
  623. this._gl.texParameteri(this._gl.TEXTURE_CUBE_MAP, this._gl.TEXTURE_WRAP_T, texture.coordinatesMode !== BABYLON.CubeTexture.CUBIC_MODE ? this._gl.REPEAT : this._gl.CLAMP_TO_EDGE);
  624. }
  625. } else {
  626. this._gl.bindTexture(this._gl.TEXTURE_2D, internalTexture);
  627. if (internalTexture._cachedWrapU !== texture.wrapU) {
  628. internalTexture._cachedWrapU = texture.wrapU;
  629. switch (texture.wrapU) {
  630. case BABYLON.Texture.WRAP_ADDRESSMODE:
  631. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.REPEAT);
  632. break;
  633. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  634. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.CLAMP_TO_EDGE);
  635. break;
  636. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  637. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_S, this._gl.MIRRORED_REPEAT);
  638. break;
  639. }
  640. }
  641. if (internalTexture._cachedWrapV !== texture.wrapV) {
  642. internalTexture._cachedWrapV = texture.wrapV;
  643. switch (texture.wrapV) {
  644. case BABYLON.Texture.WRAP_ADDRESSMODE:
  645. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.REPEAT);
  646. break;
  647. case BABYLON.Texture.CLAMP_ADDRESSMODE:
  648. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.CLAMP_TO_EDGE);
  649. break;
  650. case BABYLON.Texture.MIRROR_ADDRESSMODE:
  651. this._gl.texParameteri(this._gl.TEXTURE_2D, this._gl.TEXTURE_WRAP_T, this._gl.MIRRORED_REPEAT);
  652. break;
  653. }
  654. }
  655. }
  656. };
  657. // Dispose
  658. BABYLON.Engine.prototype.dispose = function () {
  659. // Release scenes
  660. while (this.scenes.length) {
  661. this.scenes[0].dispose();
  662. }
  663. // Release effects
  664. for (var name in this._compiledEffects.length) {
  665. this._gl.deleteProgram(this._compiledEffects[name]._program);
  666. }
  667. };
  668. // Statics
  669. BABYLON.Engine.ShadersRepository = "Babylon/Shaders/";
  670. BABYLON.Engine.ALPHA_DISABLE = 0;
  671. BABYLON.Engine.ALPHA_ADD = 1;
  672. BABYLON.Engine.ALPHA_COMBINE = 2;
  673. BABYLON.Engine.epsilon = 0.001;
  674. BABYLON.Engine.collisionsEpsilon = 0.001;
  675. BABYLON.Engine.isSupported = function () {
  676. try {
  677. var tempcanvas = document.createElement("canvas");
  678. var gl = tempcanvas.getContext("webgl") || tempcanvas.getContext("experimental-webgl");
  679. return gl != null && !!window.WebGLRenderingContext;
  680. } catch (e) {
  681. return false;
  682. }
  683. };
  684. })();