Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add environment light intensity in GLSL #1737

2 changes: 1 addition & 1 deletion libraries/pbrlib/genglsl/lib/mx_microfacet_specular.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ vec3 mx_latlong_map_lookup(vec3 dir, mat4 transform, float lod, sampler2D envSam
{
vec3 envDir = normalize((transform * vec4(dir,0.0)).xyz);
vec2 uv = mx_latlong_projection(envDir);
return textureLod(envSampler, uv, lod).rgb;
return textureLod(envSampler, uv, lod).rgb * $envLightIntensity;
}

// Return the mip level with the appropriate coverage for a filtered importance sample.
Expand Down
7 changes: 6 additions & 1 deletion resources/Lights/environment_map.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@
<input name="filtertype" type="string" value="linear" />
</image>

<multiply name="envImageAdjusted" type="color3">
<input name="in1" type="color3" nodename="envImage" />
<input name="in2" type="float" value="1.0" />
</multiply>

<!-- Return the resulting color -->
<output name="out" type="color3" nodename="envImage" />
<output name="out" type="color3" nodename="envImageAdjusted" />
</nodegraph>
</materialx>
5 changes: 5 additions & 0 deletions source/MaterialXGenShader/HwShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const string T_ENV_RADIANCE = "$envRadiance";
const string T_ENV_RADIANCE_MIPS = "$envRadianceMips";
const string T_ENV_RADIANCE_SAMPLES = "$envRadianceSamples";
const string T_ENV_IRRADIANCE = "$envIrradiance";
const string T_ENV_LIGHT_INTENSITY = "$envLightIntensity";
const string T_ENV_PREFILTER_MIP = "$envPrefilterMip";
const string T_REFRACTION_TWO_SIDED = "$refractionTwoSided";
const string T_ALBEDO_TABLE = "$albedoTable";
Expand Down Expand Up @@ -132,6 +133,7 @@ const string ENV_RADIANCE = "u_envRadiance";
const string ENV_RADIANCE_MIPS = "u_envRadianceMips";
const string ENV_RADIANCE_SAMPLES = "u_envRadianceSamples";
const string ENV_IRRADIANCE = "u_envIrradiance";
const string ENV_LIGHT_INTENSITY = "u_envLightIntensity";
const string ENV_PREFILTER_MIP = "u_envPrefilterMip";
const string REFRACTION_TWO_SIDED = "u_refractionTwoSided";
const string ALBEDO_TABLE = "u_albedoTable";
Expand Down Expand Up @@ -233,6 +235,7 @@ HwShaderGenerator::HwShaderGenerator(SyntaxPtr syntax) :
_tokenSubstitutions[HW::T_ENV_RADIANCE_MIPS] = HW::ENV_RADIANCE_MIPS;
_tokenSubstitutions[HW::T_ENV_RADIANCE_SAMPLES] = HW::ENV_RADIANCE_SAMPLES;
_tokenSubstitutions[HW::T_ENV_IRRADIANCE] = HW::ENV_IRRADIANCE;
_tokenSubstitutions[HW::T_ENV_LIGHT_INTENSITY] = HW::ENV_LIGHT_INTENSITY;
_tokenSubstitutions[HW::T_REFRACTION_TWO_SIDED] = HW::REFRACTION_TWO_SIDED;
_tokenSubstitutions[HW::T_ALBEDO_TABLE] = HW::ALBEDO_TABLE;
_tokenSubstitutions[HW::T_ALBEDO_TABLE_SIZE] = HW::ALBEDO_TABLE_SIZE;
Expand Down Expand Up @@ -366,6 +369,7 @@ ShaderPtr HwShaderGenerator::createShader(const string& name, ElementPtr element
const Matrix44 yRotationPI = Matrix44::createScale(Vector3(-1, 1, -1));
psPrivateUniforms->add(Type::MATRIX44, HW::T_ENV_MATRIX, Value::createValue(yRotationPI));
psPrivateUniforms->add(Type::FILENAME, HW::T_ENV_RADIANCE);
psPrivateUniforms->add(Type::FLOAT, HW::T_ENV_LIGHT_INTENSITY, Value::createValue(1.0f));
psPrivateUniforms->add(Type::INTEGER, HW::T_ENV_RADIANCE_MIPS, Value::createValue<int>(1));
psPrivateUniforms->add(Type::INTEGER, HW::T_ENV_RADIANCE_SAMPLES, Value::createValue<int>(16));
psPrivateUniforms->add(Type::FILENAME, HW::T_ENV_IRRADIANCE);
Expand All @@ -384,6 +388,7 @@ ShaderPtr HwShaderGenerator::createShader(const string& name, ElementPtr element
if (context.getOptions().hwWriteEnvPrefilter)
{
psPrivateUniforms->add(Type::FILENAME, HW::T_ENV_RADIANCE);
psPrivateUniforms->add(Type::FLOAT, HW::T_ENV_LIGHT_INTENSITY, Value::createValue(1.0f));
psPrivateUniforms->add(Type::INTEGER, HW::T_ENV_PREFILTER_MIP, Value::createValue<int>(1));
const Matrix44 yRotationPI = Matrix44::createScale(Vector3(-1, 1, -1));
psPrivateUniforms->add(Type::MATRIX44, HW::T_ENV_MATRIX, Value::createValue(yRotationPI));
Expand Down
3 changes: 3 additions & 0 deletions source/MaterialXGenShader/HwShaderGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Uniform variables :
$envMatrix u_envMatrix mat4 Rotation matrix for the environment.
$envIrradiance u_envIrradiance sampler2D Sampler for the texture used for diffuse environment lighting.
$envRadiance u_envRadiance sampler2D Sampler for the texture used for specular environment lighting.
$envLightIntensity u_envLightIntensity float Linear multiplier for environment lighting
$envRadianceMips u_envRadianceMips int Number of mipmaps used on the specular environment texture.
$envRadianceSamples u_envRadianceSamples int Samples to use if Filtered Importance Sampling is used for specular environment lighting.

Expand Down Expand Up @@ -126,6 +127,7 @@ extern MX_GENSHADER_API const string T_ENV_RADIANCE;
extern MX_GENSHADER_API const string T_ENV_RADIANCE_MIPS;
extern MX_GENSHADER_API const string T_ENV_RADIANCE_SAMPLES;
extern MX_GENSHADER_API const string T_ENV_IRRADIANCE;
extern MX_GENSHADER_API const string T_ENV_LIGHT_INTENSITY;
extern MX_GENSHADER_API const string T_ENV_PREFILTER_MIP;
extern MX_GENSHADER_API const string T_REFRACTION_TWO_SIDED;
extern MX_GENSHADER_API const string T_ALBEDO_TABLE;
Expand Down Expand Up @@ -183,6 +185,7 @@ extern MX_GENSHADER_API const string ENV_RADIANCE;
extern MX_GENSHADER_API const string ENV_RADIANCE_MIPS;
extern MX_GENSHADER_API const string ENV_RADIANCE_SAMPLES;
extern MX_GENSHADER_API const string ENV_IRRADIANCE;
extern MX_GENSHADER_API const string ENV_LIGHT_INTENSITY;
extern MX_GENSHADER_API const string ENV_PREFILTER_MIP;
extern MX_GENSHADER_API const string REFRACTION_TWO_SIDED;
extern MX_GENSHADER_API const string ALBEDO_TABLE;
Expand Down
14 changes: 14 additions & 0 deletions source/MaterialXRender/LightHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class MX_RENDER_API LightHandler
_directLighting(true),
_indirectLighting(true),
_usePrefilteredMap(false),
_envLightIntensity(1.0f),
_envSampleCount(DEFAULT_ENV_SAMPLE_COUNT),
_refractionTwoSided(false)
{
Expand Down Expand Up @@ -150,6 +151,18 @@ class MX_RENDER_API LightHandler
return _envSampleCount;
}

/// Set the environment light intensity.
void setEnvLightIntensity(const float intensity)
{
_envLightIntensity = intensity;
}

/// Return the environment light intensity.
float getEnvLightIntensity()
{
return _envLightIntensity;
}

/// Set the two-sided refraction property.
void setRefractionTwoSided(bool enable)
{
Expand Down Expand Up @@ -246,6 +259,7 @@ class MX_RENDER_API LightHandler
ImagePtr _envRadianceMap;
ImagePtr _envPrefilteredMap;
ImagePtr _envIrradianceMap;
float _envLightIntensity;
int _envSampleCount;

bool _refractionTwoSided;
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXRenderGlsl/GlslProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ void GlslProgram::bindLighting(LightHandlerPtr lightHandler, ImageHandlerPtr ima
Matrix44 envRotation = Matrix44::createRotationY(PI) * lightHandler->getLightTransform().getTranspose();
bindUniform(HW::ENV_MATRIX, Value::createValue(envRotation), false);
bindUniform(HW::ENV_RADIANCE_SAMPLES, Value::createValue(lightHandler->getEnvSampleCount()), false);
bindUniform(HW::ENV_LIGHT_INTENSITY, Value::createValue(lightHandler->getEnvLightIntensity()), false);
ImagePtr envRadiance = nullptr;
if (lightHandler->getIndirectLighting())
{
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXRenderMsl/MslPipelineStateObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ int GetStrideOfMetalType(MTLDataType type)
Matrix44 envRotation = Matrix44::createRotationY(PI) * lightHandler->getLightTransform().getTranspose();
bindUniform(HW::ENV_MATRIX, Value::createValue(envRotation), false);
bindUniform(HW::ENV_RADIANCE_SAMPLES, Value::createValue(lightHandler->getEnvSampleCount()), false);
bindUniform(HW::ENV_LIGHT_INTENSITY, Value::createValue(lightHandler->getEnvLightIntensity()), false);
ImageMap envLights =
{
{ HW::ENV_RADIANCE, lightHandler->getEnvRadianceMap() },
Expand Down
3 changes: 3 additions & 0 deletions source/MaterialXView/RenderPipelineGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ void GLRenderPipeline::renderFrame(void*, int shadowMapSize, const char* dirLigh
float longitudeOffset = (lightRotation / 360.0f) + 0.5f;
envMaterial->modifyUniform("longitude/in2", mx::Value::createValue(longitudeOffset));

// Apply light intensity to the environment shader.
jstone-lucasfilm marked this conversation as resolved.
Show resolved Hide resolved
envMaterial->modifyUniform("envImageAdjusted/in2", mx::Value::createValue(lightHandler->getEnvLightIntensity()));

// Render the environment mesh.
glDepthMask(GL_FALSE);
envMaterial->bindShader();
Expand Down
17 changes: 17 additions & 0 deletions source/MaterialXView/Viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ Viewer::Viewer(const std::string& materialFilename,
_userCameraEnabled(true),
_userTranslationActive(false),
_lightRotation(0.0f),
_intensityMultiplier(1.0f),
_normalizeEnvironment(false),
_splitDirectLight(false),
_generateReferenceIrradiance(false),
Expand Down Expand Up @@ -817,6 +818,22 @@ void Viewer::createAdvancedSettings(Widget* parent)
});
lightRotationBox->set_editable(true);

Widget* envLightIntensityRow = new Widget(advancedPopup);
envLightIntensityRow->set_layout(new ng::BoxLayout(ng::Orientation::Horizontal));
mx::UIProperties intensityUI;
intensityUI.uiSoftMin = mx::Value::createValue(0.0f);
intensityUI.uiSoftMax = mx::Value::createValue(10.0f);
intensityUI.uiStep = mx::Value::createValue(0.1f);

ng::FloatBox<float>* envIntensity = createFloatWidget(envLightIntensityRow, "Light Intensity:",
_intensityMultiplier, &intensityUI, [this](float value)
{
_intensityMultiplier = value;
_lightHandler->setEnvLightIntensity(_intensityMultiplier);
});
envIntensity->set_value(1.0);
envIntensity->set_editable(true);

ng::Label* shadowingLabel = new ng::Label(advancedPopup, "Shadowing Options");
shadowingLabel->set_font_size(20);
shadowingLabel->set_font("sans-bold");
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXView/Viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ class Viewer : public ng::Screen
mx::FilePath _lightRigFilename;
mx::DocumentPtr _lightRigDoc;
float _lightRotation;
float _intensityMultiplier;

// Light processing options
bool _normalizeEnvironment;
Expand Down