CCGrid.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /****************************************************************************
  2. Copyright (c) 2010-2013 cocos2d-x.org
  3. Copyright (c) 2009 On-Core
  4. Copyright (c) 2008-2010 Ricardo Quesada
  5. Copyright (c) 2011 Zynga Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. /**
  24. * Base class for cc.Grid
  25. * @class
  26. * @extends cc.Class
  27. */
  28. cc.GridBase = cc.Class.extend(/** @lends cc.GridBase# */{
  29. _active:false,
  30. _reuseGrid:0,
  31. _gridSize:null,
  32. _texture:null,
  33. _step:null,
  34. _grabber:null,
  35. _isTextureFlipped:false,
  36. _shaderProgram:null,
  37. _directorProjection:0,
  38. _dirty:false,
  39. ctor:function () {
  40. this._active=false;
  41. this._reuseGrid=0;
  42. this._gridSize=null;
  43. this._texture=null;
  44. this._step = cc.p(0, 0);
  45. this._grabber=null;
  46. this._isTextureFlipped=false;
  47. this._shaderProgram=null;
  48. this._directorProjection=0;
  49. this._dirty=false;
  50. },
  51. /**
  52. * whether or not the grid is active
  53. * @return {Boolean}
  54. */
  55. isActive:function () {
  56. return this._active;
  57. },
  58. /**
  59. * whether or not the grid is active
  60. * @param {Number} active
  61. */
  62. setActive:function (active) {
  63. this._active = active;
  64. if (!active) {
  65. var director = cc.Director.getInstance();
  66. var proj = director.getProjection();
  67. director.setProjection(proj);
  68. }
  69. },
  70. /**
  71. * get number of times that the grid will be reused
  72. * @return {Number}
  73. */
  74. getReuseGrid:function () {
  75. return this._reuseGrid;
  76. },
  77. /**
  78. * set number of times that the grid will be reused
  79. * @param reuseGrid
  80. */
  81. setReuseGrid:function (reuseGrid) {
  82. this._reuseGrid = reuseGrid;
  83. },
  84. /**
  85. * get size of the grid
  86. * @return {cc.Size}
  87. */
  88. getGridSize:function () {
  89. return cc.size(this._gridSize.width, this._gridSize.height);
  90. },
  91. /**
  92. * set size of the grid
  93. * @param {cc.Size} gridSize
  94. */
  95. setGridSize:function (gridSize) {
  96. this._gridSize.width = parseInt(gridSize.width);
  97. this._gridSize.height = parseInt(gridSize.height);
  98. },
  99. /**
  100. * get pixels between the grids
  101. * @return {cc.Point}
  102. */
  103. getStep:function () {
  104. return cc.p(this._step.x, this._step.y);
  105. },
  106. /**
  107. * set pixels between the grids
  108. * @param {cc.Point} step
  109. */
  110. setStep:function (step) {
  111. this._step.x = step.x;
  112. this._step.y = step.y;
  113. },
  114. /**
  115. * get wheter or not the texture is flipped
  116. * @return {Boolean}
  117. */
  118. isTextureFlipped:function () {
  119. return this._isTextureFlipped;
  120. },
  121. /**
  122. * set wheter or not the texture is flipped
  123. * @param {Boolean} flipped
  124. */
  125. setTextureFlipped:function (flipped) {
  126. if (this._isTextureFlipped != flipped) {
  127. this._isTextureFlipped = flipped;
  128. this.calculateVertexPoints();
  129. }
  130. },
  131. /**
  132. *
  133. * @param {cc.Size} gridSize
  134. * @param {cc.Texture2D} [texture=]
  135. * @param {Boolean} [flipped=false]
  136. * @returns {boolean}
  137. */
  138. initWithSize:function (gridSize, texture, flipped) {
  139. if (!texture) {
  140. var director = cc.Director.getInstance();
  141. var winSize = director.getWinSizeInPixels();
  142. var POTWide = cc.NextPOT(winSize.width);
  143. var POTHigh = cc.NextPOT(winSize.height);
  144. var data = new Uint8Array(POTWide * POTHigh * 4);
  145. if (!data) {
  146. cc.log("cocos2d: CCGrid: not enough memory.");
  147. return false;
  148. }
  149. texture = new cc.Texture2D();
  150. // we only use rgba8888
  151. texture.initWithData(data, cc.TEXTURE_2D_PIXEL_FORMAT_RGBA8888, POTWide, POTHigh, winSize);
  152. if (!texture) {
  153. cc.log("cocos2d: CCGrid: error creating texture");
  154. return false;
  155. }
  156. }
  157. flipped = flipped || false;
  158. this._active = false;
  159. this._reuseGrid = 0;
  160. this._gridSize = gridSize;
  161. this._texture = texture;
  162. this._isTextureFlipped = flipped;
  163. var texSize = this._texture.getContentSize();
  164. this._step.x = texSize.width / gridSize.width;
  165. this._step.y = texSize.height / gridSize.height;
  166. this._grabber = new cc.Grabber();
  167. if (!this._grabber)
  168. return false;
  169. this._grabber.grab(this._texture);
  170. this._shaderProgram = cc.ShaderCache.getInstance().programForKey(cc.SHADER_POSITION_TEXTURE);
  171. this.calculateVertexPoints();
  172. return true;
  173. },
  174. beforeDraw:function () {
  175. // save projection
  176. this._directorProjection = cc.Director.getInstance().getProjection();
  177. // 2d projection
  178. // [director setProjection:kCCDirectorProjection2D];
  179. this.set2DProjection();
  180. this._grabber.beforeRender(this._texture);
  181. },
  182. afterDraw:function (target) {
  183. this._grabber.afterRender(this._texture);
  184. // restore projection
  185. cc.Director.getInstance().setProjection(this._directorProjection);
  186. if (target.getCamera().isDirty()) {
  187. var offset = target.getAnchorPointInPoints();
  188. //
  189. // XXX: Camera should be applied in the AnchorPoint
  190. //
  191. cc.kmGLTranslatef(offset.x, offset.y, 0);
  192. target.getCamera().locate();
  193. cc.kmGLTranslatef(-offset.x, -offset.y, 0);
  194. }
  195. cc.glBindTexture2D(this._texture);
  196. // restore projection for default FBO .fixed bug #543 #544
  197. //TODO: CCDirector::sharedDirector().setProjection(CCDirector::sharedDirector().getProjection());
  198. //TODO: CCDirector::sharedDirector().applyOrientation();
  199. this.blit();
  200. },
  201. blit:function () {
  202. cc.log("cc.GridBase.blit(): Shall be overridden in subclass.");
  203. },
  204. reuse:function () {
  205. cc.log("cc.GridBase.reuse(): Shall be overridden in subclass.");
  206. },
  207. calculateVertexPoints:function () {
  208. cc.log("cc.GridBase.calculateVertexPoints(): Shall be overridden in subclass.");
  209. },
  210. set2DProjection:function () {
  211. var winSize = cc.Director.getInstance().getWinSizeInPixels();
  212. var gl = cc.renderContext;
  213. gl.viewport(0, 0, winSize.width , winSize.height);
  214. cc.kmGLMatrixMode(cc.KM_GL_PROJECTION);
  215. cc.kmGLLoadIdentity();
  216. var orthoMatrix = new cc.kmMat4();
  217. cc.kmMat4OrthographicProjection(orthoMatrix, 0, winSize.width, 0, winSize.height, -1, 1);
  218. cc.kmGLMultMatrix(orthoMatrix);
  219. cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
  220. cc.kmGLLoadIdentity();
  221. cc.setProjectionMatrixDirty()
  222. }
  223. });
  224. /**
  225. * create one cc.GridBase Object
  226. * @param {cc.Size} gridSize
  227. * @param {cc.Texture2D} [texture=]
  228. * @param {Boolean} [flipped=]
  229. * @return {cc.GridBase}
  230. */
  231. cc.GridBase.create = function (gridSize, texture, flipped) {
  232. var gridBase = new cc.GridBase();
  233. if (gridBase && gridBase.initWithSize(gridSize, texture, flipped))
  234. return gridBase;
  235. return null;
  236. };
  237. /**
  238. * cc.Grid3D is a 3D grid implementation. Each vertex has 3 dimensions: x,y,z
  239. * @class
  240. * @extends cc.GridBase
  241. */
  242. cc.Grid3D = cc.GridBase.extend(/** @lends cc.Grid3D# */{
  243. _texCoordinates:null,
  244. _vertices:null,
  245. _originalVertices:null,
  246. _indices:null,
  247. _texCoordinateBuffer:null,
  248. _verticesBuffer:null,
  249. _indicesBuffer:null,
  250. ctor:function () {
  251. cc.GridBase.prototype.ctor.call(this);
  252. this._texCoordinates=null;
  253. this._vertices=null;
  254. this._originalVertices=null;
  255. this._indices=null;
  256. this._texCoordinateBuffer=null;
  257. this._verticesBuffer=null;
  258. this._indicesBuffer=null;
  259. },
  260. /**
  261. * returns the vertex at a given position
  262. * @param {cc.Point} pos
  263. * @return {cc.Vertex3F}
  264. */
  265. vertex:function (pos) {
  266. if(pos.x !== (0| pos.x) || pos.y !== (0| pos.y))
  267. cc.log("cc.Grid3D.vertex() : Numbers must be integers");
  268. var index = 0 | ((pos.x * (this._gridSize.height + 1) + pos.y) * 3);
  269. var locVertices = this._vertices;
  270. return new cc.Vertex3F(locVertices[index], locVertices[index + 1], locVertices[index + 2]);
  271. },
  272. /**
  273. * returns the original (non-transformed) vertex at a given position
  274. * @param {cc.Point} pos
  275. * @return {cc.Vertex3F}
  276. */
  277. originalVertex:function (pos) {
  278. if(pos.x !== (0| pos.x) || pos.y !== (0| pos.y))
  279. cc.log("cc.Grid3D.originalVertex() : Numbers must be integers");
  280. var index = 0 | ((pos.x * (this._gridSize.height + 1) + pos.y) * 3);
  281. var locOriginalVertices = this._originalVertices;
  282. return new cc.Vertex3F(locOriginalVertices[index], locOriginalVertices[index + 1], locOriginalVertices[index + 2]);
  283. },
  284. /**
  285. * sets a new vertex at a given position
  286. * @param {cc.Point} pos
  287. * @param {cc.Vertex3F} vertex
  288. */
  289. setVertex:function (pos, vertex) {
  290. if(pos.x !== (0| pos.x) || pos.y !== (0| pos.y))
  291. cc.log("cc.Grid3D.setVertex() : Numbers must be integers");
  292. var index = 0 | ((pos.x * (this._gridSize.height + 1) + pos.y) * 3);
  293. var vertArray = this._vertices;
  294. vertArray[index] = vertex.x;
  295. vertArray[index + 1] = vertex.y;
  296. vertArray[index + 2] = vertex.z;
  297. this._dirty = true;
  298. },
  299. blit:function () {
  300. var n = this._gridSize.width * this._gridSize.height;
  301. cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_TEX_COORDS);
  302. this._shaderProgram.use();
  303. this._shaderProgram.setUniformsForBuiltins();
  304. var gl = cc.renderContext, locDirty = this._dirty;
  305. //
  306. // Attributes
  307. //
  308. // position
  309. gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer);
  310. if (locDirty)
  311. gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW);
  312. gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 0, 0);
  313. // texCoords
  314. gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer);
  315. if (locDirty)
  316. gl.bufferData(gl.ARRAY_BUFFER, this._texCoordinates, gl.DYNAMIC_DRAW);
  317. gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, 0);
  318. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer);
  319. if (locDirty)
  320. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.STATIC_DRAW);
  321. gl.drawElements(gl.TRIANGLES, n * 6, gl.UNSIGNED_SHORT, 0);
  322. if (locDirty)
  323. this._dirty = false;
  324. cc.INCREMENT_GL_DRAWS(1);
  325. },
  326. reuse:function () {
  327. if (this._reuseGrid > 0) {
  328. var locOriginalVertices = this._originalVertices, locVertices = this._vertices;
  329. for (var i = 0, len = this._vertices.length; i < len; i++)
  330. locOriginalVertices[i] = locVertices[i];
  331. --this._reuseGrid;
  332. }
  333. },
  334. calculateVertexPoints:function () {
  335. var gl = cc.renderContext;
  336. var width = this._texture.getPixelsWide();
  337. var height = this._texture.getPixelsHigh();
  338. var imageH = this._texture.getContentSizeInPixels().height;
  339. var locGridSize = this._gridSize;
  340. var numOfPoints = (locGridSize.width + 1) * (locGridSize.height + 1);
  341. this._vertices = new Float32Array(numOfPoints * 3);
  342. this._texCoordinates = new Float32Array(numOfPoints * 2);
  343. this._indices = new Uint16Array(locGridSize.width * locGridSize.height * 6);
  344. if(this._verticesBuffer)
  345. gl.deleteBuffer(this._verticesBuffer);
  346. this._verticesBuffer = gl.createBuffer();
  347. if(this._texCoordinateBuffer)
  348. gl.deleteBuffer(this._texCoordinateBuffer);
  349. this._texCoordinateBuffer = gl.createBuffer();
  350. if(this._indicesBuffer)
  351. gl.deleteBuffer(this._indicesBuffer);
  352. this._indicesBuffer = gl.createBuffer();
  353. var x, y, i, locIndices = this._indices, locTexCoordinates = this._texCoordinates;
  354. var locIsTextureFlipped = this._isTextureFlipped, locVertices = this._vertices;
  355. for (x = 0; x < locGridSize.width; ++x) {
  356. for (y = 0; y < locGridSize.height; ++y) {
  357. var idx = (y * locGridSize.width) + x;
  358. var x1 = x * this._step.x;
  359. var x2 = x1 + this._step.x;
  360. var y1 = y * this._step.y;
  361. var y2 = y1 + this._step.y;
  362. var a = (x * (locGridSize.height + 1) + y);
  363. var b = ((x + 1) * (locGridSize.height + 1) + y);
  364. var c = ((x + 1) * (locGridSize.height + 1) + (y + 1));
  365. var d = (x * (locGridSize.height + 1) + (y + 1));
  366. locIndices[idx * 6] = a;
  367. locIndices[idx * 6 + 1] = b;
  368. locIndices[idx * 6 + 2] = d;
  369. locIndices[idx * 6 + 3] = b;
  370. locIndices[idx * 6 + 4] = c;
  371. locIndices[idx * 6 + 5] = d;
  372. var l1 = [a * 3, b * 3, c * 3, d * 3];
  373. var e = {x:x1, y:y1, z:0}; //new cc.Vertex3F(x1, y1, 0);
  374. var f = {x:x2, y:y1, z:0}; //new cc.Vertex3F(x2, y1, 0);
  375. var g = {x:x2, y:y2, z:0}; // new cc.Vertex3F(x2, y2, 0);
  376. var h = {x:x1, y:y2, z:0}; //new cc.Vertex3F(x1, y2, 0);
  377. var l2 = [e, f, g, h];
  378. var tex1 = [a * 2, b * 2, c * 2, d * 2];
  379. var tex2 = [cc.p(x1, y1), cc.p(x2, y1), cc.p(x2, y2), cc.p(x1, y2)];
  380. for (i = 0; i < 4; ++i) {
  381. locVertices[l1[i]] = l2[i].x;
  382. locVertices[l1[i] + 1] = l2[i].y;
  383. locVertices[l1[i] + 2] = l2[i].z;
  384. locTexCoordinates[tex1[i]] = tex2[i].x / width;
  385. if (locIsTextureFlipped)
  386. locTexCoordinates[tex1[i] + 1] = (imageH - tex2[i].y) / height;
  387. else
  388. locTexCoordinates[tex1[i] + 1] = tex2[i].y / height;
  389. }
  390. }
  391. }
  392. this._originalVertices = new Float32Array(this._vertices);
  393. gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer);
  394. gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW);
  395. gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer);
  396. gl.bufferData(gl.ARRAY_BUFFER, this._texCoordinates, gl.DYNAMIC_DRAW);
  397. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer);
  398. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.STATIC_DRAW);
  399. this._dirty = true;
  400. }
  401. });
  402. /**
  403. * create one Grid3D object
  404. * @param {cc.Size} gridSize
  405. * @param {cc.Texture2D} [texture=]
  406. * @param {Boolean} [flipped=]
  407. * @return {cc.Grid3D}
  408. */
  409. cc.Grid3D.create = function (gridSize, texture, flipped) {
  410. var grid3D = new cc.Grid3D();
  411. if (grid3D && grid3D.initWithSize(gridSize, texture, flipped))
  412. return grid3D;
  413. return null;
  414. };
  415. /**
  416. * cc.TiledGrid3D is a 3D grid implementation. It differs from Grid3D in that <br/>
  417. * the tiles can be separated from the grid.
  418. * @class
  419. * @extends cc.GridBase
  420. */
  421. cc.TiledGrid3D = cc.GridBase.extend(/** @lends cc.TiledGrid3D# */{
  422. _texCoordinates:null,
  423. _vertices:null,
  424. _originalVertices:null,
  425. _indices:null,
  426. _texCoordinateBuffer:null,
  427. _verticesBuffer:null,
  428. _indicesBuffer:null,
  429. ctor:function () {
  430. cc.GridBase.prototype.ctor.call(this);
  431. this._texCoordinates=null;
  432. this._vertices=null;
  433. this._originalVertices=null;
  434. this._indices=null;
  435. this._texCoordinateBuffer=null;
  436. this._verticesBuffer=null;
  437. this._indicesBuffer=null;
  438. },
  439. /**
  440. * returns the tile at the given position
  441. * @param {cc.Point} pos
  442. * @return {cc.Quad3}
  443. */
  444. tile:function (pos) {
  445. if(pos.x !== (0| pos.x) || pos.y !== (0| pos.y))
  446. cc.log("cc.TiledGrid3D.tile() : Numbers must be integers");
  447. var idx = (this._gridSize.height * pos.x + pos.y) * 4 * 3;
  448. var locVertices = this._vertices;
  449. return new cc.Quad3(new cc.Vertex3F(locVertices[idx], locVertices[idx + 1], locVertices[idx + 2]),
  450. new cc.Vertex3F(locVertices[idx + 3], locVertices[idx + 4], locVertices[idx + 5]),
  451. new cc.Vertex3F(locVertices[idx + 6 ], locVertices[idx + 7], locVertices[idx + 8]),
  452. new cc.Vertex3F(locVertices[idx + 9], locVertices[idx + 10], locVertices[idx + 11]));
  453. },
  454. /**
  455. * returns the original tile (untransformed) at the given position
  456. * @param {cc.Point} pos
  457. * @return {cc.Quad3}
  458. */
  459. originalTile:function (pos) {
  460. if(pos.x !== (0| pos.x) || pos.y !== (0| pos.y))
  461. cc.log("cc.TiledGrid3D.originalTile() : Numbers must be integers");
  462. var idx = (this._gridSize.height * pos.x + pos.y) * 4 * 3;
  463. var locOriginalVertices = this._originalVertices;
  464. return new cc.Quad3(new cc.Vertex3F(locOriginalVertices[idx], locOriginalVertices[idx + 1], locOriginalVertices[idx + 2]),
  465. new cc.Vertex3F(locOriginalVertices[idx + 3], locOriginalVertices[idx + 4], locOriginalVertices[idx + 5]),
  466. new cc.Vertex3F(locOriginalVertices[idx + 6 ], locOriginalVertices[idx + 7], locOriginalVertices[idx + 8]),
  467. new cc.Vertex3F(locOriginalVertices[idx + 9], locOriginalVertices[idx + 10], locOriginalVertices[idx + 11]));
  468. },
  469. /**
  470. * sets a new tile
  471. * @param {cc.Point} pos
  472. * @param {cc.Quad3} coords
  473. */
  474. setTile:function (pos, coords) {
  475. if(pos.x !== (0| pos.x) || pos.y !== (0| pos.y))
  476. cc.log("cc.TiledGrid3D.setTile() : Numbers must be integers");
  477. var idx = (this._gridSize.height * pos.x + pos.y) * 12;
  478. var locVertices = this._vertices;
  479. locVertices[idx] = coords.bl.x;
  480. locVertices[idx + 1] = coords.bl.y;
  481. locVertices[idx + 2] = coords.bl.z;
  482. locVertices[idx + 3] = coords.br.x;
  483. locVertices[idx + 4] = coords.br.y;
  484. locVertices[idx + 5] = coords.br.z;
  485. locVertices[idx + 6] = coords.tl.x;
  486. locVertices[idx + 7] = coords.tl.y;
  487. locVertices[idx + 8] = coords.tl.z;
  488. locVertices[idx + 9] = coords.tr.x;
  489. locVertices[idx + 10] = coords.tr.y;
  490. locVertices[idx + 11] = coords.tr.z;
  491. this._dirty = true;
  492. },
  493. blit:function () {
  494. var n = this._gridSize.width * this._gridSize.height;
  495. this._shaderProgram.use();
  496. this._shaderProgram.setUniformsForBuiltins();
  497. //
  498. // Attributes
  499. //
  500. var gl = cc.renderContext, locDirty = this._dirty;
  501. cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POSITION | cc.VERTEX_ATTRIB_FLAG_TEX_COORDS);
  502. // position
  503. gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer);
  504. if (locDirty)
  505. gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW);
  506. gl.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 3, gl.FLOAT, false, 0, this._vertices);
  507. // texCoords
  508. gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer);
  509. if (locDirty)
  510. gl.bufferData(gl.ARRAY_BUFFER, this._texCoordinates, gl.DYNAMIC_DRAW);
  511. gl.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, gl.FLOAT, false, 0, this._texCoordinates);
  512. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer);
  513. if (locDirty)
  514. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.STATIC_DRAW);
  515. gl.drawElements(gl.TRIANGLES, n * 6, gl.UNSIGNED_SHORT, 0);
  516. if (locDirty)
  517. this._dirty = false;
  518. cc.INCREMENT_GL_DRAWS(1);
  519. },
  520. reuse:function () {
  521. if (this._reuseGrid > 0) {
  522. var locVertices = this._vertices, locOriginalVertices = this._originalVertices;
  523. for (var i = 0; i < locVertices.length; i++)
  524. locOriginalVertices[i] = locVertices[i];
  525. --this._reuseGrid;
  526. }
  527. },
  528. calculateVertexPoints:function () {
  529. var width = this._texture.getPixelsWide();
  530. var height = this._texture.getPixelsHigh();
  531. var imageH = this._texture.getContentSizeInPixels().height;
  532. var locGridSize = this._gridSize;
  533. var numQuads = locGridSize.width * locGridSize.height;
  534. this._vertices = new Float32Array(numQuads * 12);
  535. this._texCoordinates = new Float32Array(numQuads * 8);
  536. this._indices = new Uint16Array(numQuads * 6);
  537. var gl = cc.renderContext;
  538. if(this._verticesBuffer)
  539. gl.deleteBuffer(this._verticesBuffer);
  540. this._verticesBuffer = gl.createBuffer();
  541. if(this._texCoordinateBuffer)
  542. gl.deleteBuffer(this._texCoordinateBuffer);
  543. this._texCoordinateBuffer = gl.createBuffer();
  544. if(this._indicesBuffer)
  545. gl.deleteBuffer(this._indicesBuffer);
  546. this._indicesBuffer = gl.createBuffer();
  547. var x, y, i = 0;
  548. var locStep = this._step, locVertices = this._vertices, locTexCoords = this._texCoordinates, locIsTextureFlipped = this._isTextureFlipped;
  549. for (x = 0; x < locGridSize.width; x++) {
  550. for (y = 0; y < locGridSize.height; y++) {
  551. var x1 = x * locStep.x;
  552. var x2 = x1 + locStep.x;
  553. var y1 = y * locStep.y;
  554. var y2 = y1 + locStep.y;
  555. locVertices[i * 12] = x1;
  556. locVertices[i * 12 + 1] = y1;
  557. locVertices[i * 12 + 2] = 0;
  558. locVertices[i * 12 + 3] = x2;
  559. locVertices[i * 12 + 4] = y1;
  560. locVertices[i * 12 + 5] = 0;
  561. locVertices[i * 12 + 6] = x1;
  562. locVertices[i * 12 + 7] = y2;
  563. locVertices[i * 12 + 8] = 0;
  564. locVertices[i * 12 + 9] = x2;
  565. locVertices[i * 12 + 10] = y2;
  566. locVertices[i * 12 + 11] = 0;
  567. var newY1 = y1;
  568. var newY2 = y2;
  569. if (locIsTextureFlipped) {
  570. newY1 = imageH - y1;
  571. newY2 = imageH - y2;
  572. }
  573. locTexCoords[i * 8] = x1 / width;
  574. locTexCoords[i * 8 + 1] = newY1 / height;
  575. locTexCoords[i * 8 + 2] = x2 / width;
  576. locTexCoords[i * 8 + 3] = newY1 / height;
  577. locTexCoords[i * 8 + 4] = x1 / width;
  578. locTexCoords[i * 8 + 5] = newY2 / height;
  579. locTexCoords[i * 8 + 6] = x2 / width;
  580. locTexCoords[i * 8 + 7] = newY2 / height;
  581. i++;
  582. }
  583. }
  584. var locIndices = this._indices;
  585. for (x = 0; x < numQuads; x++) {
  586. locIndices[x * 6 + 0] = (x * 4 + 0);
  587. locIndices[x * 6 + 1] = (x * 4 + 1);
  588. locIndices[x * 6 + 2] = (x * 4 + 2);
  589. locIndices[x * 6 + 3] = (x * 4 + 1);
  590. locIndices[x * 6 + 4] = (x * 4 + 2);
  591. locIndices[x * 6 + 5] = (x * 4 + 3);
  592. }
  593. this._originalVertices = new Float32Array(this._vertices);
  594. gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer);
  595. gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW);
  596. gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordinateBuffer);
  597. gl.bufferData(gl.ARRAY_BUFFER, this._texCoordinates, gl.DYNAMIC_DRAW);
  598. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this._indicesBuffer);
  599. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this._indices, gl.DYNAMIC_DRAW);
  600. this._dirty = true;
  601. }
  602. });
  603. /**
  604. * create one TiledGrid3D object
  605. * @param {cc.Size} gridSize
  606. * @param {cc.Texture2D} [texture=]
  607. * @param {Boolean} [flipped=]
  608. * @return {cc.TiledGrid3D}
  609. */
  610. cc.TiledGrid3D.create = function (gridSize, texture, flipped) {
  611. var ret = new cc.TiledGrid3D();
  612. ret.initWithSize(gridSize, texture, flipped);
  613. return ret;
  614. };