Skip to content

Commit

Permalink
Fix UVs in plane and heightmaps plugins
Browse files Browse the repository at this point in the history
Tested by examples/pages/flags/backfaceTexturing.html and
examples/pages/tests/heightmaps/colorMapHeightMap.html

Also fixes out-of-range image access in custom heightmap plugin.
  • Loading branch information
Olli Etuaho committed Jun 30, 2014
1 parent 88a50ba commit 8e79f67
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 57 deletions.
7 changes: 3 additions & 4 deletions examples/pages/tests/heightmaps/colorMapHeightMap.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

<script>

// Demo of the "heightmaps/custom" custom node type with a video texture,
// plus an "effects/fog" node thrown in for fun.
// Demo of the "heightmaps/custom" custom node type.

// Point SceneJS to the bundled plugins
SceneJS.setConfigs({
Expand Down Expand Up @@ -71,7 +70,7 @@
// Dimensions
xSize:1000,
zSize:1000,
ySize:50,
ySize:200,

// Segments on X and Z axis
xSegments:10,
Expand All @@ -87,4 +86,4 @@

</script>
</body>
</html>
</html>
35 changes: 7 additions & 28 deletions src/plugins/node/heightmaps/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,21 @@
x = px * segWidth;
y = py * segHeight;

imgX = Math.round((x / width) * imageWidth);
imgY = Math.round((y / height) * imageHeight);
imgX = Math.round((x / width) * (imageWidth - 1));
imgY = Math.round((y / height) * (imageHeight - 1));

z = (imageData[(imageWidth * imgY + imgX) * 4]) / 255 * params.ySize;

if (z == undefined) {
if (z == undefined || isNaN(z)) {
z = 0;
}

positions.push(x - halfWidth);
positions.push(-y + halfHeight);
positions.push(-z);

uvs.push(py / gridX);
uvs.push(1 - px / gridZ);
}
}

Expand All @@ -149,30 +152,6 @@
indices.push(c);
indices.push(d);
indices.push(a);

// a
uvs.push(ix / gridX);
uvs.push(1 - iz / gridZ);

//b
uvs.push(ix / gridX);
uvs.push(1 - ( iz + 1 ) / gridZ);

//c
uvs.push(( ix + 1 ) / gridX);
uvs.push(1 - ( iz + 1 ) / gridZ);

//c
uvs.push(( ix + 1 ) / gridX);
uvs.push(1 - ( iz + 1 ) / gridZ);

//d
uvs.push(( ix + 1 ) / gridX, 1 - iz / gridZ);
uvs.push(1 - iz / gridZ);

//a
uvs.push(ix / gridX);
uvs.push(1 - iz / gridZ);
}
}

Expand All @@ -182,4 +161,4 @@
indices:indices
};
}
})();
})();
29 changes: 4 additions & 25 deletions src/plugins/node/prims/plane.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@
normals.push(0);
normals.push(0);
normals.push(1);

uvs.push(ix / gridX);
uvs.push(1 - iz / gridZ);
}
}

Expand All @@ -97,30 +100,6 @@
indices.push(c);
indices.push(d);
indices.push(a);

// a
uvs.push(ix / gridX);
uvs.push(1 - iz / gridZ);

//b
uvs.push(ix / gridX);
uvs.push(1 - ( iz + 1 ) / gridZ);

//c
uvs.push(( ix + 1 ) / gridX);
uvs.push(1 - ( iz + 1 ) / gridZ);

//c
uvs.push(( ix + 1 ) / gridX);
uvs.push(1 - ( iz + 1 ) / gridZ);

//d
uvs.push(( ix + 1 ) / gridX, 1 - iz / gridZ);
uvs.push(1 - iz / gridZ);

//a
uvs.push(ix / gridX);
uvs.push(1 - iz / gridZ);
}
}

Expand All @@ -134,4 +113,4 @@
indices:new Uint16Array(indices)
};
}
})();
})();

0 comments on commit 8e79f67

Please sign in to comment.