-
Notifications
You must be signed in to change notification settings - Fork 241
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
2.0 pbr #274
2.0 pbr #274
Changes from 2 commits
06a0954
d373dc2
d9842a0
5c722f9
d7d9391
8f83cc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
var Cesium = require('cesium'); | ||
var ForEach = require('./ForEach'); | ||
|
||
var defined = Cesium.defined; | ||
|
||
module.exports = getJointCountForMaterials; | ||
|
||
function getJointCountForMaterials(gltf) { | ||
var accessors = gltf.accessors; | ||
var meshes = gltf.meshes; | ||
var jointCountForMaterialId = {}; | ||
|
||
var nodesForSkinId = {}; | ||
ForEach.node(gltf, function(node) { | ||
if (defined(node.skin)) { | ||
if (!defined(nodesForSkinId[node.skin])) { | ||
nodesForSkinId[node.skin] = []; | ||
} | ||
nodesForSkinId[node.skin].push(node); | ||
} | ||
}); | ||
|
||
ForEach.skin(gltf, function(skin, skinId) { | ||
var jointCount = 1; | ||
if (defined(skin.inverseBindMatrices)) { | ||
jointCount = accessors[skin.inverseBindMatrices].count; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would make more sense to check |
||
var skinnedNodes = nodesForSkinId[skinId]; | ||
var skinnedNodesLength = skinnedNodes.length; | ||
for (var i = 0; i < skinnedNodesLength; i++) { | ||
var skinnedNode = skinnedNodes[i]; | ||
var meshId = skinnedNode.mesh; | ||
if (defined(meshId)) { | ||
var mesh = meshes[meshId]; | ||
var primitives = mesh.primitives; | ||
var primitivesLength = primitives.length; | ||
for (var k = 0; k < primitivesLength; k++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
var primitive = primitives[k]; | ||
jointCountForMaterialId[primitive.material] = jointCount; | ||
} | ||
} | ||
} | ||
}); | ||
|
||
return jointCountForMaterialId; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
'use strict'; | ||
var Cesium = require('cesium'); | ||
var addExtensionsRequired = require('./addExtensionsRequired'); | ||
var ForEach = require('./ForEach'); | ||
var getJointCountForMaterials = require('./getJointCountForMaterials'); | ||
var removeExtensionsUsed = require('./removeExtensionsUsed'); | ||
|
||
var defined = Cesium.defined; | ||
|
||
module.exports = pbrToMaterialsCommon; | ||
|
||
/** | ||
* Convert PBR materials to use a KHR_materials_common BLINN shader. | ||
* This is a lossy conversion, and as such should only be used for | ||
* compatibility reasons. Normal texture and metallic roughness information | ||
* will be lost, and some color or texture information may be lost. | ||
* | ||
* @param {Object} gltf A javascript object containing a glTF asset. | ||
* @returns {Object} The glTF asset with materials converted to KHR_materials_common | ||
*/ | ||
function pbrToMaterialsCommon(gltf) { | ||
var materialJointCount = getJointCountForMaterials(gltf); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If possible, |
||
ForEach.material(gltf, function(material, materialId) { | ||
var values = { | ||
ambient : [ 0.0, 0.0, 0.0, 1.0 ], | ||
diffuse : [ 0.0, 0.0, 0.0, 1.0 ], | ||
emission : [ 0.0, 0.0, 0.0, 1.0 ], | ||
specular : [ 0.0, 0.0, 0.0, 1.0], | ||
shininess : [ 0.0 ] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I forget, what's the latest on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a float, not an array of one float. https://github.com/KhronosGroup/glTF/tree/2.0/extensions/Khronos/KHR_materials_common There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eh, that's massively out of date... I don't really have a good answer other than that glTF 2.0 materials pre-PBR required all values to be arrays. I will put this in line with what is there currently, but it will probably change to something closer to this when it gets updated. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'm actually going to keep this as is, just considering the dated nature of the KHR_materials_common spec. For example, the spec doesn't include If anyone feels strongly opposed to that, I'm definitely open to discussing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note, this will probably be replaced by: KhronosGroup/glTF#947 at some point, so I wouldn't worry about it too much |
||
}; | ||
var pbrMetallicRoughness = material.pbrMetallicRoughness; | ||
var isPBR = defined(pbrMetallicRoughness) || | ||
(defined(material.extensions) && defined(material.extensions.KHR_materials_pbrSpecularGlossiness) || | ||
defined(material.occlusionTexture) || | ||
defined(material.emissiveFactor) || | ||
defined(material.emissiveTexture) || | ||
defined(material.doubleSided)); | ||
|
||
if (defined(pbrMetallicRoughness)) { | ||
var baseColorFactor = pbrMetallicRoughness.baseColorFactor; | ||
if (defined(baseColorFactor)) { | ||
values.diffuse = baseColorFactor; | ||
} | ||
var baseColorTexture = pbrMetallicRoughness.baseColorTexture; | ||
if (defined(baseColorTexture)) { | ||
values.diffuse = [baseColorTexture.index]; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to estimate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be possible to calculate, not estimate, I would expect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @emackey do you know of any resources for this that I should be looking at? |
||
} | ||
var extensions = material.extensions; | ||
if (defined(extensions)) { | ||
var pbrSpecularGlossiness = extensions.KHR_materials_pbrSpecularGlossiness; | ||
if (defined(pbrSpecularGlossiness)) { | ||
var diffuseFactor = pbrSpecularGlossiness.diffuseFactor; | ||
// Use baseColorTexture if it was defined | ||
if (defined(diffuseFactor) && values.diffuse.length > 1) { | ||
values.diffuse = diffuseFactor; | ||
} | ||
// diffuseTexture should be used instead of baseColorTexture if defined | ||
var diffuseTexture = pbrSpecularGlossiness.diffuseTexture; | ||
if (defined(diffuseTexture)) { | ||
values.diffuse = [diffuseTexture.index]; | ||
} | ||
var specularFactor = pbrSpecularGlossiness.specularFactor; | ||
if (defined(specularFactor)) { | ||
values.specular[0] = specularFactor[0]; | ||
values.specular[1] = specularFactor[1]; | ||
values.specular[2] = specularFactor[2]; | ||
} | ||
var glossinessFactor = pbrSpecularGlossiness.glossinessFactor; | ||
if (defined(glossinessFactor)) { | ||
values.shininess[0] = glossinessFactor; | ||
} | ||
var specularGlossinessTexture = pbrSpecularGlossiness.specularGlossinessTexture; | ||
if (defined(specularGlossinessTexture)) { | ||
values.specular = [specularGlossinessTexture.index]; | ||
} | ||
} | ||
} | ||
var occlusionTexture = material.occlusionTexture; | ||
if (defined(occlusionTexture)) { | ||
values.ambient = [occlusionTexture.index]; | ||
} | ||
var emissiveFactor = material.emissiveFactor; | ||
if (defined(emissiveFactor)) { | ||
values.emission[0] = emissiveFactor[0]; | ||
values.emission[1] = emissiveFactor[1]; | ||
values.emission[2] = emissiveFactor[2]; | ||
} | ||
var emissiveTexture = material.emissiveTexture; | ||
if (defined(emissiveTexture)) { | ||
values.emission = [emissiveTexture.index]; | ||
} | ||
var doubleSided = material.doubleSided; | ||
if (isPBR) { | ||
delete material.doubleSided; | ||
delete material.emissiveFactor; | ||
delete material.emissiveTexture; | ||
delete material.normalTexture; | ||
delete material.occlusionTexture; | ||
delete material.pbrMetallicRoughness; | ||
var materialsCommon = { | ||
technique: 'BLINN', | ||
values: values | ||
}; | ||
var jointCount = materialJointCount[materialId]; | ||
if (defined(jointCount)) { | ||
materialsCommon.jointCount = jointCount; | ||
} | ||
if (defined(doubleSided)) { | ||
materialsCommon.doubleSided = doubleSided; | ||
} | ||
material.extensions = { | ||
KHR_materials_common: materialsCommon | ||
}; | ||
addExtensionsRequired(gltf, 'KHR_materials_common'); | ||
removeExtensionsUsed(gltf, 'KHR_materials_pbrSpecularGlossiness'); | ||
} | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that it's a new file, does it need a spec?