babylon.engine.js 28 KB

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