Skip to content

Commit

Permalink
parse arrays of struct uniforms, code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Oct 10, 2015
1 parent ad75704 commit 74d3aa3
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/renderers/shaders/ShaderChunk/light_pars.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
//uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];
//uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];

struct HemisphericLight {
struct HemisphereLight {
vec3 skyColor;
vec3 groundColor;
vec3 direction;
};

uniform HemisphericLight hemisphericLights[ MAX_HEMI_LIGHTS ];
uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];

#endif

Expand Down
112 changes: 112 additions & 0 deletions src/renderers/shaders/ShaderChunk/lights_lambert_pars_vertex.glsl
Original file line number Diff line number Diff line change
@@ -1 +1,113 @@
uniform vec3 ambientLightColor;


#if MAX_DIR_LIGHTS > 0

//uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];
//uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];

struct DirectionalLight {
vec3 color;
vec3 direction;
};

uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];

void getDirLight( const in DirectionalLight directionalLight, out vec3 lightDir, out vec3 lightColor ) {

lightDir = directionalLight.direction;
lightColor = directionalLight.color;

}

#endif

#if MAX_HEMI_LIGHTS > 0

//uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];
//uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];
//uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];

struct HemisphereLight {
vec3 skyColor;
vec3 groundColor;
vec3 direction;
};

uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];

#endif

#if MAX_POINT_LIGHTS > 0

//uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];
//uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];
//uniform float pointLightDistance[ MAX_POINT_LIGHTS ];
//uniform float pointLightDecay[ MAX_POINT_LIGHTS ];

struct PointLight {
vec3 color;
vec3 position;
float decay;
float distance;
};

uniform PointLight pointLights[ MAX_POINT_LIGHTS ];

void getPointLight( const in PointLight pointLight, out vec3 lightDir, out vec3 lightColor ) {

vec3 lightPosition = pointLight.position;

vec3 lVector = lightPosition + vViewPosition.xyz;
lightDir = normalize( lVector );

lightColor = pointLight.color;
lightColor *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay );

}

#endif

#if MAX_SPOT_LIGHTS > 0

//uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];
//uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];
//uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];
//uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];
//uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];
//uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];
//uniform float spotLightDecay[ MAX_SPOT_LIGHTS ];

struct SpotLight {
vec3 color;
vec3 position;
vec3 direction;
float angleCos;
float exponent;
float distance;
float decay;
};

uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];

void getSpotLight( const in SpotLight spotLight, out vec3 lightDir, out vec3 lightColor ) {

vec3 lightPosition = spotLight.position;

vec3 lVector = lightPosition + vViewPosition.xyz;
lightDir = normalize( lVector );

float spotEffect = dot( spotLight.direction, lightDir );
spotEffect = saturate( pow( saturate( spotEffect ), spotLight.exponent ) );

lightColor = spotLight.color;
lightColor *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) );

}

#endif





8 changes: 4 additions & 4 deletions src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ vec3 viewDir = normalize( vViewPosition );

vec3 totalReflectedLight = vec3( 0.0 );

var diffuse = diffuseColor.rgb;
vec3 diffuse = diffuseColor.rgb;

#ifdef METAL

Expand All @@ -15,7 +15,7 @@ var diffuse = diffuseColor.rgb;
for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {

vec3 lightDir, lightIntensity;
getSpotLight( i, lightDir, lightIntensity );
getPointLight( pointLights[i], lightDir, lightIntensity );

if( dot( lightIntensity, lightIntensity ) > 0.0 ) {

Expand All @@ -40,7 +40,7 @@ var diffuse = diffuseColor.rgb;
for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {

vec3 lightDir, lightIntensity;
getSpotLight( i, lightDir, lightIntensity );
getSpotLight( spotLights[i], lightDir, lightIntensity );

if( dot( lightIntensity, lightIntensity ) > 0.0 ) {

Expand All @@ -65,7 +65,7 @@ var diffuse = diffuseColor.rgb;
for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {

vec3 lightDir, lightIntensity;
getDirLight( i, lightDir, lightIntensity );
getDirLight( directionalLights[i], lightDir, lightIntensity );

if( dot( lightIntensity, lightIntensity ) > 0.0 ) {

Expand Down
112 changes: 112 additions & 0 deletions src/renderers/shaders/ShaderChunk/lights_phong_pars_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,115 @@ varying vec3 vViewPosition;
varying vec3 vNormal;

#endif


#if MAX_DIR_LIGHTS > 0

//uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];
//uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];

struct DirectionalLight {
vec3 color;
vec3 direction;
};

uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];

void getDirLight( const in DirectionalLight directionalLight, out vec3 lightDir, out vec3 lightColor ) {

lightDir = directionalLight.direction;
lightColor = directionalLight.color;

}

#endif

#if MAX_HEMI_LIGHTS > 0

//uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];
//uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];
//uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];

struct HemisphereLight {
vec3 skyColor;
vec3 groundColor;
vec3 direction;
};

uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];

#endif

#if MAX_POINT_LIGHTS > 0

//uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];
//uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];
//uniform float pointLightDistance[ MAX_POINT_LIGHTS ];
//uniform float pointLightDecay[ MAX_POINT_LIGHTS ];

struct PointLight {
vec3 color;
vec3 position;
float decay;
float distance;
};

uniform PointLight pointLights[ MAX_POINT_LIGHTS ];

void getPointLight( const in PointLight pointLight, out vec3 lightDir, out vec3 lightColor ) {

vec3 lightPosition = pointLight.position;

vec3 lVector = lightPosition + vViewPosition.xyz;
lightDir = normalize( lVector );

lightColor = pointLight.color;
lightColor *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay );

}

#endif

#if MAX_SPOT_LIGHTS > 0

//uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];
//uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];
//uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];
//uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];
//uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];
//uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];
//uniform float spotLightDecay[ MAX_SPOT_LIGHTS ];

struct SpotLight {
vec3 color;
vec3 position;
vec3 direction;
float angleCos;
float exponent;
float distance;
float decay;
};

uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];

void getSpotLight( const in SpotLight spotLight, out vec3 lightDir, out vec3 lightColor ) {

vec3 lightPosition = spotLight.position;

vec3 lVector = lightPosition + vViewPosition.xyz;
lightDir = normalize( lVector );

float spotEffect = dot( spotLight.direction, lightDir );
spotEffect = saturate( pow( saturate( spotEffect ), spotLight.exponent ) );

lightColor = spotLight.color;
lightColor *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) );

}

#endif





2 changes: 2 additions & 0 deletions src/renderers/shaders/ShaderLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ THREE.ShaderLib = {
THREE.ShaderChunk[ "uv2_pars_vertex" ],
THREE.ShaderChunk[ "envmap_pars_vertex" ],
THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
THREE.ShaderChunk[ "lights_pars" ],
THREE.ShaderChunk[ "color_pars_vertex" ],
THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
THREE.ShaderChunk[ "skinning_pars_vertex" ],
Expand Down Expand Up @@ -347,6 +348,7 @@ THREE.ShaderLib = {
THREE.ShaderChunk[ "envmap_pars_fragment" ],
THREE.ShaderChunk[ "fog_pars_fragment" ],
THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
THREE.ShaderChunk[ "lights_pars" ],
THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
THREE.ShaderChunk[ "normalmap_pars_fragment" ],
Expand Down
35 changes: 31 additions & 4 deletions src/renderers/webgl/WebGLProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,53 @@ THREE.WebGLProgram = ( function () {
var uniforms = {};

var n = gl.getProgramParameter( program, gl.ACTIVE_UNIFORMS );

var arrayStructRe = /^([\w\d_]+)\[(\d+)\]\.([\w\d_]+)$/;
var arrayRe = /^([\w\d_]+)\[0\]$/;

for ( var i = 0; i < n; i ++ ) {

var info = gl.getActiveUniform( program, i );
var name = info.name;
var location = gl.getUniformLocation( program, name );

// console.log("THREE.WebGLProgram: ACTIVE UNIFORM:", name);
console.log("THREE.WebGLProgram: ACTIVE UNIFORM:", name);

var suffixPos = name.lastIndexOf( '[0]' );
if ( suffixPos !== - 1 && suffixPos === name.length - 3 ) {
var matches = arrayStructRe.exec(name);
if( matches ) {

uniforms[ name.substr( 0, suffixPos ) ] = location;
var arrayName = matches[1];
var arrayIndex = matches[2];
var arrayProperty = matches[3];

var uniformsArray = uniforms[ arrayName ];
if( ! uniformsArray ) {
uniformsArray = uniforms[ arrayName ] = [];
}
var uniformsArrayIndex = uniformsArray[ arrayIndex ];
if( ! uniformsArrayIndex ) {
uniformsArrayIndex = uniformsArray[ arrayIndex ] = {};
}
uniformsArrayIndex[ arrayProperty ] = location;

continue;
}

matches = arrayRe.exec(name)
if( matches ) {

var arrayName = matches[1];

uniforms[ arrayName ] = location;

continue;
}

uniforms[ name ] = location;

}

console.log("uniforms", uniforms);
return uniforms;

}
Expand Down

0 comments on commit 74d3aa3

Please sign in to comment.