Skip to content

Commit 442de29

Browse files
authored
fix: BufferGeometry rename uv2 to uv1 (#377)
1 parent 002e780 commit 442de29

File tree

8 files changed

+63
-54
lines changed

8 files changed

+63
-54
lines changed

src/_polyfill/uv1.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { version } from "./constants";
2+
3+
/** uv2 renamed to uv1 in r125
4+
*
5+
* https://github.com/mrdoob/three.js/pull/25943
6+
*/
7+
export const UV1 = version >= 125 ? 'uv1' : 'uv2'

src/exporters/ColladaExporter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
Texture,
1515
} from 'three'
1616
import type { TypedArray, TypedArrayConstructors } from '../types/shared'
17+
import { UV1 } from '../_polyfill/uv1'
1718

1819
/**
1920
* https://github.com/gkjohnson/collada-exporter-js
@@ -346,9 +347,9 @@ class ColladaExporter {
346347
}
347348

348349
// serialize lightmap uvs
349-
if ('uv2' in bufferGeometry.attributes) {
350+
if (UV1 in bufferGeometry.attributes) {
350351
const uvName = `${meshid}-texcoord2`
351-
gnode += this.getAttribute(bufferGeometry.attributes.uv2, uvName, ['S', 'T'], 'float')
352+
gnode += this.getAttribute(bufferGeometry.attributes[UV1], uvName, ['S', 'T'], 'float')
352353
triangleInputs += `<input semantic="TEXCOORD" source="#${uvName}" offset="0" set="1" />`
353354
}
354355

src/lines/LineSegments2.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from 'three'
1313
import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry'
1414
import { LineMaterial } from '../lines/LineMaterial'
15+
import { UV1 } from '../_polyfill/uv1'
1516

1617
const _viewport = new Vector4();
1718

@@ -78,7 +79,7 @@ function raycastWorldUnits(lineSegments, intersects) {
7879
face: null,
7980
faceIndex: i,
8081
uv: null,
81-
uv2: null,
82+
[UV1]: null,
8283
})
8384
}
8485
}
@@ -199,7 +200,7 @@ function raycastScreenSpace(lineSegments, camera, intersects) {
199200
face: null,
200201
faceIndex: i,
201202
uv: null,
202-
uv2: null,
203+
[UV1]: null,
203204
})
204205
}
205206
}

src/loaders/ColladaLoader.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
VectorKeyframeTrack,
4040
} from 'three'
4141
import { TGALoader } from '../loaders/TGALoader'
42+
import { UV1 } from '../_polyfill/uv1'
4243

4344
class ColladaLoader extends Loader {
4445
constructor(manager) {
@@ -1866,7 +1867,7 @@ class ColladaLoader extends Loader {
18661867
const position = { array: [], stride: 0 }
18671868
const normal = { array: [], stride: 0 }
18681869
const uv = { array: [], stride: 0 }
1869-
const uv2 = { array: [], stride: 0 }
1870+
const uv1 = { array: [], stride: 0 }
18701871
const color = { array: [], stride: 0 }
18711872

18721873
const skinIndex = { array: [], stride: 4 }
@@ -1981,7 +1982,7 @@ class ColladaLoader extends Loader {
19811982
break
19821983

19831984
case 'TEXCOORD1':
1984-
buildGeometryData(primitive, sources[id], input.offset, uv2.array)
1985+
buildGeometryData(primitive, sources[id], input.offset, uv1.array)
19851986
uv.stride = sources[id].stride
19861987
break
19871988

@@ -2008,8 +2009,8 @@ class ColladaLoader extends Loader {
20082009
break
20092010

20102011
case 'TEXCOORD1':
2011-
buildGeometryData(primitive, sources[input.id], input.offset, uv2.array)
2012-
uv2.stride = sources[input.id].stride
2012+
buildGeometryData(primitive, sources[input.id], input.offset, uv1.array)
2013+
uv1.stride = sources[input.id].stride
20132014
break
20142015
}
20152016
}
@@ -2025,7 +2026,7 @@ class ColladaLoader extends Loader {
20252026
}
20262027
if (color.array.length > 0) geometry.setAttribute('color', new Float32BufferAttribute(color.array, color.stride))
20272028
if (uv.array.length > 0) geometry.setAttribute('uv', new Float32BufferAttribute(uv.array, uv.stride))
2028-
if (uv2.array.length > 0) geometry.setAttribute('uv2', new Float32BufferAttribute(uv2.array, uv2.stride))
2029+
if (uv1.array.length > 0) geometry.setAttribute(UV1, new Float32BufferAttribute(uv1.array, uv1.stride))
20292030

20302031
if (skinIndex.array.length > 0) {
20312032
geometry.setAttribute('skinIndex', new Float32BufferAttribute(skinIndex.array, skinIndex.stride))
@@ -2496,14 +2497,14 @@ class ColladaLoader extends Loader {
24962497
if (value > joint.limits.max || value < joint.limits.min) {
24972498
console.warn(
24982499
'THREE.ColladaLoader: Joint ' +
2499-
jointIndex +
2500-
' value ' +
2501-
value +
2502-
' outside of limits (min: ' +
2503-
joint.limits.min +
2504-
', max: ' +
2505-
joint.limits.max +
2506-
').',
2500+
jointIndex +
2501+
' value ' +
2502+
value +
2503+
' outside of limits (min: ' +
2504+
joint.limits.min +
2505+
', max: ' +
2506+
joint.limits.max +
2507+
').',
25072508
)
25082509
} else if (joint.static) {
25092510
console.warn('THREE.ColladaLoader: Joint ' + jointIndex + ' is static.')

src/loaders/FBXLoader.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
import { unzlibSync } from 'fflate'
4444
import { NURBSCurve } from '../curves/NURBSCurve'
4545
import { decodeText } from '../_polyfill/LoaderUtils'
46+
import { UV1 } from '../_polyfill/uv1'
4647

4748
/**
4849
* Loader loads FBX file and generates Group representing FBX scene.
@@ -1274,13 +1275,8 @@ class GeometryParser {
12741275
}
12751276

12761277
buffers.uvs.forEach(function (uvBuffer, i) {
1277-
// subsequent uv buffers are called 'uv1', 'uv2', ...
1278-
let name = 'uv' + (i + 1).toString()
1279-
1280-
// the first uv buffer is just called 'uv'
1281-
if (i === 0) {
1282-
name = 'uv'
1283-
}
1278+
if (UV1 === 'uv2') i++;
1279+
const name = i === 0 ? 'uv' : `uv${i}`;
12841280

12851281
geo.setAttribute(name, new Float32BufferAttribute(buffers.uvs[i], 2))
12861282
})

src/loaders/LWOLoader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
} from 'three'
4141

4242
import { IFFParser } from './lwo/IFFParser.js'
43+
import { UV1 } from '../_polyfill/uv1.ts'
4344

4445
let _lwoTree
4546

@@ -148,7 +149,7 @@ class LWOTreeParser {
148149

149150
const materials = this.getMaterials(geometry.userData.matNames, layer.geometry.type)
150151

151-
this.duplicateUVs(geometry, materials)
152+
if (UV1 === 'uv2') this.duplicateUVs(geometry, materials)
152153

153154
if (layer.geometry.type === 'points') mesh = new Points(geometry, materials)
154155
else if (layer.geometry.type === 'lines') mesh = new LineSegments(geometry, materials)

src/misc/ProgressiveLightmap.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Mesh,
1010
} from 'three'
1111
import potpack from 'potpack'
12+
import { UV1 } from '../_polyfill/uv1'
1213

1314
/**
1415
* Progressive Light Map Accumulator, by [zalo](https://github.com/zalo/)
@@ -52,16 +53,16 @@ class ProgressiveLightMap {
5253
shader.vertexShader =
5354
'#define USE_LIGHTMAP\n' +
5455
shader.vertexShader.slice(0, -1) +
55-
' gl_Position = vec4((uv2 - 0.5) * 2.0, 1.0, 1.0); }'
56+
` gl_Position = vec4((${UV1} - 0.5) * 2.0, 1.0, 1.0); }`
5657

5758
// Fragment Shader: Set Pixels to average in the Previous frame's Shadows
5859
const bodyStart = shader.fragmentShader.indexOf('void main() {')
5960
shader.fragmentShader =
60-
'varying vec2 vUv2;\n' +
61+
`varying vec2 v${UV1 === 'uv1' ? UV1 : 'Uv2'};\n` +
6162
shader.fragmentShader.slice(0, bodyStart) +
6263
' uniform sampler2D previousShadowMap;\n uniform float averagingWindow;\n' +
6364
shader.fragmentShader.slice(bodyStart - 1, -1) +
64-
`\nvec3 texelOld = texture2D(previousShadowMap, vUv2).rgb;
65+
`\nvec3 texelOld = texture2D(previousShadowMap, v${UV1 === 'uv1' ? UV1 : 'Uv2'}).rgb;
6566
gl_FragColor.rgb = mix(texelOld, gl_FragColor.rgb, 1.0/averagingWindow);
6667
}`
6768

@@ -79,7 +80,7 @@ class ProgressiveLightMap {
7980
}
8081

8182
/**
82-
* Sets these objects' materials' lightmaps and modifies their uv2's.
83+
* Sets these objects' materials' lightmaps and modifies their uv1's.
8384
* @param {Object3D} objects An array of objects and lights to set up your lightmap.
8485
*/
8586
addObjectsToLightMap(objects) {
@@ -124,14 +125,14 @@ class ProgressiveLightMap {
124125
// Pack the objects' lightmap UVs into the same global space
125126
const dimensions = potpack(this.uv_boxes)
126127
this.uv_boxes.forEach((box) => {
127-
const uv2 = objects[box.index].geometry.getAttribute('uv').clone()
128-
for (let i = 0; i < uv2.array.length; i += uv2.itemSize) {
129-
uv2.array[i] = (uv2.array[i] + box.x + padding) / dimensions.w
130-
uv2.array[i + 1] = (uv2.array[i + 1] + box.y + padding) / dimensions.h
128+
const uv1 = objects[box.index].geometry.getAttribute('uv').clone()
129+
for (let i = 0; i < uv1.array.length; i += uv1.itemSize) {
130+
uv1.array[i] = (uv1.array[i] + box.x + padding) / dimensions.w
131+
uv1.array[i + 1] = (uv1.array[i + 1] + box.y + padding) / dimensions.h
131132
}
132133

133-
objects[box.index].geometry.setAttribute('uv2', uv2)
134-
objects[box.index].geometry.getAttribute('uv2').needsUpdate = true
134+
objects[box.index].geometry.setAttribute(UV1, uv1)
135+
objects[box.index].geometry.getAttribute(UV1).needsUpdate = true
135136
})
136137
}
137138

src/modifiers/TessellateModifier.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { BufferGeometry, Color, Float32BufferAttribute, Vector2, Vector3 } from 'three'
2+
import { UV1 } from '../_polyfill/uv1'
23

34
/**
45
* Break faces with edges longer than maxEdgeLength
@@ -57,19 +58,19 @@ class TessellateModifier {
5758
const hasNormals = attributes.normal !== undefined
5859
const hasColors = attributes.color !== undefined
5960
const hasUVs = attributes.uv !== undefined
60-
const hasUV2s = attributes.uv2 !== undefined
61+
const hasUV1s = attributes[UV1] !== undefined
6162

6263
let positions = attributes.position.array
6364
let normals = hasNormals ? attributes.normal.array : null
6465
let colors = hasColors ? attributes.color.array : null
6566
let uvs = hasUVs ? attributes.uv.array : null
66-
let uv2s = hasUV2s ? attributes.uv2.array : null
67+
let uv1s = hasUV1s ? attributes.uv1.array : null
6768

6869
let positions2 = (positions as unknown) as number[]
6970
let normals2 = (normals as unknown) as number[]
7071
let colors2 = (colors as unknown) as number[]
7172
let uvs2 = (uvs as unknown) as number[]
72-
let uv2s2 = (uv2s as unknown) as number[]
73+
let uv1s2 = (uv1s as unknown) as number[]
7374

7475
let iteration = 0
7576
let tessellating = true
@@ -113,14 +114,14 @@ class TessellateModifier {
113114
uvs2.push(u3.x, u3.y)
114115
}
115116

116-
if (hasUV2s) {
117+
if (hasUV1s) {
117118
const u21 = u2s[a]
118119
const u22 = u2s[b]
119120
const u23 = u2s[c]
120121

121-
uv2s2.push(u21.x, u21.y)
122-
uv2s2.push(u22.x, u22.y)
123-
uv2s2.push(u23.x, u23.y)
122+
uv1s2.push(u21.x, u21.y)
123+
uv1s2.push(u22.x, u22.y)
124+
uv1s2.push(u23.x, u23.y)
124125
}
125126
}
126127

@@ -146,9 +147,9 @@ class TessellateModifier {
146147
uvs2 = []
147148
}
148149

149-
if (hasUV2s) {
150-
uv2s = uv2s2 as any
151-
uv2s2 = []
150+
if (hasUV1s) {
151+
uv1s = uv1s2 as any
152+
uv1s2 = []
152153
}
153154

154155
for (let i = 0, i2 = 0, il = positions.length; i < il; i += 9, i2 += 6) {
@@ -174,10 +175,10 @@ class TessellateModifier {
174175
uc.fromArray(uvs, i2 + 4)
175176
}
176177

177-
if (hasUV2s && uv2s) {
178-
u2a.fromArray(uv2s, i2 + 0)
179-
u2b.fromArray(uv2s, i2 + 2)
180-
u2c.fromArray(uv2s, i2 + 4)
178+
if (hasUV1s && uv1s) {
179+
u2a.fromArray(uv1s, i2 + 0)
180+
u2b.fromArray(uv1s, i2 + 2)
181+
u2c.fromArray(uv1s, i2 + 4)
181182
}
182183

183184
const dab = va.distanceToSquared(vb)
@@ -192,7 +193,7 @@ class TessellateModifier {
192193
if (hasNormals) nm.lerpVectors(na, nb, 0.5)
193194
if (hasColors) cm.lerpColors(ca, cb, 0.5)
194195
if (hasUVs) um.lerpVectors(ua, ub, 0.5)
195-
if (hasUV2s) u2m.lerpVectors(u2a, u2b, 0.5)
196+
if (hasUV1s) u2m.lerpVectors(u2a, u2b, 0.5)
196197

197198
addTriangle(0, 3, 2)
198199
addTriangle(3, 1, 2)
@@ -201,7 +202,7 @@ class TessellateModifier {
201202
if (hasNormals) nm.lerpVectors(nb, nc, 0.5)
202203
if (hasColors) cm.lerpColors(cb, cc, 0.5)
203204
if (hasUVs) um.lerpVectors(ub, uc, 0.5)
204-
if (hasUV2s) u2m.lerpVectors(u2b, u2c, 0.5)
205+
if (hasUV1s) u2m.lerpVectors(u2b, u2c, 0.5)
205206

206207
addTriangle(0, 1, 3)
207208
addTriangle(3, 2, 0)
@@ -210,7 +211,7 @@ class TessellateModifier {
210211
if (hasNormals) nm.lerpVectors(na, nc, 0.5)
211212
if (hasColors) cm.lerpColors(ca, cc, 0.5)
212213
if (hasUVs) um.lerpVectors(ua, uc, 0.5)
213-
if (hasUV2s) u2m.lerpVectors(u2a, u2c, 0.5)
214+
if (hasUV1s) u2m.lerpVectors(u2a, u2c, 0.5)
214215

215216
addTriangle(0, 1, 3)
216217
addTriangle(3, 1, 2)
@@ -237,8 +238,8 @@ class TessellateModifier {
237238
geometry2.setAttribute('uv', new Float32BufferAttribute(uvs2 as any, 2))
238239
}
239240

240-
if (hasUV2s) {
241-
geometry2.setAttribute('uv2', new Float32BufferAttribute(uv2s2 as any, 2))
241+
if (hasUV1s) {
242+
geometry2.setAttribute(UV1, new Float32BufferAttribute(uv1s2 as any, 2))
242243
}
243244

244245
return geometry2

0 commit comments

Comments
 (0)