0
A
回答
1
I solved this issue by using following method:-
I create one rendure texture and add an sprite on it.
I found answer here:- http://discuss.cocos2d-x.org/t/render-texture-get-percentage-of-transparent/21123
here is the code:-
var WINDOW_WIDTH = cc.director.getWinSize().width;
var WINDOW_HEIGHT = cc.director.getWinSize().height;
rt = new cc.RenderTexture(WINDOW_WIDTH, WINDOW_HEIGHT,
sprite.getTexture().getPixelFormat());
rt.setPosition(WINDOW_WIDTH/2, WINDOW_HEIGHT/2);
this.addChild(rt, 5);
getPercentageTransparent: function() {
//
var s = rt.getSprite().getContentSize();
var tx = s.width;
var ty = s.height;
var bitsPerPixel = 4 * 8;
var bytesPerPixel = bitsPerPixel/8;
var bytesPerRow = bytesPerPixel * tx;
var myDataLength = bytesPerRow * ty;
var numberOfPixels = tx * ty;
var numberOfTransparent = 0;
var rawImagePixels = new Uint8Array(myDataLength);
rt.begin();
gl.readPixels(0, 0, tx, ty, gl.RGBA, gl.UNSIGNED_BYTE, rawImagePixels);
rt.end();
var x, y;
for (y = 0; y < ty; y++) {
// just want the last byte (alpha) for each pixel
for (x = 0; x < tx; x++) {
var alpha = rawImagePixels[(y * 4 * tx + ((x * 4) + 3))];
if (alpha < 1) {
numberOfTransparent++;
}
}
}
cc.log("Number of pixels" + numberOfPixels);
cc.log("Number of Trasparent" + numberOfTransparent);
cc.log("percentage " + (numberOfTransparent/numberOfPixels) * 100);
return (numberOfTransparent/numberOfPixels) * 100;
},
相關問題
- 1. 我如何在引擎中從Sprite中獲取像素的顏色/ alpha值?
- 2. 如何從Silverlight中的圖像獲取像素ARGB信息?
- 3. Cocos2d-x Sprite圖像名稱
- 4. 如何在cocos2d中查找縮放sprite中的像素完美碰撞?
- 5. C++/SDL:淡出已具有每像素alpha信息的表面
- 6. 如何移除cocos2d中的sprite?
- 7. 如何獲取片段着色器中的像素信息?
- 8. 如何從Windows中的Image對象獲取像素信息?
- 9. Point Sprite Alpha Blending
- 10. 如何從圖像中獲取信息?
- 11. 如何在Cocos2d iPhone中使用Sprite Sheet?
- 12. Sprite Kit的繪製方法就像Cocos2d
- 13. CGContext中的alpha像素數
- 14. Cocos2D Sprite表
- 15. cocos2d sprite動畫
- 16. 如何使用OpenGLes和cocos2d在3D中旋轉Sprite像'Fruit Ninja'
- 17. 如何獲得cocos2d中的座標?
- 18. 如何獲得cocos2d中的精靈?
- 19. OpenGL獲取背景像素信息
- 20. 如何獲得serverside信息到JavaScript?
- 21. 如何獲得git項目信息
- 22. 我如何獲得Liberator異常信息?
- 23. 如何獲得所有CPU信息
- 24. 如何使用scala獲得此信息
- 25. 「validates_uniqueness_of」 - 如何獲得錯誤信息
- 26. 我如何獲得instagram帳戶信息
- 27. 如何獲得ehcache 3.1統計信息
- 28. 如何獲得h264視頻信息?
- 29. 如何獲得此ListActivity更新信息?
- 30. 如何獲得在C BIOS信息++