1、項目需求
項目需要有一個功能:是當一個光點走過的路徑,這個路徑的位置就都亮起來的功能。
2、資料內容
功能類似這位大神的橡皮擦功能:https://forum.cocos.org/t/2-0-8/74246
但是,項目的需求還要經過的路徑周圍有模糊的外邊——也就是從中心到邊緣越來越暗。
所以對于借鑒了網上大神的shader例子,類似以下的示例:
在大神的肩膀上做了一些改動,來實現項目的需求。
3、項目示例
以下是我自己的測試項目的示例:
(請忽略這渣渣的畫質,懶得裝錄屏軟件了)
4、項目代碼
SliderPointLight.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
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 中最重要是這一句,這里參數是數組長度*數組里向量的維度 this .material.setProperty( 'colorArr' , new Float32Array(400)); //這里設置的時候需要把數組里向量的分量展開到一個一維數組 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 中最重要是這一句,這里參數是數組長度*數組里向量的維度 this .material.setProperty( 'colorArr' , new Float32Array( this .testArr)); //這里設置的時候需要把數組里向量的分量展開到一個一維數組 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: "光圈起點" } } 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如何實現劃過的位置顯示紋理的詳細內容,更多關于CocosCreator資料請關注服務器之家其它相關文章!
原文鏈接:https://blog.csdn.net/qq_21743659/article/details/108235023