-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMyMaterial.js
132 lines (94 loc) · 2.82 KB
/
MyMaterial.js
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
const fragment = [
'uniform sampler2D tDiffuse;',
'uniform float opacity;',
'varying vec2 vUv;',
'const float gamma = 2.2;',
'const float brightness = 0.0;',
'const float contrast = 1.5;',
'const float saturation = 1.5;',
'const float A = 0.15;',
'const float B = 0.50;',
'const float C = 0.10;',
'const float D = 0.20;',
'const float E = 0.02;',
'const float F = 0.30;',
'const float W = 11.2;',
'const float exposure = 2.;',
'const float white = ((W * (A * W + C * B) + D * E) / (W * (A * W + B) + D * F)) - E / F;',
'vec3 MyUncharted2ToneMapping(vec3 color) {',
'color *= exposure;',
'color = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F;',
'color /= white;',
'color = pow(color, vec3(1. / gamma));',
'return color;',
'}',
'mat4 brightnessMatrix(float brightness) {',
'return mat4(1, 0, 0, 0,',
'0, 1, 0, 0,',
'0, 0, 1, 0,',
'brightness, brightness, brightness, 1 );',
'}',
'mat4 contrastMatrix(float contrast) {',
'float t = (1.0 - contrast) / 2.0;',
'return mat4(contrast, 0, 0, 0,',
'0, contrast, 0, 0,',
'0, 0, contrast, 0,',
't, t, t, 1 );',
'}',
'mat4 saturationMatrix(float saturation) {',
'vec3 luminance = vec3(0.3086, 0.6094, 0.0820);',
'float oneMinusSat = 1.0 - saturation;',
'vec3 red = vec3( luminance.x * oneMinusSat );',
'red += vec3( saturation, 0, 0 );',
'vec3 green = vec3( luminance.y * oneMinusSat);',
'green += vec3( 0, saturation, 0 );',
'vec3 blue = vec3( luminance.z * oneMinusSat );',
'blue += vec3( 0, 0, saturation );',
'return mat4(red, 0,',
'green, 0,',
'blue, 0,',
'0, 0, 0, 1 );',
'}',
'void main() {',
'vec4 texel = texture2D(tDiffuse, vUv);',
'texel = vec4(MyUncharted2ToneMapping(texel.rgb), texel.a);',
'texel = brightnessMatrix( brightness ) *',
'contrastMatrix( contrast ) *',
'saturationMatrix( saturation ) *',
'texel;',
'gl_FragColor = opacity * texel;',
'}'
].join('\n');
const vertex = [
'varying vec2 vUv;',
'void main() {',
' vUv = uv;',
' gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);',
'}'
].join('\n');
module.exports = (THREE) => {
/**
* A simple copy shader material.
*/
class MyMaterial extends THREE.ShaderMaterial {
/**
* Constructs a new copy material.
*/
constructor() {
super({
type: "MyMaterial",
uniforms: {
tDiffuse: new THREE.Uniform(null),
opacity: new THREE.Uniform(1.0)
},
fragmentShader: fragment,
vertexShader: vertex,
depthWrite: false,
depthTest: false
});
}
}
return {
MyMaterial
};
};