0

I'm trying to create a buffergeometry plane, I'm having troubles with the uv coordinates though. I've tried to follow Correct UV mapping Three.js yet I don't get a correct result.

The uv code is below. I also saved the entire buffergeometry code at http://jsfiddle.net/94xaL/.

I would very much appreciate a hint on what I'm doing wrong here!

Thanks!

    var uvs = terrainGeom.attributes.uv.array;
    var gridX = gridY = TERRAIN_RES - 1;
    for ( iy = 0; iy < gridY; iy++ ) {
        for ( ix = 0; ix < gridX; ix++ ) {

            var i = (iy * gridY + ix) * 12;

            //0,0
            uvs[ i ] = ix / gridX
            uvs[ i + 1 ] = iy / gridY;

            //0,1
            uvs[ i + 2 ] = ix / gridX
            uvs[ i + 3 ] = ( iy + 1 ) / gridY;

            //1,0
            uvs[ i + 4 ] = ( ix + 1 ) / gridX
            uvs[ i + 5 ] = iy / gridY;

            //0,1
            uvs[ i + 6 ] = ix / gridX
            uvs[ i + 7 ] = ( iy + 1 ) / gridY;

            //1,1
            uvs[ i + 8 ] = ( ix + 1 ) / gridX
            uvs[ i + 9 ] = ( iy + 1 ) / gridY;

            //1,0
            uvs[ i + 10 ] = ( ix + 1 ) / gridX
            uvs[ i + 11 ] = iy / gridY;
        }
    }
Community
  • 1
  • 1
Doidel
  • 1,743
  • 1
  • 14
  • 22
  • edit your jsfiddle so that you have some visual output; even if wrong. – gaitat Apr 13 '14 at 12:24
  • Here: http://jsfiddle.net/94xaL/1/ I tried but it's too late in the evening atm to debug the jsfiddle too. However I uploaded the entire thing here: http://lizkats.com/temp/shaderBlack/examples/skulpt_terrain.html – Doidel Apr 13 '14 at 20:54
  • I created a plane buffergeometry (according to how BufferGeometryUtils would convert a PlaneGeometry to a BufferGeometry) and it all works now, but it's horrible: Due to the way faces are created the same vertices are defined up to 8 times. Is there a way to avoid that, e.g. by telling the buffergeometry the rule how to build faces? I will add my solution as answer later on – Doidel Apr 16 '14 at 08:09

1 Answers1

0

The latest three.js version in the dev branch now builds planes with BufferGeometry: https://github.com/mrdoob/three.js/blob/dev/src/extras/geometries/PlaneGeometry.js

If you still want to build your own you can get some inspiration there.

Doidel
  • 1,743
  • 1
  • 14
  • 22