天天看點

creator節點截圖

creator節點截圖:

public static captureSprite(node : cc.Node , file : string , callBack ?: Function , target ?: any)
    {
        let bResult = true;
        let fullPath = "";
        let width = cc.winSize.width;
        let height = cc.winSize.height;
        
        // 以下width, height變量為局部截圖的寬高
        let mainCamera : cc.Camera = cc.Camera.main;

        // 建立一個 RenderTexture,并且設定 camera 的 targetTexture 為建立的 RenderTexture,這樣 camera 的内容将會渲染到建立的 RenderTexture 中
        let texture = new cc.RenderTexture();
        let gl = cc.game["_renderContext"];

        // 左下角為0,0擷取包圍盒 , 修正包圍盒是在螢幕内部
        let sRect = node.getBoundingBoxToWorld();
        let ldPos = cc.v2(sRect.x , sRect.y);
        let rTpos = cc.v2(sRect.x + sRect.width , sRect.y + sRect.height);
        ldPos.x = Math.max(0 , ldPos.x);
        ldPos.y = Math.max(0 , ldPos.y);
        rTpos.x = Math.min(width , rTpos.x);
        rTpos.y = Math.min(height , rTpos.y);
        sRect.x = Math.floor(ldPos.x);
        sRect.y = Math.floor(ldPos.y);
        sRect.width = Math.floor(rTpos.x - ldPos.x);
        sRect.height = Math.floor(rTpos.y - ldPos.y);

        // 渲染錄影機 如果有Mask的話 添加 gl.STENCIL_INDEX8 參數 截取全屏資料
        texture.initWithSize(width, height , gl.STENCIL_INDEX8);
        mainCamera.targetTexture = texture;
        mainCamera.render(node);
        mainCamera.targetTexture = null;

        // 這樣我們就能從 RenderTexture 中擷取到資料了
        let data : any = texture.readPixels(null , sRect.x , sRect.y , sRect.width , sRect.height);
        // 以下代碼将截圖後預設倒置的圖檔回正
        let sData = new Uint8Array(sRect.width * sRect.height * 4);
        let rBytes = sRect.width * 4;
        for (let row = 0; row < sRect.height; row++)
        {
            let srow = sRect.height - 1 - row;
            let start = Math.floor(srow * sRect.width * 4);
            let reStart = row * sRect.width * 4;
            for (let i = 0; i < rBytes; i++)
            {
                sData[reStart + i] = data[start + i];
            }
        }

        // 原生調用
        if (cc.sys.isNative && jsb)
        {
            // 執行回到
            let wPath = jsb.fileUtils.getWritablePath();
            fullPath = wPath + file;
            if (jsb.fileUtils.isDirectoryExist(wPath))
            {
                jsb.fileUtils.createDirectory(wPath);
            }
            if (jsb.fileUtils.isFileExist(fullPath))
            {
                jsb.fileUtils.removeFile(fullPath);
            }
            // 儲存圖檔
            console.log(fullPath , sRect.x , sRect.y , sRect.width , sRect.height);
            if (jsb && jsb["saveImageData"] && jsb["saveImageData"](sData, sRect.width, sRect.height, fullPath) == true)
            {
                console.log("截圖成功[" + sRect.width.toString() + "x" + sRect.height.toString() + "]");
            }
            else
            {
                bResult = false;
                console.log("截圖失敗[" + sRect.width.toString() + "x" + sRect.height.toString() + "]");
            }
        }

        // 回調函數
        if (callBack)
        {
            callBack.call(target , bResult , fullPath , sData , sRect.width , sRect.height);
        }
    }
           

繼續閱讀