-
Notifications
You must be signed in to change notification settings - Fork 4
/
SynthesizeHdrMaterialContent.ts
260 lines (250 loc) · 8.91 KB
/
SynthesizeHdrMaterialContent.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
import { AbstractTexture } from '../../textures/AbstractTexture';
import { CameraComponent } from '../../components/Camera/CameraComponent';
import { ComponentRepository } from '../../core/ComponentRepository';
import {ComponentType} from '../../definitions/ComponentType';
import {CompositionType} from '../../definitions/CompositionType';
import {Count} from '../../../types/CommonTypes';
import {
ShaderSemantics,
ShaderSemanticsClass,
} from '../../definitions/ShaderSemantics';
import {ShaderType} from '../../definitions/ShaderType';
import {ShaderVariableUpdateInterval} from '../../definitions/ShaderVariableUpdateInterval';
import { AbstractMaterialContent } from '../core/AbstractMaterialContent';
import { Material } from '../core/Material';
import { VectorN } from '../../math/VectorN';
import { Scalar } from '../../math/Scalar';
import SynthesizeHDRTextureShaderVertex from '../../../webgl/shaderity_shaders/SynthesizeHDRTextureShader/SynthesizeHDRTextureShader.vert';
import SynthesizeHDRTextureShaderFragment from '../../../webgl/shaderity_shaders/SynthesizeHDRTextureShader/SynthesizeHDRTextureShader.frag';
import { RenderingArg } from '../../../webgl/types/CommonTypes';
import { ShaderSemanticsInfo } from '../../definitions/ShaderSemanticsInfo';
export class SynthesizeHdrMaterialContent extends AbstractMaterialContent {
static SynthesizeCoefficient = new ShaderSemanticsClass({
str: 'synthesizeCoefficient',
});
static TargetRegionTexture = new ShaderSemanticsClass({
str: 'targetRegionTexture',
});
static SynthesizeTexture0 = new ShaderSemanticsClass({
str: 'synthesizeTexture0',
});
static SynthesizeTexture1 = new ShaderSemanticsClass({
str: 'synthesizeTexture1',
});
static SynthesizeTexture2 = new ShaderSemanticsClass({
str: 'synthesizeTexture2',
});
static SynthesizeTexture3 = new ShaderSemanticsClass({
str: 'synthesizeTexture3',
});
static SynthesizeTexture4 = new ShaderSemanticsClass({
str: 'synthesizeTexture4',
});
static SynthesizeTexture5 = new ShaderSemanticsClass({
str: 'synthesizeTexture5',
});
private existTargetRegion: boolean;
private textureNumber: Count;
/**
* This material node uses for the glare effect and so on.
*
* If the targetRegionTexture is not specified, the shader synthesizes all the
* synthesizeTextures with all the pixels weighted by the synthesizeCoefficient.
*
* If the targetRegionTexture is specified, the shader synthesizes all the
* synthesizeTextures with weights only for the non-white pixels of
* targetRegionTexture (where the color is not (1.0, 1.0, 1.0, 1.0)). On the other
* hand, in the white area, the output value is the product of the value of each
* pixel in synthesizeTextures[0] and synthesizeCoefficient[0].
*
* @synthesizeTextures Textures to be synthesized. The shader supports up to six texture syntheses.
* @targetRegionTexture Texture to specify the area where the texture will be synthesized
*/
constructor(
synthesizeTextures: AbstractTexture[],
targetRegionTexture: AbstractTexture = AbstractMaterialContent.dummyBlackTexture
) {
super(
null,
'synthesizeHDRTextureShading',
{},
SynthesizeHDRTextureShaderVertex,
SynthesizeHDRTextureShaderFragment
);
this.existTargetRegion = targetRegionTexture != null ? true : false;
this.textureNumber = synthesizeTextures.length;
const shaderSemanticsInfoArray: ShaderSemanticsInfo[] = [
{
semantic: ShaderSemantics.FramebufferWidth,
componentType: ComponentType.Float,
compositionType: CompositionType.Scalar,
stage: ShaderType.PixelShader,
isCustomSetting: false,
updateInterval: ShaderVariableUpdateInterval.EveryTime,
soloDatum: false,
initialValue: Scalar.fromCopyNumber(synthesizeTextures[0].width),
min: 0,
max: Number.MAX_SAFE_INTEGER,
},
{
semantic: SynthesizeHdrMaterialContent.SynthesizeCoefficient,
componentType: ComponentType.Float,
compositionType: CompositionType.ScalarArray,
arrayLength: 6,
stage: ShaderType.PixelShader,
isCustomSetting: false,
updateInterval: ShaderVariableUpdateInterval.EveryTime,
soloDatum: false,
initialValue: new VectorN(new Float32Array(6)),
min: 0,
max: 1,
needUniformInDataTextureMode: true,
},
{
semantic: SynthesizeHdrMaterialContent.SynthesizeTexture0,
componentType: ComponentType.Int,
compositionType: CompositionType.Texture2D,
stage: ShaderType.PixelShader,
isCustomSetting: false,
updateInterval: ShaderVariableUpdateInterval.EveryTime,
initialValue: [
0,
synthesizeTextures[0] ?? AbstractMaterialContent.dummyBlackTexture,
],
min: 0,
max: Number.MAX_SAFE_INTEGER,
},
{
semantic: SynthesizeHdrMaterialContent.SynthesizeTexture1,
componentType: ComponentType.Int,
compositionType: CompositionType.Texture2D,
stage: ShaderType.PixelShader,
isCustomSetting: false,
updateInterval: ShaderVariableUpdateInterval.EveryTime,
initialValue: [
1,
synthesizeTextures[1] ?? AbstractMaterialContent.dummyBlackTexture,
],
min: 0,
max: Number.MAX_SAFE_INTEGER,
},
{
semantic: SynthesizeHdrMaterialContent.SynthesizeTexture2,
componentType: ComponentType.Int,
compositionType: CompositionType.Texture2D,
stage: ShaderType.PixelShader,
isCustomSetting: false,
updateInterval: ShaderVariableUpdateInterval.EveryTime,
initialValue: [
2,
synthesizeTextures[2] ?? AbstractMaterialContent.dummyBlackTexture,
],
min: 0,
max: Number.MAX_SAFE_INTEGER,
},
{
semantic: SynthesizeHdrMaterialContent.SynthesizeTexture3,
componentType: ComponentType.Int,
compositionType: CompositionType.Texture2D,
stage: ShaderType.PixelShader,
isCustomSetting: false,
updateInterval: ShaderVariableUpdateInterval.EveryTime,
initialValue: [
3,
synthesizeTextures[3] ?? AbstractMaterialContent.dummyBlackTexture,
],
min: 0,
max: Number.MAX_SAFE_INTEGER,
},
{
semantic: SynthesizeHdrMaterialContent.SynthesizeTexture4,
componentType: ComponentType.Int,
compositionType: CompositionType.Texture2D,
stage: ShaderType.PixelShader,
isCustomSetting: false,
updateInterval: ShaderVariableUpdateInterval.EveryTime,
initialValue: [
4,
synthesizeTextures[4] ?? AbstractMaterialContent.dummyBlackTexture,
],
min: 0,
max: Number.MAX_SAFE_INTEGER,
},
{
semantic: SynthesizeHdrMaterialContent.SynthesizeTexture5,
componentType: ComponentType.Int,
compositionType: CompositionType.Texture2D,
stage: ShaderType.PixelShader,
isCustomSetting: false,
updateInterval: ShaderVariableUpdateInterval.EveryTime,
initialValue: [
5,
synthesizeTextures[5] ?? AbstractMaterialContent.dummyBlackTexture,
],
min: 0,
max: Number.MAX_SAFE_INTEGER,
},
{
semantic: SynthesizeHdrMaterialContent.TargetRegionTexture,
componentType: ComponentType.Int,
compositionType: CompositionType.Texture2D,
stage: ShaderType.PixelShader,
isCustomSetting: false,
updateInterval: ShaderVariableUpdateInterval.EveryTime,
initialValue: [6, targetRegionTexture],
min: 0,
max: Number.MAX_SAFE_INTEGER,
},
];
this.setShaderSemanticsInfoArray(shaderSemanticsInfoArray);
}
setCustomSettingParametersToGpu({
material,
shaderProgram,
firstTime,
args,
}: {
material: Material;
shaderProgram: WebGLProgram;
firstTime: boolean;
args: RenderingArg;
}) {
if (args.setUniform) {
this.setWorldMatrix(shaderProgram, args.worldMatrix);
} else {
(shaderProgram as any)._gl.uniform1fv(
(shaderProgram as any).synthesizeCoefficient,
material.getParameter(SynthesizeHdrMaterialContent.SynthesizeCoefficient)
._v
);
}
/// Matrices
let cameraComponent = args.renderPass.cameraComponent;
if (cameraComponent == null) {
cameraComponent = ComponentRepository.getComponent(
CameraComponent,
CameraComponent.current
) as CameraComponent;
}
if (cameraComponent) {
this.setViewInfo(
shaderProgram,
cameraComponent,
args.isVr,
args.displayIdx
);
this.setProjection(
shaderProgram,
cameraComponent,
args.isVr,
args.displayIdx
);
}
}
get existTargetRegionTexture() {
return this.existTargetRegion;
}
get synthesizeTextureNumber() {
return this.textureNumber;
}
}