Skip to content

Commit 6a3bbeb

Browse files
authored
Merge pull request #380 from Unity-Technologies/bugfix/1322150-fixing-new-shapes-uvs
Fixing sphere and torus UV generation
2 parents dc6e9ea + f8964e9 commit 6a3bbeb

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
### Bug Fixes
1111

12+
- [case: 1322150] Fixing torus and sphere UV generation when creating New Shape.
1213
- [case: 1320936] Fixing 'ProBuilderize' action creating empty submeshes.
1314

1415
### Changes

Runtime/Shapes/Sphere.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,12 @@ public override void CopyShape(Shape shape)
7373
public override Bounds UpdateBounds(ProBuilderMesh mesh, Vector3 size, Quaternion rotation, Bounds bounds)
7474
{
7575
bounds = mesh.mesh.bounds;
76-
Vector3 boxSize = bounds.size;
77-
boxSize.x = boxSize.y = boxSize.z = Mathf.Max(boxSize.x, Mathf.Max(boxSize.y, boxSize.z));
78-
bounds.size = boxSize;
7976
return bounds;
8077
}
8178

8279
public override Bounds RebuildMesh(ProBuilderMesh mesh, Vector3 size, Quaternion rotation)
8380
{
84-
var radius = System.Math.Min(System.Math.Min(Mathf.Abs(size.x), Mathf.Abs(size.y)), Mathf.Abs(size.z));
85-
//avoid to create a degenerated sphere with a radius set to 0
86-
radius = radius < 0.001f ? 0.001f : radius;
87-
81+
var radius = .5f;
8882
// http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html
8983

9084
Vector3[] v = new Vector3[k_IcosphereTriangles.Length];
@@ -110,7 +104,7 @@ public override Bounds RebuildMesh(ProBuilderMesh mesh, Vector3 size, Quaternion
110104
{
111105
f[i / 3] = new Face(new int[3] { i, i + 1, i + 2 });
112106
f[i / 3].smoothingGroup = m_Smooth ? 1 : 0;
113-
f[i / 3].manualUV = true;
107+
f[i / 3].manualUV = false;
114108

115109
// Get the bottom most vertex of the whole shape. We'll use it as a pivot point.
116110
for (int j = 0; j < f[i / 3].indexes.Count; ++j)
@@ -124,6 +118,26 @@ public override Bounds RebuildMesh(ProBuilderMesh mesh, Vector3 size, Quaternion
124118
}
125119
}
126120
}
121+
122+
for (int i = 0; i < f.Length; i++)
123+
{
124+
var nrm = Math.Normal(v[f[i].indexesInternal[0]], v[f[i].indexesInternal[1]], v[f[i].indexesInternal[2]]);
125+
var axis = Projection.VectorToProjectionAxis(nrm);
126+
127+
if (axis == ProjectionAxis.X)
128+
f[i].textureGroup = 2;
129+
else if (axis == ProjectionAxis.Y)
130+
f[i].textureGroup = 3;
131+
else if (axis == ProjectionAxis.Z)
132+
f[i].textureGroup = 4;
133+
else if (axis == ProjectionAxis.XNegative)
134+
f[i].textureGroup = 5;
135+
else if (axis == ProjectionAxis.YNegative)
136+
f[i].textureGroup = 6;
137+
else if (axis == ProjectionAxis.ZNegative)
138+
f[i].textureGroup = 7;
139+
}
140+
127141
mesh.unwrapParameters = new UnwrapParameters()
128142
{
129143
packMargin = 30f

Runtime/Shapes/Torus.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using UnityEditor;
3+
using UnityEngine.ProBuilder.MeshOperations;
34

45
namespace UnityEngine.ProBuilder.Shapes
56
{
@@ -119,6 +120,8 @@ public override Bounds RebuildMesh(ProBuilderMesh mesh, Vector3 size, Quaternion
119120
mesh.TranslateVerticesInWorldSpace(mesh.mesh.triangles, mesh.transform.TransformDirection(-mesh.mesh.bounds.center));
120121
mesh.Refresh();
121122

123+
UVEditing.ProjectFacesBox(mesh, mesh.facesInternal);
124+
122125
return UpdateBounds(mesh, size, rotation, new Bounds());
123126
}
124127

0 commit comments

Comments
 (0)