Skip to content

armgit / kiln

Public Imported from GitHub
0

Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.

7.1 KB
Raw Blame
// Authored deterministic geometry, metre units, Y up, origin at ground.
function randomSeed(seed) { let x=seed; return function() { x=(1664525*x+1013904223)>>>0; return x/4294967296; }; }
function bag() { return {positions:[],indices:[]}; }
function vertex(b,p) { const i=b.positions.length/3; b.positions.push(...p); return i; }
function tri(b,a,c,d) { b.indices.push(a,c,d); }
function vec(p) { return new THREE.Vector3(...p); }
function tube(b,path,radii,sides=8,twist=0,groundBase=false) {
  const rings=[];
  for(let k=0;k<path.length;k++) {
    let t=vec(path[Math.min(k+1,path.length-1)]).sub(vec(path[Math.max(0,k-1)])).normalize();
    if(k===0 && groundBase) t=new THREE.Vector3(0,1,0);
    const u=new THREE.Vector3(1,0,0); if(Math.abs(t.x)>.85) u.set(0,0,1);
    u.addScaledVector(t,-u.dot(t)).normalize(); const v=t.clone().cross(u).normalize();
    const ring=[];
    for(let j=0;j<sides;j++) {
      const a=j*2*Math.PI/sides+twist*k/(path.length-1);
      const r=radii[k]*(1+.10*Math.sin(j*3.7+k*.8));
      ring.push(vertex(b,vec(path[k]).addScaledVector(u,Math.cos(a)*r).addScaledVector(v,Math.sin(a)*r).toArray()));
    }
    rings.push(ring);
  }
  for(let k=0;k<rings.length-1;k++) for(let j=0;j<sides;j++) {
    const a=rings[k][j], c=rings[k][(j+1)%sides], d=rings[k+1][j], e=rings[k+1][(j+1)%sides];
    tri(b,a,c,d); tri(b,c,e,d);
  }
  const base=vertex(b,path[0]), top=vertex(b,path[path.length-1]);
  for(let j=0;j<sides;j++) { tri(b,base,rings[0][(j+1)%sides],rings[0][j]); tri(b,top,rings[rings.length-1][j],rings[rings.length-1][(j+1)%sides]); }
}
// Keep argument sampling order to preserve the exact deterministic v1 wood geometry.
function retainDetailSample() {}
function tuft(b,center,size,phase) {
  // Retained crown mass: rounded rings with shallow integrated foliage lobes.
  const first=b.positions.length,rings=[],ys=[-.78,-.28,.28,.78],widths=[.49,.94,.96,.53],count=10;
  const tiltedVertex=(p)=>{
    const x=p[0]-center[0],y=p[1]-center[1],z=p[2]-center[2],rx=.38*Math.sin(phase*1.3),rz=.32*Math.cos(phase*.9);
    const yy=y*Math.cos(rx)-z*Math.sin(rx),zz=y*Math.sin(rx)+z*Math.cos(rx);
    return vertex(b,[center[0]+x*Math.cos(rz)-yy*Math.sin(rz),center[1]+x*Math.sin(rz)+yy*Math.cos(rz),center[2]+zz]);
  };
  for(let k=0;k<ys.length;k++) {
    const ring=[];
    for(let j=0;j<count;j++) {
      const a=j*2*Math.PI/count+phase,wave=1+.055*Math.sin(3*a+phase*.7)+.035*Math.sin(2*a-phase+k*.35);
      ring.push(tiltedVertex([center[0]+size[0]*widths[k]*wave*Math.cos(a)+size[0]*.086*k,center[1]+size[1]*(ys[k]+.05*Math.sin(3*a+phase)),center[2]+size[2]*widths[k]*wave*Math.sin(a)]));
    } rings.push(ring);
  }
  for(let k=0;k<rings.length-1;k++)for(let j=0;j<count;j++){const a=rings[k][j],c=rings[k][(j+1)%count],d=rings[k+1][j],e=rings[k+1][(j+1)%count];tri(b,a,d,c);tri(b,c,d,e);}
  const bottom=tiltedVertex([center[0]-.1*size[0],center[1]-size[1],center[2]]),top=tiltedVertex([center[0]+.23*size[0],center[1]+size[1],center[2]+.1*size[2]]);
  for(let j=0;j<count;j++){tri(b,bottom,rings[0][j],rings[0][(j+1)%count]);tri(b,top,rings[3][(j+1)%count],rings[3][j]);}
  if(!b.clumps)b.clumps=[];b.clumps.push({center:center.slice(),first,last:b.positions.length});
}
function fitCrownEnvelope(bags, targets) {
  // Adjust rounded outer profiles while keeping every clump center fixed.
  const clumps=bags.flatMap(b=>(b.clumps||[]).map(c=>({b,...c})));
  for(const [axis,sign,target] of targets) {
    const edge=c=>{let m=-Infinity;for(let i=c.first+axis;i<c.last;i+=3)m=Math.max(m,sign*c.b.positions[i]);return m;};
    const stretch=(c,t)=>{const center=c.center[axis],e=edge(c),factor=(t-sign*center)/(e-sign*center);
      for(let i=c.first+axis;i<c.last;i+=3){const d=sign*(c.b.positions[i]-center);if(d>0)c.b.positions[i]=center+sign*d*factor;}
    };
    for(const c of clumps)if(edge(c)>target)stretch(c,target);
    let outer=clumps[0];for(const c of clumps)if(edge(c)>edge(outer))outer=c;
    if(edge(outer)<target)stretch(outer,target);
  }
}
function finish(root,wood,foliage,colors) {
  const bark=gameMaterial(colors[0],{roughness:.96,flatShading:false}); bark.name='Bark';
  createPart('ConnectedWood',creaseNormals(meshGeo(wood),{angle:48}),bark,{parent:root});
  foliage.forEach((b,i)=>{ if(!b.indices.length)return; const m=gameMaterial(colors[i+1],{roughness:.88,flatShading:false}); m.name='Foliage_'+i; const g=creaseNormals(meshGeo(b),{angle:42}); createPart('Foliage_'+i,g,m,{parent:root}); });
  root.userData={units:'metres',groundDatum:0,seeded:true,purpose:'Reusable courtyard vegetation',historicalStatus:'Art direction; not a historical reconstruction'};
  return root;
}

const meta={name:'Mature courtyard olive'};
function build() {
  const root=createRoot('MatureOlive'), wood=bag(), foliage=[bag(),bag(),bag()], rng=randomSeed(1807);
  const trunk=[[0,0,0],[.01,.35,0],[-.13,.92,.05],[.10,1.45,-.07],[.07,1.97,.02],[-.15,2.42,.04],[.0,2.86,.12]];
  tube(wood,trunk,[.36,.31,.28,.27,.235,.19,.020],10,.53,true);
  for(let j=0;j<5;j++) { const a=j*1.256+.17; tube(wood,[[0,.23,0],[.40*Math.cos(a),.115,.40*Math.sin(a)],[.67*Math.cos(a),.035,.67*Math.sin(a)]],[.20,.10,.03],7,.25); }
  // Forks begin at exact retained trunk stations. Shoots begin on exact bough stations.
  const tips=[];
  for(let i=0;i<6;i++) {
    const a=i*2.399+.3, start=trunk[i%3+3], reach=1.55+.52*rng(), top=3.65+.75*rng();
    const p1=[start[0]+.50*Math.cos(a),start[1]+.46,start[2]+.50*Math.sin(a)];
    const p2=[Math.cos(a)*reach*.78,top-.40,Math.sin(a)*reach*.78];
    const p3=[Math.cos(a)*reach,top,Math.sin(a)*reach];
    tube(wood,[start,p1,p2,p3],[.16-i*.008,.11,.056,.018],6,.3);
    for(let j=0;j<3;j++) {
      const from=j===0?p1:p2, aa=a+(j-1)*.91+.20;
      const reach2=j===2?reach*.40:reach+(j===0?.06:.43);
      const end=[Math.cos(aa)*reach2,p3[1]+(j===0?-.85:j===1?.30:.48)+.13*rng(),Math.sin(aa)*reach2];
      const mid=vec(from).lerp(vec(end),.63).toArray(); mid[1]+=.14;
      tube(wood,[from,mid,end],[j===0?.06:.045,.025,.006],5,.17);
      tips.push({mid,end,a:aa});
    }
  }
  // Attached sprays with paired lanceolate leaves distributed across crown levels.
  tips.forEach((tip,i)=>{
    for(let k=0;k<2;k++) {
      const aa=tip.a+(k===0?-.6:.6), from=k===0?tip.mid:tip.end;
      const end=vec(from).add(new THREE.Vector3(Math.cos(aa)*.52,.18+.18*rng(),Math.sin(aa)*.52)).toArray();
      tube(wood,[from,end],[.012,.003],4,0);
      tuft(foliage[(i+k)%3],vec(from).lerp(vec(end),.38).toArray(),[.43+.09*Math.sin(i),.29,.36+.07*Math.cos(i)],i*.73+k*1.4);
      for(let j=0;j<3;j++) for(let side=-1;side<=1;side+=2) {
        const t=.16+j*.34, base=vec(from).lerp(vec(end),t).toArray(), angle=aa+side*(.75+.18*rng());
        retainDetailSample(foliage[(i+j+k)%3],base,[Math.cos(angle),-.15+.95*rng(),Math.sin(angle)],.37+.14*rng(),.135+.05*rng(),false,side*(.1+.55*rng()));
      }
    }
  });
  fitCrownEnvelope(foliage,[[0,-1,3.0874783992767334],[0,1,3.1430509090423584],[1,1,5.345671227637758],[2,-1,2.6326301097869873],[2,1,3.117572546005249]]);
  const heightScale=0.94; // Bake scale into geometry; reusable root remains identity.
  [wood,...foliage].forEach(b=>{for(let i=1;i<b.positions.length;i+=3)b.positions[i]*=heightScale;});
  return finish(root,wood,foliage,[0x6b5943,0x73816a,0x87947c,0x9aa38b]);
}