Modify UVs without messing them up

Hey guys.

I feel completely stupid now and am very close to breaking my keyboard.
I need to modify UVs of a hemisphere so that only half of the texture is visible (for vr purposes).

Here’s what it looks like before any modifications:

I apply this code to the hemisphere:

function setUV(geom, uv) {
        for (let i = 0; i < geom.faceVertexUvs.length; i++) {

            var layer = geom.faceVertexUvs[i];

            for (let j = 0; j < layer.length; j++) {

                var uvset = layer[j];

                for (let k = 0; k < uvset.length; k++) {
                    var c = uvset[k];
                    c.x = uv.umin + (uv.umax - uv.umin) * c.x;
                    c.y = uv.vmin + (uv.vmax - uv.vmin) * c.y;

        geom.uvsNeedUpdate = true;

setUV(hemisphere, {
            umin: 0,
            umax: 0.5,
            vmin: 0,
            vmax: 1

And here’s what I get:

Basically, i’m trying to scale initial UV set to some area on the texture.
What am I doing wrong?

Let’s say your initial range of values is [a, b]. And the new range is [c, d]. You can transform a value x like this:

( ( d − c )( x − a ) / b − a ) + c


Isn’t it what I am doing right now? (with initial range [a,b] = [0,1])

I’m not sure your calculation is equivalent. Please try it out.

Here’s a fiddle using my geometry and your scaling formula:

Still, without scaling it look perfectly good (you can check it yourself in this fiddle), but after scaling it turns into mess. The only difference is that i am using Geometry instead of BufferGeometry, but as far as i know it shouldn’t cause such behaviour (I mean, it’s still plain UV coordinates, right?)

Since you are using a half sphere, maybe we can’t linearly scale the uvs like with a plane? Any chances you alter the uv coodinates in Blender or another modeling tool?

No, I don’t think that’s the problem, this formula should work on all shapes.

To confirm my words:
I converted half sphere from Geometry to BufferGeometry and copied the code from your fiddle, not it works as it should:


Though I can continue my work now, I would really like to know why it doesn’t work with Geometry.
Is there a possibility that when I iterate over faceVertexUvs, i modify UV of the same vertex multiple times?

I’m out of ideas. I really don’t know why i does not work with Geometry

I guess I was right in my prev. post.

Here’s a fiddle with a (dirty) hack, which shows, where I was wrong in the first place.

Anyway, thanks for your time.

I see. Thanks for the feedback :+1:

In any event, this unpredictable behavior of Geometry is anything but good. One reason more to deprecated Geometry and focus on BufferGeometry.


Where is the button for 1000 likes? :slight_smile:

1 Like