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

Preserve the existence of non-default shader attributes #175

Merged
merged 15 commits into from
Nov 2, 2016

Conversation

lasalvavida
Copy link
Contributor

@lasalvavida lasalvavida commented Oct 21, 2016

Edit: The scope of this pull request has changed and the title has been updated to reflect that. I've kept the old text here as a record.

Single b3dm files can be processed via the glTF pipeline with these changes. This is based on the hard-normals branch, so that is what it is diffed against currently.

I am open to suggestions, but I think this is the best way to integrate b3dm support with gltf-pipeline given the current state of the public API, since it requires access to some of the internal machinery of gltf-pipeline.

This makes it easy to write a script to process tilesets concurrently using gltf-pipeline.

#!/bin/bash
for f in $(find $1/ -name '*.b3dm'); do
    echo $f
    node ./gltf-pipeline.js -i $f -o $f -f -z &
    NPROC=$(($NPROC+1))
    if [ "$NPROC" -ge 4 ]; then
        wait
        NPROC=0
    fi
done

@tfili
Copy link

tfili commented Oct 24, 2016

@lilleyse Can we get this reviewed? It would be helpful to get this into hard-normals then get that into master soon.

@lilleyse
Copy link
Contributor

I made some small fixes.

Talked with @tfili offline. We aren't sure if these b3dm steps belong in the gltf-pipeline. The main tangle point is when normals are generated a new shader is built, and the BATCH_ID needs to be re-inserted in the techniques and shaders. Technically this could be done as a post step in a b3dm specific pipeline.

@lilleyse
Copy link
Contributor

@pjcozzi what do you think about keeping this in here vs. 3d-tiles-tools or some other repo?

@lasalvavida
Copy link
Contributor Author

lasalvavida commented Oct 25, 2016

I've been thinking about this a bit, and it may be a better solution to just make sure that BATCHID never gets removed in the first place.

In a more general sense, I think if we generate a new shader from modelMaterialsCommon, it should declare all of the semantics from the primitive, even if it doesn't use them in the shader.

@tfili
Copy link

tfili commented Oct 26, 2016

@lasalvavida Also one thing I noticed, the way the b3dm handling is currently done I can't call it from another node script. It would be nice if I could call a function in the pipeline like processJSONWithExtras, but all the removing of b3dm headers are in the bin/gltf-pipeline.js.

@lasalvavida
Copy link
Contributor Author

I've finished the first part of this. In modelMaterialsCommon, we now preserve the existence of primitive semantics that don't exist by default in the generated shader. I think this is the correct approach and is not a 3d-tiles specific feature, since anyone doing dynamic shader modification using some non-default shader attribute would need this as well.

I'm still looking at the integration of reading and writing b3dms.

@lasalvavida
Copy link
Contributor Author

However, I think I'm going to revert the b3dm changes in this PR for now so we can get it and face normals merged sooner. After this change it would be pretty straightforward to go either path: gltf-pipeline supporting more file types or a 3d-tiles-tool that uses gltf-pipeline.

@lasalvavida lasalvavida changed the title Added support for b3dm files from the main command line Preserve the existence of non-default shader attributes Oct 26, 2016
@lasalvavida
Copy link
Contributor Author

Okay, this should be ready for a look.

@@ -727,6 +729,85 @@ function lightDefaults(gltf) {
}
}

function getShaderVariable(accessor) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you update the spacing in this file to be consistent with the rest of the codebase?

@pjcozzi
Copy link
Contributor

pjcozzi commented Oct 31, 2016

Is this already tested by other unit tests?

@lasalvavida
Copy link
Contributor Author

Is this already tested by other unit tests?

Doesn't look like it, I'll add a test.

if (defined(program)) {
vertexShaderId = program.vertexShader;
vertexShader = shaders[vertexShaderId];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't a lot of these checks always be true since it has gone through the modelMaterialsCommon? It should always have a material/technique/program. This also lets you simplify the loop.

var accessorId = attributes[semantic];
var accessor = accessors[accessorId];
var lowerCase = semantic.toLowerCase();
var attributeName = 'a_' + lowerCase;
Copy link
Contributor

Choose a reason for hiding this comment

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

For the a_batchId case this will produce a_batchid instead.

Copy link

Choose a reason for hiding this comment

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

So I don't think this matters. We replace the program.attribute, technique.attribute, technique.parameter and in the shader with the same case, so Cesium should handle it correctly because of the semantic.

Copy link
Contributor

Choose a reason for hiding this comment

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

In Cesium we build the shader with the assumption that it's called a_batchId: https://github.com/AnalyticalGraphicsInc/cesium/blob/3d-tiles/Source/Scene/Cesium3DTileBatchTable.js#L498. Maybe this isn't a good assumption though - instead should we use whatever parameter has the BATCHID semantic?

var pipelineExtras = vertexShader.extras._pipeline;
var shaderText = pipelineExtras.source.toString();
shaderText = 'attribute ' + getShaderVariable(accessor) + ' ' + attributeName + ';\n' + shaderText;
pipelineExtras.source = shaderText.toString();
Copy link
Contributor

Choose a reason for hiding this comment

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

Already a string.

}
}
}
}
Copy link
Contributor

@lilleyse lilleyse Nov 1, 2016

Choose a reason for hiding this comment

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

If the function above be can be shortened enough, I wouldn't see a problem combining the two functions.

/*
* If any primitives have semantics that aren't declared in the generated
* shaders, we want to preserve them.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Use // here.

@lilleyse
Copy link
Contributor

lilleyse commented Nov 2, 2016

The a_batchId issue is addressed in this Cesium PR: CesiumGS/cesium#4583

@tfili
Copy link

tfili commented Nov 2, 2016

@lilleyse The test has been added and I believe all the PR comments have been addressed.

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.

@tfili just needs some small fixes.

for (var semantic in attributes) {
if (attributes.hasOwnProperty(semantic)) {
if (!defined(techniqueParameterForSemantic(technique, semantic))) {
if (defined(technique)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Check isn't needed.

type: accessor.componentType
};
technique.attributes[attributeName] = lowerCase;
if (defined(program)) {
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.

technique.attributes[attributeName] = lowerCase;
if (defined(program)) {
program.attributes.push(attributeName);
if (defined(vertexShader)) {
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.

var gltfClone = clone(gltf);
addDefaults(gltfClone);

// Uses the Cesium sun as its default light source
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove this comment.

@tfili
Copy link

tfili commented Nov 2, 2016

@lilleyse I think that should be everything.

@lilleyse lilleyse merged commit dac565a into hard-normals Nov 2, 2016
@lilleyse lilleyse deleted the support-b3dm branch November 2, 2016 18:20
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.

4 participants