-
Notifications
You must be signed in to change notification settings - Fork 70
/
shader-chunk-phong.html
204 lines (162 loc) · 5.7 KB
/
shader-chunk-phong.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
<!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>
AFRAME.registerComponent('custom-material', {
schema:
{
diffuseMap: {type: "string"},
normalMap: {type: "string"},
},
init: function ()
{
this.material = 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[ "normalmap" ], // defines uniforms and sets defaults: sampler2D normalMap, vec2 normalScale
THREE.UniformsLib[ "fog" ], // values automatically sent by three.js
{
time: { value: 0 },
shininess: { value: 10 },
specular: { value: new THREE.Color("#111111") },
emissive: { value: new THREE.Color("#000000") },
}
]),
lights: true,
fog: true,
vertexShader:
`
varying vec3 vViewPosition;
${THREE.ShaderChunk["common"]}
#define USE_UV
${THREE.ShaderChunk["uv_pars_vertex"]}
${THREE.ShaderChunk["color_pars_vertex"]}
${THREE.ShaderChunk["fog_pars_vertex"]}
${THREE.ShaderChunk["normal_pars_vertex"]}
void main()
{
${THREE.ShaderChunk["uv_vertex"]}
${THREE.ShaderChunk["color_vertex"]}
${THREE.ShaderChunk["beginnormal_vertex"]}
${THREE.ShaderChunk["defaultnormal_vertex"]}
${THREE.ShaderChunk["normal_vertex"]}
${THREE.ShaderChunk["begin_vertex"]}
${THREE.ShaderChunk["project_vertex"]}
vViewPosition = - mvPosition.xyz;
${THREE.ShaderChunk["fog_vertex"]}
}
`,
fragmentShader: `
uniform vec3 diffuse;
uniform vec3 emissive;
uniform vec3 specular;
uniform float shininess;
uniform float opacity;
${THREE.ShaderChunk["common"]}
${THREE.ShaderChunk["packing"]}
${THREE.ShaderChunk["color_pars_fragment"]}
#define USE_UV
${THREE.ShaderChunk["uv_pars_fragment"]}
#define USE_MAP
${THREE.ShaderChunk["map_pars_fragment"]}
${THREE.ShaderChunk["emissivemap_pars_fragment"]}
${THREE.ShaderChunk["fog_pars_fragment"]}
${THREE.ShaderChunk["bsdfs"]}
${THREE.ShaderChunk["lights_pars_begin"]}
${THREE.ShaderChunk["normal_pars_fragment"]}
${THREE.ShaderChunk["lights_phong_pars_fragment"]}
#define USE_NORMALMAP
#define TANGENTSPACE_NORMALMAP
${THREE.ShaderChunk["normalmap_pars_fragment"]}
${THREE.ShaderChunk["specularmap_pars_fragment"]}
void main()
{
vec4 diffuseColor = vec4( diffuse, opacity );
ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
vec3 totalEmissiveRadiance = emissive;
${THREE.ShaderChunk["map_fragment"]}
${THREE.ShaderChunk["color_fragment"]}
${THREE.ShaderChunk["specularmap_fragment"]}
${THREE.ShaderChunk["normal_fragment_begin"]}
${THREE.ShaderChunk["normal_fragment_maps"]}
${THREE.ShaderChunk["emissivemap_fragment"]}
// accumulation
${THREE.ShaderChunk["lights_phong_fragment"]}
${THREE.ShaderChunk["lights_fragment_begin"]}
${THREE.ShaderChunk["lights_fragment_maps"]}
${THREE.ShaderChunk["lights_fragment_end"]}
vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;
${THREE.ShaderChunk["output_fragment"]}
${THREE.ShaderChunk["fog_fragment"]}
}
`
}); // end of ShaderMaterial
let mapTexture = new THREE.Texture();
mapTexture.image = document.querySelector(this.data.diffuseMap);
mapTexture.needsUpdate = true;
this.material.uniforms.map.value = mapTexture;
let normalTexture = new THREE.Texture();
normalTexture.image = document.querySelector(this.data.normalMap);
normalTexture.needsUpdate = true;
this.material.uniforms.normalMap.value = normalTexture;
let self = this;
this.el.addEventListener("loaded", function(event) {
self.el.getObject3D('mesh').material = self.material;
});
},
});
AFRAME.registerComponent("spinner", {
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="stoneDiffuse" src="images/stone-wall-diffuse-256.jpg"/>
<img id="stoneNormal" src="images/stone-wall-normal-256.jpg"/>
<img id="grid" src="images/color-grid.png"/>
</a-assets>
<a-sky
color = "#000337">
</a-sky>
<!-- NOTE: you must define lights, otherwise shader does not compile?! -->
<!-- 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"
custom-material="diffuseMap: #stoneDiffuse; normalMap: #stoneNormal;"
spinner>
</a-box>
</a-scene>
</body>
</html>