|
| 1 | +/** |
| 2 | + * We're replacing a couple of Pixi's methods here to fix or add some vital functionality: |
| 3 | + * |
| 4 | + * 1) Added support for Trimmed sprite sheets |
| 5 | + * 2) Skip display objects with an alpha of zero |
| 6 | + * |
| 7 | + * Hopefully we can remove this once Pixi has been updated to support these things. |
| 8 | + */ |
| 9 | + |
| 10 | +PIXI.CanvasRenderer.prototype.renderDisplayObject = function(displayObject) |
| 11 | +{ |
| 12 | + // no loger recurrsive! |
| 13 | + var transform; |
| 14 | + var context = this.context; |
| 15 | + |
| 16 | + context.globalCompositeOperation = 'source-over'; |
| 17 | + |
| 18 | + // one the display object hits this. we can break the loop |
| 19 | + var testObject = displayObject.last._iNext; |
| 20 | + displayObject = displayObject.first; |
| 21 | + |
| 22 | + do |
| 23 | + { |
| 24 | + transform = displayObject.worldTransform; |
| 25 | + |
| 26 | + if(!displayObject.visible) |
| 27 | + { |
| 28 | + displayObject = displayObject.last._iNext; |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + if(!displayObject.renderable || displayObject.alpha == 0) |
| 33 | + { |
| 34 | + displayObject = displayObject._iNext; |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + if(displayObject instanceof PIXI.Sprite) |
| 39 | + { |
| 40 | + var frame = displayObject.texture.frame; |
| 41 | + |
| 42 | + if(frame) |
| 43 | + { |
| 44 | + context.globalAlpha = displayObject.worldAlpha; |
| 45 | + |
| 46 | + if (displayObject.texture.trimmed) |
| 47 | + { |
| 48 | + context.setTransform(transform[0], transform[3], transform[1], transform[4], transform[2] + displayObject.texture.trim.x, transform[5] + displayObject.texture.trim.y); |
| 49 | + } |
| 50 | + else |
| 51 | + { |
| 52 | + context.setTransform(transform[0], transform[3], transform[1], transform[4], transform[2], transform[5]); |
| 53 | + } |
| 54 | + |
| 55 | + context.drawImage(displayObject.texture.baseTexture.source, |
| 56 | + frame.x, |
| 57 | + frame.y, |
| 58 | + frame.width, |
| 59 | + frame.height, |
| 60 | + (displayObject.anchor.x) * -frame.width, |
| 61 | + (displayObject.anchor.y) * -frame.height, |
| 62 | + frame.width, |
| 63 | + frame.height); |
| 64 | + } |
| 65 | + } |
| 66 | + else if(displayObject instanceof PIXI.Strip) |
| 67 | + { |
| 68 | + context.setTransform(transform[0], transform[3], transform[1], transform[4], transform[2], transform[5]) |
| 69 | + this.renderStrip(displayObject); |
| 70 | + } |
| 71 | + else if(displayObject instanceof PIXI.TilingSprite) |
| 72 | + { |
| 73 | + context.setTransform(transform[0], transform[3], transform[1], transform[4], transform[2], transform[5]) |
| 74 | + this.renderTilingSprite(displayObject); |
| 75 | + } |
| 76 | + else if(displayObject instanceof PIXI.CustomRenderable) |
| 77 | + { |
| 78 | + displayObject.renderCanvas(this); |
| 79 | + } |
| 80 | + else if(displayObject instanceof PIXI.Graphics) |
| 81 | + { |
| 82 | + context.setTransform(transform[0], transform[3], transform[1], transform[4], transform[2], transform[5]) |
| 83 | + PIXI.CanvasGraphics.renderGraphics(displayObject, context); |
| 84 | + } |
| 85 | + else if(displayObject instanceof PIXI.FilterBlock) |
| 86 | + { |
| 87 | + if(displayObject.open) |
| 88 | + { |
| 89 | + context.save(); |
| 90 | + |
| 91 | + var cacheAlpha = displayObject.mask.alpha; |
| 92 | + var maskTransform = displayObject.mask.worldTransform; |
| 93 | + |
| 94 | + context.setTransform(maskTransform[0], maskTransform[3], maskTransform[1], maskTransform[4], maskTransform[2], maskTransform[5]) |
| 95 | + |
| 96 | + displayObject.mask.worldAlpha = 0.5; |
| 97 | + |
| 98 | + context.worldAlpha = 0; |
| 99 | + |
| 100 | + PIXI.CanvasGraphics.renderGraphicsMask(displayObject.mask, context); |
| 101 | + context.clip(); |
| 102 | + |
| 103 | + displayObject.mask.worldAlpha = cacheAlpha; |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + context.restore(); |
| 108 | + } |
| 109 | + } |
| 110 | + // count++ |
| 111 | + displayObject = displayObject._iNext; |
| 112 | + |
| 113 | + |
| 114 | + } |
| 115 | + while(displayObject != testObject) |
| 116 | + |
| 117 | +} |
| 118 | + |
| 119 | +PIXI.WebGLBatch.prototype.update = function() |
| 120 | +{ |
| 121 | + var gl = this.gl; |
| 122 | + var worldTransform, width, height, aX, aY, w0, w1, h0, h1, index, index2, index3 |
| 123 | + |
| 124 | + var a, b, c, d, tx, ty; |
| 125 | + |
| 126 | + var indexRun = 0; |
| 127 | + |
| 128 | + var displayObject = this.head; |
| 129 | + |
| 130 | + while(displayObject) |
| 131 | + { |
| 132 | + if(displayObject.vcount === PIXI.visibleCount) |
| 133 | + { |
| 134 | + width = displayObject.texture.frame.width; |
| 135 | + height = displayObject.texture.frame.height; |
| 136 | + |
| 137 | + // TODO trim?? |
| 138 | + aX = displayObject.anchor.x;// - displayObject.texture.trim.x |
| 139 | + aY = displayObject.anchor.y; //- displayObject.texture.trim.y |
| 140 | + w0 = width * (1-aX); |
| 141 | + w1 = width * -aX; |
| 142 | + |
| 143 | + h0 = height * (1-aY); |
| 144 | + h1 = height * -aY; |
| 145 | + |
| 146 | + index = indexRun * 8; |
| 147 | + |
| 148 | + worldTransform = displayObject.worldTransform; |
| 149 | + |
| 150 | + a = worldTransform[0]; |
| 151 | + b = worldTransform[3]; |
| 152 | + c = worldTransform[1]; |
| 153 | + d = worldTransform[4]; |
| 154 | + tx = worldTransform[2]; |
| 155 | + ty = worldTransform[5]; |
| 156 | + |
| 157 | + if (displayObject.texture.trimmed) |
| 158 | + { |
| 159 | + tx += displayObject.texture.trim.x; |
| 160 | + ty += displayObject.texture.trim.y; |
| 161 | + } |
| 162 | + |
| 163 | + this.verticies[index + 0 ] = a * w1 + c * h1 + tx; |
| 164 | + this.verticies[index + 1 ] = d * h1 + b * w1 + ty; |
| 165 | + |
| 166 | + this.verticies[index + 2 ] = a * w0 + c * h1 + tx; |
| 167 | + this.verticies[index + 3 ] = d * h1 + b * w0 + ty; |
| 168 | + |
| 169 | + this.verticies[index + 4 ] = a * w0 + c * h0 + tx; |
| 170 | + this.verticies[index + 5 ] = d * h0 + b * w0 + ty; |
| 171 | + |
| 172 | + this.verticies[index + 6] = a * w1 + c * h0 + tx; |
| 173 | + this.verticies[index + 7] = d * h0 + b * w1 + ty; |
| 174 | + |
| 175 | + if(displayObject.updateFrame || displayObject.texture.updateFrame) |
| 176 | + { |
| 177 | + this.dirtyUVS = true; |
| 178 | + |
| 179 | + var texture = displayObject.texture; |
| 180 | + |
| 181 | + var frame = texture.frame; |
| 182 | + var tw = texture.baseTexture.width; |
| 183 | + var th = texture.baseTexture.height; |
| 184 | + |
| 185 | + this.uvs[index + 0] = frame.x / tw; |
| 186 | + this.uvs[index +1] = frame.y / th; |
| 187 | + |
| 188 | + this.uvs[index +2] = (frame.x + frame.width) / tw; |
| 189 | + this.uvs[index +3] = frame.y / th; |
| 190 | + |
| 191 | + this.uvs[index +4] = (frame.x + frame.width) / tw; |
| 192 | + this.uvs[index +5] = (frame.y + frame.height) / th; |
| 193 | + |
| 194 | + this.uvs[index +6] = frame.x / tw; |
| 195 | + this.uvs[index +7] = (frame.y + frame.height) / th; |
| 196 | + |
| 197 | + displayObject.updateFrame = false; |
| 198 | + } |
| 199 | + |
| 200 | + // TODO this probably could do with some optimisation.... |
| 201 | + if(displayObject.cacheAlpha != displayObject.worldAlpha) |
| 202 | + { |
| 203 | + displayObject.cacheAlpha = displayObject.worldAlpha; |
| 204 | + |
| 205 | + var colorIndex = indexRun * 4; |
| 206 | + this.colors[colorIndex] = this.colors[colorIndex + 1] = this.colors[colorIndex + 2] = this.colors[colorIndex + 3] = displayObject.worldAlpha; |
| 207 | + this.dirtyColors = true; |
| 208 | + } |
| 209 | + } |
| 210 | + else |
| 211 | + { |
| 212 | + index = indexRun * 8; |
| 213 | + |
| 214 | + this.verticies[index + 0 ] = 0; |
| 215 | + this.verticies[index + 1 ] = 0; |
| 216 | + |
| 217 | + this.verticies[index + 2 ] = 0; |
| 218 | + this.verticies[index + 3 ] = 0; |
| 219 | + |
| 220 | + this.verticies[index + 4 ] = 0; |
| 221 | + this.verticies[index + 5 ] = 0; |
| 222 | + |
| 223 | + this.verticies[index + 6] = 0; |
| 224 | + this.verticies[index + 7] = 0; |
| 225 | + } |
| 226 | + |
| 227 | + indexRun++; |
| 228 | + displayObject = displayObject.__next; |
| 229 | + } |
| 230 | +} |
0 commit comments