Skip to content

Commit

Permalink
Tweak UV assignment so that textures are not mirror-imaged
Browse files Browse the repository at this point in the history
  • Loading branch information
fenomas committed Feb 2, 2016
1 parent 768e998 commit 26b5ba6
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ function constructMeshDataFromMasks(i, d, u, v, len1, len2,
if (mask[n]) {

var maskVal = mask[n]
// var dir = (maskVal > 0)
var dir = (maskVal > 0) ? 1 : -1
var ao = aomask[n]

//Compute width of area with same mask/aomask values
Expand Down Expand Up @@ -546,19 +546,19 @@ function constructMeshDataFromMasks(i, d, u, v, len1, len2,
x[0]+du[0]+dv[0], x[1]+du[1]+dv[1], x[2]+du[2]+dv[2],
x[0] +dv[0], x[1] +dv[1], x[2] +dv[2] )

// add uv values
if (d===0) {
// draw +x/-x faces in different order, so that
// texture-space's V axis matches world-space's Y
mesh.uvs.push( 0, w )

// add uv values, with the order and sign depending on
// axis and direction so as to avoid mirror-image textures
if (d===2) {
mesh.uvs.push( 0, h )
mesh.uvs.push( -dir*w, h )
mesh.uvs.push( -dir*w, 0 )
mesh.uvs.push( 0, 0 )
mesh.uvs.push( h, 0 )
mesh.uvs.push( h, w )
} else {
mesh.uvs.push( 0, h )
mesh.uvs.push( w, h )
mesh.uvs.push( w, 0 )
mesh.uvs.push( 0, w )
mesh.uvs.push( 0, 0 )
mesh.uvs.push( dir*h, 0 )
mesh.uvs.push( dir*h, w )
}

// Add indexes, ordered clockwise for the facing direction;
Expand Down Expand Up @@ -590,13 +590,15 @@ function constructMeshDataFromMasks(i, d, u, v, len1, len2,
}

// norms depend on which direction the mask was solid in..
var norm = [0,0,0]
norm[d] = maskVal>0 ? 1 : -1
var norm0 = d===0 ? dir : 0
var norm1 = d===1 ? dir : 0
var norm2 = d===2 ? dir : 0

// same norm for all vertices
mesh.normals.push(norm[0], norm[1], norm[2],
norm[0], norm[1], norm[2],
norm[0], norm[1], norm[2],
norm[0], norm[1], norm[2] )
mesh.normals.push(norm0, norm1, norm2,
norm0, norm1, norm2,
norm0, norm1, norm2,
norm0, norm1, norm2 )


//Zero-out mask
Expand Down

0 comments on commit 26b5ba6

Please sign in to comment.