Chapter 4 · Recipes

4.2An outfit: several garments in one Solver group

Garments that must touch each other need the same Solver instance. Share Gravity, Collision and Self Collision across them as single instances too. Before you start, run the report (3.3) and the Wrap check (3.4): a garment that a render mesh follows through a Tnpix Wrap must keep its point count, so it gets no Tnpix Garment.

Layers. Take the garments in dressing order, innermost first. In the Self Collision's garment list, layer 0 lies against the body and higher numbers lie further out. With Layer ordering on, where two garments on different layers meet, the lower one holds and the upper one gives way, layerHoldStrength times harder (4 by default), and the outer garment's friction acts between them (3.7). The garment list stores four parallel arrays (handles, layers, frictions, thicknesses); tpaGarmentEntry adds a garment or changes its entry and keeps the four in step.

global tpaGarmentEntry, tpaInch
-- one inch in the scene's unit: 1.0 in an inch scene, 2.54 in a centimetre scene
fn tpaInch = (
    local m = case units.SystemType of (
        #inches: 0.0254
        #feet: 0.3048
        #miles: 1609.344
        #millimeters: 0.001
        #centimeters: 0.01
        #meters: 1.0
        #kilometers: 1000.0
        default: 0.0254
    )
    0.0254 / (m * units.SystemScale)
)
-- add garment g to Self Collision sc's garment list, or change its entry: its layer (0 = against the body),
-- its own friction and its own thickness (-1 = not set); returns its row in the list
fn tpaGarmentEntry sc g layer fric thick = (
    local h = g.inode.handle
    local k = findItem (sc.memberHandles as array) h
    if k == 0 do ( append sc.memberHandles h; k = sc.memberHandles.count )
    while sc.memberLayers.count < k do append sc.memberLayers 0
    while sc.memberFrictions.count < k do append sc.memberFrictions -1.0
    while sc.memberThicknesses.count < k do append sc.memberThicknesses -1.0
    sc.memberLayers[k] = layer
    sc.memberFrictions[k] = fric
    sc.memberThicknesses[k] = thick
    k
)

Each garment gets its own material, with its own friction and grip. Here the shirt and the coat take Material Standard and the jacket takes Material Advanced:

undo "TenPix: outfit" on (
    local order = #($Shirt, $Jacket, $Coat)            -- innermost first; no Tnpix Garment on a Wrap's proxy (3.4)
    local inch = tpaInch()                              -- one inch in the scene's unit
    for o in order do (
        if o == $Jacket then (
            local ma = Tnpix_Material_Advanced()
            addModifier o ma
            local k = tpaMatIndex o ma
            TnpixTool.matPresetAt o k 4                 -- Heavy Fabric; a preset resets every dial first
            TnpixTool.matSetAt o k "friction" 0.6       -- then its own friction and grip (3.7)
            TnpixTool.matSetAt o k "grip" 4.0
        ) else (
            addModifier o (Tnpix_Material_Standard())
            TnpixTool.goldPreset o 0                    -- Cotton (approx); friction 0.35 and grip 3, switched on
        )
    )
    local sc = Tnpix_Self_Collision()
    sc.tnEnabled = true
    sc.thickness = 0.15 * inch                          -- a radius, in scene units: layers settle about twice this apart
    sc.friction = 0.0                                   -- acts only where no material sets friction (3.7)
    sc.overrideMaterialFriction = false                 -- each garment's material friction acts
    sc.mode = 2                                         -- Optimized
    sc.layerOrdering = true                             -- the lower layer holds, the upper one gives way
    sc.layerHoldStrength = 4.0
    local grav = Tnpix_Gravity()
    local col = Tnpix_Collision()
    local s = Tnpix_Solver()
    for i = 1 to order.count do (
        local o = order[i]
        addModifier o sc
        addModifier o grav
        addModifier o col
        addModifier o s                                 -- the same instance: one Solver group
        tpaGarmentEntry sc o (i - 1) -1.0 -1.0          -- shirt layer 0, jacket 1, coat 2
    )
    TnpixTool.colAdd order[1] $Body                     -- the collider list is group-wide: the body, the boots ...
)
  • One garment's entry. tpaGarmentEntry (tpaFirst $Jacket Tnpix_Self_Collision) $Jacket 1 -1.0 0.3 gives the jacket a shell of its own, 0.3 in the scene's unit, and keeps its layer. A garment's own friction in the list acts only where no material sets friction, unless Override material friction is on (3.7).
  • Self-collision thickness. It is a radius: two layers settle about twice this far apart. The panel suggests 0.05 to 0.30 inch, and a new modifier takes 0.15 inch in the scene's unit.
  • Adding an existing garment to a group. Add the group's shared modifiers and its Solver instance to it, for example addModifier $Pants (tpaFirst $Shirt Tnpix_Solver). An object carries one Solver, so a garment that has its own Solver must lose it first; ask before removing it. In the TenPix Graph, the artist can use + Add object instead.