Skip to content

Commit 9761489

Browse files
committed
Abstract UVs generation function
1 parent 3fe11f4 commit 9761489

1 file changed

Lines changed: 39 additions & 54 deletions

File tree

examples/jsm/geometries/RoundedBoxBufferGeometry.js

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,40 @@ import {
33
Vector3
44
} from "../../../build/three.module.js";
55

6+
const tempNormal = new Vector3();
7+
function applyUv( faceDirVector, normal, uvAxis, projectionAxis, radius, sideLength ) {
8+
9+
const totArcLength = 2 * Math.PI * radius / 4;
10+
11+
// length of the planes between the arcs on each axis
12+
const centerLength = Math.max( sideLength - 2 * radius, 0 );
13+
const halfArc = Math.PI / 4;
14+
15+
// Get the vector projected onto the Y plane
16+
tempNormal.copy( normal );
17+
tempNormal[ projectionAxis ] = 0;
18+
tempNormal.normalize();
19+
20+
// total amount of UV space alloted to a single arc
21+
const arcUvRatio = 0.5 * totArcLength / ( totArcLength + centerLength );
22+
23+
// the distance along one arc the point is at
24+
const arcAngleRatio = 1.0 - ( tempNormal.angleTo( faceDirVector ) / halfArc );
25+
26+
if ( Math.sign( tempNormal[ uvAxis ] ) === 1 ) {
27+
28+
return arcAngleRatio * arcUvRatio;
29+
30+
} else {
31+
32+
// total amount of UV space alloted to the plane between the arcs
33+
const lenUv = centerLength / ( totArcLength + centerLength );
34+
return lenUv + arcUvRatio + arcUvRatio * ( 1.0 - arcAngleRatio );
35+
36+
}
37+
38+
}
39+
640
class RoundedBoxBufferGeometry extends BoxBufferGeometry {
741

842
constructor( width = 1, height = 1, depth = 1, segments = 1, radius = 1 ) {
@@ -28,10 +62,7 @@ class RoundedBoxBufferGeometry extends BoxBufferGeometry {
2862
const uvs = this.attributes.uv.array;
2963

3064
const faceTris = positions.length / 6;
31-
const arcLength = 2 * Math.PI * radius / 4;
32-
const widthLength = Math.max( width - 2 * radius, 0 );
33-
const heightLength = Math.max( height - 2 * radius, 0 );
34-
const depthLength = Math.max( depth - 2 * radius, 0 );
65+
const faceDirVector = new Vector3();
3566

3667
for ( let i = 0, j = 0; i < positions.length; i += 3, j += 2 ) {
3768

@@ -52,56 +83,10 @@ class RoundedBoxBufferGeometry extends BoxBufferGeometry {
5283

5384
case 0: // right
5485

55-
const tempNormal = new Vector3();
56-
const rightVector = new Vector3( 1, 0, 0 );
57-
const totArcLength = 2 * Math.PI * radius / 4;
58-
59-
// length of the planes between the arcs on each axis
60-
const heightLength = Math.max( height - 2 * radius, 0 );
61-
const depthLength = Math.max( depth - 2 * radius, 0 );
62-
const halfArc = Math.PI / 4;
63-
64-
// Get the vector projected onto the Y plane
65-
tempNormal.copy( normal );
66-
tempNormal.y = 0;
67-
tempNormal.normalize();
68-
69-
// total amount of UV space alloted to a single arc
70-
const arcUvRatioZ = 0.5 * totArcLength / ( totArcLength + depthLength );
71-
72-
// the distance along one arc the point is at
73-
const arcAngleRatioZ = 1.0 - ( tempNormal.angleTo( rightVector ) / halfArc );
74-
75-
if ( Math.sign( tempNormal.z ) === 1 ) {
76-
77-
uvs[ j + 0 ] = arcAngleRatioZ * arcUvRatioZ;
78-
79-
} else {
80-
81-
// total amount of UV space alloted to the plane between the arcs
82-
const lenUv = depthLength / ( totArcLength + depthLength );
83-
uvs[ j + 0 ] = lenUv + arcUvRatioZ + arcUvRatioZ * ( 1.0 - arcAngleRatioZ );
84-
85-
}
86-
87-
tempNormal.copy( normal );
88-
tempNormal.z = 0;
89-
tempNormal.normalize();
90-
91-
const arcUvRatioY = 0.5 * totArcLength / ( totArcLength + heightLength );
92-
const arcAngleRatioY = 1.0 - ( tempNormal.angleTo( rightVector ) / halfArc );
93-
94-
if ( Math.sign( tempNormal.y ) === - 1 ) {
95-
96-
uvs[ j + 1 ] = arcAngleRatioY * arcUvRatioY;
97-
98-
} else {
99-
100-
const lenUv = heightLength / ( totArcLength + heightLength );
101-
uvs[ j + 1 ] = lenUv + arcUvRatioY + arcUvRatioY * ( 1.0 - arcAngleRatioY );
102-
103-
}
104-
86+
// generate UVs along Z then Y
87+
faceDirVector.set( 1, 0, 0 );
88+
uvs[ j + 0 ] = applyUv( faceDirVector, normal, 'z', 'y', radius, depth );
89+
uvs[ j + 1 ] = 1.0 - applyUv( faceDirVector, normal, 'y', 'z', radius, height );
10590
break;
10691

10792

0 commit comments

Comments
 (0)