Skip to content

Commit

Permalink
MeshBasicMaterial: Add support for lightMap (#9975)
Browse files Browse the repository at this point in the history
  • Loading branch information
WestLangley authored and mrdoob committed Nov 6, 2016
1 parent f1edcc0 commit f8df139
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
12 changes: 10 additions & 2 deletions docs/api/materials/MeshBasicMaterial.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta charset="utf-8" />
<base href="../../" />
<script src="list.js"></script>
<script src="page.js"></script>
Expand Down Expand Up @@ -41,6 +41,8 @@ <h3>[name]( [page:Object parameters] )</h3>
<div>
color — geometry color in hexadecimal. Default is 0xffffff.<br />
map — Set texture map. Default is null <br />
lightMap — Set light map. Default is null.<br />
lightMapIntensity — Set light map intensity. Default is 1.<br />
aoMap — Set ao map. Default is null.<br />
aoMapIntensity — Set ao map intensity. Default is 1.<br />
specularMap — Set specular map. Default is null.<br />
Expand Down Expand Up @@ -71,11 +73,17 @@ <h3>[property:Texture map]</h3>
Set texture map. Default is null.
</div>

<h3>[property:Texture lightMap]</h3>
<div>Set light map. Default is null. The lightMap requires a second set of UVs.</div>

<h3>[property:Float lightMapIntensity]</h3>
<div>Intensity of the baked light. Default is 1.</div>

<h3>[property:Texture aoMap]</h3>
<div>Set ambient occlusion map. Default is null.</div>

<h3>[property:Float aoMapIntensity]</h3>
<div>TODO</div>
<div>Intensity of the ambient occlusion effect. Default is 1. Zero is no occlusion effect.</div>

<h3>[property:Texture specularMap]</h3>
<div>Set specular map. Default is null.</div>
Expand Down
9 changes: 9 additions & 0 deletions src/materials/MeshBasicMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { Color } from '../math/Color';
* opacity: <float>,
* map: new THREE.Texture( <Image> ),
*
* lightMap: new THREE.Texture( <Image> ),
* lightMapIntensity: <float>
*
* aoMap: new THREE.Texture( <Image> ),
* aoMapIntensity: <float>
*
Expand Down Expand Up @@ -45,6 +48,9 @@ function MeshBasicMaterial( parameters ) {

this.map = null;

this.lightMap = null;
this.lightMapIntensity = 1.0;

this.aoMap = null;
this.aoMapIntensity = 1.0;

Expand Down Expand Up @@ -84,6 +90,9 @@ MeshBasicMaterial.prototype.copy = function ( source ) {

this.map = source.map;

this.lightMap = source.lightMap;
this.lightMapIntensity = source.lightMapIntensity;

this.aoMap = source.aoMap;
this.aoMapIntensity = source.aoMapIntensity;

Expand Down
28 changes: 7 additions & 21 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,13 @@ function WebGLRenderer( parameters ) {
uniforms.specularMap.value = material.specularMap;
uniforms.alphaMap.value = material.alphaMap;

if ( material.lightMap ) {

uniforms.lightMap.value = material.lightMap;
uniforms.lightMapIntensity.value = material.lightMapIntensity;

}

if ( material.aoMap ) {

uniforms.aoMap.value = material.aoMap;
Expand Down Expand Up @@ -2083,13 +2090,6 @@ function WebGLRenderer( parameters ) {

function refreshUniformsLambert( uniforms, material ) {

if ( material.lightMap ) {

uniforms.lightMap.value = material.lightMap;
uniforms.lightMapIntensity.value = material.lightMapIntensity;

}

if ( material.emissiveMap ) {

uniforms.emissiveMap.value = material.emissiveMap;
Expand All @@ -2103,13 +2103,6 @@ function WebGLRenderer( parameters ) {
uniforms.specular.value = material.specular;
uniforms.shininess.value = Math.max( material.shininess, 1e-4 ); // to prevent pow( 0.0, 0.0 )

if ( material.lightMap ) {

uniforms.lightMap.value = material.lightMap;
uniforms.lightMapIntensity.value = material.lightMapIntensity;

}

if ( material.emissiveMap ) {

uniforms.emissiveMap.value = material.emissiveMap;
Expand Down Expand Up @@ -2157,13 +2150,6 @@ function WebGLRenderer( parameters ) {

}

if ( material.lightMap ) {

uniforms.lightMap.value = material.lightMap;
uniforms.lightMapIntensity.value = material.lightMapIntensity;

}

if ( material.emissiveMap ) {

uniforms.emissiveMap.value = material.emissiveMap;
Expand Down
1 change: 1 addition & 0 deletions src/renderers/shaders/ShaderLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var ShaderLib = {
uniforms: Object.assign( {},
UniformsLib.common,
UniformsLib.aomap,
UniformsLib.lightmap,
UniformsLib.fog
),

Expand Down
21 changes: 16 additions & 5 deletions src/renderers/shaders/ShaderLib/meshbasic_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ uniform float opacity;
#include <map_pars_fragment>
#include <alphamap_pars_fragment>
#include <aomap_pars_fragment>
#include <lightmap_pars_fragment>
#include <envmap_pars_fragment>
#include <fog_pars_fragment>
#include <specularmap_pars_fragment>
Expand All @@ -33,14 +34,24 @@ void main() {
#include <alphatest_fragment>
#include <specularmap_fragment>

ReflectedLight reflectedLight;
reflectedLight.directDiffuse = vec3( 0.0 );
reflectedLight.directSpecular = vec3( 0.0 );
reflectedLight.indirectDiffuse = diffuseColor.rgb;
reflectedLight.indirectSpecular = vec3( 0.0 );
ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );

// accumulation (baked indirect lighting only)
#ifdef USE_LIGHTMAP

reflectedLight.indirectDiffuse += texture2D( lightMap, vUv2 ).xyz * lightMapIntensity;

#else

reflectedLight.indirectDiffuse += vec3( 1.0 );

#endif

// modulation
#include <aomap_fragment>

reflectedLight.indirectDiffuse *= diffuseColor.rgb;

vec3 outgoingLight = reflectedLight.indirectDiffuse;

#include <normal_flip>
Expand Down

0 comments on commit f8df139

Please sign in to comment.