Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript

small consolidation of code for weight painting

Scott Petrovic94ba4a570faa
diff --git a/src/lib/geometry/VertexAdjacency.ts b/src/lib/geometry/VertexAdjacency.ts
new file mode 100644
index 0000000..dcf123e
--- /dev/null
+++ b/src/lib/geometry/VertexAdjacency.ts
@@ -0,0 +1,35 @@
+import type { BufferGeometry } from 'three'
+
+const adjacency_cache = new WeakMap<BufferGeometry, number[][]>()
+
+/**
+ * Connected vertex indices per vertex, built from the geometry's triangles and
+ * cached per geometry. Treat the result as read-only: it is shared by callers.
+ */
+export function vertex_adjacency (geometry: BufferGeometry): number[][] {
+ const cached = adjacency_cache.get(geometry)
+ if (cached !== undefined) { return cached }
+
+ const vertex_count = geometry.attributes.position?.count ?? 0
+ const neighbors: Array<Set<number>> = Array.from({ length: vertex_count }, () => new Set<number>())
+ const connect = (a: number, b: number): void => {
+ if (a === b || a < 0 || b < 0 || a >= vertex_count || b >= vertex_count) { return }
+ neighbors[a].add(b)
+ neighbors[b].add(a)
+ }
+
+ const index = geometry.index
+ const index_count = index !== null ? index.count : vertex_count
+ for (let i = 0; i + 2 < index_count; i += 3) {
+ const a = index !== null ? index.getX(i) : i
+ const b = index !== null ? index.getX(i + 1) : i + 1
+ const c = index !== null ? index.getX(i + 2) : i + 2
+ connect(a, b)
+ connect(b, c)
+ connect(c, a)
+ }
+
+ const result = neighbors.map(set => [...set])
+ adjacency_cache.set(geometry, result)
+ return result
+}
diff --git a/src/lib/processes/weight-painting/WeightPaintBrush.ts b/src/lib/processes/weight-painting/WeightPaintBrush.ts
index 14adfcb..f5b97b5 100644
--- a/src/lib/processes/weight-painting/WeightPaintBrush.ts
+++ b/src/lib/processes/weight-painting/WeightPaintBrush.ts
@@ -1,4 +1,5 @@
-import { MathUtils, Matrix3, Mesh, SkinnedMesh, Triangle, Vector3, type Bone, type BufferGeometry, type Intersection, type PerspectiveCamera } from 'three'
+import { MathUtils, Matrix3, Mesh, SkinnedMesh, Triangle, Vector3, type Bone, type Intersection, type PerspectiveCamera } from 'three'
+import { vertex_adjacency } from '../../geometry/VertexAdjacency.ts'
import { WeightPaintManager, type BrushVertex, type WeightPaintMode } from './WeightPaintManager.ts'
/**
@@ -24,8 +25,6 @@ export class WeightPaintBrush {
private readonly sweep = new Vector3()
private readonly sweep_offset = new Vector3()
private readonly sweep_closest = new Vector3()
- // triangle adjacency per geometry, used to spread a dab across the surface
- private readonly adjacency_cache = new WeakMap<BufferGeometry, number[][]>()
constructor (private readonly manager: WeightPaintManager) {}
@@ -201,7 +200,7 @@ export class WeightPaintBrush {
falloffs.set(triangle[i], influence)
}
- const adjacency = this.adjacency_for(object.geometry)
+ const adjacency = vertex_adjacency(object.geometry)
for (const index of triangle) {
const influence = (falloffs.get(index) ?? 0) * 0.5
if (influence <= 0) { continue }
@@ -212,34 +211,6 @@ export class WeightPaintBrush {
return falloffs
}
- /** Vertex adjacency built from the geometry's triangles, cached per geometry. */
- private adjacency_for (geometry: BufferGeometry): number[][] {
- const cached = this.adjacency_cache.get(geometry)
- if (cached !== undefined) { return cached }
-
- const positions = geometry.attributes.position
- const vertex_count = positions?.count ?? 0
- const neighbours: Array<Set<number>> = Array.from({ length: vertex_count }, () => new Set<number>())
- const index = geometry.getIndex()
- const triangle_count = Math.floor((index !== null ? index.count : vertex_count) / 3)
-
- for (let triangle = 0; triangle < triangle_count; triangle++) {
- const a = index !== null ? index.getX(triangle * 3) : triangle * 3
- const b = index !== null ? index.getX(triangle * 3 + 1) : triangle * 3 + 1
- const c = index !== null ? index.getX(triangle * 3 + 2) : triangle * 3 + 2
- neighbours[a]?.add(b)
- neighbours[a]?.add(c)
- neighbours[b]?.add(a)
- neighbours[b]?.add(c)
- neighbours[c]?.add(a)
- neighbours[c]?.add(b)
- }
-
- const adjacency = neighbours.map(set => [...set])
- this.adjacency_cache.set(geometry, adjacency)
- return adjacency
- }
-
/** World-space brush radius at a surface point, derived from the pixel size. */
public world_radius (camera: PerspectiveCamera, point: Vector3): number {
const forward = camera.getWorldDirection(new Vector3())
diff --git a/src/lib/processes/weight-painting/WeightPaintManager.ts b/src/lib/processes/weight-painting/WeightPaintManager.ts
index bb4bec5..2c180c7 100644
--- a/src/lib/processes/weight-painting/WeightPaintManager.ts
+++ b/src/lib/processes/weight-painting/WeightPaintManager.ts
@@ -1,6 +1,7 @@
import { Vector3, type Bone, type BufferGeometry, type Mesh } from 'three'
import { Generators } from '../../Generators'
import { Utility } from '../../Utilities.js'
+import { vertex_adjacency } from '../../geometry/VertexAdjacency.ts'
export interface PaintedInfluence {
bone_name: string
@@ -64,7 +65,6 @@ export class WeightPaintManager {
private current_stroke_backups: Map<Mesh, Map<number, SkinBackup>> | null = null
private readonly bone_colors = Generators.generate_deterministic_bone_colors(120)
private readonly mirror_cache = new WeakMap<BufferGeometry, MirrorData | null>()
- private readonly adjacency_cache = new WeakMap<BufferGeometry, number[][]>()
/** Which side of the body a bone belongs to, based on common naming patterns. */
public static bone_side (bone_name: string): BoneSide {
@@ -362,27 +362,48 @@ export class WeightPaintManager {
for (const brush_vertex of vertices) {
const current = this.current_influences(mesh, geometry_overrides, brush_vertex.vertex_index, bones)
const current_weight = current.get(bone_name) ?? 0
- const amount = Math.min(1, Math.max(0, strength * brush_vertex.falloff))
+ const amount = WeightPaintManager.brush_amount(strength, brush_vertex.falloff)
const target_weight = mode === 'add'
? current_weight + (1 - current_weight) * amount
: current_weight * (1 - amount)
const updated = this.renormalize(current, bone_name, target_weight, bones)
- this.record_stroke_backup(mesh, brush_vertex.vertex_index)
- geometry_overrides.set(brush_vertex.vertex_index, updated)
- this.write_skin_influences(mesh, brush_vertex.vertex_index, updated, bones)
- this.recolor_vertex(mesh, brush_vertex.vertex_index, updated, bones)
+ this.commit_vertex(mesh, geometry_overrides, brush_vertex.vertex_index, updated, bones)
painted_count++
}
- if (painted_count > 0) {
+ this.flush_paint(mesh, painted_count > 0)
+ return painted_count
+ }
+
+ /** Fraction (0-1) a vertex moves towards the target for one dab. */
+ private static brush_amount (strength: number, falloff: number): number {
+ return Math.min(1, Math.max(0, strength * falloff))
+ }
+
+ /** Stores a vertex's new influences as an override and writes them to the mesh. */
+ private commit_vertex (
+ mesh: Mesh,
+ geometry_overrides: GeometryWeightOverrides,
+ vertex_index: number,
+ influences: PaintedInfluence[],
+ bones: Bone[]
+ ): void {
+ this.record_stroke_backup(mesh, vertex_index)
+ geometry_overrides.set(vertex_index, influences)
+ this.write_skin_influences(mesh, vertex_index, influences, bones)
+ this.recolor_vertex(mesh, vertex_index, influences, bones)
+ }
+
+ /** Uploads the changed attributes once after a batch of commit_vertex calls. */
+ private flush_paint (mesh: Mesh, skin_changed: boolean): void {
+ if (skin_changed) {
this.mark_skin_attributes_dirty(mesh)
}
const color_attribute = mesh.geometry.attributes.color
if (color_attribute !== undefined) {
color_attribute.needsUpdate = true
}
- return painted_count
}
/** The brush vertices plus their counterparts across the symmetry plane. */
@@ -412,7 +433,7 @@ export class WeightPaintManager {
strength: number,
bones: Bone[]
): number {
- const adjacency = this.adjacency_for(mesh.geometry)
+ const adjacency = vertex_adjacency(mesh.geometry)
const influence_cache = new Map<number, Map<string, number>>()
const influences_at = (vertex_index: number): Map<string, number> => {
const cached = influence_cache.get(vertex_index)
@@ -426,7 +447,7 @@ export class WeightPaintManager {
// on the order the brush vertices are processed in.
const smoothed = new Map<number, PaintedInfluence[]>()
for (const vertex of vertices) {
- const amount = Math.min(1, Math.max(0, strength * vertex.falloff))
+ const amount = WeightPaintManager.brush_amount(strength, vertex.falloff)
if (amount <= 1e-6) { continue }
const neighbors = adjacency[vertex.vertex_index] ?? []
@@ -463,57 +484,13 @@ export class WeightPaintManager {
}
for (const [vertex_index, influences] of smoothed) {
- this.record_stroke_backup(mesh, vertex_index)
- geometry_overrides.set(vertex_index, influences)
- this.write_skin_influences(mesh, vertex_index, influences, bones)
- this.recolor_vertex(mesh, vertex_index, influences, bones)
+ this.commit_vertex(mesh, geometry_overrides, vertex_index, influences, bones)
}
- if (smoothed.size > 0) {
- this.mark_skin_attributes_dirty(mesh)
- const color_attribute = mesh.geometry.attributes.color
- if (color_attribute !== undefined) { color_attribute.needsUpdate = true }
- }
+ if (smoothed.size > 0) { this.flush_paint(mesh, true) }
return smoothed.size
}
- /** Connected vertex indices, built lazily from the geometry's triangles. */
- private adjacency_for (geometry: BufferGeometry): number[][] {
- const cached = this.adjacency_cache.get(geometry)
- if (cached !== undefined) { return cached }
-
- const vertex_count = geometry.attributes.position?.count ?? 0
- const neighbors: Array<Set<number>> = Array.from({ length: vertex_count }, () => new Set<number>())
- const connect = (a: number, b: number): void => {
- if (a === b || a < 0 || b < 0 || a >= vertex_count || b >= vertex_count) { return }
- neighbors[a].add(b)
- neighbors[b].add(a)
- }
-
- const index = geometry.index
- if (index !== null) {
- for (let i = 0; i + 2 < index.count; i += 3) {
- const a = index.getX(i)
- const b = index.getX(i + 1)
- const c = index.getX(i + 2)
- connect(a, b)
- connect(b, c)
- connect(c, a)
- }
- } else {
- // Non-indexed geometry: vertices in the same triangle are neighbours.
- for (let i = 0; i + 2 < vertex_count; i += 3) {
- connect(i, i + 1)
- connect(i + 1, i + 2)
- connect(i + 2, i)
- }
- }
-
- const result = neighbors.map(set => [...set])
- this.adjacency_cache.set(geometry, result)
- return result
- }
-
/**
* Finds matching vertices across the model's X symmetry plane. Built lazily
* and cached per geometry; only used when mirror painting is enabled.