-
Notifications
You must be signed in to change notification settings - Fork 67
/
shader-chunk-lambert.html
228 lines (154 loc) · 4.85 KB
/
shader-chunk-lambert.html
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
<!DOCTYPE html>
<html>
<head>
<title>A-Frame - using shader chunk</title>
<script src="js/aframe-master-v1.3.0.min.js"></script>
<script src="js/aframe-environment-component.js"></script>
</head>
<body>
<script>
var customMaterial = new THREE.ShaderMaterial({
uniforms: THREE.UniformsUtils.merge([
THREE.UniformsLib[ "common" ], // defines uniforms and sets defaults: vec3 diffuse, float opacity, mat3 uvTransform, sampler2D map
THREE.UniformsLib[ "lights" ], // values automatically sent by three.js
THREE.UniformsLib[ "fog" ], // values automatically sent by three.js
{
time: { value: 0 },
// color: { value: new THREE.Color("#FFFFFF") },
// diffuseMap: { type: 't' }
}
]),
lights: true,
fog: true,
vertexShader:
`
// defines constants and functions
${THREE.ShaderChunk["common"]}
#define USE_UV
// defines vec2 vUv, mat3 uvTransform
${THREE.ShaderChunk["uv_pars_vertex"]}
// defines light-related things
${THREE.ShaderChunk["lights_pars_begin"]}
// defines varying float vFogDepth;
${THREE.ShaderChunk["fog_pars_vertex"]}
// values to pass along to fragment shader
varying vec3 vLightFront;
varying vec3 vIndirectFront;
void main()
{
// TODO: include vertexColors
// transforms uv coordinates and assigns to vUv
${THREE.ShaderChunk["uv_vertex"]}
// defines vec3 objectNormal
${THREE.ShaderChunk["beginnormal_vertex"]}
// defines vec3 transformedNormal
${THREE.ShaderChunk["defaultnormal_vertex"]}
// defines vec3 transformed = vec3(position)
${THREE.ShaderChunk["begin_vertex"]}
// defines vec3 mvPosition = vec3(position) // used by fog_vertex
// sets gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
${THREE.ShaderChunk["project_vertex"]}
// do light-related calculations
${THREE.ShaderChunk["lights_lambert_vertex"]}
// need this for the fog_vertex code -- ???
${THREE.ShaderChunk["fog_vertex"]}
}
`,
fragmentShader: `
${THREE.ShaderChunk["common"]}
#define USE_UV
// defines vec2 vUv
${THREE.ShaderChunk["uv_pars_fragment"]}
#define USE_MAP
// defines sampler2D map
${THREE.ShaderChunk["map_pars_fragment"]}
// defines light-related things
${THREE.ShaderChunk["lights_pars_begin"]}
// defines some light functions??
${THREE.ShaderChunk["bsdfs"]}
// defines uniform vec3 fogColor; varying float vFogDepth; uniform float fogDensity; uniform float fogNear; uniform float fogFar;
${THREE.ShaderChunk["fog_pars_fragment"]}
// values received from vertex shader
varying vec3 vLightFront;
varying vec3 vIndirectFront;
uniform vec3 diffuse;
uniform float opacity;
void main()
{
// starting point for Lambert-based shaders
vec4 diffuseColor = vec4( diffuse, opacity );
// apply (multiply) sampled texture to diffuseColor
${THREE.ShaderChunk["map_fragment"]}
// example of injecting custom code
// diffuseColor *= vec4(vUv.x, vUv.y, 0.0, 1.0);
// ShaderChunk["output_fragment"]: assigns gl_FragColor, requires outgoingLight
// doing it manually instead; simplifying light calculations
gl_FragColor = vec4(diffuseColor.rgb * (vLightFront * 1.0 + vIndirectFront / 2.0) / 3.14, diffuseColor.a);
// apply fog calculation to gl_FragColor
${THREE.ShaderChunk["fog_fragment"]}
}
`
});
AFRAME.registerComponent("ao-data", {
schema: {
},
init: function()
{
},
play: function()
{
let loader = new THREE.TextureLoader();
let tex = loader.load("images/color-grid.png");
// TODO: get this from a-assets instead of a three.loader
customMaterial.uniforms.map.value = tex;
this.el.object3D.children[0].material = customMaterial;
},
tick: function()
{
this.el.object3D.rotateX(0.010);
this.el.object3D.rotateY(0.007);
}
});
</script>
<a-scene fog="type: linear; color: #000011; near: 1; far: 10;">
<a-assets>
<img id="wallDiffuse" src="images/stone-wall-diffuse-256.jpg" />
<img id="wallNormal" src="images/stone-wall-normal-256.jpg" />
<img id="wallAO" src="images/ao-map.png" />
<img id="grid" src="images/color-grid.png" />
</a-assets>
<a-sky
color = "#000337">
</a-sky>
<!-- testing point light, attached to camera -->
<!--
<a-camera>
<a-entity
light="type: point;">
</a-entity>
</a-camera>
-->
<!-- testing other lights -->
<!--
<a-entity
light="type: ambient; color: #666666;">
</a-entity>
<a-entity
light="type: directional; color: #FFFFFF;"
position="-1 4 0">
</a-entity>
-->
<a-plane
id="floor"
width="100" height="100"
material="src: #grid; repeat: 20 20;"
rotation="-90 0 0">
</a-plane>
<a-box
id="test"
position="0 1.5 -2"
ao-data>
</a-box>
</a-scene>
</body>
</html>