Skip to content

Commit

Permalink
fix: BufferGeometry rename uv2 to uv1 (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoHamuy authored Aug 5, 2024
1 parent 002e780 commit 442de29
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 54 deletions.
7 changes: 7 additions & 0 deletions src/_polyfill/uv1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { version } from "./constants";

/** uv2 renamed to uv1 in r125
*
* https://github.com/mrdoob/three.js/pull/25943
*/
export const UV1 = version >= 125 ? 'uv1' : 'uv2'
5 changes: 3 additions & 2 deletions src/exporters/ColladaExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Texture,
} from 'three'
import type { TypedArray, TypedArrayConstructors } from '../types/shared'
import { UV1 } from '../_polyfill/uv1'

/**
* https://github.com/gkjohnson/collada-exporter-js
Expand Down Expand Up @@ -346,9 +347,9 @@ class ColladaExporter {
}

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

Expand Down
5 changes: 3 additions & 2 deletions src/lines/LineSegments2.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from 'three'
import { LineSegmentsGeometry } from '../lines/LineSegmentsGeometry'
import { LineMaterial } from '../lines/LineMaterial'
import { UV1 } from '../_polyfill/uv1'

const _viewport = new Vector4();

Expand Down Expand Up @@ -78,7 +79,7 @@ function raycastWorldUnits(lineSegments, intersects) {
face: null,
faceIndex: i,
uv: null,
uv2: null,
[UV1]: null,
})
}
}
Expand Down Expand Up @@ -199,7 +200,7 @@ function raycastScreenSpace(lineSegments, camera, intersects) {
face: null,
faceIndex: i,
uv: null,
uv2: null,
[UV1]: null,
})
}
}
Expand Down
27 changes: 14 additions & 13 deletions src/loaders/ColladaLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
VectorKeyframeTrack,
} from 'three'
import { TGALoader } from '../loaders/TGALoader'
import { UV1 } from '../_polyfill/uv1'

class ColladaLoader extends Loader {
constructor(manager) {
Expand Down Expand Up @@ -1866,7 +1867,7 @@ class ColladaLoader extends Loader {
const position = { array: [], stride: 0 }
const normal = { array: [], stride: 0 }
const uv = { array: [], stride: 0 }
const uv2 = { array: [], stride: 0 }
const uv1 = { array: [], stride: 0 }
const color = { array: [], stride: 0 }

const skinIndex = { array: [], stride: 4 }
Expand Down Expand Up @@ -1981,7 +1982,7 @@ class ColladaLoader extends Loader {
break

case 'TEXCOORD1':
buildGeometryData(primitive, sources[id], input.offset, uv2.array)
buildGeometryData(primitive, sources[id], input.offset, uv1.array)
uv.stride = sources[id].stride
break

Expand All @@ -2008,8 +2009,8 @@ class ColladaLoader extends Loader {
break

case 'TEXCOORD1':
buildGeometryData(primitive, sources[input.id], input.offset, uv2.array)
uv2.stride = sources[input.id].stride
buildGeometryData(primitive, sources[input.id], input.offset, uv1.array)
uv1.stride = sources[input.id].stride
break
}
}
Expand All @@ -2025,7 +2026,7 @@ class ColladaLoader extends Loader {
}
if (color.array.length > 0) geometry.setAttribute('color', new Float32BufferAttribute(color.array, color.stride))
if (uv.array.length > 0) geometry.setAttribute('uv', new Float32BufferAttribute(uv.array, uv.stride))
if (uv2.array.length > 0) geometry.setAttribute('uv2', new Float32BufferAttribute(uv2.array, uv2.stride))
if (uv1.array.length > 0) geometry.setAttribute(UV1, new Float32BufferAttribute(uv1.array, uv1.stride))

if (skinIndex.array.length > 0) {
geometry.setAttribute('skinIndex', new Float32BufferAttribute(skinIndex.array, skinIndex.stride))
Expand Down Expand Up @@ -2496,14 +2497,14 @@ class ColladaLoader extends Loader {
if (value > joint.limits.max || value < joint.limits.min) {
console.warn(
'THREE.ColladaLoader: Joint ' +
jointIndex +
' value ' +
value +
' outside of limits (min: ' +
joint.limits.min +
', max: ' +
joint.limits.max +
').',
jointIndex +
' value ' +
value +
' outside of limits (min: ' +
joint.limits.min +
', max: ' +
joint.limits.max +
').',
)
} else if (joint.static) {
console.warn('THREE.ColladaLoader: Joint ' + jointIndex + ' is static.')
Expand Down
10 changes: 3 additions & 7 deletions src/loaders/FBXLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
import { unzlibSync } from 'fflate'
import { NURBSCurve } from '../curves/NURBSCurve'
import { decodeText } from '../_polyfill/LoaderUtils'
import { UV1 } from '../_polyfill/uv1'

/**
* Loader loads FBX file and generates Group representing FBX scene.
Expand Down Expand Up @@ -1274,13 +1275,8 @@ class GeometryParser {
}

buffers.uvs.forEach(function (uvBuffer, i) {
// subsequent uv buffers are called 'uv1', 'uv2', ...
let name = 'uv' + (i + 1).toString()

// the first uv buffer is just called 'uv'
if (i === 0) {
name = 'uv'
}
if (UV1 === 'uv2') i++;
const name = i === 0 ? 'uv' : `uv${i}`;

geo.setAttribute(name, new Float32BufferAttribute(buffers.uvs[i], 2))
})
Expand Down
3 changes: 2 additions & 1 deletion src/loaders/LWOLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
} from 'three'

import { IFFParser } from './lwo/IFFParser.js'
import { UV1 } from '../_polyfill/uv1.ts'

let _lwoTree

Expand Down Expand Up @@ -148,7 +149,7 @@ class LWOTreeParser {

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

this.duplicateUVs(geometry, materials)
if (UV1 === 'uv2') this.duplicateUVs(geometry, materials)

if (layer.geometry.type === 'points') mesh = new Points(geometry, materials)
else if (layer.geometry.type === 'lines') mesh = new LineSegments(geometry, materials)
Expand Down
21 changes: 11 additions & 10 deletions src/misc/ProgressiveLightmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Mesh,
} from 'three'
import potpack from 'potpack'
import { UV1 } from '../_polyfill/uv1'

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

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

Expand All @@ -79,7 +80,7 @@ class ProgressiveLightMap {
}

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

objects[box.index].geometry.setAttribute('uv2', uv2)
objects[box.index].geometry.getAttribute('uv2').needsUpdate = true
objects[box.index].geometry.setAttribute(UV1, uv1)
objects[box.index].geometry.getAttribute(UV1).needsUpdate = true
})
}

Expand Down
39 changes: 20 additions & 19 deletions src/modifiers/TessellateModifier.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BufferGeometry, Color, Float32BufferAttribute, Vector2, Vector3 } from 'three'
import { UV1 } from '../_polyfill/uv1'

/**
* Break faces with edges longer than maxEdgeLength
Expand Down Expand Up @@ -57,19 +58,19 @@ class TessellateModifier {
const hasNormals = attributes.normal !== undefined
const hasColors = attributes.color !== undefined
const hasUVs = attributes.uv !== undefined
const hasUV2s = attributes.uv2 !== undefined
const hasUV1s = attributes[UV1] !== undefined

let positions = attributes.position.array
let normals = hasNormals ? attributes.normal.array : null
let colors = hasColors ? attributes.color.array : null
let uvs = hasUVs ? attributes.uv.array : null
let uv2s = hasUV2s ? attributes.uv2.array : null
let uv1s = hasUV1s ? attributes.uv1.array : null

let positions2 = (positions as unknown) as number[]
let normals2 = (normals as unknown) as number[]
let colors2 = (colors as unknown) as number[]
let uvs2 = (uvs as unknown) as number[]
let uv2s2 = (uv2s as unknown) as number[]
let uv1s2 = (uv1s as unknown) as number[]

let iteration = 0
let tessellating = true
Expand Down Expand Up @@ -113,14 +114,14 @@ class TessellateModifier {
uvs2.push(u3.x, u3.y)
}

if (hasUV2s) {
if (hasUV1s) {
const u21 = u2s[a]
const u22 = u2s[b]
const u23 = u2s[c]

uv2s2.push(u21.x, u21.y)
uv2s2.push(u22.x, u22.y)
uv2s2.push(u23.x, u23.y)
uv1s2.push(u21.x, u21.y)
uv1s2.push(u22.x, u22.y)
uv1s2.push(u23.x, u23.y)
}
}

Expand All @@ -146,9 +147,9 @@ class TessellateModifier {
uvs2 = []
}

if (hasUV2s) {
uv2s = uv2s2 as any
uv2s2 = []
if (hasUV1s) {
uv1s = uv1s2 as any
uv1s2 = []
}

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

if (hasUV2s && uv2s) {
u2a.fromArray(uv2s, i2 + 0)
u2b.fromArray(uv2s, i2 + 2)
u2c.fromArray(uv2s, i2 + 4)
if (hasUV1s && uv1s) {
u2a.fromArray(uv1s, i2 + 0)
u2b.fromArray(uv1s, i2 + 2)
u2c.fromArray(uv1s, i2 + 4)
}

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

addTriangle(0, 3, 2)
addTriangle(3, 1, 2)
Expand All @@ -201,7 +202,7 @@ class TessellateModifier {
if (hasNormals) nm.lerpVectors(nb, nc, 0.5)
if (hasColors) cm.lerpColors(cb, cc, 0.5)
if (hasUVs) um.lerpVectors(ub, uc, 0.5)
if (hasUV2s) u2m.lerpVectors(u2b, u2c, 0.5)
if (hasUV1s) u2m.lerpVectors(u2b, u2c, 0.5)

addTriangle(0, 1, 3)
addTriangle(3, 2, 0)
Expand All @@ -210,7 +211,7 @@ class TessellateModifier {
if (hasNormals) nm.lerpVectors(na, nc, 0.5)
if (hasColors) cm.lerpColors(ca, cc, 0.5)
if (hasUVs) um.lerpVectors(ua, uc, 0.5)
if (hasUV2s) u2m.lerpVectors(u2a, u2c, 0.5)
if (hasUV1s) u2m.lerpVectors(u2a, u2c, 0.5)

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

if (hasUV2s) {
geometry2.setAttribute('uv2', new Float32BufferAttribute(uv2s2 as any, 2))
if (hasUV1s) {
geometry2.setAttribute(UV1, new Float32BufferAttribute(uv1s2 as any, 2))
}

return geometry2
Expand Down

0 comments on commit 442de29

Please sign in to comment.