/
Material.ts
242 lines (189 loc) · 6.45 KB
/
Material.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
import { StorageGPUBuffer } from "../gfx/graphics/webGpu/core/buffer/StorageGPUBuffer";
import { UniformGPUBuffer } from "../gfx/graphics/webGpu/core/buffer/UniformGPUBuffer";
import { Texture } from "../gfx/graphics/webGpu/core/texture/Texture";
import { RenderShaderPass } from "../gfx/graphics/webGpu/shader/RenderShaderPass";
import { PassType } from "../gfx/renderJob/passRenderer/state/RendererType";
import { Shader } from "../gfx/graphics/webGpu/shader/Shader";
import { Color } from "../math/Color";
import { Vector2 } from "../math/Vector2";
import { Vector3 } from "../math/Vector3";
import { Vector4 } from "../math/Vector4";
import { BlendMode } from "./BlendMode";
export class Material {
/**
*
* Material Unique Identifier
*/
public instanceID: string;
/**
*
* name of this material
*/
public name: string;
public enable: boolean = true;
private _defaultSubShader: RenderShaderPass;
protected _shader: Shader;
constructor() {
}
public set shader(shader: Shader) {
this._shader = shader;
this._defaultSubShader = shader.getDefaultShaders()[0];
}
public get shader(): Shader {
return this._shader;
}
public get doubleSide(): boolean {
return this._defaultSubShader.doubleSide;
}
public set doubleSide(value: boolean) {
this._defaultSubShader.doubleSide = value;
}
public get castShadow(): boolean {
return this._defaultSubShader.shaderState.castShadow;
}
public set castShadow(value: boolean) {
let shaderState = this._defaultSubShader.shaderState;
if (value != shaderState.castShadow) {
shaderState.castShadow = value;
}
}
public get acceptShadow(): boolean {
return this._defaultSubShader.shaderState.acceptShadow;
}
public set acceptShadow(value: boolean) {
let shaderState = this._defaultSubShader.shaderState;
if (shaderState.acceptShadow != value) {
shaderState.acceptShadow = value;
this._defaultSubShader.noticeShaderChange();
this._defaultSubShader.noticeValueChange();
}
}
public get blendMode(): BlendMode {
return this._defaultSubShader.blendMode;
}
public set blendMode(value: BlendMode) {
this._defaultSubShader.blendMode = value;
}
public get depthCompare(): GPUCompareFunction {
return this._defaultSubShader.depthCompare;
}
public set depthCompare(value: GPUCompareFunction) {
this._defaultSubShader.depthCompare = value;
}
public get transparent(): boolean {
return this._defaultSubShader.shaderState.transparent;
}
public set transparent(value: boolean) {
this._defaultSubShader.shaderState.transparent = value;
if (value) {
this._defaultSubShader.renderOrder = 3000;
}
}
public get cullMode(): GPUCullMode {
return this._defaultSubShader.cullMode;
}
public set cullMode(value: GPUCullMode) {
if (this._defaultSubShader.cullMode != value) {
for (let list of this._shader.passShader.values()) {
for (let pass of list) {
pass.cullMode = value;
}
}
this._defaultSubShader.cullMode = value;
}
}
public get depthWriteEnabled(): boolean {
return this._defaultSubShader.depthWriteEnabled;
}
public set depthWriteEnabled(value: boolean) {
this._defaultSubShader.depthWriteEnabled = value;
}
public set useBillboard(value: boolean) {
this._defaultSubShader.setDefine("USE_BILLBOARD", value);
}
/**
* get render pass by renderType
* @param passType
* @returns
*/
public getPass(passType: PassType) {
return this._shader.getSubShaders(passType);
}
/**
* get all color render pass
* @returns
*/
public getAllPass(): RenderShaderPass[] {
return this._shader.getSubShaders(PassType.COLOR);
}
/**
* clone one material
* @returns Material
*/
public clone() {
let newMat = new Material();
newMat.shader = this.shader.clone();
return newMat;
}
destroy(force: boolean) {
this._shader.destroy();
this._shader = null;
}
public setDefine(define: string, value: boolean) {
this.shader.setDefine(define, value);
}
public setTexture(propertyName: string, texture: Texture) {
this._shader.setTexture(propertyName, texture);
}
public setStorageBuffer(propertyName: string, buffer: StorageGPUBuffer) {
this._shader.setStorageBuffer(propertyName, buffer);
}
public setUniformBuffer(propertyName: string, buffer: UniformGPUBuffer) {
this._shader.setStorageBuffer(propertyName, buffer);
}
public setUniformFloat(propertyName: string, value: number) {
this._shader.setUniformFloat(propertyName, value);
}
public setUniformVector2(propertyName: string, value: Vector2) {
this._shader.setUniformVector2(propertyName, value);
}
public setUniformVector3(propertyName: string, value: Vector3) {
this._shader.setUniformVector3(propertyName, value);
}
public setUniformVector4(propertyName: string, value: Vector4) {
this._shader.setUniformVector4(propertyName, value);
}
public setUniformColor(propertyName: string, value: Color) {
this._shader.setUniformColor(propertyName, value);
}
public getUniformFloat(str: string) {
return this._shader.getUniform(str).data;
}
public getUniformV2(str: string): Vector2 {
return this._shader.getUniformVector2(str);
}
public getUniformV3(str: string): Vector3 {
return this._shader.getUniformVector3(str);
}
public getUniformV4(str: string): Vector4 {
return this._shader.getUniformVector4(str);
}
public getUniformColor(str: string) {
return this._shader.getUniformColor(str);
}
public getTexture(str: string) {
return this._shader.getTexture(str);
}
public getStorageBuffer(str: string) {
return this._shader.getStorageBuffer(str);
}
public getStructStorageBuffer(str: string) {
return this._shader.getStructStorageBuffer(str);
}
public getUniformBuffer(str: string) {
return this._shader.getUniformBuffer(str);
}
public applyUniform() {
this._shader.applyUniform();
}
}