CocosCreator如何實(shí)現(xiàn)劃過(guò)的位置顯示紋理
1、項(xiàng)目需求
項(xiàng)目需要有一個(gè)功能:是當(dāng)一個(gè)光點(diǎn)走過(guò)的路徑,這個(gè)路徑的位置就都亮起來(lái)的功能。
2、資料內(nèi)容
功能類似這位大神的橡皮擦功能:https://forum.cocos.org/t/2-0-8/74246

但是,項(xiàng)目的需求還要經(jīng)過(guò)的路徑周圍有模糊的外邊——也就是從中心到邊緣越來(lái)越暗。
所以對(duì)于借鑒了網(wǎng)上大神的shader例子,類似以下的示例:

在大神的肩膀上做了一些改動(dòng),來(lái)實(shí)現(xiàn)項(xiàng)目的需求。
3、項(xiàng)目示例
以下是我自己的測(cè)試項(xiàng)目的示例:

(請(qǐng)忽略這渣渣的畫質(zhì),懶得裝錄屏軟件了)
4、項(xiàng)目代碼
SliderPointLight.ts
const {
ccclass,
property
} = cc._decorator;
@ccclass
export default class Follow_spot extends cc.Component {
@property(cc.Node)
bg: cc.Node = null;
material: cc.Material = null;
center: number[] = [0.5, 0.5];
testArr: number[] = [];
onLoad() {
this.material = this.bg.getComponent(cc.Sprite).getMaterial(0);
this.material.setProperty('wh_ratio', this.bg.width / this.bg.height);
this.material.setProperty('center', this.center);
//js 中最重要是這一句,這里參數(shù)是數(shù)組長(zhǎng)度*數(shù)組里向量的維度
this.material.setProperty('colorArr', new Float32Array(400));
//這里設(shè)置的時(shí)候需要把數(shù)組里向量的分量展開到一個(gè)一維數(shù)組
this.material.setProperty('colorArr', []);
this.bg.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveEvent, this);
}
touchMoveEvent(evt: cc.Event.EventTouch) {
this.center[0] = evt.getLocation().x / this.bg.width;
this.center[1] = 1 - evt.getLocation().y / this.bg.height;
console.log(this.center);
this.material.setProperty('center', this.center);
if (this.testArr.length >= 400) {
this.testArr.shift();
this.testArr.shift();
}
this.testArr.push(this.center[0]);
this.testArr.push(this.center[1]);
//js 中最重要是這一句,這里參數(shù)是數(shù)組長(zhǎng)度*數(shù)組里向量的維度
this.material.setProperty('colorArr', new Float32Array(this.testArr));
//這里設(shè)置的時(shí)候需要把數(shù)組里向量的分量展開到一個(gè)一維數(shù)組
this.material.setProperty('colorArr', this.testArr);
}
}
SliderPointLight.effect
CCEffect % {
techniques:
-passes:
-vert: vs
frag: fs
blendState:
targets:
-blend: true
rasterizerState:
cullMode: none
properties:
texture: {
value: white
}
wh_ratio: {
value: 1.78,
editor: {
tooltip: "寬高比"
}
}
blur: {
value: 0.35,
editor: {
tooltip: "光圈模糊程度"
}
}
radius: {
value: 0.5,
editor: {
tooltip: "光圈半徑"
}
}
center: {
value: [0.5, 0.5],
editor: {
tooltip: "光圈起點(diǎn)"
}
}
colorArr: {
value: [0.5, 0.5, 0.5, 0.5]
}
} %
CCProgram vs % {
precision highp float;
#include <cc-global>
#include <cc-local>
in vec3 a_position;
in vec4 a_color;
out vec4 v_color;
#if USE_TEXTURE
in vec2 a_uv0;
out vec2 v_uv0;
#endif
void main() {
vec4 pos = vec4(a_position, 1);
#if CC_USE_MODEL
pos = cc_matViewProj * cc_matWorld * pos;
#else
pos = cc_matViewProj * pos;
#endif
#if USE_TEXTURE
v_uv0 = a_uv0;
#endif
v_color = a_color;
gl_Position = pos;
}
} %
CCProgram fs % {
precision highp float;
#include <alpha-test>
in vec4 v_color;
#if USE_TEXTURE
in vec2 v_uv0;
uniform sampler2D texture;
#endif
uniform ARGS {
float radius;
float blur;
vec2 center;
float wh_ratio;
};
//effect定義
uniform Metaball {
vec4 colorArr[100];
};
void main() {
vec4 o = vec4(1, 1, 1, 0);
o *= texture(texture, v_uv0);
o *= v_color;
float circle = radius * radius;
for (int i = 0; i < 100; i++) {
float colorX = colorArr[i].x;
float colorY = colorArr[i].y;
float rx = colorX * wh_ratio;
float ry = colorY;
float dis = (v_uv0.x * wh_ratio - rx) * (v_uv0.x * wh_ratio - rx) + (v_uv0.y - ry) * (v_uv0.y - ry);
o.a = smoothstep(circle, circle - blur, dis) + o.a;
}
gl_FragColor = o;
}
}%
以上就是CocosCreator如何實(shí)現(xiàn)劃過(guò)的位置顯示紋理的詳細(xì)內(nèi)容,更多關(guān)于CocosCreator資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 詳解cocoscreater預(yù)制體prefab
- 如何在CocosCreator中利用常駐節(jié)點(diǎn)做圖層管理
- 游戲開發(fā)中如何使用CocosCreator進(jìn)行音效處理
- CocosCreator ScrollView優(yōu)化系列之分幀加載
- 詳解CocosCreator項(xiàng)目結(jié)構(gòu)機(jī)制
- 如何使用CocosCreator對(duì)象池
- 整理CocosCreator常用知識(shí)點(diǎn)
- 全面講解CocosCreator熱更新
- CocosCreator經(jīng)典入門項(xiàng)目之flappybird
- CocosCreator通用框架設(shè)計(jì)之網(wǎng)絡(luò)
- 如何用CocosCreator實(shí)現(xiàn)射擊小游戲
- 怎樣在CocosCreator中使用游戲手柄
相關(guān)文章
JS如何調(diào)用WebAssembly編譯出來(lái)的.wasm文件
這篇文章主要介紹了關(guān)于WebAssembly編譯出來(lái)的.wasm文件js如何調(diào)用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
layui異步加載table表中某一列數(shù)據(jù)的例子
今天小編就為大家分享一篇layui異步加載table表中某一列數(shù)據(jù)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
在vscode上直接運(yùn)行typescript的操作方法
在學(xué)習(xí)typescript的過(guò)程中發(fā)現(xiàn)在vscode上不能很好地的輸出typescript的運(yùn)行結(jié)果,需要先將typescript編譯為javascript,在通過(guò)node執(zhí)行js文件得到結(jié)果,這篇文章給大家介紹如何在vscode上直接運(yùn)行typescript,感興趣的朋友一起看看吧2023-12-12
利用canvas判斷點(diǎn)與封閉圖形的包含關(guān)系
今天在寫代碼的時(shí)候遇到一個(gè)場(chǎng)景,在一個(gè)封閉圖形頂點(diǎn)已知的情況下判斷點(diǎn)擊時(shí)是否點(diǎn)擊在圖形內(nèi)部,本文給大家介紹了如何利用canvas判斷點(diǎn)與封閉圖形的包含關(guān)系,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-04-04
javascript while語(yǔ)句和do while語(yǔ)句的區(qū)別分析
這篇文章通過(guò)實(shí)例代碼較詳細(xì)的給大家介紹了javascript while語(yǔ)句和do while語(yǔ)句的區(qū)別,感興趣的朋友一起看看吧2007-12-12

