Commit 7f038a34 authored by 范雪寒's avatar 范雪寒

feat: 透明像素check方法支持原生环境

parent a2bc518b
......@@ -2,7 +2,9 @@ import {
RandomInt
} from "../script/utils";
// 参考: https://forum.cocos.org/t/creator/38530/56
// 参考:
// https://forum.cocos.org/t/creator/38530/56
// https://forum.cocos.org/t/topic/116551
cc.Class({
extends: cc.Component,
......@@ -44,33 +46,49 @@ cc.Class({
return false;
}
if (!this.camera) {
const checkCameraNode = cc.find('Canvas/CheckCamera');
if (!checkCameraNode) {
throw '找不到[Canvas/CheckCamera]。';
}
this.camera = checkCameraNode.getComponent(cc.Camera);
}
if (this.camera.cullingMask != -2) {
console.warn(`[${this.camera.cullingMask}]: 检查CheckCamera的cullingMask值,应该只选择camera`);
}
this.camera.node.active = true;
let posInRect = cc.v2(
parseInt(posInNode.x - offset.x + (rect.width * sprite.anchorX)),
parseInt(posInNode.y - offset.y + (rect.height * sprite.anchorY))
parseInt(posInNode.x - offset.x + rect.width * sprite.anchorX),
parseInt(posInNode.y - offset.y + rect.height * sprite.anchorY)
);
let tex = spriteFrame.getTexture();
this.camera.enable = true;
let rt;
let destory = false;
if (tex instanceof cc.RenderTexture) {
throw "图片宽高都小于512的时候,图片资源的packable属性要设置为false。";
}
var scale = 8;
var cvs = this._textureIdMapDataContainer[tex.getId()];
if (!cvs) {
cvs = document.createElement("canvas");
var ctx = cvs.getContext('2d');
cvs.width = tex.width;
cvs.height = tex.height;
ctx.drawImage(tex.getHtmlElementObj(), 0, 0, tex.width, tex.height, 0, 0, tex.width / scale, tex.height / scale);
this._textureIdMapDataContainer[tex.getId()] = cvs;
rt = tex;
} else {
rt = new cc.RenderTexture()
rt.initWithSize(tex.width, tex.height, cc.gfx.RB_FMT_S8);
rt.drawTextureAt(tex, 0, 0);
destory = true;
}
// data就是这个texture的rgba值数组
this.camera.targetTexture = rt;
this.camera.render(undefined);
let data;
var ctx = cvs.getContext('2d');
//图集里的图片可能会旋转
if (spriteFrame.isRotated()) {
data = ctx.getImageData((rect.x + posInRect.y) / scale, (rect.y + posInRect.x) / scale, 1, 1).data;
data = rt.readPixels(null, rect.x + posInRect.y, rect.y + posInRect.x, 1, 1);
} else {
data = ctx.getImageData((rect.x + posInRect.x) / scale, (rect.y + rect.height - posInRect.y) / scale, 1, 1).data;
data = rt.readPixels(null, rect.x + posInRect.x, rect.y + rect.height - posInRect.y, 1, 1);
}
this.camera.targetTexture = null;
this.camera.enable = false;
this.camera.node.active = false;
if (destory) {
rt.destroy();
}
if (data[3] > 0) {
......
{
"ver": "2.3.5",
"uuid": "47b37aca-fd01-47f4-806b-0ff0db60ab63",
"uuid": "2b5f48f2-ec1c-4557-bbe5-4844d164689e",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"genMipmaps": false,
"packable": true,
"width": 310,
"height": 389,
"width": 217,
"height": 180,
"platformSettings": {},
"subMetas": {
"QQ图片20211002174831": {
"QQ20211008-115534": {
"ver": "1.0.4",
"uuid": "5a6e7e46-3e7f-4f3f-a75a-731f1d221be7",
"rawTextureUuid": "47b37aca-fd01-47f4-806b-0ff0db60ab63",
"uuid": "40f686d0-e3e8-4898-86a5-cd61e848cab1",
"rawTextureUuid": "2b5f48f2-ec1c-4557-bbe5-4844d164689e",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
......@@ -22,10 +22,10 @@
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 310,
"height": 389,
"rawWidth": 310,
"rawHeight": 389,
"width": 217,
"height": 180,
"rawWidth": 217,
"rawHeight": 180,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
......
{
"ver": "2.3.5",
"uuid": "4eb7cf8b-6dde-4163-98ab-3a63ac62fe8c",
"type": "sprite",
"wrapMode": "clamp",
"filterMode": "bilinear",
"premultiplyAlpha": false,
"genMipmaps": false,
"packable": true,
"width": 468,
"height": 415,
"platformSettings": {},
"subMetas": {
"QQ20211008-115640": {
"ver": "1.0.4",
"uuid": "44119ee4-7023-4988-9c82-f7f734482b08",
"rawTextureUuid": "4eb7cf8b-6dde-4163-98ab-3a63ac62fe8c",
"trimType": "auto",
"trimThreshold": 1,
"rotated": false,
"offsetX": 0,
"offsetY": 0,
"trimX": 0,
"trimY": 0,
"width": 468,
"height": 415,
"rawWidth": 468,
"rawHeight": 415,
"borderTop": 0,
"borderBottom": 0,
"borderLeft": 0,
"borderRight": 0,
"subMetas": {}
}
}
}
\ No newline at end of file
......@@ -6,7 +6,7 @@
"filterMode": "bilinear",
"premultiplyAlpha": false,
"genMipmaps": false,
"packable": false,
"packable": true,
"width": 278,
"height": 275,
"platformSettings": {},
......
......@@ -2,5 +2,44 @@
"last-module-event-record-time": 1600327709742,
"migrate-history": [
"cloud-function"
]
],
"group-list": [
"default",
"camera"
],
"collision-matrix": [
[
true
],
[
false,
false
]
],
"excluded-modules": [
"3D Physics/Builtin"
],
"preview-port": 7456,
"design-resolution-width": 960,
"design-resolution-height": 640,
"fit-width": false,
"fit-height": true,
"use-project-simulator-setting": false,
"simulator-orientation": false,
"use-customize-simulator": true,
"simulator-resolution": {
"width": 960,
"height": 640
},
"clear-simulator-cache": true,
"facebook": {
"enable": false,
"appID": "",
"live": {
"enable": false
},
"audience": {
"enable": false
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment