Skip to content

Commit 1a9e254

Browse files
committed
Merge pull request #7382 from WestLangley/dev-standard
Inital PR for MeshStandardMaterial - Part 2
2 parents bc6ed16 + 5c69e10 commit 1a9e254

File tree

7 files changed

+392
-0
lines changed

7 files changed

+392
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/**
2+
* @author WestLangley / http://github.com/WestLangley
3+
*
4+
* parameters = {
5+
* color: <hex>,
6+
* roughness: <float>,
7+
* reflectivity: <float>,
8+
* metalness: <float>,
9+
10+
* emissive: <hex>,
11+
* opacity: <float>,
12+
*
13+
* map: new THREE.Texture( <Image> ),
14+
*
15+
* lightMap: new THREE.Texture( <Image> ),
16+
* lightMapIntensity: <float>
17+
*
18+
* aoMap: new THREE.Texture( <Image> ),
19+
* aoMapIntensity: <float>
20+
*
21+
* emissiveMap: new THREE.Texture( <Image> ),
22+
*
23+
* bumpMap: new THREE.Texture( <Image> ),
24+
* bumpScale: <float>,
25+
*
26+
* normalMap: new THREE.Texture( <Image> ),
27+
* normalScale: <Vector2>,
28+
*
29+
* displacementMap: new THREE.Texture( <Image> ),
30+
* displacementScale: <float>,
31+
* displacementBias: <float>,
32+
*
33+
* roughnessMap: new THREE.Texture( <Image> ),
34+
*
35+
* reflectivityMap: new THREE.Texture( <Image> ),
36+
*
37+
* metalnessMap: new THREE.Texture( <Image> ),
38+
*
39+
* alphaMap: new THREE.Texture( <Image> ),
40+
*
41+
* envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ),
42+
* refractionRatio: <float>,
43+
*
44+
* shading: THREE.SmoothShading,
45+
* blending: THREE.NormalBlending,
46+
* depthTest: <bool>,
47+
* depthWrite: <bool>,
48+
*
49+
* wireframe: <boolean>,
50+
* wireframeLinewidth: <float>,
51+
*
52+
* vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
53+
*
54+
* skinning: <bool>,
55+
* morphTargets: <bool>,
56+
* morphNormals: <bool>,
57+
*
58+
* fog: <bool>
59+
* }
60+
*/
61+
62+
THREE.MeshStandardMaterial = function ( parameters ) {
63+
64+
THREE.Material.call( this );
65+
66+
this.type = 'MeshStandardMaterial';
67+
68+
this.color = new THREE.Color( 0xffffff ); // diffuse
69+
this.roughness = 0.5;
70+
this.reflectivity = 1;
71+
this.metalness = 0;
72+
73+
this.emissive = new THREE.Color( 0x000000 );
74+
75+
this.map = null;
76+
77+
this.lightMap = null;
78+
this.lightMapIntensity = 1.0;
79+
80+
this.aoMap = null;
81+
this.aoMapIntensity = 1.0;
82+
83+
this.emissiveMap = null;
84+
85+
this.bumpMap = null;
86+
this.bumpScale = 1;
87+
88+
this.normalMap = null;
89+
this.normalScale = new THREE.Vector2( 1, 1 );
90+
91+
this.displacementMap = null;
92+
this.displacementScale = 1;
93+
this.displacementBias = 0;
94+
95+
this.roughnessMap = null;
96+
97+
this.reflectivityMap = null;
98+
99+
this.metalnessMap = null;
100+
101+
this.alphaMap = null;
102+
103+
this.envMap = null;
104+
this.envMapIntensity = 1.0;
105+
106+
this.refractionRatio = 0.98;
107+
108+
this.fog = true;
109+
110+
this.shading = THREE.SmoothShading;
111+
112+
this.wireframe = false;
113+
this.wireframeLinewidth = 1;
114+
this.wireframeLinecap = 'round';
115+
this.wireframeLinejoin = 'round';
116+
117+
this.vertexColors = THREE.NoColors;
118+
119+
this.skinning = false;
120+
this.morphTargets = false;
121+
this.morphNormals = false;
122+
123+
this.setValues( parameters );
124+
125+
};
126+
127+
THREE.MeshStandardMaterial.prototype = Object.create( THREE.Material.prototype );
128+
THREE.MeshStandardMaterial.prototype.constructor = THREE.MeshStandardMaterial;
129+
130+
THREE.MeshStandardMaterial.prototype.copy = function ( source ) {
131+
132+
THREE.Material.prototype.copy.call( this, source );
133+
134+
this.color.copy( source.color );
135+
this.roughness = source.roughness;
136+
this.reflectivity = source.reflectivity;
137+
this.metalness = source.metalness;
138+
139+
this.emissive.copy( source.emissive );
140+
141+
this.map = source.map;
142+
143+
this.lightMap = source.lightMap;
144+
this.lightMapIntensity = source.lightMapIntensity;
145+
146+
this.aoMap = source.aoMap;
147+
this.aoMapIntensity = source.aoMapIntensity;
148+
149+
this.emissiveMap = source.emissiveMap;
150+
151+
this.bumpMap = source.bumpMap;
152+
this.bumpScale = source.bumpScale;
153+
154+
this.normalMap = source.normalMap;
155+
this.normalScale.copy( source.normalScale );
156+
157+
this.displacementMap = source.displacementMap;
158+
this.displacementScale = source.displacementScale;
159+
this.displacementBias = source.displacementBias;
160+
161+
this.roughnessMap = source.roughnessMap;
162+
163+
this.reflectivityMap = source.reflectivityMap;
164+
165+
this.metalnessMap = source.metalnessMap;
166+
167+
this.alphaMap = source.alphaMap;
168+
169+
this.envMap = source.envMap;
170+
this.envMapIntensity = source.envMapIntensity;
171+
172+
this.refractionRatio = source.refractionRatio;
173+
174+
this.fog = source.fog;
175+
176+
this.shading = source.shading;
177+
178+
this.wireframe = source.wireframe;
179+
this.wireframeLinewidth = source.wireframeLinewidth;
180+
this.wireframeLinecap = source.wireframeLinecap;
181+
this.wireframeLinejoin = source.wireframeLinejoin;
182+
183+
this.vertexColors = source.vertexColors;
184+
185+
this.skinning = source.skinning;
186+
this.morphTargets = source.morphTargets;
187+
this.morphNormals = source.morphNormals;
188+
189+
return this;
190+
191+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#ifdef USE_ENVMAP
2+
3+
float reflectivityFactor = reflectivity; // fix add map - replace specular strength?
4+
5+
vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );
6+
7+
// Transforming Normal Vectors with the Inverse Transformation
8+
vec3 worldNormal = inverseTransformDirection( normal, viewMatrix );
9+
10+
#ifdef ENVMAP_MODE_REFLECTION
11+
12+
vec3 reflectVec = reflect( cameraToVertex, worldNormal );
13+
14+
#else
15+
16+
vec3 reflectVec = refract( cameraToVertex, worldNormal, refractionRatio );
17+
18+
#endif
19+
20+
#ifdef DOUBLE_SIDED
21+
22+
float flipNormal = ( float( gl_FrontFacing ) * 2.0 - 1.0 );
23+
24+
#else
25+
26+
float flipNormal = 1.0;
27+
28+
#endif
29+
30+
#ifdef ENVMAP_TYPE_CUBE
31+
32+
#if defined( TEXTURE_CUBE_LOD_EXT )
33+
34+
float bias = pow( roughness, 0.5 ) * 7.0; // from bhouston - there are other models for this calculation (roughness; not roughnesFactor)
35+
36+
vec4 envMapColor = textureCubeLodEXT( envMap, flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ), bias );
37+
38+
#else
39+
40+
vec4 envMapColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );
41+
42+
#endif
43+
44+
#elif defined( ENVMAP_TYPE_EQUIREC )
45+
46+
vec2 sampleUV;
47+
sampleUV.y = saturate( flipNormal * reflectVec.y * 0.5 + 0.5 );
48+
sampleUV.x = atan( flipNormal * reflectVec.z, flipNormal * reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
49+
vec4 envMapColor = texture2D( envMap, sampleUV );
50+
51+
#elif defined( ENVMAP_TYPE_SPHERE )
52+
53+
vec3 reflectView = flipNormal * normalize((viewMatrix * vec4( reflectVec, 0.0 )).xyz + vec3(0.0,0.0,1.0));
54+
vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5 );
55+
56+
#endif
57+
58+
envMapColor.rgb = inputToLinear( envMapColor.rgb );
59+
60+
outgoingLight += envBRDFApprox( specularColor, roughnessFactor, normal, viewDir ) * envMapColor.rgb * envMapIntensity * reflectivityFactor;
61+
62+
#endif
63+
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
vec3 viewDir = normalize( vViewPosition );
2+
3+
vec3 totalDiffuseLight = vec3( 0.0 );
4+
vec3 totalSpecularLight = vec3( 0.0 );
5+
6+
7+
// roughness linear remapping
8+
9+
roughnessFactor = roughnessFactor * 0.5 + 0.5; // disney's remapping of [ 0, 1 ] roughness to [ 0.5, 1 ]
10+
11+
12+
// metalness effect on color
13+
14+
vec3 specularColor = mix( vec3( 0.04 ), diffuseColor.rgb, metalnessFactor );
15+
16+
diffuseColor.rgb *= ( 1.0 - metalnessFactor );
17+
18+
19+
#if MAX_POINT_LIGHTS > 0
20+
21+
for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
22+
23+
vec3 lightColor = pointLightColor[ i ];
24+
25+
vec3 lightPosition = pointLightPosition[ i ];
26+
vec3 lVector = lightPosition + vViewPosition.xyz;
27+
vec3 lightDir = normalize( lVector );
28+
29+
// attenuation
30+
31+
float attenuation = calcLightAttenuation( length( lVector ), pointLightDistance[ i ], pointLightDecay[ i ] );
32+
33+
// diffuse
34+
35+
float cosineTerm = saturate( dot( normal, lightDir ) );
36+
37+
totalDiffuseLight += lightColor * attenuation * cosineTerm;
38+
39+
// specular
40+
41+
vec3 brdf = BRDF_GGX( specularColor, roughnessFactor, normal, lightDir, viewDir );
42+
43+
totalSpecularLight += brdf * specularStrength * lightColor * attenuation * cosineTerm;
44+
45+
46+
}
47+
48+
#endif
49+
50+
#if MAX_SPOT_LIGHTS > 0
51+
52+
for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
53+
54+
vec3 lightColor = spotLightColor[ i ];
55+
56+
vec3 lightPosition = spotLightPosition[ i ];
57+
vec3 lVector = lightPosition + vViewPosition.xyz;
58+
vec3 lightDir = normalize( lVector );
59+
60+
float spotEffect = dot( spotLightDirection[ i ], lightDir );
61+
62+
if ( spotEffect > spotLightAngleCos[ i ] ) {
63+
64+
spotEffect = saturate( pow( saturate( spotEffect ), spotLightExponent[ i ] ) );
65+
66+
// attenuation
67+
68+
float attenuation = calcLightAttenuation( length( lVector ), spotLightDistance[ i ], spotLightDecay[ i ] );
69+
70+
attenuation *= spotEffect;
71+
72+
// diffuse
73+
74+
float cosineTerm = saturate( dot( normal, lightDir ) );
75+
76+
totalDiffuseLight += lightColor * attenuation * cosineTerm;
77+
78+
// specular
79+
80+
vec3 brdf = BRDF_GGX( specularColor, roughnessFactor, normal, lightDir, viewDir );
81+
82+
totalSpecularLight += brdf * specularStrength * lightColor * attenuation * cosineTerm;
83+
84+
}
85+
86+
}
87+
88+
#endif
89+
90+
#if MAX_DIR_LIGHTS > 0
91+
92+
for ( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {
93+
94+
vec3 lightColor = directionalLightColor[ i ];
95+
96+
vec3 lightDir = directionalLightDirection[ i ];
97+
98+
// diffuse
99+
100+
float cosineTerm = saturate( dot( normal, lightDir ) );
101+
102+
totalDiffuseLight += lightColor * cosineTerm;
103+
104+
// specular
105+
106+
vec3 brdf = BRDF_GGX( specularColor, roughnessFactor, normal, lightDir, viewDir );
107+
108+
totalSpecularLight += brdf * specularStrength * lightColor * cosineTerm;
109+
110+
}
111+
112+
#endif
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
float metalnessFactor = metalness;
2+
3+
#ifdef USE_METALNESSMAP
4+
5+
vec4 texelMetalness = texture2D( metalnessMap, vUv );
6+
metalnessFactor = texelMetalness.r; // note equality, not *=
7+
8+
#endif
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#ifdef USE_METALNESSMAP
2+
3+
uniform sampler2D metalnessMap;
4+
5+
#endif
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
float roughnessFactor = roughness;
2+
3+
#ifdef USE_ROUGHNESSMAP
4+
5+
vec4 texelRoughness = texture2D( roughnessMap, vUv );
6+
roughnessFactor *= texelRoughness.r;
7+
8+
#endif

0 commit comments

Comments
 (0)