Skip to content

Commit

Permalink
Merge pull request #318 from shehzan10/normals-fix
Browse files Browse the repository at this point in the history
Fixes in modelMaterialsCommon
  • Loading branch information
lilleyse committed Sep 19, 2017
2 parents 816df1d + 61cd91a commit 914fb57
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions lib/processModelMaterialsCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,27 @@ var vertexShaderCount = 0;
var fragmentShaderCount = 0;
var programCount = 0;

function generateTechnique(gltf, khrMaterialsCommon, lightParameters, optimizeForCesium) {
function checkMaterialMeshesForNormals(gltf, materialId) {
var meshes = gltf.meshes;
for (var meshId in meshes) {
if (meshes.hasOwnProperty(meshId)) {
var mesh = meshes[meshId];
if (defined(mesh.primitives)) {
var primitivesLength = mesh.primitives.length;
for (var p = 0; p < primitivesLength; p++) {
if (mesh.primitives[p].material === materialId) {
if (defined(mesh.primitives[p].attributes.NORMAL)) {
return true;
}
}
}
}
}
}
return false;
}

function generateTechnique(gltf, materialId, khrMaterialsCommon, lightParameters, optimizeForCesium) {
var techniques = gltf.techniques;
var shaders = gltf.shaders;
var programs = gltf.programs;
Expand All @@ -208,13 +228,19 @@ function generateTechnique(gltf, khrMaterialsCommon, lightParameters, optimizeFo
var fragmentShaderId = getNextId(shaders, 'fragmentShader', fragmentShaderCount);
var programId = getNextId(programs, 'program', programCount);

var hasNormals = (lightingModel !== 'CONSTANT');
var hasNormals = (lightingModel !== 'CONSTANT' && checkMaterialMeshesForNormals(gltf, materialId));

// Add techniques

var modelViewSemantic = 'MODELVIEW';
if (gltf.extensionsUsed.indexOf('CESIUM_RTC') >= 0) {
modelViewSemantic = 'CESIUM_RTC_MODELVIEW';
}

var techniqueParameters = {
// Add matrices
modelViewMatrix: {
semantic: 'MODELVIEW',
semantic: modelViewSemantic,
type: WebGLConstants.FLOAT_MAT4
},
projectionMatrix: {
Expand Down Expand Up @@ -483,7 +509,8 @@ function generateTechnique(gltf, khrMaterialsCommon, lightParameters, optimizeFo
} else {
fragmentLightingBlock += ' vec3 l = vec3(0.0, 0.0, 1.0);\n';
}
fragmentLightingBlock += ' diffuseLight += vec3(1.0, 1.0, 1.0) * max(dot(normal,l), 0.);\n';
var minimumLighting = optimizeForCesium ? 0.2 : 0.0;
fragmentLightingBlock += ' diffuseLight += vec3(1.0, 1.0, 1.0) * max(dot(normal,l), ' + minimumLighting + ');\n';

if (hasSpecular) {
if (lightingModel === 'BLINN') {
Expand Down Expand Up @@ -948,7 +975,7 @@ function modelMaterialsCommon(gltf, options) {
var techniqueKey = getTechniqueKey(khrMaterialsCommon);
var technique = techniques[techniqueKey];
if (!defined(technique)) {
technique = generateTechnique(gltf, khrMaterialsCommon, lightParameters, options.optimizeForCesium);
technique = generateTechnique(gltf, materialId, khrMaterialsCommon, lightParameters, options.optimizeForCesium);
techniques[techniqueKey] = technique;
}

Expand Down

0 comments on commit 914fb57

Please sign in to comment.