new default UV coordinates on some mesh primitives are poor/missing #47478

Closed
opened 2016-02-18 18:02:42 +01:00 by Francis OBrien · 24 comments

System Information
Windows 10, Nvidia Geforce 860m, 16gig of ram

Blender Version
2.76.11

Short description of error

The new default UV coordinates on some mesh primitives are poor/missing.

The uv sphere and ico sphere have messy uvs. I think the range should also be limited to 0 to 1.
I've attempted to attached pics.

Torus and Suzanne are missing uvs altogether.

I could mock better uvs if that helps?

cheers

blender_uv_ico_sphere.jpg

blender_uv_sphere.jpg

**System Information** Windows 10, Nvidia Geforce 860m, 16gig of ram **Blender Version** 2.76.11 **Short description of error** The new default UV coordinates on some mesh primitives are poor/missing. The uv sphere and ico sphere have messy uvs. I think the range should also be limited to 0 to 1. I've attempted to attached pics. Torus and Suzanne are missing uvs altogether. I could mock better uvs if that helps? cheers ![blender_uv_ico_sphere.jpg](https://archive.blender.org/developer/F283752/blender_uv_ico_sphere.jpg) ![blender_uv_sphere.jpg](https://archive.blender.org/developer/F283754/blender_uv_sphere.jpg)
Author

Changed status to: 'Open'

Changed status to: 'Open'
Author

Added subscriber: @Francis-4

Added subscriber: @Francis-4

Added subscriber: @mont29

Added subscriber: @mont29

Thanks for the report, but this is no bug really, more a set of TODOs. There are several issues here:

  • UVs of spheres have to be generated procedurally, since user can choose the number of subdivisions etc. If you check results of some other unwrapping methods in Blender, none is really better for spheres here - and quite a few also go outside of normalized coordinates. Not to say this is good result, rather than it’s a TODO to enhance our procedural spherical unwrapping.
  • Monkey we'd need a hard-coded UVmap. Also a nice TODO.
  • Torus is another problem, it’s actually generated from python, so ,it cannot really use same system as other primitives here, again a nice TODO to add its own default UVs of course.

The last two points seems to be good candidates for our 'quick hacks', will add those. First point is most likely more involved (especially for icosphere).

Thanks for the report, but this is no bug really, more a set of TODOs. There are several issues here: * UVs of spheres have to be generated procedurally, since user can choose the number of subdivisions etc. If you check results of some other unwrapping methods in Blender, none is really better for spheres here - and quite a few also go outside of normalized coordinates. Not to say this is good result, rather than it’s a TODO to enhance our procedural spherical unwrapping. * Monkey we'd need a hard-coded UVmap. Also a nice TODO. * Torus is another problem, it’s actually generated from python, so ,it cannot really use same system as other primitives here, again a nice TODO to add its own default UVs of course. The last two points seems to be good candidates for our 'quick hacks', will add those. First point is most likely more involved (especially for icosphere).

Added #47488 and #47489 quick hacks.

Added #47488 and #47489 quick hacks.
Author

Hi Bastien

Yes users complicate things! And I can see it's more involved from a coding point of view when it comes to changing the parameters on primitives.

I'll keep a look out for these todos as it's something I've been looking forward to in Blender for over 10 years.

cheers

Hi Bastien Yes users complicate things! And I can see it's more involved from a coding point of view when it comes to changing the parameters on primitives. I'll keep a look out for these todos as it's something I've been looking forward to in Blender for over 10 years. cheers

Added subscriber: @pdlla

Added subscriber: @pdlla
peter lu self-assigned this 2016-08-27 00:40:46 +02:00

I'm giving this task a go together with the #47488

I'm giving this task a go together with the #47488

Added subscriber: @ChauHo

Added subscriber: @ChauHo

For UV sphere, I think should use this coordinate pattern:
uvSphereCoord.png

For UV sphere, I think should use this coordinate pattern: ![uvSphereCoord.png](https://archive.blender.org/developer/F344142/uvSphereCoord.png)

For ico sphere I think can use this coordinate system:
icoSphereCoord.png

Subdivide triangles for other sub division level

For ico sphere I think can use this coordinate system: ![icoSphereCoord.png](https://archive.blender.org/developer/F344188/icoSphereCoord.png) Subdivide triangles for other sub division level

Added subscriber: @candreacchio

Added subscriber: @candreacchio

For UV sphere I would suggest equirectangular mapping

For UV sphere I would suggest equirectangular mapping

Carlo, like this map:
uvSphereCoordRect.png

Carlo, like this map: ![uvSphereCoordRect.png](https://archive.blender.org/developer/F347037/uvSphereCoordRect.png)

Screen Shot 2016-09-01 at 9.11.48 AM.png

Ok, about to submit my first patch! I went for the map that you suggested Chau. I hardcoded the UV map so it's dependent on the face/loop order. I hope that's OK.

The code below generates a map close to that map. I hard coded its output, and modified it to be exact. The function is totally useable but the exact map is better. Including this here here in case I ever need it again (rather than committing it).



/* unused function for assisting in generating hardcoded icosphere UVs 
 * note, using this function in BM_mesh_calc_uvs_sphere will give acceptable results for icosphere
 */
static void bm_mesh_calc_uvs_sphere_ico_face(BMFace *f, const int cd_loop_uv_offset)
{
    float *uvs[4];
    BMLoop *l;
    BMIter iter;
    float dx;
    int loop_index, loop_index_max_x;
    
    BLI_assert(f->len <= 4);
    
    /* check if face has vertex at pole, if so compute a nearbye point */
    int zcount = 0;
    float avgx = 0, avgy = 0;
    BM_ITER_ELEM_INDEX (l, &iter, f, BM_LOOPS_OF_FACE, loop_index) {
        if(l->v->co[0] == 0 && l->v->co[1] == 0){
            zcount++;
        } else {
            avgx += l->v->co[0];
            avgy += l->v->co[1];
        }
    }
    avgx /= (f->len - zcount);
    avgy /= (f->len - zcount);
    
    BM_ITER_ELEM_INDEX (l, &iter, f, BM_LOOPS_OF_FACE, loop_index) {
        MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
        float x = l->v->co[0];
        float y = l->v->co[1];
        float z = l->v->co[2];
        
        float len = sqrtf(x * x + y * y + z * z);
        float theta;
        /* use neigboring point to compute angle for poles */
        if(x == 0 && y == 0){
            theta = atan2f(avgx,avgy);
        }else{
            theta = atan2f(x,y);
        }
        float phi = saacos(z / len);
        luv->uv[0] = 0.5f + theta / (float)M_PI / 2.0f;
        luv->uv[1] = 1.0f -  phi / (float)M_PI;
        
        uvs[loop_index] = luv->uv;
    }
    
    
    /* Fix awkwardly-wrapping UVs */
    loop_index_max_x = 0;
    for (loop_index = 1; loop_index < f->len; loop_index++) {
        if (uvs[loop_index][0] > uvs[loop_index_max_x][0]) {
            loop_index_max_x = loop_index;
        }
    }
    for (loop_index = 0; loop_index < f->len; loop_index++) {
        if (loop_index != loop_index_max_x) {
            dx = uvs[loop_index_max_x][0] - uvs[loop_index][0];
            if (dx > 0.5f) {
                uvs[loop_index][0] += 1;
            }
        }
    }
    
    /* scale the UVs to be equilateral */
    for (loop_index = 0; loop_index < f->len; loop_index++) {
        uvs[loop_index][0] = uvs[loop_index][0]*10.0f/11.0f-0.05f; /* this is slightly off */
        uvs[loop_index][1] *= 0.47238f;
    }
}

![Screen Shot 2016-09-01 at 9.11.48 AM.png](https://archive.blender.org/developer/F349803/Screen_Shot_2016-09-01_at_9.11.48_AM.png) Ok, about to submit my first patch! I went for the map that you suggested Chau. I hardcoded the UV map so it's dependent on the face/loop order. I hope that's OK. The code below generates a map close to that map. I hard coded its output, and modified it to be exact. The function is totally useable but the exact map is better. Including this here here in case I ever need it again (rather than committing it). ``` /* unused function for assisting in generating hardcoded icosphere UVs * note, using this function in BM_mesh_calc_uvs_sphere will give acceptable results for icosphere */ static void bm_mesh_calc_uvs_sphere_ico_face(BMFace *f, const int cd_loop_uv_offset) { float *uvs[4]; BMLoop *l; BMIter iter; float dx; int loop_index, loop_index_max_x; BLI_assert(f->len <= 4); /* check if face has vertex at pole, if so compute a nearbye point */ int zcount = 0; float avgx = 0, avgy = 0; BM_ITER_ELEM_INDEX (l, &iter, f, BM_LOOPS_OF_FACE, loop_index) { if(l->v->co[0] == 0 && l->v->co[1] == 0){ zcount++; } else { avgx += l->v->co[0]; avgy += l->v->co[1]; } } avgx /= (f->len - zcount); avgy /= (f->len - zcount); BM_ITER_ELEM_INDEX (l, &iter, f, BM_LOOPS_OF_FACE, loop_index) { MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset); float x = l->v->co[0]; float y = l->v->co[1]; float z = l->v->co[2]; float len = sqrtf(x * x + y * y + z * z); float theta; /* use neigboring point to compute angle for poles */ if(x == 0 && y == 0){ theta = atan2f(avgx,avgy); }else{ theta = atan2f(x,y); } float phi = saacos(z / len); luv->uv[0] = 0.5f + theta / (float)M_PI / 2.0f; luv->uv[1] = 1.0f - phi / (float)M_PI; uvs[loop_index] = luv->uv; } /* Fix awkwardly-wrapping UVs */ loop_index_max_x = 0; for (loop_index = 1; loop_index < f->len; loop_index++) { if (uvs[loop_index][0] > uvs[loop_index_max_x][0]) { loop_index_max_x = loop_index; } } for (loop_index = 0; loop_index < f->len; loop_index++) { if (loop_index != loop_index_max_x) { dx = uvs[loop_index_max_x][0] - uvs[loop_index][0]; if (dx > 0.5f) { uvs[loop_index][0] += 1; } } } /* scale the UVs to be equilateral */ for (loop_index = 0; loop_index < f->len; loop_index++) { uvs[loop_index][0] = uvs[loop_index][0]*10.0f/11.0f-0.05f; /* this is slightly off */ uvs[loop_index][1] *= 0.47238f; } } ```

This is the modified BM_mesh_calc_uvs_sphere function for outputting icosphere UVs to console. Again, putting it here instead of committing.


void BM_mesh_calc_uvs_sphere(BMesh *bm, const short oflag)
{
	BMFace *f;
	BMIter iter, iter2;

	const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV);

	BLI_assert(cd_loop_uv_offset != -1); /* caller is responsible for giving us UVs */

	BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
		if (!BMO_face_flag_test(bm, f, oflag))
			continue;

		bm_mesh_calc_uvs_sphere_face(f, cd_loop_uv_offset);
	}
    
    /* print UVs for icosphere
    float icouvs[20][3][2];
    int find = 0;
    BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) {
        if (!BMO_face_flag_test(bm, f, oflag))
            continue;
        
        BMLoop *l;
        int loop_index;
        int vind = 0;
        BM_ITER_ELEM_INDEX (l, &iter2, f, BM_LOOPS_OF_FACE, loop_index) {
            MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
            icouvs[find][vind][0] = luv->uv[0];
            icouvs[find][vind][1] = luv->uv[1];
            vind++;
        }
        find++;
    }
    for(int i = 0; i < 20; i++)
    {
        printf("{{%ff,%ff},{%ff,%ff},{%ff,%ff}},\n",icouvs[i][0][0],icouvs[i][0][1],icouvs[i][1][0],icouvs[i][1][1],icouvs[i][2][0],icouvs[i][2][1]);
    }*/
}
This is the modified BM_mesh_calc_uvs_sphere function for outputting icosphere UVs to console. Again, putting it here instead of committing. ``` void BM_mesh_calc_uvs_sphere(BMesh *bm, const short oflag) { BMFace *f; BMIter iter, iter2; const int cd_loop_uv_offset = CustomData_get_offset(&bm->ldata, CD_MLOOPUV); BLI_assert(cd_loop_uv_offset != -1); /* caller is responsible for giving us UVs */ BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) { if (!BMO_face_flag_test(bm, f, oflag)) continue; bm_mesh_calc_uvs_sphere_face(f, cd_loop_uv_offset); } /* print UVs for icosphere float icouvs[20][3][2]; int find = 0; BM_ITER_MESH (f, &iter, bm, BM_FACES_OF_MESH) { if (!BMO_face_flag_test(bm, f, oflag)) continue; BMLoop *l; int loop_index; int vind = 0; BM_ITER_ELEM_INDEX (l, &iter2, f, BM_LOOPS_OF_FACE, loop_index) { MLoopUV *luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset); icouvs[find][vind][0] = luv->uv[0]; icouvs[find][vind][1] = luv->uv[1]; vind++; } find++; } for(int i = 0; i < 20; i++) { printf("{{%ff,%ff},{%ff,%ff},{%ff,%ff}},\n",icouvs[i][0][0],icouvs[i][0][1],icouvs[i][1][0],icouvs[i][1][1],icouvs[i][2][0],icouvs[i][2][1]); }*/ } ```

Added UV sphere map using equirectangular map. I would have liked to use quads on the top and bottom row but those faces only have 3 vertices so I went with the spiky triangles.

Screen Shot 2016-09-07 at 10.54.51 AM.png

The rounding is done by scaling the x coordinate by an arbitrary factor of sin(phi*0.7) i.e. the code looks something like

        theta = atan2f(y,x);
        float phi = acos(z / len);
        float phiScaled = (phi-M_PI/2.0f)*0.7f + M_PI/2.0f;
        luv->uv[0] = 0.5f + sin(phiScaled) * theta / (float)M_PI / 2.0f;
        luv->uv[1] = 1.0f -  phiScaled / (float)M_PI;
Added UV sphere map using equirectangular map. I would have liked to use quads on the top and bottom row but those faces only have 3 vertices so I went with the spiky triangles. ![Screen Shot 2016-09-07 at 10.54.51 AM.png](https://archive.blender.org/developer/F355553/Screen_Shot_2016-09-07_at_10.54.51_AM.png) The rounding is done by scaling the x coordinate by an arbitrary factor of sin(phi*0.7) i.e. the code looks something like ``` theta = atan2f(y,x); float phi = acos(z / len); float phiScaled = (phi-M_PI/2.0f)*0.7f + M_PI/2.0f; luv->uv[0] = 0.5f + sin(phiScaled) * theta / (float)M_PI / 2.0f; luv->uv[1] = 1.0f - phiScaled / (float)M_PI; ```

Adding UVs to Monkey and Torus are #47488 and #47489 respectively. D2202 and D2186 add better UVs for icosphere and uvsphere respectively. My first 2 patches :D! Can I/someone mark this task as resolved if/when D2202 and D2186 are accepted?

Adding UVs to Monkey and Torus are #47488 and #47489 respectively. [D2202](https://archive.blender.org/developer/D2202) and [D2186](https://archive.blender.org/developer/D2186) add better UVs for icosphere and uvsphere respectively. My first 2 patches :D! Can I/someone mark this task as resolved if/when [D2202](https://archive.blender.org/developer/D2202) and [D2186](https://archive.blender.org/developer/D2186) are accepted?
Author

@ peter lu

looking good so far. I've never mapped primitives using your solutions but they look interesting and useful.

However can you keep the uv coords between 0 and 1? I think it's a better solution.

cheers

@ peter lu looking good so far. I've never mapped primitives using your solutions but they look interesting and useful. However can you keep the uv coords between 0 and 1? I think it's a better solution. cheers
Author

here are a few default mesh and uv examples from Maya. I'm not saying copy them :) But this is how the competition does it

sphere_pinched_at_poles_uvs.jpg

sphere_sawtooth_at_poles_uvs.jpg

torus_uvs.jpg

here are a few default mesh and uv examples from Maya. I'm not saying copy them :) But this is how the competition does it ![sphere_pinched_at_poles_uvs.jpg](https://archive.blender.org/developer/F356076/sphere_pinched_at_poles_uvs.jpg) ![sphere_sawtooth_at_poles_uvs.jpg](https://archive.blender.org/developer/F356078/sphere_sawtooth_at_poles_uvs.jpg) ![torus_uvs.jpg](https://archive.blender.org/developer/F356080/torus_uvs.jpg)

@Francis-4

It shifts the uvs to fit in [0,1] now :), I just used an old screenshot (TBH though I was almost about to be too lazy to do this XD).

Thanks for those reference images. Those are actually special cases of my current implementation. Maybe I'll add them as input parameters to the UV spehere.

@Francis-4 It shifts the uvs to fit in [0,1] now :), I just used an old screenshot (TBH though I was almost about to be too lazy to do this XD). Thanks for those reference images. Those are actually special cases of my current implementation. Maybe I'll add them as input parameters to the UV spehere.

Two bottom pictures from Maya follow my first maps for UV sphere and torus.

Two bottom pictures from Maya follow my first maps for UV sphere and torus.

This issue was referenced by a070a5befa

This issue was referenced by a070a5befa1110120547333d84c6e67cae53d648

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
7 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#47478
No description provided.