agent (cont.)

+1:45
  • read_file{"EndLine":400,"Path":"~/work/lowmarch/review/vroid-02/index.html","StartLine":1}

    input

    {
      "EndLine": 400,
      "Path": "~/work/lowmarch/review/vroid-02/index.html",
      "StartLine": 1
    }

    result, 401 lines

    Showing lines 1-400 of 1737
    1: <meta charset="utf-8">
    2: <title>Lowmarch Model Viewer</title>
    3: <meta name="viewport" content="width=device-width, initial-scale=1">
    4: <link rel="preconnect" href="https://fonts.googleapis.com">
    5: <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    6: <link href="https://fonts.googleapis.com/css2?family=Alegreya+Sans:ital,wght@0,400;0,500;0,700;1,400&family=Pixelify+Sans:wght@500;700&family=Silkscreen&display=swap" rel="stylesheet">
    7: <style>
    8: /* Layout: the stage fills the window. Controls float over it in two bars (top: what to show, bottom:
    9:    turn and camera); notes, display options, pieces and the round's checks live in a side panel that
    10:    docks on wide screens and slides over the stage on narrow ones. The stage and its bars are a
    11:    fixed dark "screen"; the panel follows the page theme. */
    12: :root {
    13:   --bg: #eceef4; --panel: #f7f8fb; --fg: #1d1a2c; --muted: #5d5a73; --line: #d5d7e3;
    14:   --accent: #3a4ca0; --bad: #a2302a; --ok: #2d6a3e;
    15:   --screen: #100e18; --screen-2: #191626; --screen-line: #2c2840; --screen-fg: #e9e6f2; --screen-dim: #9c98b5;
    16:   --screen-on: #2a3366; --screen-on-line: #5a6fc8; --hud: rgba(16, 14, 24, 0.82); --focus: #f0c64a;
    17:   --f-display: "Pixelify Sans", ui-monospace, "Courier New", monospace;
    18:   --f-body: "Alegreya Sans", "Gill Sans", "Trebuchet MS", system-ui, sans-serif;
    19:   --f-label: "Silkscreen", "Pixelify Sans", ui-monospace, monospace;
    20:   --panel-w: 380px;
    21: }
    22: @media (prefers-color-scheme: dark) {
    23:   :root:not([data-theme="light"]) {
    24:     --bg: #13121b; --panel: #1b1a27; --fg: #e9e8f2; --muted: #a29fba; --line: #2d2b40;
    25:     --accent: #8fa8f0; --bad: #f08a80; --ok: #8fd6a0; color-scheme: dark;
    26:   }
    27: }
    28: :root[data-theme="dark"] {
    29:   --bg: #13121b; --panel: #1b1a27; --fg: #e9e8f2; --muted: #a29fba; --line: #2d2b40;
    30:   --accent: #8fa8f0; --bad: #f08a80; --ok: #8fd6a0; color-scheme: dark;
    31: }
    32: * { box-sizing: border-box; }
    33: html, body { height: 100%; margin: 0; }
    34: body { background: var(--screen); color: var(--fg); font: 16px/1.5 var(--f-body); overflow: hidden; }
    35: button:focus-visible, input:focus-visible, label:focus-within, a:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
    36: p { margin: 0; }
    37: 
    38: .app { position: fixed; inset: 0; display: grid; grid-template-columns: minmax(0, 1fr) auto; }
    39: .view { position: relative; min-width: 0; overflow: hidden; background: var(--screen-2); }
    40: .stage { position: absolute; inset: 0; touch-action: none; }
    41: .stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
    42: .status[hidden] { display: none; }
    43: .status { position: absolute; inset: 0; display: grid; place-items: center; font: 12px/1.4 var(--f-label); letter-spacing: 0.06em;
    44:           color: var(--screen-dim); text-transform: uppercase; text-align: center; padding: 20px; pointer-events: none; }
    45: 
    46: .hud { position: absolute; left: 0; right: 0; display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center;
    47:        padding: 10px 16px; color: var(--screen-fg); pointer-events: none; }
    48: .hud > * { pointer-events: auto; }
    49: .hud.top { top: 0; background: linear-gradient(var(--hud), rgba(16, 14, 24, 0.55) 70%, transparent); padding-bottom: 18px; }
    50: .hud.bottom { bottom: 0; background: linear-gradient(transparent, rgba(16, 14, 24, 0.55) 30%, var(--hud)); padding-top: 18px; }
    51: .brand { display: grid; gap: 0; margin-right: 6px; }
    52: .brand .eyebrow { font: 10px/1.3 var(--f-label); letter-spacing: 0.1em; text-transform: uppercase; color: var(--screen-dim); }
    53: .brand h1 { font: 700 20px/1.1 var(--f-display); margin: 0; white-space: nowrap; }
    54: .btn {
    55:   font: 11px/1 var(--f-label); letter-spacing: 0.06em; text-transform: uppercase;
    56:   background: rgba(16, 14, 24, 0.6); color: var(--screen-fg); border: 1px solid var(--screen-line);
    57:   padding: 8px 10px; border-radius: 3px; cursor: pointer; white-space: nowrap;
    58: }
    59: .btn:hover { border-color: var(--screen-on-line); }
    60: .btn[aria-pressed="true"] { background: var(--screen-on); border-color: var(--screen-on-line); }
    61: .btn[disabled] { opacity: 0.4; cursor: default; }
    62: .seg { display: inline-flex; }
    63: .seg[hidden] { display: none; }
    64: select.btn { max-width: 18rem; }                                    /* the grouped character list */
    65: select.btn option, select.btn optgroup { background: var(--screen); color: var(--screen-fg); }
    66: .seg .btn { border-radius: 0; margin-left: -1px; }
    67: .seg .btn:first-child { border-radius: 3px 0 0 3px; margin-left: 0; }
    68: .seg .btn:last-child { border-radius: 0 3px 3px 0; }
    69: .grow { flex: 1; }
    70: .turn { display: flex; gap: 10px; align-items: center; flex: 1 1 260px; min-width: 0; }
    71: .turn input { flex: 1; min-width: 80px; accent-color: var(--screen-on-line); }
    72: .turn output { font: 12px/1 var(--f-label); font-variant-numeric: tabular-nums; min-width: 40px; text-align: right; }
    73: .camhint { font-size: 13px; color: var(--screen-dim); }
    74: 
    75: .panel { width: var(--panel-w); background: var(--panel); color: var(--fg); border-left: 1px solid var(--line);
    76:          overflow-y: auto; overscroll-behavior: contain; padding: 18px 20px 40px; display: grid; gap: 26px; align-content: start; }
    77: .panel[hidden] { display: none; }
    78: .panel header { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
    79: .panel h2 { font: 700 20px/1.15 var(--f-display); margin: 0; }
    80: .panel h3 { font: 11px/1.4 var(--f-label); letter-spacing: 0.1em; text-transform: uppercase; color: var(--muted); margin: 0 0 8px; }
    81: .panel section { display: grid; gap: 8px; align-content: start; }
    82: .panel ul, .panel ol { margin: 0; padding-left: 1.15em; display: grid; gap: 8px; }
    83: .panel .close { font: 11px/1 var(--f-label); text-transform: uppercase; letter-spacing: 0.06em; background: none; color: var(--muted);
    84:                 border: 1px solid var(--line); border-radius: 3px; padding: 7px 9px; cursor: pointer; }
    85: .opts { display: flex; flex-wrap: wrap; gap: 6px; }
    86: .chip { font: 11px/1 var(--f-label); letter-spacing: 0.04em; text-transform: uppercase; display: inline-flex; gap: 6px;
    87:         align-items: center; padding: 7px 9px; border: 1px solid var(--line); border-radius: 999px; cursor: pointer; user-select: none; }
    88: .chip input { accent-color: var(--accent); margin: 0; }
    89: .check { font-size: 15px; display: grid; gap: 4px; }
    90: .check .sum { font-weight: 700; }
    91: .check .sum.bad { color: var(--bad); }
    92: .check .sum.ok { color: var(--ok); }
    93: .check details { color: var(--muted); font-size: 14px; }
    94: .check summary { cursor: pointer; }
    95: .sheets { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
    96: .sheets a { display: grid; gap: 4px; color: var(--accent); font-size: 14px; text-decoration: none; }
    97: .sheets img { width: 100%; height: 120px; object-fit: cover; object-position: top; border: 1px solid var(--line); border-radius: 3px; background: var(--screen); }
    98: .muted { color: var(--muted); }
    99: .panel footer { font-size: 13px; color: var(--muted); border-top: 1px solid var(--line); padding-top: 14px; }
    100: 
    101: @media (max-width: 900px) {
    102:   .app { grid-template-columns: minmax(0, 1fr); }
    103:   .panel { position: fixed; top: 0; right: 0; bottom: 0; width: min(var(--panel-w), 100vw); z-index: 5; box-shadow: -8px 0 24px rgba(0, 0, 0, 0.35); }
    104:   .camhint { display: none; }
    105:   .brand h1 { font-size: 17px; }
    106:   .hud { padding-inline: 12px; }
    107: }
    108: 
    109: /* compare: who stands where, under their feet; the picker's chips */
    110: .tags { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
    111: .tags span { position: absolute; transform: translateX(-50%); font: 11px/1.25 var(--f-label); letter-spacing: 0.04em;
    112:              text-transform: uppercase; text-align: center; color: var(--screen-fg); background: rgba(16, 14, 24, 0.72);
    113:              border: 1px solid var(--screen-line); border-radius: 3px; padding: 4px 6px; max-width: 16rem; }
    114: .pick { display: inline-flex; flex-wrap: wrap; gap: 6px; align-items: center; }
    115: .pick .btn { display: inline-flex; gap: 6px; align-items: center; }
    116: .pick .btn b { font-weight: 400; color: var(--screen-dim); }
    117: body.sheet .hud, body.sheet .panel, body.sheet .tags { display: none; }
    118: #sheetOut { display: none; }
    119: </style>
    120: 
    121: <div class="app">
    122:   <div class="view">
    123:     <div class="stage" id="stage"><div class="status" id="status">Loading the models…</div></div>
    124:     <div class="tags" id="tags" aria-hidden="true"></div>
    125:     <header class="hud top">
    126:       <div class="brand"><span class="eyebrow">Lowmarch · Model viewer</span><h1>Lowmarch cast</h1></div>
    127:       <span class="seg" role="group" aria-label="View">
    128:         <button class="btn" type="button" data-view="duel" aria-pressed="true">Duel</button>
    129:         <button class="btn" type="button" data-view="battle" aria-pressed="false">Battle</button>
    130:         <button class="btn" type="button" data-view="bust" aria-pressed="false">Portrait</button>
    131:         <button class="btn" type="button" data-view="lineup" aria-pressed="false" id="lineupBtn">Lineup</button>
    132:         <button class="btn" type="button" data-view="compare" aria-pressed="false" id="compareBtn" hidden>Compare</button>
    133:       </span>
    134:       <span class="seg" id="who" role="group" aria-label="Character"></span>
    135:       <span class="seg" role="group" aria-label="Pose" id="poseSeg" hidden>
    136:         <button class="btn" type="button" data-pose="up" aria-pressed="false">Arms up</button>
    137:         <button class="btn" type="button" data-pose="out" aria-pressed="true">Arms out</button>
    138:         <button class="btn" type="button" data-pose="down" aria-pressed="false">Arms down</button>
    139:       </span>
    140:       <select class="btn" id="skinSel" aria-label="Skin tone" hidden></select>
    141:       <span class="seg" role="group" aria-label="Motion" id="motionSeg" hidden>
    142:         <button class="btn" type="button" data-motion="still" aria-pressed="true">Still</button>
    143:         <button class="btn" type="button" data-motion="bounce" aria-pressed="false">Bounce</button>
    144:         <button class="btn" type="button" data-motion="twist" aria-pressed="false">Twist</button>
    145:       </span>
    146:       <span class="seg" role="group" aria-label="Animation" id="clipSeg">
    147:         <button class="btn" type="button" data-clip="clash" aria-pressed="true">Clash</button>
    148:         <button class="btn" type="button" data-clip="idle" aria-pressed="false">Idle</button>
    149:         <button class="btn" type="button" data-clip="attack" aria-pressed="false">Attack</button>
    150:         <button class="btn" type="button" data-clip="apose" aria-pressed="false">Arms out</button>
    151:       </span>
    152:       <span class="seg" role="group" aria-label="Renderer">
    153:         <button class="btn" type="button" data-mode="painted" aria-pressed="true">Painted</button>
    154:         <button class="btn" type="button" data-mode="3d" aria-pressed="false">3D</button>
    155:       </span>
    156:       <span class="grow"></span>
    157:       <button class="btn" type="button" id="panelBtn" aria-controls="panel" aria-expanded="false">Notes &amp; options</button>
    158:     </header>
    159:     <footer class="hud bottom">
    160:       <div class="turn">
    161:         <button class="btn" type="button" id="play" aria-pressed="false">Turn</button>
    162:         <input type="range" id="yaw" min="0" max="359" step="1" value="0" aria-label="Turn angle">
    163:         <output id="yawOut" for="yaw">0°</output>
    164:       </div>
    165:       <button class="btn" type="button" id="reset">Reset camera</button>
    166:       <span class="camhint" id="camHint">Drag to orbit · right-drag to pan · scroll to zoom · double-click a spot to orbit round it</span>
    167:     </footer>
    168:   </div>
    169: 
    170:   <aside class="panel" id="panel" hidden aria-label="Notes and options">
    171:     <header><h2>This round</h2><button class="close" type="button" id="panelClose">Close</button></header>
    172:     <section>
    173:       <ul>
    174:         <li><b>Test: MakeHuman bodies with VRoid heads</b> (the first three in <b>Lineup</b>, labelled "MH", next to the VRoid soldier on the far left). Same soldier kit. The bodies come from MakeHuman's CC0 shape controls (a slim young soldier, a stocky 50-year-old, a huge brute); the head is VRoid's CC0 male head, with its anime eyes, expressions and hair, scaled to each body and set on its neck the way VRoid does it (the neck runs up inside the head). Faces vary through our face presets. Sleeves: the puffs are now sized to the arm's real thickness (they were buried in MakeHuman's thicker arms, which is what looked like muscle), and on these bodies cloth spans the hollows between muscles.</li>
    175:         <li><b>Common enemies.</b> Press <b>Lineup</b> to see ten generic units in the enemy army's crimson, after a study of Unicorn Overlord's generic classes:
    176:           <ul>
    177:             <li><b>Soldier:</b> kettle hat, army-colour doublet with puffed sleeves, breastplate with plackart, a fauld of round scales, big leather cuffs, halberd.</li>
    178:             <li><b>Fighter:</b> kettle hat worn low, quilted gambeson, breastplate, spaulders, knife-pleated skirt, greaves, wooden round shield, sword.</li>
    179:             <li><b>Housecarl:</b> nasal helm over a mail coif, mail hauberk over the gambeson, short cape, wide belt, long axe.</li>
    180:             <li><b>Hoplite:</b> close helm with eye slit and breaths, coat of plates over mail, spaulders, a puffed capelet, a big steel shield with the army's device, spear.</li>
    181:             <li><b>Berserker</b> (huge, bare-armed, fur capelet, great axe), <b>hunter</b> (hood, cape, bow, quiver), <b>witch</b> (robe, pointed hat, staff) and a masked <b>thief</b>.</li>
    182:           </ul></li>
    183:         <li><b>Builds and faces vary.</b> The soldier's kit is also worn by a stocky grey veteran and a slim woman. Builds run from petite (the thief, 1.4 m) to huge (the berserker), with a new overall height control and oversized hands, as the study's units have. Faces differ in shape (jaw, chin, brow, nose, cheeks), expression, hair and eye colour, skin tone, and beards and moustaches. The lineup's check sheet has rows of faces with helmets off and on.</li>
    184:         <li><b>Gear kit:</b> helmets drawn on the skull chart, coifs and hoods, quilted coats, mail and scale textures, coats of plates, breastplates, puffed sleeves, pleated skirts, flared cuffs, heater, round and tower shields, spears, a halberd, axes, a bow, a dagger, a staff, robes and a witch's hat.</li>
    185:         <li><b>Hair:</b> Elaine's side-swept bangs and the nape and temple fixes from last round. Next for hair: studying how VRoid's sample hair is built, since you prefer it to the kit's.</li>
    186:       </ul>
    187:     </section>
    188:     <section>
    189:       <h3>Checks this build</h3>
    190:       <div id="checks" class="muted">Loading…</div>
    191:     </section>
    192:     <section>
    193:       <h3>Check sheets</h3>
    194:       <p class="muted">What I looked at: every view and pose, rendered by this page.</p>
    195:       <div class="sheets" id="sheets"></div>
    196:     </section>
    197:     <section>
    198:       <h3>Display</h3>
    199:       <div class="opts">
    200:         <button class="btn" type="button" id="cloth" aria-pressed="true">Cloth physics</button>
    201:         <button class="btn" type="button" id="physics" aria-pressed="true">Spring bones</button>
    202:         <button class="btn" type="button" id="wind" aria-pressed="false">Wind</button>
    203:         <button class="btn" type="button" id="resetCloth">Drop cloth</button>
    204:         <button class="btn" type="button" id="stepped" aria-pressed="false">12 fps</button>
    205:         <button class="btn" type="button" id="shade" aria-pressed="true">3D: toon</button>
    206:         <button class="btn" type="button" id="xray" aria-pressed="false">X-ray costume</button>
    207:       </div>
    208:     </section>
    209:     <section>
    210:       <h3>Pieces</h3>
    211:       <div class="opts" id="parts" aria-label="Pieces to show"></div>
    212:     </section>
    213:     <footer>Lowmarch, 8 October 2026. Base bodies: VRoid's CC0 sample models. Aldric's hair is from a VRoid Hub sample (commercial use and modification allowed). Costumes, swords, terrain, renderer and cloth are ours. Unicorn Overlord is Vanillaware's and Saber is Type-Moon's; we study their look, and no image of either is used here. The Saber study is built from written measurements of her design, to benchmark the armour kit; it is not part of the game.</footer>
    214:   </aside>
    215: </div>
    216: <textarea id="sheetOut" aria-hidden="true"></textarea>
    217: 
    218: <script type="importmap">
    219: { "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",
    220:                "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/" } }
    221: </script>
    222: <script type="module">
    223: import * as THREE from "three";
    224: import { OrbitControls } from "three/addons/controls/OrbitControls.js";
    225: import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
    226: 
    227: const $ = id => document.getElementById(id);
    228: const statusEl = $("status");
    229: const params = new URLSearchParams(location.search);
    230: const SHEET = params.get("sheet");                    // a character's name, or "duel": render a check sheet
    231: let CAM;
    232: try { CAM = await (await fetch("camera.json")).json(); }
    233: catch (e) { statusEl.textContent = "Couldn't load camera.json. Open this page from its published link."; throw e; }
    234: 
    235: const GROUP_LABELS = { body: "Body", hair: "Hair", tunic: "Tunic", tails: "Tails", belt: "Belt", trousers: "Trousers / tights",
    236:   boots: "Boots", gloves: "Gloves", collar: "Collar", pauldron: "Shoulder plates", cape: "Cape", trim: "Gold trim",
    237:   doublet: "Doublet", plate: "Plate", tabard: "Tabard", mantle: "Mantle", shield: "Shield", dress: "Dress",
    238:   sword: "Sword", weapon: "Weapon", helmet: "Helmet", hood: "Hood", padding: "Padded coat", mail: "Mail",
    239:   terrain: "Ground" };
    240: const COSTUME = new Set(["tunic", "tails", "belt", "trousers", "boots", "gloves", "collar", "pauldron", "cape", "trim",
    241:   "doublet", "plate", "tabard", "mantle", "shield", "dress", "helmet", "hood", "padding", "mail"]);
    242: // role ids for the info pass (features 4-9 get no inner lines; hair 3 and 11 shade the forehead)
    243: const ROLE_ID = { face: 1, body: 2, hair: 3, line: 4, iris: 5, white: 6, highlight: 7, brow: 8, mouth: 9, cloth: 10, hair_b: 11 };
    244: let nextRole = 12;
    245: const roleId = r => (ROLE_ID[r] ??= nextRole++);
    246: const VIEW_YAW = { duel: 0, battle: 60, bust: 30, lineup: 20, compare: 0 };
    247: const state = { view: "duel", who: CAM.characters[0].name, yaw: 0, mode: "painted", skin: "", toon: true, xray: false, hidden: new Set(),
    248:                 cloth: true, wind: false, playing: false, stepped: false, clip: "clash", pose: "out", compare: [], motion: "still",
    249:                 physics: true };
    250: const charDef = name => CAM.characters.find(c => c.name === name);
    251: // a workshop page (camera.json "page"): its own title and notes, and no duel
    252: const PAGE = CAM.page || null;
    253: const NO_DUEL = PAGE?.duel === false;
    254: if (PAGE) {
    255:   document.title = `${PAGE.title} · Lowmarch`;
    256:   document.querySelector(".brand .eyebrow").textContent = PAGE.eyebrow || "Lowmarch";
    257:   document.querySelector(".brand h1").textContent = PAGE.title;
    258:   if (PAGE.notes) document.querySelector("#panel section ul").innerHTML = PAGE.notes;
    259: }
    260: // the armory: bodies compared side by side (the Compare view) in three poses, instead of clips and a lineup
    261: const ARMORY_PAGE = !!PAGE?.armory;
    262: const MAX_COMPARE = 6;
    263: if (ARMORY_PAGE) {
    264:   $("compareBtn").hidden = false; $("poseSeg").hidden = false; $("clipSeg").hidden = true; $("lineupBtn").hidden = true;
    265:   $("motionSeg").hidden = false;
    266:   if (PAGE.skins && PAGE.skins.length) {             // skin picker: try any tone of the range on the bodies shown
    267:     const sel = $("skinSel");
    268:     sel.append(new Option("Skin: as built", ""));
    269:     for (const g of [...new Set(PAGE.skins.map(s => s.group))]) {
    270:       const og = document.createElement("optgroup"); og.label = g;
    271:       PAGE.skins.filter(s => s.group === g).forEach(s => og.append(new Option(s.label, s.name)));
    272:       sel.append(og);
    273:     }
    274:     if (PAGE.skins.some(s => s.name === params.get("skin"))) state.skin = params.get("skin");    // ?skin=<tone>
    275:     sel.value = state.skin || "";
    276:     sel.hidden = false;
    277:     sel.addEventListener("change", () => { state.skin = sel.value; });
    278:   }
    279:   if (["still", "bounce", "twist"].includes(params.get("motion"))) state.motion = params.get("motion");
    280:   state.compare = (params.get("compare") || "").split(",").filter(n => charDef(n)).slice(0, MAX_COMPARE);
    281:   if (!state.compare.length) state.compare = CAM.characters.slice(0, 2).map(c => c.name);
    282:   if (["up", "out", "down"].includes(params.get("pose"))) state.pose = params.get("pose");
    283: }
    284: if (NO_DUEL) {
    285:   document.querySelector('[data-view="duel"]').hidden = true;
    286:   document.querySelector('[data-clip="clash"]').hidden = true;
    287:   state.clip = "idle";
    288: }
    289: 
    290: const hex = h => {                                     // raw palette value, no colour-space conversion
    291:   const n = parseInt(h.slice(1), 16);
    292:   return new THREE.Color().setRGB((n >> 16 & 255) / 255, (n >> 8 & 255) / 255, (n & 255) / 255, THREE.LinearSRGBColorSpace);
    293: };
    294: const rawTex = t => { if (!t) return null; const c = t.clone(); c.colorSpace = THREE.NoColorSpace; c.needsUpdate = true; return c; };
    295: const gradient = (() => {
    296:   const d = new Uint8Array([70, 70, 70, 255, 140, 140, 140, 255, 205, 205, 205, 255, 255, 255, 255, 255]);
    297:   const t = new THREE.DataTexture(d, 4, 1, THREE.RGBAFormat); t.minFilter = t.magFilter = THREE.NearestFilter; t.needsUpdate = true; return t;
    298: })();
    299: const white = (() => { const t = new THREE.DataTexture(new Uint8Array([255, 255, 255, 255]), 1, 1, THREE.RGBAFormat); t.needsUpdate = true; return t; })();
    300: 
    301: // ---- scene and camera -----------------------------------------------------------------------------------------------
    302: const scene = new THREE.Scene();
    303: const hemi = new THREE.HemisphereLight(0xfff6ee, 0x3a3450, 1.35);
    304: const sun = new THREE.DirectionalLight(0xffffff, 1.6);
    305: scene.add(hemi, sun);
    306: const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 40);
    307: 
    308: // ---- shaders --------------------------------------------------------------------------------------------------------
    309: const SKIN_HEAD = `#include <common>
    310:   #include <skinning_pars_vertex>`;
    311: const SKIN_BODY = `#include <beginnormal_vertex>
    312:     #include <skinbase_vertex>
    313:     #include <skinnormal_vertex>
    314:     #include <begin_vertex>
    315:     #include <skinning_vertex>`;
    316: const PAINT_VS = `${SKIN_HEAD}
    317:   attribute float edge; attribute vec4 _plate; uniform float uLift;
    318:   varying vec3 vN; varying vec2 vUv; varying vec3 vView; varying vec3 vObj; varying float vEdge; varying vec4 vPlate;
    319:   void main() {
    320:     ${SKIN_BODY}
    321:     vN = normalize(normalMatrix * objectNormal); vUv = uv; vObj = position; vEdge = edge; vPlate = _plate;
    322:     vec4 mv = modelViewMatrix * vec4(transformed, 1.0); vView = -mv.xyz;
    323:     mv.z += uLift;                                              // brows: drawn over the bangs (depth lift toward the camera)
    324:     gl_Position = projectionMatrix * mv;
    325:   }`;
    326: const HSV = `
    327:   vec3 rgb2hsv(vec3 c) {
    328:     vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    329:     vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    330:     vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
    331:     float d = q.x - min(q.w, q.y);
    332:     return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + 1e-10)), d / (q.x + 1e-10), q.x);
    333:   }
    334:   vec3 hsv2rgb(vec3 c) {
    335:     vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    336:     vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    337:     return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
    338:   }`;
    339: // painted renderer, after the Unicorn Overlord study: tight terminators, hue-shifted ramps built from
    340: // one base colour per material, hard reflection bands on steel, strand highlights on hair, a stage
    341: // grade and a back rim
    342: const PAINT_FS = `
    343:   uniform int uKind; uniform vec3 uBase; uniform vec3 uKey; uniform sampler2D uMap; uniform int uAlpha;
    344:   uniform int uPass; uniform vec4 uInfo; uniform vec3 uEye[3]; uniform vec3 uFlat; uniform vec3 uSkinRef;
    345:   uniform vec3 uGold; uniform vec3 uAmbient; uniform vec3 uKeyCol; uniform vec3 uRimCol; uniform float uGrain;
    346:   uniform int uFaceTex; uniform int uDecal; uniform vec3 uMotif; uniform vec3 uBand; uniform vec3 uSkinTone; uniform int uShadeOnly;
    347:   uniform int uSkinRamp; uniform vec3 uSkinL; uniform vec3 uSkinS; uniform vec3 uSkinC;
    348:   uniform sampler2D tCastInfo; uniform sampler2D tCastDepth; uniform vec2 uCastRes; uniform int uCastOn;
    349:   uniform float uCastNear; uniform float uCastFar; uniform float uCastPpm;
    350:   // Cast shadows on skin, in screen space (the "depth offset" shadows of anime toon shaders): walk from this pixel
    351:   // toward the key light across the info pass already drawn; if the face (the jaw) or hair stands in front of that
    352:   // ray, this pixel is in its shadow. The shape is the caster's own outline as the camera sees it, at any angle.
    353:   float castShadow() {
    354:     vec2 uv = gl_FragCoord.xy / uCastRes, px = 1.0 / uCastRes;
    355:     float z0 = uCastNear + texture2D(tCastDepth, uv).x * (uCastFar - uCastNear);
    356:     float sxy = length(uKey.xy);
    357:     if (sxy < 0.05) return 0.0;
    358:     vec2 dir = uKey.xy / sxy;
    359:     float lz = uKey.z / sxy;                                    // the ray's rise toward the camera per metre across
    360:     for (int s = 1; s <= 14; s++) {
    361:       float dm = float(s) * 0.003;                              // 3 mm steps across the screen, up to 4.2 cm
    362:       vec2 q = uv + dir * dm * uCastPpm * px;
    363:       vec4 ni = texture2D(tCastInfo, q);
    364:       if (ni.a < 0.5) continue;
    365:       int r = int(ni.r * 255.0 + 0.5);
    366:       if (r != 1 && r != 3 && r != 11) continue;                // casters: the face (jaw, chin) and hair
    367:       float zq = uCastNear + texture2D(tCastDepth, q).x * (uCastFar - uCastNear);
    368:       float zr = z0 - lz * dm;
    369:       if (zq < zr - 0.004 && zq > zr - 0.20) return 1.0;
    370:     }
    371:     return 0.0;
    372:   }
    373:   varying vec3 vN; varying vec2 vUv; varying vec3 vView; varying vec3 vObj; varying float vEdge; varying vec4 vPlate;
    374:   ${HSV}
    375:   float hash(vec3 p) { p = fract(p * 0.3183099 + 0.1); p *= 17.0; return fract(p.x * p.y * p.z * (p.x + p.y + p.z)); }
    376:   float vnoise(vec3 x) {
    377:     vec3 i = floor(x), f = fract(x); f = f * f * (3.0 - 2.0 * f);
    378:     return mix(mix(mix(hash(i), hash(i + vec3(1,0,0)), f.x), mix(hash(i + vec3(0,1,0)), hash(i + vec3(1,1,0)), f.x), f.y),
    379:                mix(mix(hash(i + vec3(0,0,1)), hash(i + vec3(1,0,1)), f.x), mix(hash(i + vec3(0,1,1)), hash(i + vec3(1,1,1)), f.x), f.y), f.z);
    380:   }
    381:   float toward(float h, float target, float amt) { float d = target - h; d -= floor(d + 0.5); return fract(h + clamp(d, -amt, amt)); }
    382:   vec3 tone(vec3 base, float hue, float amt, float sMul, float vMul) {
    383:     vec3 h = rgb2hsv(base);
    384:     return hsv2rgb(vec3(toward(h.x, hue, amt), clamp(h.y * sMul, 0.0, 1.0), clamp(h.z * vMul, 0.0, 1.0)));
    385:   }
    386:   float st(float x, float t) { return smoothstep(t - 0.03, t + 0.03, x); }
    387:   // Steel and gold. Hard reflection bands (dark below the horizon, a dark core under a white streak)
    388:   // read well where the surface turns fast: rims, tubes, lames. On a broad plate the reflection
    389:   // barely changes, so a band there fills the whole plate (the blotches): bands fade by how sharply
    390:   // the surface curves (normal change per metre on screen), and broad plates get a soft tone that
    391:   // lightens up each outline plate (hgt: height in the plate 0..1, or -1 without plate data).
    392:   vec3 metal(bool gold, vec3 base, vec3 n, vec3 v, float lit, float hgt) {
    393:     vec3 r = reflect(-v, n);
    394:     float ry = r.y + (vnoise(vObj * vec3(3.0, 14.0, 3.0)) - 0.5) * 0.14;   // jitter the bands along their length, not into blobs
    395:     float curv = length(fwidth(n)) / max(length(fwidth(vView)), 1e-5);
    396:     float sharp = smoothstep(5.0, 14.0, curv);
    397:     // a shaped plate's domes (over the bust, a crowned panel) turn the bands into closed rings: plates
    398:     // keep only a little of them and take a crisp highlight toward the light instead
    399:     if (hgt >= 0.0) sharp *= 0.25; else sharp = max(sharp, 0.3);

    1 more lines not shown.

  • read_file{"EndLine":800,"Path":"~/work/lowmarch/review/vroid-02/index.html","StartLine":401}

    input

    {
      "EndLine": 800,
      "Path": "~/work/lowmarch/review/vroid-02/index.html",
      "StartLine": 401
    }

    result, 401 lines

    Showing lines 401-800 of 1737
    401:     if (!gold) { s0 = tone(base, 0.68, 0.10, 2.4, 0.32); s1 = tone(base, 0.68, 0.06, 1.7, 0.76);
    402:                  s2 = tone(base, 0.12, 0.0, 0.8, 0.88); s3 = vec3(0.97, 0.96, 0.92); }
    403:     else { s0 = vec3(0.30, 0.20, 0.30); s1 = tone(base, 0.20, 0.03, 1.0, 0.72); s2 = base; s3 = tone(base, 0.16, 0.03, 0.5, 1.5); }
    404:     vec3 hard = mix(s0, s1, st(ry, -0.42));                          // facing down: dark
    405:     hard = mix(hard, s2, st(ry, -0.08));                             // facing us: the plate's own grey
    406:     hard = mix(hard, s0, st(ry, 0.36));                              // dark core under the streak
    407:     hard = mix(hard, s3, st(ry, 0.47));                              // the white streak
    408:     hard = mix(hard, s2 * 1.06, st(ry, 0.70));
    409:     float k = smoothstep(-0.55, 0.30, r.y);                          // broad: darker toward the ground (no jitter:
    410:     if (hgt >= 0.0) k = clamp(k * 0.45 + hgt * 0.80 - 0.18, 0.0, 1.0);   // on a flat plate it streaks)
    411:     else k = clamp(k * 0.6 + n.y * 0.35 + 0.05, 0.0, 1.0);           // without plate data: lighter facing up
    412:     vec3 soft = mix(s1 * 0.9, s2, k);
    413:     float hl = dot(n, normalize(uKey + v));                          // the key's highlight, a hard-edged shape
    414:     if (hgt >= 0.0) soft = mix(soft, mix(s2, s3, 0.6), st(hl, 0.955) * smoothstep(0.45, 0.8, hgt));   // the upper part only
    415:     vec3 col = mix(soft, hard, sharp);
    416:     return mix(col * 0.72 + s0 * 0.28, col, st(lit, 0.30));
    417:   }
    418:   void main() {
    419:     vec4 tex = texture2D(uMap, vUv);
    420:     if (uAlpha == 1 && tex.a < 0.5) discard;
    421:     if (uAlpha == 2 && (tex.a < 0.5 || dot(tex.rgb, vec3(1.0 / 3.0)) >= 0.42)) discard;
    422:     if (uPass == 1) { gl_FragColor = uInfo; return; }
    423:     vec3 n = normalize(vN); if (!gl_FrontFacing) n = -n;
    424:     vec3 v = normalize(vView);
    425:     float lit = dot(n, uKey) * 0.5 + 0.5;
    426:     if (uShadeOnly == 1) lit = min(lit, 0.37);                        // shadow cards only ever darken skin, to a soft shade
    427:     else if (uCastOn == 1 && uKind == 1 && int(uInfo.x * 255.0 + 0.5) == 2 && castShadow() > 0.5)
    428:       lit = min(lit, 0.37);                                           // body skin under the jaw's or the hair's shadow
    429:     float g1 = vnoise(vObj * 40.0), g2 = vnoise(vObj * 150.0);
    430:     vec3 col;
    431:     if (uKind == 0 || uKind == 5 || uKind == 6) {                    // cloth, leather, ground
    432:       vec3 hb = rgb2hsv(uBase);
    433:       bool cool = hb.x > 0.42 && hb.x < 0.86;
    434:       float sh = uKind == 5 ? 0.93 : (cool ? 0.70 : 0.96);
    435:       float li = cool ? 0.52 : 0.13;
    436:       vec3 c0 = tone(uBase, sh, 0.09, 1.12, 0.40), c1 = tone(uBase, sh, 0.05, 1.08, 0.64);
    437:       vec3 c3 = tone(uBase, li, 0.03, 0.80, 1.20);
    438:       float l = lit + (g1 - 0.5) * uGrain - (uKind == 6 ? 0.22 : 0.0);  // ground: a flat top sits at the base colour
    439:       col = mix(c0, c1, st(l, 0.30)); col = mix(col, uBase, st(l, 0.50)); col = mix(col, c3, st(l, 0.91));
    440:       if (uKind == 5) col *= 0.965 + 0.07 * vnoise(vObj * 22.0);       // mottled leather
    441:     } else if (uKind == 1) {                                           // skin: pale, nearly unshaded from the front
    442:       vec3 sL = vec3(0.973, 0.882, 0.835), sS = vec3(0.776, 0.659, 0.608), sC = vec3(0.451, 0.322, 0.310);
    443:       if (uSkinRamp == 1) { sL = uSkinL; sS = uSkinS; sC = uSkinC; }   // the character's tone, designed per tone
    444:       col = mix(sC, sS, st(lit, 0.20)); col = mix(col, sL, st(lit, 0.38));
    445:       if (uFaceTex == 1) col *= clamp(tex.rgb / uSkinRef, 0.55, 1.05);
    446:       if (uFaceTex == 1 && vUv.x > 0.38 && vUv.x < 0.62 && vUv.y > 0.68 && vUv.y < 0.84) {
    447:         // the closed mouth: VRoid paints a 2-texel line that the clamp above leaves faint; ink it and widen it
    448:         // (the darkest of a few taps), so it follows the mouth's shape keys as a drawn line
    449:         float d = 0.0, sl = dot(uSkinRef, vec3(1.0 / 3.0));
    450:         for (int k = -2; k <= 2; k++) for (int j = -1; j <= 1; j++) {
    451:           vec3 t = texture2D(uMap, vUv + vec2(float(j) * 1.2, float(k) * 0.8) / 512.0).rgb;
    452:           d = max(d, 1.0 - dot(t, vec3(1.0 / 3.0)) / sl);
    453:         }
    454:         float taper = 1.0 - 0.55 * smoothstep(0.035, 0.065, abs(vUv.x - 0.5));   // a brush stroke: lighter at the corners
    455:         col = mix(col, vec3(0.42, 0.22, 0.22), smoothstep(0.10, 0.22, d) * taper);
    456:       }
    457:       if (uSkinRamp == 0) col *= uSkinTone;                            // older builds: one tint over the light skin
    458:     } else if (uKind == 2) {                                           // hair: four even bands, hard strand highlights
    459:       float strand = dot(tex.rgb, vec3(1.0 / 3.0));
    460:       vec3 c0 = tone(uBase, 0.72, 0.07, 1.10, 0.45), c1 = tone(uBase, 0.72, 0.035, 1.05, 0.68);
    461:       vec3 c2 = tone(uBase, 0.56, 0.02, 0.92, 1.10), c3 = tone(uBase, 0.56, 0.03, 0.55, 1.42);
    462:       float l = lit + (strand - 0.6) * 0.12;
    463:       col = mix(c0, c1, st(l, 0.40)); col = mix(col, uBase, st(l, 0.56)); col = mix(col, c2, st(l, 0.84));
    464:       float sp = dot(n, normalize(uKey + v)) + (strand - 0.6) * 0.18;
    465:       col = mix(col, c3, st(sp, 0.90) * (1.0 - st(sp, 0.985)));
    466:     } else if (uKind == 3 || uKind == 4) {                             // steel, gold
    467:       bool plate = vPlate.x > 0.5;
    468:       float e = vPlate.y, w = vPlate.z;
    469:       bool gilt = uKind == 4 || (plate && w > 0.0 && e < w);            // an outline plate's gold border
    470:       col = metal(gilt, gilt && uKind == 3 ? uGold : uBase, n, v, lit, plate ? vPlate.w : -1.0);
    471:       if (plate && w > 0.0) {                                           // engraved lines: one inside the border, one on its seam
    472:         float aa = fwidth(e) * 1.2 + 1e-5;
    473:         float l1 = 1.0 - smoothstep(0.0010, 0.0010 + aa, abs(e - (w + 0.0050)));
    474:         float l2 = 1.0 - smoothstep(0.0006, 0.0006 + aa, abs(e - w));
    475:         col = mix(col, col * 0.42, max(l1, l2 * 0.8));
    476:       }
    477:     } else if (uKind == 7) {                                           // iris
    478:       float l = dot(tex.rgb, vec3(1.0 / 3.0));
    479:       col = l < 0.3 ? mix(uEye[0], uEye[1], l / 0.3) : mix(uEye[1], uEye[2], clamp((l - 0.3) / 0.6, 0.0, 1.0));
    480:     } else if (uKind == 8) { col = tex.rgb; }
    481:     else if (uKind == 10) {                                            // the open mouth: VRoid's map read as three parts,
    482:       float lum = dot(tex.rgb, vec3(1.0 / 3.0));                       // teeth (white), tongue (its light pink) and the
    483:       col = mix(vec3(0.36, 0.11, 0.14), vec3(0.78, 0.38, 0.42), smoothstep(0.53, 0.58, tex.g));   // dark inside
    484:       col = mix(col, vec3(0.98, 0.96, 0.95), smoothstep(0.80, 0.86, lum));
    485:     }
    486:     else { col = uFlat; }
    487:     if (vPlate.x > 0.5 && vPlate.z > 0.0 && uKind != 3 && uKind != 4) {   // a cloth outline piece's gold border,
    488:       float e = vPlate.y, w = vPlate.z, aa = fwidth(e) * 1.2 + 1e-5;       // and a fine gold line inside it
    489:       float band = 1.0 - smoothstep(w - aa, w, e);
    490:       float l1 = 1.0 - smoothstep(0.0006, 0.0006 + aa, abs(e - (w + 0.0040)));
    491:       col = mix(col, metal(true, uGold, n, v, lit, -1.0), max(band, l1 * 0.9));
    492:     }
    493:     if (uDecal == 1) {                                                 // a design in the piece's own coordinates:
    494:       vec3 dm = tex.rgb;                                               // R paint, G groove, B gold inlay
    495:       float pm = smoothstep(0.3, 0.7, dm.r), gm = smoothstep(0.3, 0.7, dm.g), au = smoothstep(0.3, 0.7, dm.b);
    496:       if (au > 0.0) col = mix(col, metal(true, uGold, n, v, lit, -1.0), au);
    497:       if (pm > 0.0) {
    498:         vec3 pc = mix(tone(uMotif, 0.70, 0.05, 1.10, 0.62), uMotif, st(lit, 0.42));
    499:         col = mix(col, mix(pc, tone(uMotif, 0.56, 0.03, 0.8, 1.25), st(lit, 0.92)), pm);
    500:       }
    501:       if (gm > 0.0) col = mix(col, col * 0.30, gm);
    502:     }
    503:     if (vEdge > 0.5) {                                                 // the cloth's border band
    504:       vec3 gd = tone(uBand, 0.20, 0.03, 1.0, 0.72);
    505:       col = mix(gd, uBand, st(lit, 0.45)); col = mix(col, tone(uBand, 0.16, 0.03, 0.5, 1.45), st(lit, 0.85));
    506:     }
    507:     if (uKind <= 6) {
    508:       vec3 grade = mix(uAmbient, uKeyCol, st(lit, 0.55));
    509:       col *= uKind == 1 && uSkinRamp == 1 ? mix(vec3(1.0), grade, 0.3) : grade;   // a designed skin ramp takes less of it
    510:       float fr = pow(1.0 - max(dot(n, v), 0.0), 4.0);
    511:       float back = clamp(dot(n, normalize(vec3(0.55, 0.45, -0.35))), 0.0, 1.0);
    512:       col += uRimCol * fr * smoothstep(0.1, 0.5, back);
    513:       if (uKind != 1) col *= 1.0 + (g2 - 0.5) * 0.06;
    514:     }
    515:     gl_FragColor = vec4(clamp(col, 0.0, 1.0), 1.0);
    516:   }`;
    517: const PAINT_COMP = `
    518:   uniform sampler2D tColor; uniform sampler2D tInfo; uniform sampler2D tDepth; uniform vec2 uRes;
    519:   uniform float uNear; uniform float uFar; uniform float uGap; uniform float uSil; uniform float uInner; uniform float uPpm;
    520:   ${HSV}
    521:   float z(vec2 p) { return uNear + texture2D(tDepth, p).x * (uFar - uNear); }
    522:   int rid(vec4 i) { return int(i.r * 255.0 + 0.5); }
    523:   float pid(vec4 i) { return floor(i.g * 255.0 + 0.5) + floor(i.b * 255.0 + 0.5) * 256.0; }
    524:   vec3 ink(vec3 c) { vec3 h = rgb2hsv(c); return hsv2rgb(vec3(h.x, clamp(h.y, 0.20, 0.45), 0.19)); }
    525:   // How far (px) the mouth's opening runs from this pixel in direction d (2 px steps, up to 128 px).
    526:   float mouthRun(vec2 uv, vec2 d) {
    527:     float n = 0.0;
    528:     for (int s = 1; s <= 64; s++) { vec4 a = texture2D(tInfo, uv + d * float(2 * s)); if (a.a < 0.5 || rid(a) != 9) break; n += 2.0; }
    529:     return n;
    530:   }
    531:   // Brows drawn over the bangs: where hair lies just above or below the stroke, the brow is seen through the hair,
    532:   // so it takes some of the hair's colour and doesn't sit on it like a sticker.
    533:   vec3 browOverHair(vec2 uv, vec2 px, vec3 col) {
    534:     float d = 3.5 * uSil;
    535:     vec4 a = texture2D(tInfo, uv + vec2(0.0, d) * px), b = texture2D(tInfo, uv - vec2(0.0, d) * px);
    536:     bool ha = a.a > 0.5 && (rid(a) == 3 || rid(a) == 11), hb = b.a > 0.5 && (rid(b) == 3 || rid(b) == 11);
    537:     if (!(ha || hb)) return col;
    538:     vec3 hc = ha ? texture2D(tColor, uv + vec2(0.0, d) * px).rgb : texture2D(tColor, uv - vec2(0.0, d) * px).rgb;
    539:     return mix(col, hc * 0.80, ha && hb ? 0.68 : 0.34);
    540:   }
    541:   void main() {
    542:     vec2 uv = gl_FragCoord.xy / uRes, px = 1.0 / uRes;
    543:     vec4 inf = texture2D(tInfo, uv);
    544:     if (inf.a < 0.5) {                                          // outer line: grow the silhouette outward
    545:       vec4 best = vec4(0.0); vec2 bq = uv;
    546:       for (int k = 0; k < 12; k++) {
    547:         float a = float(k) * 0.5236;
    548:         for (int s = 1; s <= 2; s++) {
    549:           vec2 q = uv + vec2(cos(a), sin(a)) * uSil * float(s) * 0.5 * px;
    550:           vec4 ni = texture2D(tInfo, q);
    551:           if (ni.a > 0.5) { best = ni; bq = q; }
    552:         }
    553:       }
    554:       if (best.a > 0.5) {
    555:         int r = rid(best);
    556:         if (r >= 4 && r <= 7) { gl_FragColor = vec4(0.0); return; }
    557:         gl_FragColor = vec4(ink(texture2D(tColor, bq).rgb), 1.0); return;
    558:       }
    559:       gl_FragColor = vec4(0.0); return;
    560:     }
    561:     vec3 col = texture2D(tColor, uv).rgb; int r0 = rid(inf); float p0 = pid(inf); float z0 = z(uv);
    562:     bool feature = r0 >= 4 && r0 <= 9;
    563:     if (r0 == 9) {                                              // the open mouth's rim: a lip line where it meets the face
    564:       for (int k = 0; k < 8; k++) {
    565:         float a = float(k) * 0.785398;
    566:         vec2 q = uv + vec2(cos(a), sin(a)) * uSil * 0.9 * px;
    567:         vec4 ni = texture2D(tInfo, q);
    568:         if (ni.a > 0.5 && rid(ni) == 1) { col = mix(ink(texture2D(tColor, q).rgb), vec3(0.30, 0.10, 0.12), 0.4); break; }
    569:       }
    570:       float hu = mouthRun(uv, vec2(0.0, px.y)), hd = mouthRun(uv, vec2(0.0, -px.y)), h = hu + hd + 1.0;
    571:       // teeth only in an opening tall enough to show them (8 to 11 mm, measured on the face): a slit or parted lips
    572:       // (angry, sad) is all teeth in VRoid's mesh and read as a white pill; it reads as the dark inside instead
    573:       float thin = 1.0 - smoothstep(0.008, 0.011, h / uPpm);
    574:       if (thin > 0.0 && dot(col, vec3(1.0 / 3.0)) >= 0.8) col = mix(col, vec3(0.30, 0.10, 0.13), thin);
    575:       float up = 0.0;                                           // the upper lip's shadow: the top of the opening is dark
    576:       for (int s = 2; s <= 5; s++) { vec4 a = texture2D(tInfo, uv + vec2(0.0, float(s)) * uSil * px); if (a.a > 0.5 && rid(a) == 1) up = 1.0; }
    577:       if (up > 0.5 && dot(col, vec3(1.0 / 3.0)) < 0.8) col = min(col, vec3(0.36, 0.11, 0.14));   // not the teeth
    578:       // a tall opening (shout, surprise) is dark in its upper part, the tongue only low in it; VRoid's map stretched
    579:       // over a big opening would fill it with tongue. Small openings keep the map's read.
    580:       float deep = 0.5 * smoothstep(14.0 * uSil, 30.0 * uSil, h), t = (hu + 0.5) / h;
    581:       if (deep > 0.0 && dot(col, vec3(1.0 / 3.0)) < 0.8) col = mix(vec3(0.30, 0.10, 0.13), col, smoothstep(deep - 0.08, deep + 0.08, t));
    582:     }
    583:     if (r0 == 8) col = browOverHair(uv, px, col);
    584:     if (!feature) {
    585:       for (int k = 0; k < 4; k++) {
    586:         vec2 d = k == 0 ? vec2(1.0, 0.0) : (k == 1 ? vec2(-1.0, 0.0) : (k == 2 ? vec2(0.0, -1.0) : vec2(0.0, 1.0)));
    587:         vec2 q = uv + d * px * uInner;
    588:         vec4 ni = texture2D(tInfo, q);
    589:         if (z(q) > z0 + uGap && pid(ni) != p0) { col = ink(col); break; }
    590:       }
    591:       float acc = 0.0, rad = uSil * 3.0;
    592:       for (int k = 0; k < 8; k++) {
    593:         float a = float(k) * 0.785398 + 0.39;
    594:         vec2 q = uv + vec2(cos(a), sin(a)) * rad * px;
    595:         vec4 ni = texture2D(tInfo, q);
    596:         if (ni.a > 0.5) acc += clamp((z0 - z(q)) / 0.05, 0.0, 1.0);
    597:       }
    598:       col *= 1.0 - 0.22 * acc / 8.0;
    599:       if (r0 == 1) {                                            // the bangs shade the forehead
    600:         float hair = 0.0;
    601:         for (int s = 1; s <= 4; s++) { vec4 a = texture2D(tInfo, uv + vec2(0.0, float(s)) * uSil * px); int ra = rid(a); if (a.a > 0.5 && (ra == 3 || ra == 11)) hair = 1.0; }
    602:         col = mix(col, col * vec3(0.84, 0.74, 0.76), 0.75 * hair);
    603:       }
    604:     }
    605:     gl_FragColor = vec4(col, 1.0);
    606:   }`;
    607: 
    608: const compCam = new THREE.Camera();
    609: function compPass(fs, uniforms) {
    610:   const mat = new THREE.ShaderMaterial({ uniforms, vertexShader: `void main() { gl_Position = vec4(position.xy, 0.0, 1.0); }`,
    611:                                          fragmentShader: fs, transparent: true });
    612:   const s = new THREE.Scene(); s.add(new THREE.Mesh(new THREE.PlaneGeometry(2, 2), mat));
    613:   return { mat, scene: s };
    614: }
    615: 
    616: // ---- the renderer ---------------------------------------------------------------------------------------------------
    617: const stageEl = $("stage");
    618: const R = new THREE.WebGLRenderer({ antialias: false, alpha: true, preserveDrawingBuffer: !!SHEET });
    619: R.setClearColor(0x000000, 0);
    620: R.outputColorSpace = THREE.LinearSRGBColorSpace;
    621: stageEl.appendChild(R.domElement);
    622: const paint = compPass(PAINT_COMP, { tColor: { value: null }, tInfo: { value: null }, tDepth: { value: null }, uRes: { value: new THREE.Vector2() },
    623:   uNear: { value: 0.1 }, uFar: { value: 40 }, uGap: { value: 0.012 }, uSil: { value: 2 }, uInner: { value: 1 }, uPpm: { value: 1000 } });
    624: const ctl = new OrbitControls(camera, R.domElement);
    625: ctl.enableDamping = true; ctl.zoomToCursor = true; ctl.screenSpacePanning = true;
    626: ctl.minZoom = 0.3; ctl.maxZoom = 12;
    627: let freeLook = false;
    628: ctl.addEventListener("start", () => { freeLook = true; });
    629: R.domElement.addEventListener("dblclick", focusAt);
    630: const targets = { rt: null, rtInfo: null, key: "" };
    631: function targetsFor(w, h) {
    632:   const key = `${w}x${h}`;
    633:   if (targets.key === key) return;
    634:   [targets.rt, targets.rtInfo].forEach(t => t && t.dispose());
    635:   const o = { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter };
    636:   targets.rt = new THREE.WebGLRenderTarget(w, h, o);
    637:   targets.rtInfo = new THREE.WebGLRenderTarget(w, h, { ...o, depthTexture: new THREE.DepthTexture(w, h) });
    638:   targets.key = key;
    639: }
    640: 
    641: // ---- cloth -------------------------------------------------------------------------------------------------------
    642: // The skinned cape/tails stay hidden and provide the pinned row (they follow the animated body);
    643: // a plain display mesh in world space shows the simulated cloth. Colliders ride the bones.
    644: const _v = new THREE.Vector3(), _a = new THREE.Vector3(), _ab = new THREE.Vector3(), _cp = new THREE.Vector3();
    645: const _A = new THREE.Vector3(), _B = new THREE.Vector3();
    646: class Cloth {
    647:   constructor(skinned, U, V, trims, capsules, bone, closed = false, hemOnly = false) {
    648:     this.skinned = skinned; this.U = U; this.V = V; this.trims = trims; this.closed = closed; this.hemOnly = hemOnly;
    649:     this.n = skinned.geometry.attributes.position.count;
    650:     this.caps = capsules.map(c => ({ a: bone(c.bones[0]), b: bone(c.bones[1]), r: c.r + 0.006 })).filter(c => c.a && c.b);
    651:     this.x = new Float32Array(this.n * 3); this.prev = new Float32Array(this.n * 3);
    652:     const idx = (i, j) => i * V + j, links = [], bends = [], edge = new Float32Array(this.n);
    653:     for (let i = 0; i < U; i++) for (let j = 0; j < V; j++) {
    654:       // a closed cloth (a skirt) wraps around: the last column links back to the first
    655:       if (i + 1 < U || closed) links.push([idx(i, j), idx((i + 1) % U, j)]);
    656:       if (j + 1 < V) links.push([idx(i, j), idx(i, j + 1)]);
    657:       if (i + 2 < U || closed) bends.push([idx(i, j), idx((i + 2) % U, j)]);
    658:       if (j + 2 < V) bends.push([idx(i, j), idx(i, j + 2)]);
    659:       edge[idx(i, j)] = ((j === 0 && !hemOnly) || j === V - 1 || (!closed && trims.length > 1 && (i === 0 || i === U - 1))) ? 1 : 0;
    660:     }
    661:     const src = skinned.geometry.attributes.position, r = src.array;
    662:     const len = ([a, b]) => Math.hypot(r[3*a] - r[3*b], r[3*a+1] - r[3*b+1], r[3*a+2] - r[3*b+2]);
    663:     this.links = links.map(l => [l[0], l[1], len(l)]);
    664:     this.bends = bends.map(l => [l[0], l[1], len(l)]);
    665:     this.pinned = new Uint8Array(this.n); for (let i = 0; i < U; i++) this.pinned[idx(i, 0)] = 1;
    666:     const g = new THREE.BufferGeometry();
    667:     g.setAttribute("position", new THREE.BufferAttribute(new Float32Array(this.n * 3), 3).setUsage(THREE.DynamicDrawUsage));
    668:     g.setAttribute("normal", new THREE.BufferAttribute(new Float32Array(this.n * 3), 3));
    669:     g.setAttribute("uv", skinned.geometry.attributes.uv || new THREE.BufferAttribute(new Float32Array(this.n * 2), 2));
    670:     g.setAttribute("edge", new THREE.BufferAttribute(edge, 1));
    671:     g.setIndex(skinned.geometry.index);
    672:     this.display = new THREE.Mesh(g, skinned.material);
    673:     this.display.userData = { ...skinned.userData, cloth: true };
    674:     this.display.frustumCulled = false;
    675:     this.lines = new THREE.LineSegments(new THREE.BufferGeometry(), null);
    676:     this.lines.frustumCulled = false;
    677:   }
    678:   rest(k, out) { this.skinned.getVertexPosition(k, out); return out.applyMatrix4(this.skinned.matrixWorld); }
    679:   reset(lift) {
    680:     for (let k = 0; k < this.n; k++) {
    681:       this.rest(k, _v);
    682:       if (lift && !this.pinned[k]) { const j = k % this.V; _v.y += 0.0035 * j * j; }
    683:       this.x[3*k] = this.prev[3*k] = _v.x; this.x[3*k+1] = this.prev[3*k+1] = _v.y; this.x[3*k+2] = this.prev[3*k+2] = _v.z;
    684:     }
    685:   }
    686:   step(dt, t) {
    687:     const x = this.x, pv = this.prev, pin = this.pinned;
    688:     const g = -9.8 * dt * dt, damp = 0.985;
    689:     const gust = state.wind ? (0.9 + 0.6 * Math.sin(t * 1.7) + 0.3 * Math.sin(t * 4.3)) * dt * dt * 4.0 : 0;
    690:     for (let k = 0; k < this.n; k++) {
    691:       if (pin[k]) continue;
    692:       for (let c = 0; c < 3; c++) {
    693:         const i = 3*k + c, v = (x[i] - pv[i]) * damp;
    694:         pv[i] = x[i]; x[i] += v + (c === 1 ? g : 0) + (c === 0 ? gust : 0) + (c === 2 ? gust * 0.4 : 0);
    695:       }
    696:     }
    697:     const pins = [];
    698:     for (let k = 0; k < this.n; k++) if (pin[k]) pins.push([k, this.rest(k, new THREE.Vector3())]);
    699:     const caps = this.caps.map(c => ({ a: c.a.getWorldPosition(new THREE.Vector3()), b: c.b.getWorldPosition(new THREE.Vector3()), r: c.r }));
    700:     // a closed skirt's axis: the middle of its pinned waist row. Inside a leg collider, its cloth is
    701:     // always pushed away from this axis; pushed to the nearest side, a leg stepping wide drags the
    702:     // fabric in between the legs
    703:     const axis = new THREE.Vector3();
    704:     if (this.closed) { for (const [, w] of pins) axis.add(w); axis.multiplyScalar(1 / Math.max(1, pins.length)); }
    705:     const solve = (L, stiff) => {
    706:       for (const [ia, ib, R] of L) {
    707:         const a = 3 * ia, b = 3 * ib;
    708:         const dx = x[b] - x[a], dy = x[b+1] - x[a+1], dz = x[b+2] - x[a+2];
    709:         const d = Math.hypot(dx, dy, dz) || 1e-9, f = (d - R) / d * stiff;
    710:         const wa = pin[ia] ? 0 : 1, wb = pin[ib] ? 0 : 1, s = wa + wb;
    711:         if (!s) continue;
    712:         const ka = f * wa / s, kb = f * wb / s;
    713:         x[a] += dx * ka; x[a+1] += dy * ka; x[a+2] += dz * ka;
    714:         x[b] -= dx * kb; x[b+1] -= dy * kb; x[b+2] -= dz * kb;
    715:       }
    716:     };
    717:     const touch = this.touch || (this.touch = new Uint8Array(this.n)); touch.fill(0);
    718:     for (let it = 0; it < 10; it++) {
    719:       for (const [k, w] of pins) { x[3*k] = w.x; x[3*k+1] = w.y; x[3*k+2] = w.z; }
    720:       solve(this.links, 1.0);
    721:       solve(this.bends, 0.2);
    722:       for (let k = 0; k < this.n; k++) {
    723:         if (pin[k]) continue;
    724:         _v.set(x[3*k], x[3*k+1], x[3*k+2]);
    725:         let jumped = false;
    726:         for (const c of caps) {
    727:           _ab.subVectors(c.b, c.a);
    728:           const tt = THREE.MathUtils.clamp(_a.subVectors(_v, c.a).dot(_ab) / _ab.lengthSq(), 0, 1);
    729:           _cp.copy(c.a).addScaledVector(_ab, tt);
    730:           const d = _v.distanceTo(_cp);
    731:           if (d < c.r && d > 1e-6) {
    732:             if (this.closed) {
    733:               _A.set(_v.x - axis.x, 0, _v.z - axis.z);                     // outward, horizontally
    734:               _B.set(_v.x - _cp.x, 0, _v.z - _cp.z);
    735:               if (_A.lengthSq() > 1e-8 && _B.dot(_A) < 0) {                 // the nearest way out is inward: go round
    736:                 _A.normalize(); _v.set(_cp.x + _A.x * c.r, _v.y, _cp.z + _A.z * c.r); jumped = true; continue;
    737:               }
    738:             }
    739:             _v.sub(_cp).multiplyScalar(c.r / d).add(_cp);
    740:           }
    741:         }
    742:         // inelastic contact: most of a push-out doesn't turn into velocity, so a limb moving into the
    743:         // cloth carries it instead of flinging it
    744:         const px = _v.x - x[3*k], py = _v.y - x[3*k+1], pz = _v.z - x[3*k+2];
    745:         if (jumped) { pv[3*k] = _v.x; pv[3*k+1] = _v.y; pv[3*k+2] = _v.z; }      // a jump round a leg carries no speed
    746:         else if (px * px + py * py + pz * pz > 1e-12) {
    747:           pv[3*k] += px * 0.8; pv[3*k+1] += py * 0.8; pv[3*k+2] += pz * 0.8;
    748:           touch[k] = 1;
    749:         }
    750:         x[3*k] = _v.x; x[3*k+1] = _v.y; x[3*k+2] = _v.z;
    751:       }
    752:     }
    753:     // friction, once per step: cloth touching a limb keeps 40% of its speed, so a stepping knee
    754:     // carries a skirt forward instead of the fabric sliding off round the leg
    755:     for (let k = 0; k < this.n; k++) if (touch[k]) for (let c = 0; c < 3; c++) { const i = 3*k + c; pv[i] = x[i] - (x[i] - pv[i]) * 0.4; }
    756:   }
    757:   write() {
    758:     const p = this.display.geometry.attributes.position;
    759:     p.array.set(this.x); p.needsUpdate = true;
    760:     this.display.geometry.computeVertexNormals(); this.display.geometry.computeBoundingSphere();
    761:     const pts = [], idx = (i, j) => i * this.V + j, X = this.x;
    762:     const seg = (a, b) => pts.push(X[3*a], X[3*a+1], X[3*a+2], X[3*b], X[3*b+1], X[3*b+2]);
    763:     const U = this.U, last = this.closed ? U : U - 1;
    764:     for (let i = 0; i < last; i++) { if (!this.hemOnly) seg(idx(i, 0), idx((i + 1) % U, 0)); seg(idx(i, this.V - 1), idx((i + 1) % U, this.V - 1)); }
    765:     if (this.trims.length > 1 && !this.closed) for (let j = 0; j + 1 < this.V; j++) { seg(idx(0, j), idx(0, j + 1)); seg(idx(this.U - 1, j), idx(this.U - 1, j + 1)); }
    766:     const g = this.lines.geometry;
    767:     g.setAttribute("position", new THREE.Float32BufferAttribute(pts, 3)); g.computeBoundingSphere();
    768:   }
    769: }
    770: 
    771: // A long skirt is posed, not simulated: each column hangs from its waist point at its rest shape;
    772: // anything that would sit inside a leg is pushed straight out along its column, and everything below
    773: // a contact hangs from it, so the skirt tents over a stepping knee. Column lengths are kept (the hem
    774: // rises as it tents) and the pushes are smoothed round the skirt and over time. A free cloth tube
    775: // against moving leg colliders slid off the knee, jumped round the legs or drifted, whatever the
    776: // friction; this can't end up between the legs.
    777: class SkirtRig extends Cloth {
    778:   constructor(skinned, U, V, trims, capsules, bone, hemOnly) {
    779:     super(skinned, U, V, trims, capsules, bone, true, hemOnly);
    780:     const src = skinned.geometry.attributes.position, idx = (i, j) => i * V + j;
    781:     const P = k => new THREE.Vector3(src.getX(k), src.getY(k), src.getZ(k));
    782:     const C = new THREE.Vector3(); for (let i = 0; i < U; i++) C.add(P(idx(i, 0))); C.multiplyScalar(1 / U);
    783:     this.r0 = new Float32Array(U * V); this.h0 = new Float32Array(U * V); this.t0 = new Float32Array(U * V);
    784:     this.push = new Float32Array(U * V);
    785:     for (let i = 0; i < U; i++) {
    786:       const W = P(idx(i, 0)), d = new THREE.Vector3(W.x - C.x, 0, W.z - C.z).normalize(), t = new THREE.Vector3(-d.z, 0, d.x);
    787:       for (let j = 0; j < V; j++) {
    788:         const o = P(idx(i, j)).sub(W);
    789:         this.r0[idx(i, j)] = o.dot(d); this.h0[idx(i, j)] = o.y; this.t0[idx(i, j)] = o.dot(t);
    790:       }
    791:     }
    792:   }
    793:   pose(dt, instant) {
    794:     const U = this.U, V = this.V, X = this.x, idx = (i, j) => i * V + j;
    795:     const W = [], C = new THREE.Vector3();
    796:     for (let i = 0; i < U; i++) { W.push(this.rest(idx(i, 0), new THREE.Vector3())); C.add(W[i]); }
    797:     C.multiplyScalar(1 / U);
    798:     const caps = this.caps.map(c => ({ a: c.a.getWorldPosition(new THREE.Vector3()), b: c.b.getWorldPosition(new THREE.Vector3()), r: c.r + 0.012 }));
    799:     const target = new Float32Array(U * V), p = new THREE.Vector3(), cp = new THREE.Vector3(), ab = new THREE.Vector3(), ap = new THREE.Vector3();

    1 more lines not shown.