CCMotionStreak.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. Copyright (c) 2008-2009 Jason Booth
  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. * cc.MotionStreak manages a Ribbon based on it's motion in absolute space. <br/>
  25. * You construct it with a fadeTime, minimum segment size, texture path, texture <br/>
  26. * length and color. The fadeTime controls how long it takes each vertex in <br/>
  27. * the streak to fade out, the minimum segment size it how many pixels the <br/>
  28. * streak will move before adding a new ribbon segment, and the texture <br/>
  29. * length is the how many pixels the texture is stretched across. The texture <br/>
  30. * is vertically aligned along the streak segment.
  31. * @class
  32. * @extends cc.Node
  33. *
  34. * @property {cc.Texture2D} texture - Texture used for the motion streak.
  35. * @property {Boolean} fastMode - Indicate whether use fast mode.
  36. * @property {Boolean} startingPositionInitialized - Indicate whether starting position initialized.
  37. * @example
  38. * //example
  39. * new cc.MotionStreak(2, 3, 32, cc.color.GREEN, s_streak);
  40. */
  41. cc.MotionStreak = cc.Node.extend(/** @lends cc.MotionStreak# */{
  42. texture:null,
  43. fastMode:false,
  44. startingPositionInitialized:false,
  45. _blendFunc:null,
  46. _stroke:0,
  47. _fadeDelta:0,
  48. _minSeg:0,
  49. _maxPoints:0,
  50. _nuPoints:0,
  51. _previousNuPoints:0,
  52. /* Pointers */
  53. _pointVertexes:null,
  54. _pointState:null,
  55. // webgl
  56. _vertices:null,
  57. _colorPointer:null,
  58. _texCoords:null,
  59. _verticesBuffer:null,
  60. _colorPointerBuffer:null,
  61. _texCoordsBuffer:null,
  62. _className:"MotionStreak",
  63. /**
  64. * creates and initializes a motion streak with fade in seconds, minimum segments, stroke's width, color, texture filename or texture <br/>
  65. * Constructor of cc.MotionStreak
  66. * @param {Number} fade time to fade
  67. * @param {Number} minSeg minimum segment size
  68. * @param {Number} stroke stroke's width
  69. * @param {Number} color
  70. * @param {string|cc.Texture2D} texture texture filename or texture
  71. */
  72. ctor: function (fade, minSeg, stroke, color, texture) {
  73. cc.Node.prototype.ctor.call(this);
  74. this._positionR = cc.p(0, 0);
  75. this._blendFunc = new cc.BlendFunc(cc.SRC_ALPHA, cc.ONE_MINUS_SRC_ALPHA);
  76. this._vertexWebGLBuffer = cc._renderContext.createBuffer();
  77. this.fastMode = false;
  78. this.startingPositionInitialized = false;
  79. this.texture = null;
  80. this._stroke = 0;
  81. this._fadeDelta = 0;
  82. this._minSeg = 0;
  83. this._maxPoints = 0;
  84. this._nuPoints = 0;
  85. this._previousNuPoints = 0;
  86. /** Pointers */
  87. this._pointVertexes = null;
  88. this._pointState = null;
  89. // webgl
  90. this._vertices = null;
  91. this._colorPointer = null;
  92. this._texCoords = null;
  93. this._verticesBuffer = null;
  94. this._colorPointerBuffer = null;
  95. this._texCoordsBuffer = null;
  96. if(texture !== undefined)
  97. this.initWithFade(fade, minSeg, stroke, color, texture);
  98. },
  99. /**
  100. * Gets the texture.
  101. * @return {cc.Texture2D}
  102. */
  103. getTexture:function () {
  104. return this.texture;
  105. },
  106. /**
  107. * Set the texture.
  108. * @param {cc.Texture2D} texture
  109. */
  110. setTexture:function (texture) {
  111. if (this.texture != texture)
  112. this.texture = texture;
  113. },
  114. /**
  115. * Gets the blend func.
  116. * @return {cc.BlendFunc}
  117. */
  118. getBlendFunc:function () {
  119. return this._blendFunc;
  120. },
  121. /**
  122. * Set the blend func.
  123. * @param {Number} src
  124. * @param {Number} dst
  125. */
  126. setBlendFunc:function (src, dst) {
  127. if (dst === undefined) {
  128. this._blendFunc = src;
  129. } else {
  130. this._blendFunc.src = src;
  131. this._blendFunc.dst = dst;
  132. }
  133. },
  134. /**
  135. * Gets opacity.
  136. * @warning cc.MotionStreak.getOpacity has not been supported.
  137. * @returns {number}
  138. */
  139. getOpacity:function () {
  140. cc.log("cc.MotionStreak.getOpacity has not been supported.");
  141. return 0;
  142. },
  143. /**
  144. * Set opacity.
  145. * @warning cc.MotionStreak.setOpacity has not been supported.
  146. * @param opacity
  147. */
  148. setOpacity:function (opacity) {
  149. cc.log("cc.MotionStreak.setOpacity has not been supported.");
  150. },
  151. /**
  152. * set opacity modify RGB.
  153. * @warning cc.MotionStreak.setOpacityModifyRGB has not been supported.
  154. * @param value
  155. */
  156. setOpacityModifyRGB:function (value) {
  157. },
  158. /**
  159. * Checking OpacityModifyRGB.
  160. * @returns {boolean}
  161. */
  162. isOpacityModifyRGB:function () {
  163. return false;
  164. },
  165. /**
  166. * <p>
  167. * callback that is called every time the node leaves the 'stage'. <br/>
  168. * If the node leaves the 'stage' with a transition, this callback is called when the transition finishes. <br/>
  169. * During onExit you can't access a sibling node. <br/>
  170. * If you override onExit, you shall call its parent's onExit with this._super().
  171. * </p>
  172. * @function
  173. */
  174. onExit:function(){
  175. cc.Node.prototype.onExit.call(this);
  176. if(this._verticesBuffer)
  177. cc._renderContext.deleteBuffer(this._verticesBuffer);
  178. if(this._texCoordsBuffer)
  179. cc._renderContext.deleteBuffer(this._texCoordsBuffer);
  180. if(this._colorPointerBuffer)
  181. cc._renderContext.deleteBuffer(this._colorPointerBuffer);
  182. },
  183. /**
  184. * Checking fast mode.
  185. * @returns {boolean}
  186. */
  187. isFastMode:function () {
  188. return this.fastMode;
  189. },
  190. /**
  191. * set fast mode
  192. * @param {Boolean} fastMode
  193. */
  194. setFastMode:function (fastMode) {
  195. this.fastMode = fastMode;
  196. },
  197. /**
  198. * Checking starting position initialized.
  199. * @returns {boolean}
  200. */
  201. isStartingPositionInitialized:function () {
  202. return this.startingPositionInitialized;
  203. },
  204. /**
  205. * Set Starting Position Initialized.
  206. * @param {Boolean} startingPositionInitialized
  207. */
  208. setStartingPositionInitialized:function (startingPositionInitialized) {
  209. this.startingPositionInitialized = startingPositionInitialized;
  210. },
  211. /**
  212. * initializes a motion streak with fade in seconds, minimum segments, stroke's width, color and texture filename or texture
  213. * @param {Number} fade time to fade
  214. * @param {Number} minSeg minimum segment size
  215. * @param {Number} stroke stroke's width
  216. * @param {Number} color
  217. * @param {string|cc.Texture2D} texture texture filename or texture
  218. * @return {Boolean}
  219. */
  220. initWithFade:function (fade, minSeg, stroke, color, texture) {
  221. if(!texture)
  222. throw "cc.MotionStreak.initWithFade(): Invalid filename or texture";
  223. if (typeof(texture) === "string")
  224. texture = cc.textureCache.addImage(texture);
  225. cc.Node.prototype.setPosition.call(this, cc.p(0,0));
  226. this.anchorX = 0;
  227. this.anchorY = 0;
  228. this.ignoreAnchor = true;
  229. this.startingPositionInitialized = false;
  230. this.fastMode = true;
  231. this._minSeg = (minSeg == -1.0) ? (stroke / 5.0) : minSeg;
  232. this._minSeg *= this._minSeg;
  233. this._stroke = stroke;
  234. this._fadeDelta = 1.0 / fade;
  235. var locMaxPoints = (0 | (fade * 60)) + 2;
  236. this._nuPoints = 0;
  237. this._pointState = new Float32Array(locMaxPoints);
  238. this._pointVertexes = new Float32Array(locMaxPoints * 2);
  239. this._vertices = new Float32Array(locMaxPoints * 4);
  240. this._texCoords = new Float32Array(locMaxPoints * 4);
  241. this._colorPointer = new Uint8Array(locMaxPoints * 8);
  242. this._maxPoints = locMaxPoints;
  243. var gl = cc._renderContext;
  244. this._verticesBuffer = gl.createBuffer();
  245. this._texCoordsBuffer = gl.createBuffer();
  246. this._colorPointerBuffer = gl.createBuffer();
  247. // Set blend mode
  248. this._blendFunc.src = gl.SRC_ALPHA;
  249. this._blendFunc.dst = gl.ONE_MINUS_SRC_ALPHA;
  250. // shader program
  251. this.shaderProgram = cc.shaderCache.programForKey(cc.SHADER_POSITION_TEXTURECOLOR);
  252. this.texture = texture;
  253. this.color = color;
  254. this.scheduleUpdate();
  255. //bind buffer
  256. gl.bindBuffer(gl.ARRAY_BUFFER, this._verticesBuffer);
  257. gl.bufferData(gl.ARRAY_BUFFER, this._vertices, gl.DYNAMIC_DRAW);
  258. gl.bindBuffer(gl.ARRAY_BUFFER, this._texCoordsBuffer);
  259. gl.bufferData(gl.ARRAY_BUFFER, this._texCoords, gl.DYNAMIC_DRAW);
  260. gl.bindBuffer(gl.ARRAY_BUFFER, this._colorPointerBuffer);
  261. gl.bufferData(gl.ARRAY_BUFFER, this._colorPointer, gl.DYNAMIC_DRAW);
  262. return true;
  263. },
  264. /**
  265. * color used for the tint
  266. * @param {cc.Color} color
  267. */
  268. tintWithColor:function (color) {
  269. this.color = color;
  270. // Fast assignation
  271. var locColorPointer = this._colorPointer;
  272. for (var i = 0, len = this._nuPoints * 2; i < len; i++) {
  273. locColorPointer[i * 4] = color.r;
  274. locColorPointer[i * 4 + 1] = color.g;
  275. locColorPointer[i * 4 + 2] = color.b;
  276. }
  277. },
  278. /**
  279. * Remove all living segments of the ribbon
  280. */
  281. reset:function () {
  282. this._nuPoints = 0;
  283. },
  284. /**
  285. * Set the position. <br />
  286. *
  287. * @param {cc.Point|Number} position
  288. * @param {Number} [yValue=undefined] If not exists, the first parameter must be cc.Point.
  289. */
  290. setPosition:function (position, yValue) {
  291. this.startingPositionInitialized = true;
  292. if(yValue === undefined){
  293. this._positionR.x = position.x;
  294. this._positionR.y = position.y;
  295. } else {
  296. this._positionR.x = position;
  297. this._positionR.y = yValue;
  298. }
  299. },
  300. /**
  301. * Gets the position.x
  302. * @return {Number}
  303. */
  304. getPositionX:function () {
  305. return this._positionR.x;
  306. },
  307. /**
  308. * Set the position.x
  309. * @param {Number} x
  310. */
  311. setPositionX:function (x) {
  312. this._positionR.x = x;
  313. if(!this.startingPositionInitialized)
  314. this.startingPositionInitialized = true;
  315. },
  316. /**
  317. * Gets the position.y
  318. * @return {Number}
  319. */
  320. getPositionY:function () {
  321. return this._positionR.y;
  322. },
  323. /**
  324. * Set the position.y
  325. * @param {Number} y
  326. */
  327. setPositionY:function (y) {
  328. this._positionR.y = y;
  329. if(!this.startingPositionInitialized)
  330. this.startingPositionInitialized = true;
  331. },
  332. /**
  333. * Render function using the canvas 2d context or WebGL context, internal usage only, please do not call this function
  334. * @function
  335. * @param {CanvasRenderingContext2D | WebGLRenderingContext} ctx The render context
  336. */
  337. draw:function (ctx) {
  338. if (this._nuPoints <= 1)
  339. return;
  340. if(this.texture && this.texture.isLoaded()){
  341. ctx = ctx || cc._renderContext;
  342. cc.nodeDrawSetup(this);
  343. cc.glEnableVertexAttribs(cc.VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
  344. cc.glBlendFunc(this._blendFunc.src, this._blendFunc.dst);
  345. cc.glBindTexture2D(this.texture);
  346. //position
  347. ctx.bindBuffer(ctx.ARRAY_BUFFER, this._verticesBuffer);
  348. ctx.bufferData(ctx.ARRAY_BUFFER, this._vertices, ctx.DYNAMIC_DRAW);
  349. ctx.vertexAttribPointer(cc.VERTEX_ATTRIB_POSITION, 2, ctx.FLOAT, false, 0, 0);
  350. //texcoords
  351. ctx.bindBuffer(ctx.ARRAY_BUFFER, this._texCoordsBuffer);
  352. ctx.bufferData(ctx.ARRAY_BUFFER, this._texCoords, ctx.DYNAMIC_DRAW);
  353. ctx.vertexAttribPointer(cc.VERTEX_ATTRIB_TEX_COORDS, 2, ctx.FLOAT, false, 0, 0);
  354. //colors
  355. ctx.bindBuffer(ctx.ARRAY_BUFFER, this._colorPointerBuffer);
  356. ctx.bufferData(ctx.ARRAY_BUFFER, this._colorPointer, ctx.DYNAMIC_DRAW);
  357. ctx.vertexAttribPointer(cc.VERTEX_ATTRIB_COLOR, 4, ctx.UNSIGNED_BYTE, true, 0, 0);
  358. ctx.drawArrays(ctx.TRIANGLE_STRIP, 0, this._nuPoints * 2);
  359. cc.g_NumberOfDraws ++;
  360. }
  361. },
  362. /**
  363. * <p>schedules the "update" method. <br/>
  364. * It will use the order number 0. This method will be called every frame. <br/>
  365. * Scheduled methods with a lower order value will be called before the ones that have a higher order value.<br/>
  366. * Only one "update" method could be scheduled per node.</p>
  367. * @param {Number} delta
  368. */
  369. update:function (delta) {
  370. if (!this.startingPositionInitialized)
  371. return;
  372. delta *= this._fadeDelta;
  373. var newIdx, newIdx2, i, i2;
  374. var mov = 0;
  375. // Update current points
  376. var locNuPoints = this._nuPoints;
  377. var locPointState = this._pointState, locPointVertexes = this._pointVertexes, locVertices = this._vertices;
  378. var locColorPointer = this._colorPointer;
  379. for (i = 0; i < locNuPoints; i++) {
  380. locPointState[i] -= delta;
  381. if (locPointState[i] <= 0)
  382. mov++;
  383. else {
  384. newIdx = i - mov;
  385. if (mov > 0) {
  386. // Move data
  387. locPointState[newIdx] = locPointState[i];
  388. // Move point
  389. locPointVertexes[newIdx * 2] = locPointVertexes[i * 2];
  390. locPointVertexes[newIdx * 2 + 1] = locPointVertexes[i * 2 + 1];
  391. // Move vertices
  392. i2 = i * 2;
  393. newIdx2 = newIdx * 2;
  394. locVertices[newIdx2 * 2] = locVertices[i2 * 2];
  395. locVertices[newIdx2 * 2 + 1] = locVertices[i2 * 2 + 1];
  396. locVertices[(newIdx2 + 1) * 2] = locVertices[(i2 + 1) * 2];
  397. locVertices[(newIdx2 + 1) * 2 + 1] = locVertices[(i2 + 1) * 2 + 1];
  398. // Move color
  399. i2 *= 4;
  400. newIdx2 *= 4;
  401. locColorPointer[newIdx2 + 0] = locColorPointer[i2 + 0];
  402. locColorPointer[newIdx2 + 1] = locColorPointer[i2 + 1];
  403. locColorPointer[newIdx2 + 2] = locColorPointer[i2 + 2];
  404. locColorPointer[newIdx2 + 4] = locColorPointer[i2 + 4];
  405. locColorPointer[newIdx2 + 5] = locColorPointer[i2 + 5];
  406. locColorPointer[newIdx2 + 6] = locColorPointer[i2 + 6];
  407. } else
  408. newIdx2 = newIdx * 8;
  409. var op = locPointState[newIdx] * 255.0;
  410. locColorPointer[newIdx2 + 3] = op;
  411. locColorPointer[newIdx2 + 7] = op;
  412. }
  413. }
  414. locNuPoints -= mov;
  415. // Append new point
  416. var appendNewPoint = true;
  417. if (locNuPoints >= this._maxPoints)
  418. appendNewPoint = false;
  419. else if (locNuPoints > 0) {
  420. var a1 = cc.pDistanceSQ(cc.p(locPointVertexes[(locNuPoints - 1) * 2], locPointVertexes[(locNuPoints - 1) * 2 + 1]),
  421. this._positionR) < this._minSeg;
  422. var a2 = (locNuPoints == 1) ? false : (cc.pDistanceSQ(
  423. cc.p(locPointVertexes[(locNuPoints - 2) * 2], locPointVertexes[(locNuPoints - 2) * 2 + 1]), this._positionR) < (this._minSeg * 2.0));
  424. if (a1 || a2)
  425. appendNewPoint = false;
  426. }
  427. if (appendNewPoint) {
  428. locPointVertexes[locNuPoints * 2] = this._positionR.x;
  429. locPointVertexes[locNuPoints * 2 + 1] = this._positionR.y;
  430. locPointState[locNuPoints] = 1.0;
  431. // Color assignment
  432. var offset = locNuPoints * 8;
  433. var locDisplayedColor = this._displayedColor;
  434. locColorPointer[offset] = locDisplayedColor.r;
  435. locColorPointer[offset + 1] = locDisplayedColor.g;
  436. locColorPointer[offset + 2] = locDisplayedColor.b;
  437. //*((ccColor3B*)(m_pColorPointer + offset+4)) = this._color;
  438. locColorPointer[offset + 4] = locDisplayedColor.r;
  439. locColorPointer[offset + 5] = locDisplayedColor.g;
  440. locColorPointer[offset + 6] = locDisplayedColor.b;
  441. // Opacity
  442. locColorPointer[offset + 3] = 255;
  443. locColorPointer[offset + 7] = 255;
  444. // Generate polygon
  445. if (locNuPoints > 0 && this.fastMode) {
  446. if (locNuPoints > 1)
  447. cc.vertexLineToPolygon(locPointVertexes, this._stroke, this._vertices, locNuPoints, 1);
  448. else
  449. cc.vertexLineToPolygon(locPointVertexes, this._stroke, this._vertices, 0, 2);
  450. }
  451. locNuPoints++;
  452. }
  453. if (!this.fastMode)
  454. cc.vertexLineToPolygon(locPointVertexes, this._stroke, this._vertices, 0, locNuPoints);
  455. // Updated Tex Coords only if they are different than previous step
  456. if (locNuPoints && this._previousNuPoints != locNuPoints) {
  457. var texDelta = 1.0 / locNuPoints;
  458. var locTexCoords = this._texCoords;
  459. for (i = 0; i < locNuPoints; i++) {
  460. locTexCoords[i * 4] = 0;
  461. locTexCoords[i * 4 + 1] = texDelta * i;
  462. locTexCoords[(i * 2 + 1) * 2] = 1;
  463. locTexCoords[(i * 2 + 1) * 2 + 1] = texDelta * i;
  464. }
  465. this._previousNuPoints = locNuPoints;
  466. }
  467. this._nuPoints = locNuPoints;
  468. }
  469. });
  470. /**
  471. * Please use new cc.MotionStreak instead. <br />
  472. * Creates and initializes a motion streak with fade in seconds, minimum segments, stroke's width, color, texture filename or texture
  473. * @deprecated since v3.0 please use new cc.MotionStreak instead.
  474. * @param {Number} fade time to fade
  475. * @param {Number} minSeg minimum segment size
  476. * @param {Number} stroke stroke's width
  477. * @param {Number} color
  478. * @param {string|cc.Texture2D} texture texture filename or texture
  479. * @return {cc.MotionStreak}
  480. * @example
  481. * //example
  482. * new cc.MotionStreak(2, 3, 32, cc.color.GREEN, s_streak);
  483. */
  484. cc.MotionStreak.create = function (fade, minSeg, stroke, color, texture) {
  485. return new cc.MotionStreak(fade, minSeg, stroke, color, texture);
  486. };