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

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

parent a2bc518b
......@@ -72,31 +72,43 @@
"__id__": 3
},
{
"__id__": 5
"__id__": 6
},
{
"__id__": 7
"__id__": 8
},
{
"__id__": 9
"__id__": 10
},
{
"__id__": 11
"__id__": 12
},
{
"__id__": 13
"__id__": 14
},
{
"__id__": 16
},
{
"__id__": 18
},
{
"__id__": 20
},
{
"__id__": 22
}
],
"_active": true,
"_components": [
{
"__id__": 15
"__id__": 24
},
{
"__id__": 16
"__id__": 25
},
{
"__id__": 17
"__id__": 26
}
],
"_prefab": null,
......@@ -149,7 +161,7 @@
},
{
"__type__": "cc.Node",
"_name": "Main Camera",
"_name": "bg",
"_objFlags": 0,
"_parent": {
"__id__": 2
......@@ -159,6 +171,130 @@
"_components": [
{
"__id__": 4
},
{
"__id__": 5
}
],
"_prefab": null,
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 20,
"g": 0,
"b": 0,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 1280,
"height": 720
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
0,
0,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": "42MpxSgJpOzrTNphxUdFR6"
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 3
},
"_enabled": true,
"_materials": [
{
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_spriteFrame": {
"__uuid__": "a23235d1-15db-4b95-8439-a2e005bfff91"
},
"_type": 0,
"_sizeMode": 0,
"_fillType": 0,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_atlas": null,
"_id": "7f6ikn5vVHrpiLRWbp0+v2"
},
{
"__type__": "cc.Widget",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 3
},
"_enabled": true,
"alignMode": 1,
"_target": null,
"_alignFlags": 45,
"_left": 0,
"_right": 0,
"_top": 0,
"_bottom": 0,
"_verticalCenter": 0,
"_horizontalCenter": 0,
"_isAbsLeft": true,
"_isAbsRight": true,
"_isAbsTop": true,
"_isAbsBottom": true,
"_isAbsHorizontalCenter": true,
"_isAbsVerticalCenter": true,
"_originalWidth": 100,
"_originalHeight": 100,
"_id": "7a02y8sHxAMZGqC67lTFto"
},
{
"__type__": "cc.Node",
"_name": "Main Camera",
"_objFlags": 0,
"_parent": {
"__id__": 2
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 7
}
],
"_prefab": null,
......@@ -214,11 +350,11 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 3
"__id__": 6
},
"_enabled": true,
"_cullingMask": 4294967295,
"_clearFlags": 7,
"_clearFlags": 6,
"_backgroundColor": {
"__type__": "cc.Color",
"r": 0,
......@@ -256,7 +392,7 @@
"_active": true,
"_components": [
{
"__id__": 6
"__id__": 9
}
],
"_prefab": null,
......@@ -282,8 +418,8 @@
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
142,
-115,
-160.263,
-106.552,
0,
0,
0,
......@@ -312,7 +448,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 5
"__id__": 8
},
"_enabled": true,
"_materials": [
......@@ -350,7 +486,7 @@
"_active": true,
"_components": [
{
"__id__": 8
"__id__": 11
}
],
"_prefab": null,
......@@ -376,8 +512,8 @@
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
282,
-266,
-18,
-254,
0,
0,
0,
......@@ -406,7 +542,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 7
"__id__": 10
},
"_enabled": true,
"_materials": [
......@@ -445,7 +581,7 @@
"_active": true,
"_components": [
{
"__id__": 10
"__id__": 13
}
],
"_prefab": null,
......@@ -464,22 +600,22 @@
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
"x": 0.7,
"y": 0.7
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
-256,
186,
-426,
159,
0,
0,
0,
0,
1,
1,
1,
0.5,
0.5,
1
]
},
......@@ -501,7 +637,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 9
"__id__": 12
},
"_enabled": true,
"_materials": [
......@@ -539,7 +675,7 @@
"_active": true,
"_components": [
{
"__id__": 12
"__id__": 15
}
],
"_prefab": null,
......@@ -553,8 +689,8 @@
},
"_contentSize": {
"__type__": "cc.Size",
"width": 506.74,
"height": 130.4
"width": 386.74,
"height": 90.4
},
"_anchorPoint": {
"__type__": "cc.Vec2",
......@@ -565,8 +701,8 @@
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
-344,
-10,
-438,
7,
0,
0,
0,
......@@ -595,7 +731,7 @@
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 11
"__id__": 14
},
"_enabled": true,
"_materials": [
......@@ -605,8 +741,8 @@
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_string": "图片宽高都小于512的时候,\n图片资源的packable属性\n要设置为false",
"_N$string": "图片宽高都小于512的时候,\n图片资源的packable属性\n要设置为false",
"_string": "图片宽高都小于512时\n依然好用",
"_N$string": "图片宽高都小于512时\n依然好用",
"_fontSize": 40,
"_lineHeight": 40,
"_enableWrapText": true,
......@@ -625,7 +761,105 @@
},
{
"__type__": "cc.Node",
"_name": "QQ图片20211002174831",
"_name": "CheckCamera",
"_objFlags": 0,
"_parent": {
"__id__": 2
},
"_children": [],
"_active": false,
"_components": [
{
"__id__": 17
}
],
"_prefab": null,
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 1280,
"height": 720
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
0,
0,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": "28XkvTciZAMrWvfIWss6/f"
},
{
"__type__": "cc.Camera",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 16
},
"_enabled": true,
"_cullingMask": -2,
"_clearFlags": 6,
"_backgroundColor": {
"__type__": "cc.Color",
"r": 0,
"g": 0,
"b": 0,
"a": 255
},
"_depth": 0,
"_zoomRatio": 1,
"_targetTexture": null,
"_fov": 60,
"_orthoSize": 10,
"_nearClip": 1,
"_farClip": 4096,
"_ortho": true,
"_rect": {
"__type__": "cc.Rect",
"x": 0,
"y": 0,
"width": 1,
"height": 1
},
"_renderStages": 1,
"_alignWithScreen": true,
"_id": "b6tb/Y0edEZaYhZI2GFROB"
},
{
"__type__": "cc.Node",
"_name": "QQ20211008-115534",
"_objFlags": 0,
"_parent": {
"__id__": 2
......@@ -634,7 +868,7 @@
"_active": true,
"_components": [
{
"__id__": 14
"__id__": 19
}
],
"_prefab": null,
......@@ -648,8 +882,8 @@
},
"_contentSize": {
"__type__": "cc.Size",
"width": 310,
"height": 389
"width": 217,
"height": 180
},
"_anchorPoint": {
"__type__": "cc.Vec2",
......@@ -660,8 +894,102 @@
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
-516,
209,
513,
261,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": "0c6070aLhIRJBsOaoVbC72"
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 18
},
"_enabled": true,
"_materials": [
{
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_spriteFrame": {
"__uuid__": "40f686d0-e3e8-4898-86a5-cd61e848cab1"
},
"_type": 0,
"_sizeMode": 1,
"_fillType": 0,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_atlas": null,
"_id": "2fxKsm+iJDFLuQtQqPNh+N"
},
{
"__type__": "cc.Node",
"_name": "QQ20211008-115640",
"_objFlags": 0,
"_parent": {
"__id__": 2
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 21
}
],
"_prefab": null,
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 468,
"height": 415
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
457,
14,
0,
0,
0,
......@@ -669,7 +997,7 @@
1,
0.7,
0.7,
1
0.7
]
},
"_eulerAngles": {
......@@ -683,14 +1011,14 @@
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": "3b/6IPXGRJyKw+ljrx/W2g"
"_id": "a1IRfzc9lJ2qcRXnpqE3Qn"
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 13
"__id__": 20
},
"_enabled": true,
"_materials": [
......@@ -701,7 +1029,7 @@
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_spriteFrame": {
"__uuid__": "5a6e7e46-3e7f-4f3f-a75a-731f1d221be7"
"__uuid__": "44119ee4-7023-4988-9c82-f7f734482b08"
},
"_type": 0,
"_sizeMode": 1,
......@@ -715,7 +1043,102 @@
"_fillRange": 0,
"_isTrimmedMode": true,
"_atlas": null,
"_id": "5a2PpVUkNG1JxLJK5RLySE"
"_id": "06wGhnEDhBYqgei1D+F6hg"
},
{
"__type__": "cc.Node",
"_name": "New Label",
"_objFlags": 0,
"_parent": {
"__id__": 2
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 23
}
],
"_prefab": null,
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 255,
"g": 255,
"b": 255,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 360,
"height": 81.36
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
472.703,
-177.03,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": "bcw+mcVqhEIKR+snm9rTlu"
},
{
"__type__": "cc.Label",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 22
},
"_enabled": true,
"_materials": [
{
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_string": "需要增加一个摄像机,\n以及设置节点分组。",
"_N$string": "需要增加一个摄像机,\n以及设置节点分组。",
"_fontSize": 36,
"_lineHeight": 36,
"_enableWrapText": true,
"_N$file": null,
"_isSystemFontUsed": true,
"_spacingX": 0,
"_batchAsBitmap": false,
"_styleFlags": 0,
"_underlineHeight": 0,
"_N$horizontalAlign": 1,
"_N$verticalAlign": 1,
"_N$fontFamily": "Arial",
"_N$overflow": 0,
"_N$cacheMode": 0,
"_id": "12KWYGmP1IvJepa4eo510F"
},
{
"__type__": "cc.Canvas",
......
......@@ -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