cocos2d-x lua 3.10 在lua层使用RenderTexture得到像素rgba数据

其他导出接口 接上篇:
http://www.jianshu.com/p/862cb1764734

local MainScene = class("MainScene", cc.load("mvc").ViewBase)

--MainScene.RESOURCE_FILENAME = "MainScene.csb"
function MainScene:onCreate()
    printf("resource node = %s", tostring(self:getResourceNode()))
    
    --绘制图片
    self.sprite = cc.Sprite:create("red3.png")
    self.sprite:setPosition(50, 50)
    self:addChild(self.sprite, 1)
    local size = self.sprite:getContentSize() -- 返回size

    --创建渲染
    local renderTexture1 = cc.RenderTexture:create(size.width, size.height, cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888)
    renderTexture1:begin()
    --这里可能是复制顶点数据等
    local pTempSpr = cc.Sprite:createWithSpriteFrame(self.sprite:getSpriteFrame())
    pTempSpr:setPosition(cc.p(size.width/2, size.height/2))
    pTempSpr:visit()
    renderTexture1:endToLua()--这个之后必须隔帧取(或者加个定时器 当前帧取到的数据全是0)
    renderTexture1:retain()--必须加这个 保存对象 否则下帧就被回收了 不需要了记得release

    local scheduler = cc.Director:getInstance():getScheduler()
    local function scheduleCallBack()
        --do something
        --...
        local pImage = renderTexture1:newImage()
        for j = 0, 2 do
            for i = 0, 2 do
                print(i, j)
                local ovt = pImage:getOneData(i, j, size)
                dump(ovt, "ovt = ")
            end
        end

        renderTexture1:release()--结束删除对象

        if nil ~= schedulEntry then
            scheduler:unscheduleScriptEntry(schedulEntry)
        end
    end
    if nil ~= schedulEntry then
        scheduler:unscheduleScriptEntry(schedulEntry)
    end
    schedulEntry = scheduler:scheduleScriptFunc(scheduleCallBack, 1, false)
end

return MainScene

输出

[LUA-print] resource node = nil
libpng warning: iCCP: known incorrect sRGB profile
[LUA-print] dump from: [string ".\packages/mvc/AppBase.lua"]:23: in function 'ct
or'
[LUA-print] - "AppBase configs" = {
[LUA-print] -     "defaultSceneName" = "MainScene"
[LUA-print] -     "modelsRoot" = {
[LUA-print] -         1 = "app.models"
[LUA-print] -     }
[LUA-print] -     "viewsRoot" = {
[LUA-print] -         1 = "app.views"
[LUA-print] -     }
[LUA-print] - }
[LUA-print] 111111111111111111111
[LUA-print] resource node = nil
[LUA-print] 0   0
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:33: in function <[st
ring ".\app/views/MainScene.lua"]:25>
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 255
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 255
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 1   0
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:33: in function <[st
ring ".\app/views/MainScene.lua"]:25>
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 254
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 254
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 2   0
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:33: in function <[st
ring ".\app/views/MainScene.lua"]:25>
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 255
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 255
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 0   1
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:33: in function <[st
ring ".\app/views/MainScene.lua"]:25>
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 254
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 254
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 1   1
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:33: in function <[st
ring ".\app/views/MainScene.lua"]:25>
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 0
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 0
[LUA-print] -     "r" = 0
[LUA-print] - }
[LUA-print] 2   1
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:33: in function <[st
ring ".\app/views/MainScene.lua"]:25>
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 254
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 254
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 0   2
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:33: in function <[st
ring ".\app/views/MainScene.lua"]:25>
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 255
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 255
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 1   2
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:33: in function <[st
ring ".\app/views/MainScene.lua"]:25>
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 254
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 254
[LUA-print] -     "r" = 92
[LUA-print] - }
[LUA-print] 2   2
[LUA-print] dump from: [string ".\app/views/MainScene.lua"]:33: in function <[st
ring ".\app/views/MainScene.lua"]:25>
[LUA-print] - "ovt = " = {
[LUA-print] -     "a" = 255
[LUA-print] -     "b" = 0
[LUA-print] -     "g" = 255
[LUA-print] -     "r" = 92
[LUA-print] - }

至于为什么有些是255 有些是254 还不知道为什么
可能复制顶点中有什么对像素造成了偏差 可能是某个OPENGL参数需要设置

图片继续使用上篇文章末尾给出的原图即可
就是一张9像素 3*3的绿色图片 中间1像素镂空

你可能感兴趣的:(cocos2d-x lua 3.10 在lua层使用RenderTexture得到像素rgba数据)