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

Added processPbrMetallicRoughness #301

Merged
merged 24 commits into from
Aug 7, 2017

Conversation

moneimne
Copy link

@lilleyse, this is the file that has been tested within Cesium.

Copy link
Contributor

@lilleyse lilleyse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start @moneimne!

While there is some duplicate code between this and processModelMaterialsCommon, I think it's fine for now. I added some comments on more general purpose functions that could be separated out cleanly.

Also once integrated with the rest of the pipeline, start writing tests for this function. processModelMaterialsCommonSpec can serve as inspiration.

defined,
defaultValue,
WebGLConstants) {
'use strict';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To start getting this integrated into the rest of the pipeline, change the includes to work with Node. Then call processPbrMetallicRoughness in Pipeline.processJSONWithExtras right after processModelMaterialsCommon.

WebGLConstants) {
'use strict';

function webGLConstantToGlslType(webGLValue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also used in processModelMaterialsCommon so move to its own file.

}
}

function glslTypeToWebGLConstant(glslType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment.

}
}

function generateTechnique(gltf, material, frameState, optimizeForCesium) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove frameState.

/**
* @private
*/
function processPbrMetallicRoughness(gltf, frameState, options) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By convention move this to the top of the file (also fix for processModelMaterialsCommon).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove frameState.

fragmentShader += 'const float M_PI = 3.141592653589793;\n';

var lambertianDiffuse = '';
lambertianDiffuse += 'vec3 lambertianDiffuse(vec3 baseColor) {\n';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In GLSL code we put the { on a separate line.

fragmentShader += lambertianDiffuse + fresnelSchlick2 + fresnelSchlick + smithVisibilityG1 + smithVisibilityGGX + GGX;

var fragmentShaderMain = '';
fragmentShaderMain += 'void main(void) {\n';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combine these two.

var lambertianDiffuse = '';
lambertianDiffuse += 'vec3 lambertianDiffuse(vec3 baseColor) {\n';
lambertianDiffuse += ' return baseColor / M_PI;\n';
lambertianDiffuse += '}\n\n';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing a multiline string is usually cleaner. Also use 4 spaces of indentation throughout.

var lambertianDiffuse =
    'vec3 lambertianDiffuse(vec3 baseColor) {\n' +
    '    return baseColor / M_PI;\n' +
    '}\n\n';

// Generate fragment shader's lighting block
var fragmentLightingBlock = '';
fragmentLightingBlock += ' vec3 lightColor = vec3(1.0, 1.0, 1.0);\n';
fragmentLightingBlock += ' vec3 l = normalize(czm_sunDirectionEC);\n';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using czm_sunDirectionEC should only happen if optimizeForCesium is true. Otherwise the light direction should originate from the camera (0.0, 0.0, 1.0) like how processModelMaterialsCommon handles it.

// Add normal mapping to fragment shader
if (hasNormals) {
fragmentShaderMain += ' vec3 ng = normalize(v_normal);\n';
if (defined(parameterValues.normalTexture) && (hasTangents || frameState.context._standardDerivatives)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard derivatives will need to handled differently since we don't have this info available at conversion time. Check if there is a #define associated with the GL_OES_standard_derivatives extension, and add that in here. Check out https://github.com/AnalyticalGraphicsInc/cesium/blob/master/Source/Shaders/ShadowVolumeFS.glsl as an example.


if (optimizeForCesium) {
fragmentShader += ' vec3 diffuseIrradiance = vec3(0.5);\n';
fragmentShader += ' vec3 specularIrradiance = textureCube(czm_cubeMap, r).rgb;\n';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be u_SpecularEnvSampler instead of using a Cesium builtin?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should probably clarify the name, but I added the specular environment map as a Cesium builtin for now since anyone using Cesium will use the same map.

fragmentShader += ' vec3 diffuseIrradiance = vec3(0.5);\n';
fragmentShader += ' vec3 specularIrradiance = textureCube(czm_cubeMap, r).rgb;\n';
fragmentShader += ' specularIrradiance = mix(specularIrradiance, diffuseIrradiance, roughness);\n'; // Fake LOD
fragmentShader += ' vec2 brdfLUT = texture2D(czm_brdfLUT, vec2(NdotV, 1.0 - roughness)).rg;\n';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above, should you only use a Cesium builtin if optimizeForCesium?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! Made this change in a recent commit.

fragmentShader += ' vec3 color = NdotL * lightColor * (diffuseContribution + specularContribution);\n';

if (optimizeForCesium) {
fragmentShader += ' vec3 diffuseIrradiance = vec3(0.5);\n';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will there be an option to sample a diffuse environment map too? Similar to how we sample diffuse in the glTF-WebGL-PBR reference implementation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for now, we are using a very simple gradient as the environment map, so I thought a diffuse gray would be acceptable for the diffuse irradiance. The change to using a proper diffuse environment map would be simple to add, but I think this can come with a switch to a more complex environment map.

@moneimne
Copy link
Author

@lilleyse, all the models from the sample repo have been tested and load correctly into Cesium when passed through the pipeline.

@lilleyse
Copy link
Contributor

The latest code changes look good.

There are some jsHint errors and test failures.

(Yes, this branch and 2.0 are pre-eslint).

@lilleyse
Copy link
Contributor

lilleyse commented Aug 7, 2017

Thanks @moneimne. Will merge this into the 2.0 branch and then leave that parked for a little while.

@lilleyse lilleyse merged commit 6965cf0 into CesiumGS:2.0 Aug 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants