Skip to content

Commit aac8829

Browse files
committed
Added bump and specular maps to simple skin shader.
Added corresponding example. Tweaked bit previous bump map example.
1 parent bb415a9 commit aac8829

File tree

3 files changed

+425
-9
lines changed

3 files changed

+425
-9
lines changed

examples/js/ShaderSkin.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ THREE.ShaderSkin = {
1212
// - physically based specular term (Kelemen/Szirmay-Kalos specular reflectance)
1313
//
1414
// - diffuse map
15+
// - bump map
16+
// - specular map
1517
// - point and directional lights (use with "lights: true" material option)
1618
// - fog (use with "fog: true" material option)
1719
// - shadow maps
@@ -28,6 +30,9 @@ THREE.ShaderSkin = {
2830

2931
{
3032

33+
"enableBump" : { type: "i", value: 0 },
34+
"enableSpecular": { type: "i", value: 0 },
35+
3136
"tDiffuse" : { type: "t", value: 0, texture: null },
3237
"tBeckmann" : { type: "t", value: 1, texture: null },
3338

@@ -39,6 +44,13 @@ THREE.ShaderSkin = {
3944
"uRoughness": { type: "f", value: 0.15 },
4045
"uSpecularBrightness": { type: "f", value: 0.75 },
4146

47+
"bumpMap" : { type: "t", value: 2, texture: null },
48+
"bumpScale" : { type: "f", value: 1 },
49+
50+
"specularMap" : { type: "t", value: 3, texture: null },
51+
52+
"offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
53+
4254
"uWrapRGB": { type: "v3", value: new THREE.Vector3( 0.75, 0.375, 0.1875 ) }
4355

4456
}
@@ -47,6 +59,12 @@ THREE.ShaderSkin = {
4759

4860
fragmentShader: [
4961

62+
"#define USE_BUMPMAP",
63+
"#extension GL_OES_standard_derivatives : enable",
64+
65+
"uniform bool enableBump;",
66+
"uniform bool enableSpecular;",
67+
5068
"uniform vec3 uAmbientColor;",
5169
"uniform vec3 uDiffuseColor;",
5270
"uniform vec3 uSpecularColor;",
@@ -60,6 +78,8 @@ THREE.ShaderSkin = {
6078
"uniform sampler2D tDiffuse;",
6179
"uniform sampler2D tBeckmann;",
6280

81+
"uniform sampler2D specularMap;",
82+
6383
"varying vec3 vNormal;",
6484
"varying vec2 vUv;",
6585

@@ -84,6 +104,7 @@ THREE.ShaderSkin = {
84104

85105
THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
86106
THREE.ShaderChunk[ "fog_pars_fragment" ],
107+
THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
87108

88109
// Fresnel term
89110

@@ -140,6 +161,25 @@ THREE.ShaderSkin = {
140161
"vec3 normal = normalize( vNormal );",
141162
"vec3 viewPosition = normalize( vViewPosition );",
142163

164+
"float specularStrength;",
165+
166+
"if ( enableSpecular ) {",
167+
168+
"vec4 texelSpecular = texture2D( specularMap, vUv );",
169+
"specularStrength = texelSpecular.r;",
170+
171+
"} else {",
172+
173+
"specularStrength = 1.0;",
174+
175+
"}",
176+
177+
"#ifdef USE_BUMPMAP",
178+
179+
"if ( enableBump ) normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",
180+
181+
"#endif",
182+
143183
// point lights
144184

145185
"vec3 specularTotal = vec3( 0.0 );",
@@ -168,7 +208,7 @@ THREE.ShaderSkin = {
168208
"float pointSpecularWeight = KS_Skin_Specular( normal, lVector, viewPosition, uRoughness, uSpecularBrightness );",
169209

170210
"pointTotal += lDistance * uDiffuseColor * pointLightColor[ i ] * pointDiffuseWeight;",
171-
"specularTotal += lDistance * uSpecularColor * pointLightColor[ i ] * pointSpecularWeight;",
211+
"specularTotal += lDistance * uSpecularColor * pointLightColor[ i ] * pointSpecularWeight * specularStrength;",
172212

173213
"}",
174214

@@ -193,7 +233,7 @@ THREE.ShaderSkin = {
193233
"float dirSpecularWeight = KS_Skin_Specular( normal, dirVector, viewPosition, uRoughness, uSpecularBrightness );",
194234

195235
"dirTotal += uDiffuseColor * directionalLightColor[ i ] * dirDiffuseWeight;",
196-
"specularTotal += uSpecularColor * directionalLightColor[ i ] * dirSpecularWeight;",
236+
"specularTotal += uSpecularColor * directionalLightColor[ i ] * dirSpecularWeight * specularStrength;",
197237

198238
"}",
199239

@@ -223,6 +263,8 @@ THREE.ShaderSkin = {
223263

224264
vertexShader: [
225265

266+
"uniform vec4 offsetRepeat;",
267+
226268
"varying vec3 vNormal;",
227269
"varying vec2 vUv;",
228270

@@ -237,7 +279,7 @@ THREE.ShaderSkin = {
237279

238280
"vNormal = normalMatrix * normal;",
239281

240-
"vUv = uv;",
282+
"vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
241283

242284
"gl_Position = projectionMatrix * mvPosition;",
243285

examples/webgl_materials_bumpmap.html

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
var mouseX = 0;
6868
var mouseY = 0;
6969

70+
var targetX = 0;
71+
var targetY = 0;
72+
7073
var windowHalfX = window.innerWidth / 2;
7174
var windowHalfY = window.innerHeight / 2;
7275

@@ -236,10 +239,10 @@
236239

237240
}
238241

239-
function onDocumentMouseMove(event) {
242+
function onDocumentMouseMove( event ) {
240243

241-
mouseX = ( event.clientX - windowHalfX ) * 10;
242-
mouseY = ( event.clientY - windowHalfY ) * 10;
244+
mouseX = ( event.clientX - windowHalfX );
245+
mouseY = ( event.clientY - windowHalfY );
243246

244247
}
245248

@@ -256,12 +259,13 @@
256259

257260
function render() {
258261

259-
var ry = mouseX * 0.0003, rx = mouseY * 0.0003;
262+
targetX = mouseX * .001;
263+
targetY = mouseY * .001;
260264

261265
if ( mesh ) {
262266

263-
mesh.rotation.y = ry;
264-
mesh.rotation.x = rx;
267+
mesh.rotation.y += 0.05 * ( targetX - mesh.rotation.y );
268+
mesh.rotation.x += 0.05 * ( targetY - mesh.rotation.x );
265269

266270
}
267271

0 commit comments

Comments
 (0)