-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLightRenderer.qml
240 lines (192 loc) · 6.29 KB
/
LightRenderer.qml
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
import QtQuick 2.7
QtObject {
id: self
property color ambient: 'black'
property real softness: 0
property bool specular: true
property real ascend: 1.0
property real samples: 0.5
property ShaderEffectSource heightMap
property ListModel model: ListModel {
onCountChanged: {
self.__createLights();
}
}
property ListModel fboModel: ListModel {}
property var lightMap: ambientLight.fbo
property var specularMap: ambientLight.specularFbo
property size lightMapSize: Qt.size(256, 256)
property size specularMapSize: Qt.size(256, 256)
property var ambientLight : QtObject {
property ShaderEffectSource fbo: ShaderEffectSource {
textureSize: self.lightMapSize
sourceItem: ShaderEffect {
width: 100
height: 100
property color color: self.ambient
fragmentShader: "
uniform vec4 color;
void main() {
gl_FragColor = color;
}
"
}
}
property ShaderEffectSource specularFbo: ShaderEffectSource {
textureSize: self.specularMapSize
sourceItem: Rectangle {
width: 1; height: 1
color: "transparent"
}
}
}
property Component __pointLightComponent: Component {
id: pointLight
QtObject {
id: self
property var context
property ShaderEffectSource base
property var specularBase
property Light light
property ShaderEffectSource fbo: ShaderEffectSource {
textureSize: self.context.lightMapSize
sourceItem: ShaderEffect {
width: 10
height: 10
property var src: self.context.heightMap
property var base: self.base
property color color: self.light.color
property point pos: self.light.pos
property real size: self.light.size
property real ascend: self.context.ascend
property real samples: self.context.samples
property real softness: self.context.softness
property point fragSize: Qt.point(1.0/src.textureSize.width, 1.0/src.textureSize.height)
// fake dependency
property var specularMap: specularBase
fragmentShader: "
varying vec2 qt_TexCoord0;
uniform sampler2D base;
uniform sampler2D src;
uniform vec4 color;
uniform vec2 pos;
uniform float size;
uniform float ascend;
uniform vec2 fragSize;
uniform float softness;
uniform float samples;
void main() {
lowp vec4 c = texture2D(src, qt_TexCoord0);
mediump vec2 dir = normalize(pos - qt_TexCoord0);
mediump float fullDist = distance(pos, qt_TexCoord0);
mediump float localAscend = ascend - c.a;
mediump float localHeight = 1.0 - c.a;
mediump float dist = fullDist / localAscend;
mediump float shadow = smoothstep(0.3*size, size, fullDist);
if (shadow != 1.0) {
mediump float s = c.a;
mediump float rangeStep = fragSize.x * samples;
for (float range = rangeStep; range < dist; range += rangeStep) {
mediump float step = range / dist;
lowp float a = texture2D(src, qt_TexCoord0 + dist * dir * step).a - c.a;
s = max(s, c.a + a*localHeight/step);
if (s >= 1.0) break;
}
shadow = max(shadow, smoothstep(softness, 1.0, s - c.a));
}
lowp float lightness = 1.0 - shadow;
lowp vec3 dcolor = color.rgb * lightness;
gl_FragColor.rgb = texture2D(base, qt_TexCoord0).rgb * (vec3(1.0) - dcolor) + dcolor;
gl_FragColor.a = lightness;
}
"
}
}
property ShaderEffectSource specularFbo: ShaderEffectSource {
textureSize: self.context.specularMapSize
sourceItem: ShaderEffect {
width: 10
height: 10
property var heightMap: self.context.heightMap
property var lightMap: self.fbo
property var base: self.specularBase
property var norm: self.context.normalMap
property point pos: self.light.pos
property real size: self.light.size
property real ascend: self.context.ascend
property color color: self.light.color
fragmentShader: "
varying vec2 qt_TexCoord0;
uniform vec4 color;
uniform sampler2D base;
uniform sampler2D norm;
uniform sampler2D heightMap;
uniform sampler2D lightMap;
uniform vec2 pos;
uniform float size;
uniform float ascend;
void main() {
lowp vec4 c = texture2D(heightMap, qt_TexCoord0);
mediump vec3 light = normalize(vec3(qt_TexCoord0, c.a) - vec3(pos, ascend));
mediump vec2 sn = (texture2D(norm, qt_TexCoord0).rg - vec2(0.5)) * 2.0;
mediump vec3 n = normalize(vec3(sn, 1.0));
mediump vec3 ref = reflect(light, n);
mediump float d = smoothstep(0.99, 1.0, dot(ref, vec3(0.0, 0.0, 1.0)));
gl_FragColor = texture2D(base, qt_TexCoord0);
if (d > 0.0) {
lowp vec3 scolor = color.rgb * d * texture2D(lightMap, qt_TexCoord0).a;
gl_FragColor.rgb = gl_FragColor.rgb * (vec3(1.0) - scolor) + scolor;
}
}
"
}
}
}
}
function __createLights() {
{
for (var i = 0; i < fboModel.count; ++i) fboModel.get(i).light.destroy();
fboModel.clear();
}
var baseLight = self.ambientLight;
{
for (var i = 0; i < self.model.count; ++i) {
var m = self.model.get(i);
var l = pointLight.createObject(null, {
light: m.light,
base: baseLight.fbo,
specularBase: baseLight.specularFbo,
context: self
});
fboModel.append({light: l});
baseLight = l;
}
}
self.lightMap = baseLight.fbo;
self.specularMap = baseLight.specularFbo;
}
Component.onCompleted: {
__createLights();
}
property ShaderEffectSource normalMap: ShaderEffectSource {
sourceItem: ShaderEffect {
width: src.width
height: src.height
property var src: self.heightMap
property point fragSize: Qt.point(1.0/src.textureSize.width, 1.0/src.textureSize.height)
fragmentShader: "
varying vec2 qt_TexCoord0;
uniform sampler2D src;
uniform vec2 fragSize;
void main() {
mediump float x1 = texture2D(src, vec2(qt_TexCoord0.x - fragSize.x, qt_TexCoord0.y)).a;
mediump float x2 = texture2D(src, vec2(qt_TexCoord0.x + fragSize.x, qt_TexCoord0.y)).a;
mediump float y1 = texture2D(src, vec2(qt_TexCoord0.x, qt_TexCoord0.y - fragSize.y)).a;
mediump float y2 = texture2D(src, vec2(qt_TexCoord0.x, qt_TexCoord0.y + fragSize.y)).a;
mediump vec2 n = -vec2(x2 - x1, y2 - y1) * 0.5 + vec2(0.5);
gl_FragColor = vec4(n.x, n.y, 1.0, 1.0);
}
"
}
}
}