babylon.engine.js 31 KB

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