Skip to content

Commit

Permalink
backward compability for uv2
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoHamuy committed Aug 5, 2024
1 parent 46cb577 commit 3322b22
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 12 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 ('uv1' in bufferGeometry.attributes) {
if (UV1 in bufferGeometry.attributes) {
const uvName = `${meshid}-texcoord2`
gnode += this.getAttribute(bufferGeometry.attributes.uv1, 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 _start = new Vector3()
const _end = new Vector3()
Expand Down Expand Up @@ -67,7 +68,7 @@ function raycastWorldUnits(lineSegments, intersects) {
face: null,
faceIndex: i,
uv: null,
uv1: null,
[UV1]: null,
})
}
}
Expand Down Expand Up @@ -187,7 +188,7 @@ function raycastScreenSpace(lineSegments, camera, intersects) {
face: null,
faceIndex: i,
uv: null,
uv1: null,
[UV1]: null,
})
}
}
Expand Down
3 changes: 2 additions & 1 deletion 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 @@ -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 (uv1.array.length > 0) geometry.setAttribute('uv1', new Float32BufferAttribute(uv1.array, uv1.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
2 changes: 2 additions & 0 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,6 +1275,7 @@ class GeometryParser {
}

buffers.uvs.forEach(function (uvBuffer, i) {
if (UV1 === 'uv2') i++;
const name = i === 0 ? 'uv' : `uv${i}`;

geo.setAttribute(name, new Float32BufferAttribute(buffers.uvs[i], 2))
Expand Down
20 changes: 20 additions & 0 deletions 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,6 +149,8 @@ class LWOTreeParser {

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

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)
else mesh = new Mesh(geometry, materials)
Expand Down Expand Up @@ -220,6 +223,23 @@ class LWOTreeParser {
return m.name === name
})[0]
}

// If the material has an aoMap, duplicate UVs
duplicateUVs(geometry, materials) {
let duplicateUVs = false

if (!Array.isArray(materials)) {
if (materials.aoMap) duplicateUVs = true
} else {
materials.forEach(function (material) {
if (material.aoMap) duplicateUVs = true
})
}

if (!duplicateUVs) return

geometry.setAttribute('uv2', new BufferAttribute(geometry.attributes.uv.array, 2))
}
}

class MaterialParser {
Expand Down
11 changes: 6 additions & 5 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((uv1 - 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 vuv1;\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, vuv1).rgb;
`\nvec3 texelOld = texture2D(previousShadowMap, v${UV1 === 'uv1' ? UV1 : 'Uv2'}).rgb;
gl_FragColor.rgb = mix(texelOld, gl_FragColor.rgb, 1.0/averagingWindow);
}`

Expand Down Expand Up @@ -130,8 +131,8 @@ class ProgressiveLightMap {
uv1.array[i + 1] = (uv1.array[i + 1] + box.y + padding) / dimensions.h
}

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

Expand Down
5 changes: 3 additions & 2 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,7 +58,7 @@ class TessellateModifier {
const hasNormals = attributes.normal !== undefined
const hasColors = attributes.color !== undefined
const hasUVs = attributes.uv !== undefined
const hasUV1s = attributes.uv1 !== undefined
const hasUV1s = attributes[UV1] !== undefined

let positions = attributes.position.array
let normals = hasNormals ? attributes.normal.array : null
Expand Down Expand Up @@ -238,7 +239,7 @@ class TessellateModifier {
}

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

return geometry2
Expand Down

0 comments on commit 3322b22

Please sign in to comment.