Skip to content

Commit bc6ed16

Browse files
committed
Merge pull request #7381 from WestLangley/dev-standard
Inital PR for MeshStandardMaterial
2 parents 19c1fc2 + e5443a1 commit bc6ed16

File tree

14 files changed

+373
-9
lines changed

14 files changed

+373
-9
lines changed

src/renderers/WebGLRenderer.js

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ THREE.WebGLRenderer = function ( parameters ) {
637637

638638
_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.normal );
639639

640-
if ( material.type !== 'MeshPhongMaterial' && material.shading === THREE.FlatShading ) {
640+
if ( material.type !== 'MeshPhongMaterial' && material.type !== 'MeshStandardMaterial' && material.shading === THREE.FlatShading ) {
641641

642642
for ( var i = 0, l = object.count * 3; i < l; i += 9 ) {
643643

@@ -1573,6 +1573,7 @@ THREE.WebGLRenderer = function ( parameters ) {
15731573

15741574
if ( material instanceof THREE.ShaderMaterial ||
15751575
material instanceof THREE.MeshPhongMaterial ||
1576+
material instanceof THREE.MeshStandardMaterial ||
15761577
material.envMap ) {
15771578

15781579
if ( p_uniforms.cameraPosition !== undefined ) {
@@ -1587,6 +1588,7 @@ THREE.WebGLRenderer = function ( parameters ) {
15871588
if ( material instanceof THREE.MeshPhongMaterial ||
15881589
material instanceof THREE.MeshLambertMaterial ||
15891590
material instanceof THREE.MeshBasicMaterial ||
1591+
material instanceof THREE.MeshStandardMaterial ||
15901592
material instanceof THREE.ShaderMaterial ||
15911593
material.skinning ) {
15921594

@@ -1665,6 +1667,7 @@ THREE.WebGLRenderer = function ( parameters ) {
16651667

16661668
if ( material instanceof THREE.MeshPhongMaterial ||
16671669
material instanceof THREE.MeshLambertMaterial ||
1670+
material instanceof THREE.MeshStandardMaterial ||
16681671
material.lights ) {
16691672

16701673
if ( _lightsNeedUpdate ) {
@@ -1690,7 +1693,8 @@ THREE.WebGLRenderer = function ( parameters ) {
16901693

16911694
if ( material instanceof THREE.MeshBasicMaterial ||
16921695
material instanceof THREE.MeshLambertMaterial ||
1693-
material instanceof THREE.MeshPhongMaterial ) {
1696+
material instanceof THREE.MeshPhongMaterial ||
1697+
material instanceof THREE.MeshStandardMaterial ) {
16941698

16951699
refreshUniformsCommon( m_uniforms, material );
16961700

@@ -1715,6 +1719,10 @@ THREE.WebGLRenderer = function ( parameters ) {
17151719

17161720
refreshUniformsPhong( m_uniforms, material );
17171721

1722+
} else if ( material instanceof THREE.MeshStandardMaterial ) {
1723+
1724+
refreshUniformsStandard( m_uniforms, material );
1725+
17181726
} else if ( material instanceof THREE.MeshDepthMaterial ) {
17191727

17201728
m_uniforms.mNear.value = camera.near;
@@ -1932,6 +1940,74 @@ THREE.WebGLRenderer = function ( parameters ) {
19321940

19331941
}
19341942

1943+
function refreshUniformsStandard ( uniforms, material ) {
1944+
1945+
uniforms.roughness.value = material.roughness;
1946+
//uniforms.reflectivity.value = material.reflectivity; // part of uniforms common
1947+
uniforms.metalness.value = material.metalness;
1948+
1949+
if ( material.roughnessMap ) {
1950+
1951+
uniforms.roughnessMap.value = material.roughnessMap;
1952+
1953+
}
1954+
1955+
if ( material.reflectivityMap ) {
1956+
1957+
uniforms.reflectivityMap.value = material.reflectivityMap;
1958+
1959+
}
1960+
1961+
if ( material.metalnessMap ) {
1962+
1963+
uniforms.metalnessMap.value = material.metalnessMap;
1964+
1965+
}
1966+
1967+
if ( material.lightMap ) {
1968+
1969+
uniforms.lightMap.value = material.lightMap;
1970+
uniforms.lightMapIntensity.value = material.lightMapIntensity;
1971+
1972+
}
1973+
1974+
if ( material.emissiveMap ) {
1975+
1976+
uniforms.emissiveMap.value = material.emissiveMap;
1977+
1978+
}
1979+
1980+
if ( material.bumpMap ) {
1981+
1982+
uniforms.bumpMap.value = material.bumpMap;
1983+
uniforms.bumpScale.value = material.bumpScale;
1984+
1985+
}
1986+
1987+
if ( material.normalMap ) {
1988+
1989+
uniforms.normalMap.value = material.normalMap;
1990+
uniforms.normalScale.value.copy( material.normalScale );
1991+
1992+
}
1993+
1994+
if ( material.displacementMap ) {
1995+
1996+
uniforms.displacementMap.value = material.displacementMap;
1997+
uniforms.displacementScale.value = material.displacementScale;
1998+
uniforms.displacementBias.value = material.displacementBias;
1999+
2000+
}
2001+
2002+
if ( material.envMap ) {
2003+
2004+
//uniforms.envMap.value = material.envMap; // part of uniforms common
2005+
uniforms.envMapIntensity.value = material.envMapIntensity;
2006+
2007+
}
2008+
2009+
}
2010+
19352011
function refreshUniformsLights ( uniforms, lights ) {
19362012

19372013
uniforms.ambientLightColor.value = lights.ambient;

src/renderers/shaders/ShaderChunk/common.glsl

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,89 @@ vec3 BRDF_BlinnPhong( in vec3 specularColor, in float shininess, in vec3 normal,
9999

100100
}
101101

102+
// Microfacet Models for Refraction through Rough Surfaces - equation (34)
103+
// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html
104+
// alpha is "roughness squared" in Disney’s reparameterization
105+
float G_GGX_Smith( in float alpha, in float dotNL, in float dotNV ) {
106+
107+
// geometry term = G(l)⋅G(v) / 4(n⋅l)(n⋅v)
108+
109+
float a2 = alpha * alpha;
110+
111+
float gl = dotNL + pow( a2 + ( 1.0 - a2 ) * dotNL * dotNL, 0.5 );
112+
113+
float gv = dotNV + pow( a2 + ( 1.0 - a2 ) * dotNV * dotNV, 0.5 );
114+
115+
return 1.0 / ( gl * gv );
116+
117+
}
118+
119+
// Microfacet Models for Refraction through Rough Surfaces - equation (33)
120+
// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html
121+
// alpha is "roughness squared" in Disney’s reparameterization
122+
float D_GGX( in float alpha, in float dotNH ) {
123+
124+
// factor of 1/PI in distribution term omitted
125+
126+
float a2 = alpha * alpha;
127+
128+
float denom = dotNH * dotNH * ( a2 - 1.0 ) + 1.0; // avoid alpha = 0 with dotNH = 1
129+
130+
return a2 / ( denom * denom );
131+
132+
}
133+
134+
// GGX Distribution, Schlick Fresnel, GGX-Smith Visibility
135+
vec3 BRDF_GGX( in vec3 specularColor, in float roughness, in vec3 normal, in vec3 lightDir, in vec3 viewDir ) {
136+
137+
// factor of 1/PI in BRDF omitted
138+
139+
float alpha = roughness * roughness; // UE4's roughness
140+
141+
vec3 halfDir = normalize( lightDir + viewDir );
142+
143+
float dotNL = saturate( dot( normal, lightDir ) );
144+
float dotNV = saturate( dot( normal, viewDir ) );
145+
float dotNH = saturate( dot( normal, halfDir ) );
146+
float dotLH = saturate( dot( lightDir, halfDir ) );
147+
148+
vec3 F = F_Schlick( specularColor, dotLH );
149+
150+
float G = G_GGX_Smith( alpha, dotNL, dotNV );
151+
152+
float D = D_GGX( alpha, dotNH );
153+
154+
return F * G * D;
155+
156+
}
157+
158+
vec3 BRDF_Lambert( in vec3 diffuseColor ) {
159+
160+
// factor of 1/PI in BRDF omitted
161+
162+
return diffuseColor;
163+
164+
}
165+
166+
// ref: https://www.unrealengine.com/blog/physically-based-shading-on-mobile - environmentBRDF for GGX on mobile
167+
vec3 envBRDFApprox( vec3 specularColor, float roughness, in vec3 normal, in vec3 viewDir ) {
168+
169+
float dotNV = saturate( dot( normal, viewDir ) );
170+
171+
const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );
172+
173+
const vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );
174+
175+
vec4 r = roughness * c0 + c1;
176+
177+
float a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;
178+
179+
vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw;
180+
181+
return specularColor * AB.x + AB.y;
182+
183+
}
184+
102185
vec3 inputToLinear( in vec3 a ) {
103186

104187
#ifdef GAMMA_INPUT

src/renderers/shaders/ShaderChunk/envmap_pars_fragment.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#endif
99
uniform float flipEnvMap;
1010

11-
#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG )
11+
#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG ) || defined( STANDARD )
1212

1313
uniform float refractionRatio;
1414

src/renderers/shaders/ShaderChunk/envmap_pars_vertex.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP ) && ! defined( PHONG )
1+
#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP ) && ! defined( PHONG ) && ! defined( STANDARD )
22

33
varying vec3 vReflect;
44

src/renderers/shaders/ShaderChunk/envmap_vertex.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP ) && ! defined( PHONG )
1+
#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP ) && ! defined( PHONG ) && ! defined( STANDARD )
22

33
vec3 cameraToVertex = normalize( worldPosition.xyz - cameraPosition );
44

src/renderers/shaders/ShaderChunk/uv_pars_fragment.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) || defined( USE_EMISSIVEMAP )
1+
#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) || defined( USE_EMISSIVEMAP ) || defined( USE_ROUGHNESSMAP ) || defined( USE_REFLECTIVITYMAP ) || defined( USE_METALNESSMAP )
22

33
varying vec2 vUv;
44

src/renderers/shaders/ShaderChunk/uv_pars_vertex.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) || defined( USE_EMISSIVEMAP )
1+
#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) || defined( USE_EMISSIVEMAP ) || defined( USE_ROUGHNESSMAP ) || defined( USE_REFLECTIVITYMAP ) || defined( USE_METALNESSMAP )
22

33
varying vec2 vUv;
44
uniform vec4 offsetRepeat;

src/renderers/shaders/ShaderChunk/uv_vertex.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) || defined( USE_EMISSIVEMAP )
1+
#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) || defined( USE_EMISSIVEMAP ) || defined( USE_ROUGHNESSMAP ) || defined( USE_REFLECTIVITYMAP ) || defined( USE_METALNESSMAP )
22

33
vUv = uv * offsetRepeat.zw + offsetRepeat.xy;
44

src/renderers/shaders/ShaderChunk/worldpos_vertex.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if defined( USE_ENVMAP ) || defined( PHONG ) || defined( LAMBERT ) || defined ( USE_SHADOWMAP )
1+
#if defined( USE_ENVMAP ) || defined( PHONG ) || defined( STANDARD ) || defined( LAMBERT ) || defined ( USE_SHADOWMAP )
22

33
#ifdef USE_SKINNING
44

0 commit comments

Comments
 (0)