forked from cocos/cocos4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline.ts
More file actions
320 lines (279 loc) · 9.01 KB
/
Copy pathline.ts
File metadata and controls
320 lines (279 loc) · 9.01 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { ccclass, help, executeInEditMode, menu, tooltip, displayOrder, type, serializable,
range, visible, override, displayName } from 'cc.decorator';
import { Material, Texture2D } from '../asset/assets';
import { Vec3, cclegacy, Vec4, Vec2, CCBoolean, Color } from '../core';
import { LineModel } from './models/line-model';
import { builtinResMgr } from '../asset/asset-manager';
import CurveRange from './animator/curve-range';
import GradientRange from './animator/gradient-range';
import { ModelRenderer } from '../misc';
const CC_USE_WORLD_SPACE = 'CC_USE_WORLD_SPACE';
const CC_USE_WORLD_SCALE = 'CC_USE_WORLD_SCALE';
const define = { CC_USE_WORLD_SPACE: false, CC_USE_WORLD_SCALE: true };
@ccclass('cc.Line')
@help('i18n:cc.Line')
@menu('Effects/Line')
@executeInEditMode
export class Line extends ModelRenderer {
@type(Texture2D)
private _texture = null;
/**
* @zh 显示的纹理。
* @en Texture used.
*/
@type(Texture2D)
@displayOrder(0)
@tooltip('i18n:line.texture')
get texture (): null {
return this._texture;
}
set texture (val) {
this._texture = val;
if (this.material) {
this.material.setProperty('mainTexture', val);
}
}
@serializable
private _material: Material | null = null;
@type(Material)
@displayOrder(1)
@tooltip('i18n:line.material')
@displayName('Material')
get lineMaterial (): Material | null {
return this.getSharedMaterial(0);
}
set lineMaterial (val) {
this.setSharedMaterial(val, 0);
}
@override
@visible(false)
@serializable
get sharedMaterials (): (Material | null)[] {
return super.sharedMaterials;
}
set sharedMaterials (val) {
super.sharedMaterials = val;
}
@serializable
private _worldSpace = false;
/**
* @zh positions是否为世界空间坐标。
* @en Whether positions are world space coordinates.
*/
@displayOrder(1)
@tooltip('i18n:line.worldSpace')
get worldSpace (): boolean {
return this._worldSpace;
}
set worldSpace (val) {
this._worldSpace = val;
const matIns = this.getMaterialInstance(0);
if (matIns) {
define[CC_USE_WORLD_SPACE] = this.worldSpace;
matIns.recompileShaders(define);
if (this._models[0]) {
this._models[0].setSubModelMaterial(0, matIns);
}
}
}
@type([Vec3])
private _positions: Vec3[] = [];
/**
* @en Inflection point positions of each polyline.
* @zh 每段折线的拐点坐标。
*/
@type([Vec3])
@displayOrder(2)
@tooltip('i18n:line.positions')
get positions (): Vec3[] {
return this._positions;
}
set positions (val) {
this._positions = val;
if (this._models[0]) {
const lineModel = this._models[0] as LineModel;
lineModel.addLineVertexData(this._positions, this.width, this.color);
}
}
/**
* @zh 线段的宽度。
* @en Width of this line.
*/
@type(CurveRange)
@range([0, 1])
@displayOrder(3)
@tooltip('i18n:line.width')
get width (): CurveRange {
return this._width;
}
set width (val) {
this._width = val;
if (this._models[0]) {
const lineModel = this._models[0] as LineModel;
lineModel.addLineVertexData(this._positions, this._width, this._color);
}
}
@serializable
private _width = new CurveRange();
/**
* @zh 线段颜色。
* @en Color of this line.
*/
@type(GradientRange)
@displayOrder(6)
@tooltip('i18n:line.color')
get color (): GradientRange {
return this._color;
}
set color (val) {
this._color = val;
if (this._models[0]) {
const lineModel = this._models[0] as LineModel;
lineModel.addLineVertexData(this._positions, this._width, this._color);
}
}
@serializable
private _color = new GradientRange();
@serializable
private _tile = new Vec2(1, 1);
private _tile_offset: Vec4 = new Vec4();
/**
* @zh 图块数。
* @en Texture tile count.
*/
@type(Vec2)
@displayOrder(4)
@tooltip('i18n:line.tile')
get tile (): Vec2 {
return this._tile;
}
set tile (val) {
this._tile.set(val);
if (this.material) {
this._tile_offset.x = this._tile.x;
this._tile_offset.y = this._tile.y;
this.material.setProperty('mainTiling_Offset', this._tile_offset);
}
}
@serializable
private _offset = new Vec2(0, 0);
@type(Vec2)
@displayOrder(5)
@tooltip('i18n:line.offset')
get offset (): Vec2 {
return this._offset;
}
set offset (val) {
this._offset.set(val);
if (this.material) {
this._tile_offset.z = this._offset.x;
this._tile_offset.w = this._offset.y;
this.material.setProperty('mainTiling_Offset', this._tile_offset);
}
}
constructor () {
super();
}
public onLoad (): void {
const model = cclegacy.director.root.createModel(LineModel) as LineModel;
if (this._models.length === 0) {
this._models.push(model);
} else {
this._models[0] = model;
}
model.node = model.transform = this.node;
if (this._material) {
this.lineMaterial = this._material;
this._material = null;
}
if (this.lineMaterial === null) {
const mat = builtinResMgr.get<Material>('default-trail-material');
this.material = mat;
}
const matIns = this.getMaterialInstance(0);
if (matIns) {
define[CC_USE_WORLD_SPACE] = this.worldSpace;
matIns.recompileShaders(define);
model.updateMaterial(matIns);
}
model.setCapacity(100);
}
public onEnable (): void {
super.onEnable();
if (this._models.length === 0 || !this._models[0]) {
return;
}
this._attachToScene();
this.texture = this._texture;
this.tile = this._tile;
this.offset = this._offset;
const lineModel = this._models[0] as LineModel;
lineModel.addLineVertexData(this._positions, this.width, this.color);
}
public onDisable (): void {
if (this._models.length > 0 && this._models[0]) {
this._detachFromScene();
}
}
protected _attachToScene (): void {
super._attachToScene();
if (this._models.length > 0 && this._models[0] && this.node && this.node.scene) {
const lineModel = this._models[0];
if (lineModel.scene) {
this._detachFromScene();
}
this._getRenderScene().addModel(lineModel);
}
}
/**
* @engineInternal
*/
public _detachFromScene (): void {
super._detachFromScene();
if (this._models.length > 0 && this._models[0]) {
const lineModel = this._models[0];
if (lineModel.scene) {
lineModel.scene.removeModel(lineModel);
}
}
}
public updateColor (): void {
if (this._color) {
if (this._models[0]) {
const lineModel = this._models[0] as LineModel;
lineModel.addLineVertexData(this._positions, this._width, this._color);
}
}
}
protected _onMaterialModified (index: number, material: Material | null): void {
super._onMaterialModified(index, material);
const matIns = this.getMaterialInstance(0);
if (matIns) {
define[CC_USE_WORLD_SPACE] = this.worldSpace;
matIns.recompileShaders(define);
if (this._models[0]) {
const lineModel = this._models[0] as LineModel;
lineModel.updateMaterial(matIns);
}
}
}
}