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

R68 Lightmap failure (+ fix) #5299

Closed
Fabrice3D opened this issue Sep 11, 2014 · 4 comments
Closed

R68 Lightmap failure (+ fix) #5299

Fabrice3D opened this issue Sep 11, 2014 · 4 comments

Comments

@Fabrice3D
Copy link

using r68. In case of lightmap, if two sets of uvs are defined in json, uvs = [ [primary uv set], [ secondary uv set] ]. The engine either renders weird geometry or generates webgl errors.

The cause is found by faulty increase of face index.

for ( i = 0; i < nUvLayers; i ++ ) {  //loop over uv sets
     uvLayer = json.uvs[ i ];  //assign the json uv set data from json parser
    geometry.faceVertexUvs[ i ][ fi ] = []; //make array for indices lookup
        for ( j = 0; j < 3; j ++ ) { //loop over the 3 vertices (or 4 for quad case)
            uvIndex = faces[ offset ++ ]; //get faces index.

in a one face case, the last line becomes on first iteration, offset = 0,1,2, on second iteration offset becomes 3,4,5, while the secondary set should still point at 0,1,2.

fix:
add on top "parseModel" method:

var collectedIndices = [0,0,0,0];

the quad loop becomes:

if ( hasFaceVertexUv ) {

                    for ( i = 0; i < nUvLayers; i ++ ) {

                        uvLayer = json.uvs[ i ];

                        geometry.faceVertexUvs[ i ][ fi ] = [];
                        geometry.faceVertexUvs[ i ][ fi + 1 ] = []

                        for ( j = 0; j < 4; j ++ ) {

                            if(i == 0) collectedIndices[j] = offset++;

                            uvIndex = faces[ collectedIndices[j] ];

                            u = uvLayer[ uvIndex * 2 ];
                            v = uvLayer[ uvIndex * 2 + 1 ];

                            uv = new THREE.Vector2( u, v );

                            if ( j !== 2 ) geometry.faceVertexUvs[ i ][ fi ].push( uv );
                            if ( j !== 0 ) geometry.faceVertexUvs[ i ][ fi + 1 ].push( uv );

                        }

                    }

                }

the 3 vertices case becomes:

for ( i = 0; i < nUvLayers; i ++ ) {

                        uvLayer = json.uvs[ i ];

                        geometry.faceVertexUvs[i][ fi ] = [];

                        for ( j = 0; j < 3; j ++ ) {

                            if(i == 0) collectedIndices[j] = offset++;

                            uvIndex = faces[ collectedIndices[j] ];

                            u = uvLayer[ uvIndex * 2 ];
                            v = uvLayer[ uvIndex * 2 + 1 ];

                            uv = new THREE.Vector2( u, v );
                            geometry.faceVertexUvs[ i ][ fi ].push( uv );
                        }

                    }

Now the engine renders models using 2 sets of uv's with lightmap as expected.

@ringodotnl
Copy link

Thx Fab for posting this!;)

@mrdoob
Copy link
Owner

mrdoob commented Sep 14, 2014

Where is this? JSONLoader?

@Fabrice3D
Copy link
Author

Yes, in JSONLoader.prototype.parse function parseModel, line 119.

@Mugen87
Copy link
Collaborator

Mugen87 commented Nov 29, 2018

JSONLoader has been removed, see #15310.

@Mugen87 Mugen87 closed this as completed Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants