Skip to content

Commit

Permalink
Restore three.js passing UV coords, though unused (commented out - te…
Browse files Browse the repository at this point in the history
…xCoord=vUv stretches)
  • Loading branch information
deathcap committed Jan 12, 2014
1 parent bd3a80b commit bf7a410
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ function Texture(game, opts) {
vertexShader: [
'varying vec3 vNormal;',
'varying vec3 vPosition;',
'varying vec2 vUv;', // to set from three.js's "uv" attribute passed in
'',
'void main() {',
' vNormal = normal;',
' vPosition = position;',
' vUv = uv;',
'',
' gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);',
'}'
Expand All @@ -101,6 +103,7 @@ function Texture(game, opts) {
'',
'varying vec3 vNormal;',
'varying vec3 vPosition;',
'varying vec2 vUv;',
'',
'void main() {',
' vec2 tileUV = vec2(dot(vNormal.zxy, vPosition),', // [0..1] offset within tile face
Expand All @@ -124,7 +127,9 @@ function Texture(game, opts) {

' vec2 texCoord = tileOffset + tileSizeUV * fract(tileUV);',
'',
' //texCoord = vUv;', // original, stretch
' gl_FragColor = texture2D(tileMap, texCoord);',
' //gl_FragColor = vec4(vUv.s, vUv.t, 1.0, 1.0);', // to coordinates test without texturing
'}'
].join('\n')
},
Expand Down Expand Up @@ -351,7 +356,7 @@ Texture.prototype.paint = function(mesh, materials) {
*/

mesh.geometry.faces.forEach(function(face, i) {
//if (mesh.geometry.faceVertexUvs[0].length < 1) return;
if (mesh.geometry.faceVertexUvs[0].length < 1) return;

if (isVoxelMesh) {
var index = Math.floor(face.color.b*255 + face.color.g*255*255 + face.color.r*255*255*255);
Expand Down Expand Up @@ -404,8 +409,13 @@ Texture.prototype.paint = function(mesh, materials) {
// TODO: fix this uniform update affecting ALL meshes! need to clone?
mesh.surfaceMesh.material.materials[0].uniforms.tileOffsets.value[faceIndex] = new self.game.THREE.Vector2(topUV[0], 1.0 - topUV[1]); // TODO: set dimensions from other UV coords (vs tileSize)
//mesh.surfaceMesh.material.materials[0].uniforms.tileOffsets.value[faceIndex] = new self.game.THREE.Vector2(atlasuv[0][0], atlasuv[0][1]);

for (var j = 0; j < mesh.geometry.faceVertexUvs[0][i].length; j++) {
mesh.geometry.faceVertexUvs[0][i][j].set(atlasuv[j][0], 1 - atlasuv[j][1]);
}
});
//mesh.surfaceMesh.material.materials[0].uniforms.tileOffsets.needsUpdate = true; // no apparent effect

mesh.geometry.uvsNeedUpdate = true;
};

Texture.prototype.sprite = function(name, w, h, cb) {
Expand Down

0 comments on commit bf7a410

Please sign in to comment.