Shade with vertex normals in the renderer, and add crease-angle normal smoothing so turned objects stop banding.
This commit is contained in:
parent
01e9605f1e
commit
8a2d4acff1
4 changed files with 130 additions and 7 deletions
|
|
@ -36,6 +36,7 @@ import {
|
|||
jitter,
|
||||
latheGeometry,
|
||||
mergeColoured,
|
||||
smoothNormals,
|
||||
type Level,
|
||||
} from '../core/index.ts'
|
||||
|
||||
|
|
@ -115,8 +116,12 @@ export function createModel(overrides: Partial<CeramicVaseConfig> = {}) {
|
|||
),
|
||||
]
|
||||
|
||||
// Smoothed at 40 degrees. The wall is a curve and reads as one; the
|
||||
// lip, the foot and the base cap all turn through more than that and
|
||||
// stay as edges. Flat shading put a visible band at every level of the
|
||||
// profile, which on a glazed white surface is the first thing you see.
|
||||
return {
|
||||
body: { slot: 'ceramic' as const, geometry: mergeColoured(pieces) },
|
||||
body: { slot: 'ceramic' as const, geometry: smoothNormals(mergeColoured(pieces), 40) },
|
||||
}
|
||||
},
|
||||
}, overrides)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { BufferAttribute, BufferGeometry, Color } from 'three'
|
||||
import { BufferAttribute, BufferGeometry, Color, Vector3 } from 'three'
|
||||
import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js'
|
||||
|
||||
/**
|
||||
|
|
@ -6,8 +6,14 @@ import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js'
|
|||
*
|
||||
* Everything is generated NON-INDEXED. The reason: on non-indexed geometry
|
||||
* computeVertexNormals() gives every triangle its own normal, so flat shading
|
||||
* becomes a natural consequence of the geometry — no material flag is needed.
|
||||
* In lowpoly that is exactly what we want.
|
||||
* becomes a natural consequence of the geometry and no material flag is needed.
|
||||
*
|
||||
* That was the whole answer in a kit with a lowpoly budget, where a barrel is
|
||||
* meant to read as staves. This kit has no such budget and is full of turned
|
||||
* and pressed objects that are genuinely smooth: a kettle, a basin, a mug, a
|
||||
* bollard, a traffic cone. Flat shading those puts visible bands across every
|
||||
* curve, so `smoothNormals` below is the way out, applied per object rather
|
||||
* than globally, because the choice belongs to the object.
|
||||
*
|
||||
* Position frame: point(a, r, y) = (sin a · r, y, cos a · r)
|
||||
* So a = 0 → the +Z direction; as a grows it turns toward +X.
|
||||
|
|
@ -868,3 +874,75 @@ export function mottleGeometry(
|
|||
colour.needsUpdate = true
|
||||
return geometry
|
||||
}
|
||||
|
||||
/**
|
||||
* Averages normals across edges that are not creases, in place.
|
||||
*
|
||||
* Non-indexed geometry has no idea which triangles are neighbours, so this
|
||||
* finds out the only way available: vertices at the same POSITION belong
|
||||
* together. Positions are quantised before comparison because two triangles
|
||||
* that share an edge were generated from the same numbers and land on the same
|
||||
* point to the last bit, while anything merely close is a different corner.
|
||||
*
|
||||
* The crease angle is what makes it usable. Averaging every shared vertex
|
||||
* would round the rim of the vase into the wall and the foot into the table,
|
||||
* which is how a smoothed lowpoly object turns to soap. Only faces that agree
|
||||
* to within the crease angle are averaged with each other, so a curve comes
|
||||
* out smooth and an edge stays an edge, with no hand-marked smoothing groups.
|
||||
*
|
||||
* @param crease Degrees. 30 keeps chamfers crisp; 60 smooths all but hard
|
||||
* corners; above about 80 everything melts together.
|
||||
*/
|
||||
export function smoothNormals(geometry: BufferGeometry, crease = 40): BufferGeometry {
|
||||
const position = geometry.getAttribute('position')
|
||||
if (!position || geometry.getIndex()) return geometry
|
||||
const count = position.count
|
||||
const limit = Math.cos((crease * Math.PI) / 180)
|
||||
|
||||
// Face normal per triangle, computed once.
|
||||
const faceNormal = new Float32Array((count / 3) * 3)
|
||||
const ax = new Vector3(), bx = new Vector3(), cx = new Vector3()
|
||||
const e1 = new Vector3(), e2 = new Vector3(), n = new Vector3()
|
||||
for (let f = 0; f < count / 3; f += 1) {
|
||||
ax.fromBufferAttribute(position, f * 3)
|
||||
bx.fromBufferAttribute(position, f * 3 + 1)
|
||||
cx.fromBufferAttribute(position, f * 3 + 2)
|
||||
n.copy(e1.subVectors(bx, ax)).cross(e2.subVectors(cx, ax))
|
||||
if (n.lengthSq() > 0) n.normalize()
|
||||
faceNormal[f * 3] = n.x; faceNormal[f * 3 + 1] = n.y; faceNormal[f * 3 + 2] = n.z
|
||||
}
|
||||
|
||||
// Which faces meet at each position.
|
||||
const at = new Map<string, number[]>()
|
||||
const key = (i: number): string => {
|
||||
const x = Math.round(position.getX(i) * 1e5)
|
||||
const y = Math.round(position.getY(i) * 1e5)
|
||||
const z = Math.round(position.getZ(i) * 1e5)
|
||||
return `${x},${y},${z}`
|
||||
}
|
||||
for (let i = 0; i < count; i += 1) {
|
||||
const k = key(i)
|
||||
const list = at.get(k)
|
||||
if (list) list.push(Math.floor(i / 3))
|
||||
else at.set(k, [Math.floor(i / 3)])
|
||||
}
|
||||
|
||||
const out = new Float32Array(count * 3)
|
||||
const sum = new Vector3()
|
||||
for (let i = 0; i < count; i += 1) {
|
||||
const face = Math.floor(i / 3)
|
||||
n.set(faceNormal[face * 3]!, faceNormal[face * 3 + 1]!, faceNormal[face * 3 + 2]!)
|
||||
sum.set(0, 0, 0)
|
||||
for (const other of at.get(key(i)) ?? [face]) {
|
||||
const m = new Vector3(faceNormal[other * 3]!, faceNormal[other * 3 + 1]!, faceNormal[other * 3 + 2]!)
|
||||
// Only neighbours on the same side of the crease contribute. A face
|
||||
// beyond it keeps its own normal and the edge survives.
|
||||
if (m.dot(n) >= limit) sum.add(m)
|
||||
}
|
||||
if (sum.lengthSq() === 0) sum.copy(n)
|
||||
sum.normalize()
|
||||
out[i * 3] = sum.x; out[i * 3 + 1] = sum.y; out[i * 3 + 2] = sum.z
|
||||
}
|
||||
geometry.setAttribute('normal', new BufferAttribute(out, 3))
|
||||
return geometry
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ export {
|
|||
prismGeometry,
|
||||
roughenGeometry,
|
||||
staveGeometry,
|
||||
smoothNormals,
|
||||
taperedBoxGeometry,
|
||||
} from './geometry.ts'
|
||||
export type { Level, MottleOptions, RoughenOptions, SheetLevel, Vec3 } from './geometry.ts'
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ import { deflateSync } from 'node:zlib'
|
|||
|
||||
import {
|
||||
Box3,
|
||||
Matrix3,
|
||||
Mesh,
|
||||
PerspectiveCamera,
|
||||
Sphere,
|
||||
|
|
@ -44,6 +45,17 @@ interface Triangle {
|
|||
/** Unlit surface (flame). The vertex colour is the result directly. */
|
||||
readonly unlit: boolean
|
||||
readonly opacity: number
|
||||
/**
|
||||
* Vertex normals, when the geometry carries ones that differ from the face.
|
||||
*
|
||||
* Null on flat-shaded geometry, which is most of it: these models are
|
||||
* non-indexed and `computeVertexNormals` then hands every triangle its own
|
||||
* normal, so interpolating three copies of the same vector would be work
|
||||
* for nothing.
|
||||
*/
|
||||
readonly na: Vector3 | null
|
||||
readonly nb: Vector3 | null
|
||||
readonly nc: Vector3 | null
|
||||
}
|
||||
|
||||
function collect(root: Object3D): Triangle[] {
|
||||
|
|
@ -68,20 +80,34 @@ function collect(root: Object3D): Triangle[] {
|
|||
const unlit = material.isMeshBasicMaterial === true
|
||||
const opacity = material.transparent ? (material.opacity ?? 1) : 1
|
||||
|
||||
const vertex = (i: number): { p: Vector3; c: [number, number, number] } => {
|
||||
const normals = geometry.getAttribute('normal')
|
||||
// Rotation only: a normal is a direction, so the translation in the world
|
||||
// matrix must not move it.
|
||||
const rotation = new Matrix3().getNormalMatrix(object.matrixWorld)
|
||||
|
||||
const vertex = (i: number): { p: Vector3; c: [number, number, number]; n: Vector3 | null } => {
|
||||
const v = index ? index.getX(i) : i
|
||||
const p = new Vector3().fromBufferAttribute(position, v).applyMatrix4(object.matrixWorld)
|
||||
const c: [number, number, number] = colour
|
||||
? [colour.getX(v), colour.getY(v), colour.getZ(v)]
|
||||
: [0.8, 0.8, 0.8]
|
||||
return { p, c }
|
||||
const n = normals
|
||||
? new Vector3().fromBufferAttribute(normals, v).applyMatrix3(rotation).normalize()
|
||||
: null
|
||||
return { p, c, n }
|
||||
}
|
||||
|
||||
for (let i = 0; i < count; i += 3) {
|
||||
const a = vertex(i), b = vertex(i + 1), c = vertex(i + 2)
|
||||
// Only worth interpolating when the three actually disagree. On flat
|
||||
// geometry they are identical and the face normal is both cheaper and
|
||||
// exactly as correct.
|
||||
const smooth = a.n !== null && b.n !== null && c.n !== null
|
||||
&& (a.n.dot(b.n) < 0.9995 || a.n.dot(c.n) < 0.9995)
|
||||
out.push({
|
||||
a: a.p, b: b.p, c: c.p,
|
||||
ca: a.c, cb: b.c, cc: c.c,
|
||||
na: smooth ? a.n : null, nb: smooth ? b.n : null, nc: smooth ? c.n : null,
|
||||
metalness: material.metalness ?? 0,
|
||||
roughness: material.roughness ?? 0.8,
|
||||
unlit,
|
||||
|
|
@ -256,7 +282,20 @@ function raster(frame: Frame, camera: PerspectiveCamera, triangles: readonly Tri
|
|||
tri.ca[1] * w1 + tri.cb[1] * w2 + tri.cc[1] * w0,
|
||||
tri.ca[2] * w1 + tri.cb[2] * w2 + tri.cc[2] * w0,
|
||||
]
|
||||
const rgb = shade(tri, normal, albedo)
|
||||
// Interpolate the normal across the face when the geometry asked for
|
||||
// smooth shading. Without this the renderer is flat by construction
|
||||
// and cannot show the difference between a faceted vase and a turned
|
||||
// one, which makes it useless for judging exactly the objects this
|
||||
// kit is full of.
|
||||
let shadingNormal = normal
|
||||
if (tri.na && tri.nb && tri.nc) {
|
||||
shadingNormal = new Vector3(
|
||||
tri.na.x * w1 + tri.nb.x * w2 + tri.nc.x * w0,
|
||||
tri.na.y * w1 + tri.nb.y * w2 + tri.nc.y * w0,
|
||||
tri.na.z * w1 + tri.nb.z * w2 + tri.nc.z * w0,
|
||||
).normalize()
|
||||
}
|
||||
const rgb = shade(tri, shadingNormal, albedo)
|
||||
const i = at * 3
|
||||
const alpha = tri.opacity
|
||||
frame.colour[i] = frame.colour[i]! * (1 - alpha) + rgb[0] * alpha
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue