/
skinned-mesh-renderer.ts
152 lines (129 loc) · 4.83 KB
/
skinned-mesh-renderer.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
/*
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
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.
*/
/**
* @packageDocumentation
* @module model
*/
import { AnimationClip } from '../../animation/animation-clip';
import { Material } from '../../assets';
import { Skeleton } from '../../assets/skeleton';
import { ccclass, executeInEditMode, executionOrder, help, menu, tooltip, type } from 'cc.decorator';
import { models } from '../../renderer';
import { Node } from '../../scene-graph/node';
import { MeshRenderer } from './mesh-renderer';
import { SkeletalAnimation } from '../../animation/skeletal-animation';
import { legacyCC } from '../../global-exports';
/**
* @en The skinned mesh renderer component.
* @zh 蒙皮网格渲染器组件。
*/
@ccclass('cc.SkinnedMeshRenderer')
@help('i18n:cc.SkinnedMeshRenderer')
@executionOrder(100)
@executeInEditMode
@menu('Components/SkinnedMeshRenderer')
export class SkinnedMeshRenderer extends MeshRenderer {
@type(Skeleton)
protected _skeleton: Skeleton | null = null;
@type(Node)
protected _skinningRoot: Node | null = null;
protected _clip: AnimationClip | null = null;
/**
* @en The skeleton asset.
* @zh 骨骼资源。
*/
@type(Skeleton)
get skeleton () {
return this._skeleton;
}
set skeleton (val) {
if (val === this._skeleton) { return; }
this._skeleton = val;
this._update();
}
/**
* @en The skinning root. (The node where the controlling Animation is located)
* 骨骼根节点的引用,对应控制此模型的动画组件所在节点。
*/
@type(Node)
@tooltip('i18n:model.skinning_root')
get skinningRoot () {
return this._skinningRoot;
}
set skinningRoot (value) {
if (value === this._skinningRoot) { return; }
this._skinningRoot = value;
this._updateModelType();
this._update();
}
get model () {
return this._model as models.SkinningModel | models.BakedSkinningModel | null;
}
constructor () {
super();
this._modelType = models.BakedSkinningModel;
}
public __preload () {
this._updateModelType();
}
public uploadAnimation (clip: AnimationClip | null) {
this._clip = clip;
if (this.model && this.model.uploadAnimation) {
this.model.uploadAnimation(clip);
}
}
public setUseBakedAnimation (val = true) {
const modelType = val ? models.BakedSkinningModel : models.SkinningModel;
if (this._modelType === modelType) { return; }
this._modelType = modelType;
if (this._model) {
legacyCC.director.root.destroyModel(this._model);
this._model = null;
this._models.length = 0;
this._updateModels();
this._updateCastShadow();
if (this.enabledInHierarchy) {
this._attachToScene();
}
}
}
public setMaterial (material: Material | null, index: number) {
super.setMaterial(material, index);
if (this._modelType === models.SkinningModel) {
this.getMaterialInstance(index);
}
}
protected _updateModelParams () {
this._update(); // should bind skeleton before super create pso
super._updateModelParams();
}
private _updateModelType () {
if (!this._skinningRoot) { return; }
const comp = this._skinningRoot.getComponent('cc.SkeletalAnimation') as SkeletalAnimation;
if (comp) { this.setUseBakedAnimation(comp.useBakedAnimation); }
}
private _update () {
if (this.model) {
this.model.bindSkeleton(this._skeleton, this._skinningRoot, this._mesh);
if (this.model.uploadAnimation) { this.model.uploadAnimation(this._clip); }
}
}
}