three-live 1.3.2 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -1
- package/dist/index.d.ts +190 -0
- package/dist/three-author-engine.js +171 -163
- package/dist/three-author.cjs +145 -163
- package/dist/three-author.d.ts +190 -0
- package/dist/three-author.js +145 -163
- package/dist/three-convert.cjs +142 -160
- package/dist/three-convert.d.ts +190 -0
- package/dist/three-convert.js +142 -160
- package/dist/three-live-ai.cjs +41 -0
- package/dist/three-live-ai.d.ts +759 -0
- package/dist/three-live-ai.js +41 -0
- package/dist/three-live-ai.min.js +41 -0
- package/dist/three-live-animation.cjs +6 -6
- package/dist/three-live-animation.d.ts +190 -0
- package/dist/three-live-animation.js +5 -5
- package/dist/three-live-animation.min.js +5 -5
- package/dist/three-live-cad.cjs +2 -2
- package/dist/three-live-cad.d.ts +190 -0
- package/dist/three-live-cad.js +2 -2
- package/dist/three-live-cad.min.js +2 -2
- package/dist/three-live-element.cjs +50 -85
- package/dist/three-live-element.d.ts +190 -0
- package/dist/three-live-element.js +50 -85
- package/dist/three-live-full.cjs +907 -189
- package/dist/three-live-full.js +907 -189
- package/dist/three-live-full.min.js +907 -189
- package/dist/three-live-procedural.cjs +16 -0
- package/dist/three-live-procedural.d.ts +759 -0
- package/dist/three-live-procedural.js +16 -0
- package/dist/three-live-procedural.min.js +16 -0
- package/dist/three-live-sandbox.html +11 -1
- package/dist/three-live-studio.cjs +72 -17
- package/dist/three-live-studio.d.ts +190 -0
- package/dist/three-live-studio.js +72 -17
- package/dist/three-live-studio.min.js +73 -18
- package/dist/three-live-tsl.cjs +103 -0
- package/dist/three-live-tsl.d.ts +759 -0
- package/dist/three-live-tsl.js +103 -0
- package/dist/three-live-tsl.min.js +103 -0
- package/dist/three-live-viewer.cjs +50 -85
- package/dist/three-live-viewer.d.ts +190 -0
- package/dist/three-live-viewer.js +50 -85
- package/dist/three-live-viewer.min.js +50 -85
- package/dist/three-live-webgpu.cjs +585 -0
- package/dist/three-live-webgpu.d.ts +759 -0
- package/dist/three-live-webgpu.js +585 -0
- package/dist/three-live-webgpu.min.js +585 -0
- package/dist/three-live.cjs +147 -165
- package/dist/three-live.js +147 -165
- package/dist/three-live.min.js +147 -165
- package/dist/three-view.cjs +49 -84
- package/dist/three-view.d.ts +190 -0
- package/dist/three-view.js +50 -85
- package/dist/three-xr.cjs +141 -159
- package/dist/three-xr.d.ts +190 -0
- package/dist/three-xr.js +140 -158
- package/package.json +21 -1
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/var oe="__THREE_LIVE_REGISTRY__";function le(){let e=typeof globalThis<"u"?globalThis:{};if(!e[oe]){let n={plugins:[],decoders:new Map};try{Object.defineProperty(e,oe,{value:n,writable:!1,configurable:!1,enumerable:!1})}catch{e[oe]=n}}return e[oe]}var et=le().plugins;function ze(e){let n=le();n.workerExtensionInstallers||(n.workerExtensionInstallers=[]),n.workerExtensionInstallers.includes(e)||n.workerExtensionInstallers.push(e)}function Me(e){let n=le().plugins;n.some(c=>c.name===e.name)||n.push(e),e.onRegister?.()}function ne(e,n){if(!e||!e.MeshPhysicalMaterial)return null;if(n?.authorNamespace?.TSL&&n?.authorNamespace?.MeshStandardNodeMaterial)return{TSL:n.authorNamespace.TSL,MeshStandardNodeMaterial:n.authorNamespace.MeshStandardNodeMaterial,MeshPhysicalNodeMaterial:n.authorNamespace.MeshPhysicalNodeMaterial};let c=128,i=16,m=0,d=(l,a,o)=>{typeof e.warnUnsupported=="function"&&e.warnUnsupported(`${a} [${l}]`,o)},_=l=>l==="vec4"?4:l==="vec3"?3:l==="vec2"?2:1,s=l=>l===4?"vec4":l===3?"vec3":l===2?"vec2":"float",t=(l,a=0)=>{let o=Number(l);return Number.isFinite(o)?Math.fround(o):a},x=l=>!!(l&&l.isTSLNode===!0),u=(l,a=1)=>{if(x(l))return l;if(typeof l=="number"||typeof l=="boolean"){let o=t(Number(l),0);return a>1?new g("const",s(a),[],new Array(a).fill(o)):new g("const","float",[],[o])}if(typeof l=="string"){let o=new e.Color(l);return new g("const","vec3",[],[t(o.r,1),t(o.g,1),t(o.b,1)])}if(Array.isArray(l)||l instanceof Float32Array){let o=Math.max(1,Math.min(4,l.length)),r=[];for(let $=0;$<o;$++)r.push(t(l[$],0));return new g("const",s(o),[],r)}if(l&&typeof l=="object"){let o=l;if("isColor"in o||"r"in o&&"g"in o&&"b"in o)return new g("const","vec3",[],[t(o.r,1),t(o.g,1),t(o.b,1)]);if("x"in o&&"y"in o)return"z"in o&&"w"in o?new g("const","vec4",[],[t(o.x),t(o.y),t(o.z),t(o.w,1)]):"z"in o?new g("const","vec3",[],[t(o.x),t(o.y),t(o.z)]):new g("const","vec2",[],[t(o.x),t(o.y)])}return new g("const","float",[],[0])},N=(l,a)=>{let o=u(l),r=u(a);if(o._type===r._type)return[o,r,o._type];if(o._type==="float")return[o,r,r._type];if(r._type==="float")return[o,r,o._type];let $=_(o._type),P=_(r._type);if(P>$)r=r.swizzle("xyzw".slice(0,$));else{let T=[r];for(let V=P;V<$;V++)T.push(new g("const","float",[],[0]));r=new g("construct",o._type,T)}return[o,r,o._type]},w=(l,a)=>{let o=u(l);if(o._type===a)return o;let r=_(a),$=_(o._type);if(r===1)return o.swizzle("x");if($===1)return o._op==="const"&&o._val?new g("const",a,[],new Array(r).fill(o._val[0])):new g("construct",a,[o]);if($>r)return o.swizzle("xyzw".slice(0,r));let P=[o];for(let T=$;T<r;T++)P.push(new g("const","float",[],[T===3?1:0]));return new g("construct",a,P)};class g{constructor(a,o,r,$,P,T){this.isTSLNode=!0,this._op=a,this._type=o,this._args=r,$!==void 0&&(this._val=$),P!==void 0&&(this._name=P),T!==void 0&&(this._mask=T)}get nodeType(){return this._type}get value(){return this._uVal}set value(a){this._op==="uniform"&&(this._uVal=a)}swizzle(a){let o=String(a||"x").trim().replace(/[rgba]/g,$=>"xyzw"["rgba".indexOf($)]);if(!/^[xyzw]{1,4}$/.test(o))return d("TL501",`Invalid TSL swizzle '${a}'`,".x"),new g("const","float",[],[0]);let r=_(this._type);if(r===1&&o==="x")return this;for(let $=0;$<o.length;$++)if("xyzw".indexOf(o[$])>=r)return d("TL501",`TSL swizzle '.${a}' out of range`,"0"),new g("const",s(o.length),[],new Array(o.length).fill(0));return new g("swizzle",s(o.length),[this],void 0,void 0,o)}clamp(a=0,o=1){let r=u(a),$=u(o);return(r._type!=="float"||$._type!=="float")&&(r=w(r,this._type),$=w($,this._type)),new g("clamp",this._type,[this,r,$])}saturate(){return this.clamp(0,1)}oneMinus(){return u(1,_(this._type)).sub(this)}mix(a,o){let[r,$,P]=N(this,a),T=u(o);return T._type!=="float"&&T._type!==P&&(T=w(T,P)),new g("mix",P,[r,$,T])}step(a){let o=u(a);return this._type==="float"?new g("step",o._type,[this,o]):new g("step",this._type,[this,w(o,this._type)])}smoothstep(a,o){let r=u(o),$=this,P=u(a);return($._type!=="float"||P._type!=="float")&&($=w($,r._type),P=w(P,r._type)),new g("smoothstep",r._type,[$,P,r])}dot(a){let[o,r]=N(this,a);return new g("dot","float",[o,r])}cross(a){return new g("cross","vec3",[w(this,"vec3"),w(a,"vec3")])}length(){return new g("length","float",[this])}distance(a){let[o,r]=N(this,a);return new g("distance","float",[o,r])}reflect(a){let o=this._type==="float"?w(this,"vec3"):this;return new g("reflect",o._type,[o,w(a,o._type)])}remap(a,o,r,$){let P=this.sub(a).div(u(o).sub(a)).clamp(0,1);return u(r).mix($,P)}}let k=g.prototype;for(let[l,a]of[["x","x"],["y","y"],["z","z"],["w","w"],["r","x"],["g","y"],["b","z"],["a","w"],["xy","xy"],["xz","xz"],["yz","yz"],["xyz","xyz"],["rgb","xyz"],["xyzw","xyzw"],["rgba","xyzw"]])Object.defineProperty(k,l,{get(){return this.swizzle(a)}});let y=["add","sub","mul","div","mod","pow","atan2","min","max"];for(let l of y)k[l]=function(a){let[o,r,$]=N(this,a);return new g(l,$,[o,r])};let L=["neg","abs","sign","floor","ceil","fract","sqrt","exp","log","sin","cos","normalize"];for(let l of L)k[l]=function(){return new g(l,this._type,[this])};let h=(l,a)=>{let o=new g("builtin",a,[],void 0,l),r=()=>o;return Object.setPrototypeOf(r,k),Object.defineProperty(r,"length",{value:k.length,configurable:!0}),Object.assign(r,o),r},S=(l,a)=>{let o=_(l);if(a.length===0)return new g("const",l,[],new Array(o).fill(0));if(a.length===1)return w(a[0],l);let r=a.map(A=>u(A)),$=!0,P=[],T=0;for(let A of r)if(T+=_(A._type),A._op==="const"&&A._val)for(let M of A._val)P.push(M);else $=!1;if($&&P.length>=o)return new g("const",l,[],P.slice(0,o));if(T===o)return new g("construct",l,r);let V=[];for(let A of r){let M=_(A._type);for(let z=0;z<M&&V.length<o;z++)V.push(M===1?A:A.swizzle("xyzw"[z]))}for(;V.length<o;)V.push(new g("const","float",[],[V.length===3?1:0]));return new g("construct",l,V)},D=h("positionWorld","vec3"),f={float:(l=0)=>w(l,"float"),vec2:(...l)=>S("vec2",l),vec3:(...l)=>S("vec3",l),vec4:(...l)=>S("vec4",l),color:(l=16777215,a,o)=>{if(typeof l=="number"&&typeof a=="number"&&typeof o=="number"){let $=new e.Color(l,a,o);return new g("const","vec3",[],[t($.r,1),t($.g,1),t($.b,1)])}if(x(l))return w(l,"vec3");let r=new e.Color(l);return new g("const","vec3",[],[t(r.r,1),t(r.g,1),t(r.b,1)])},uniform:(l=0,a)=>{let o=u(l),r=typeof a=="string"&&a.trim().length>0?a.trim().replace(/[^A-Za-z0-9_]/g,"_").slice(0,63):`uParam_${m++}`;/^[0-9]/.test(r)&&(r="_"+r.slice(0,62)),(r==="__proto__"||r==="constructor"||r==="prototype")&&(r=`uParam_${m++}`);let $=new g("uniform",o._type,[],void 0,r);return $._uVal=l,$},positionLocal:h("positionLocal","vec3"),positionWorld:D,normalLocal:h("normalLocal","vec3"),normalWorld:h("normalWorld","vec3"),viewDirection:h("viewDirection","vec3"),uv:h("uv","vec2"),vertexColor:h("vertexColor","vec4"),instanceIndex:h("instanceIndex","float"),time:h("time","float"),clamp:(l,a=0,o=1)=>u(l).clamp(a,o),saturate:l=>u(l).saturate(),oneMinus:l=>u(l).oneMinus(),mix:(l,a,o)=>u(l).mix(a,o),step:(l,a)=>u(l).step(a),smoothstep:(l,a,o)=>u(l).smoothstep(a,o),dot:(l,a)=>u(l).dot(a),cross:(l,a)=>u(l).cross(a),length:l=>u(l).length(),distance:(l,a)=>u(l).distance(a),reflect:(l,a)=>u(l).reflect(a),remap:(l,a,o,r,$)=>u(l).remap(a,o,r,$),fresnel:(l=2)=>new g("fresnel","float",[w(l,"float")]),simplexNoise2D:l=>new g("simplexNoise2D","float",[w(l,"vec2")]),simplexNoise3D:l=>new g("simplexNoise3D","float",[w(l,"vec3")]),voronoi2D:(l,a=1)=>new g("voronoi2D","float",[w(l,"vec2"),w(a,"float")]),checker:l=>new g("checker","float",[w(l,"vec2")]),triplanar:(l=1,a=4)=>{let o=u(l),r=o._type==="vec3"?o:D.mul(w(o,"float"));return new g("triplanar","float",[r,w(a,"float")])},hueShift:(l,a)=>new g("hueShift","vec3",[w(l,"vec3"),w(a,"float")]),bumpFromHeight:(l,a=1)=>new g("bumpFromHeight","vec3",[w(l,"float"),w(a,"float")]),Loop:()=>(d("TL504","THREE.TSL.Loop","loop-free TSL constant"),new g("const","float",[],[0])),While:()=>(d("TL504","THREE.TSL.While","loop-free TSL constant"),new g("const","float",[],[0]))};for(let l of y)f[l]=(a,o)=>u(a)[l](o);for(let l of L)f[l]=a=>u(a)[l]();class v extends e.MeshPhysicalMaterial{get opacityNode(){return this._opacityNode}set opacityNode(a){this._opacityNode=a,a!=null&&!(Number(this.alphaTest)>0)&&(this.transparent=!0)}constructor(a={}){super(a),this._opacityNode=null,this.isMeshStandardNodeMaterial=!0,this.colorNode=a.colorNode??null,this.opacityNode=a.opacityNode??null,this.emissiveNode=a.emissiveNode??null,this.roughnessNode=a.roughnessNode??null,this.metalnessNode=a.metalnessNode??null,this.normalScaleNode=a.normalScaleNode??null,this.bumpNode=a.bumpNode??null,this.positionNode=a.positionNode??null}_serializeTslGraph(){let a=[["color",this.colorNode,"vec3"],["opacity",this.opacityNode,"float"],["emissive",this.emissiveNode,"vec3"],["roughness",this.roughnessNode,"float"],["metalness",this.metalnessNode,"float"],["normalScale",this.normalScaleNode,"float"],["bump",this.bumpNode,"vec3"],["position",this.positionNode,"vec3"]],o=[];for(let[b,j,F]of a)j!=null&&o.push([b,w(j,F)]);if(o.length===0)return null;let r=[],$={},P=new Map,T=new Set,V="",A="",M=(b,j)=>{if(V)return 0;if(!b||!b.isTSLNode)return V="TL501",A="Invalid non-TSL node in material socket",0;if(T.has(b))return V="TL501",A="Cyclic TSL node reference",0;if(j&&(b._op==="builtin"&&b._name==="viewDirection"||b._op==="fresnel"||b._op==="bumpFromHeight"))return V="TL503",A=`Fragment-only TSL op '${b._name||b._op}' used in positionNode`,0;T.add(b);let F=[];for(let q of b._args)if(F.push(M(q,j)),V)return T.delete(b),0;if(T.delete(b),b._op==="uniform"){let q=b._name||"uParam";if(q in $){if($[q].type!==b._type)return V="TL501",A=`Conflicting types for TSL uniform '${q}' (${$[q].type} vs ${b._type})`,0}else{if(Object.keys($).length>=i)return V="TL502",A=`TSL material exceeds MAX_TSL_UNIFORMS (${i})`,0;let pe=w(b._uVal??0,b._type)._val??[0];$[q]={type:b._type,value:b._type==="float"?pe[0]:pe}}}let O=`${b._op}|${b._type}|${F.join(",")}|${b._val?b._val.join(","):""}|${b._name??""}|${b._mask??""}`,ue=P.get(O);if(ue!==void 0)return ue;if(r.length>=c)return V="TL502",A=`TSL material exceeds MAX_TSL_NODES (${c})`,0;let fe=r.length,K={op:b._op,type:b._type};return F.length>0&&(K.args=F),b._val&&(K.value=[...b._val]),b._name&&(K.name=b._name),b._mask&&(K.mask=b._mask),r.push(K),P.set(O,fe),fe},z={};for(let[b,j]of o)if(z[b]=M(j,b==="position"),V)return d(V,A,"base MeshStandardMaterial factors"),null;let U={version:1,nodes:r,outputs:z};return Object.keys($).length>0&&(U.uniforms=$),U}clone(){let a=new v;for(let o of["name","_em","roughness","metalness","emissiveIntensity","opacity","transparent","wireframe","side","doubleSided","map","normalMap","roughnessMap","metalnessMap","emissiveMap","aoMap","aoMapIntensity","bumpMap","bumpScale","clearcoat","clearcoatRoughness","sheen","sheenRoughness","ior","transmission","thickness","attenuationDistance","dispersion","iridescence","iridescenceIOR","colorNode","opacityNode","emissiveNode","roughnessNode","metalnessNode","normalScaleNode","bumpNode","positionNode"])a[o]=this[o];return a.color.copy(this.color),a.emissive.copy(this.emissive),a.normalScale.copy(this.normalScale),a.sheenColor.copy(this.sheenColor),a.attenuationColor.copy(this.attenuationColor),a}}let p={TSL:f,TSLNode:g,MeshStandardNodeMaterial:v,MeshPhysicalNodeMaterial:v};return n&&typeof n=="object"&&(n.authorNamespace||(n.authorNamespace={}),Object.assign(n.authorNamespace,p)),p}var Z="GOOGLE_material_tsl",Ie=128,Se=16,De=3600,Ve={positionLocal:"vec3",positionWorld:"vec3",normalLocal:"vec3",normalWorld:"vec3",viewDirection:"vec3",uv:"vec2",vertexColor:"vec4",instanceIndex:"float",time:"float"},Ue={color:"vec3",opacity:"float",emissive:"vec3",roughness:"float",metalness:"float",normalScale:"float",bump:"vec3",position:"vec3"},Ae=new Set(["neg","abs","sign","floor","ceil","fract","sqrt","exp","log","sin","cos","normalize"]),je=new Set(["add","sub","mul","div","mod","pow","atan2","min","max"]),me={cross:[["vec3","vec3"],"vec3"],fresnel:[["float"],"float"],simplexNoise2D:[["vec2"],"float"],checker:[["vec2"],"float"],simplexNoise3D:[["vec3"],"float"],voronoi2D:[["vec2","float"],"float"],triplanar:[["vec3","float"],"float"],hueShift:[["vec3","float"],"vec3"],bumpFromHeight:[["float","float"],"vec3"]},Fe=/^[A-Za-z_][A-Za-z0-9_]{0,63}$/,Ce=new Set(["__proto__","constructor","prototype"]);function _e(e,n){let c=Math.abs(n)<1e-6?n<0?-1e-6:1e-6:n;return e-c*Math.floor(e/c)}function C(e){return e==="vec4"?4:e==="vec3"?3:e==="vec2"?2:1}function ve(e){return e===1?"float":e===2?"vec2":e===3?"vec3":e===4?"vec4":null}function re(e){return e==="float"||e==="vec2"||e==="vec3"||e==="vec4"}function I(e,n,c="Use loop-free THREE.TSL expressions on MeshStandardNodeMaterial (<= 128 nodes, <= 16 uniforms)."){return{ok:!1,issue:{code:e,message:n,hint:c}}}function B(e,n){for(let c=0;c<n.length;c++)e=Math.imul((e^n.charCodeAt(c))>>>0,16777619)>>>0;return e}var de=new Float32Array(1),Re=new Uint32Array(de.buffer);function We(e,n){return de[0]=n,Math.imul((e^Re[0])>>>0,16777619)>>>0}function ie(e,n){let c=C(e);if(c===1){let m=typeof n=="number"?n:Array.isArray(n)&&n.length===1?Number(n[0]):NaN;return Number.isFinite(m)?[Math.fround(m),0,0,0]:null}if(!Array.isArray(n)&&!(n instanceof Float32Array)||n.length<c)return null;let i=[0,0,0,0];for(let m=0;m<c;m++){let d=Number(n[m]);if(!Number.isFinite(d))return null;i[m]=Math.fround(d)}return i}function Y(e,n="Material"){if(!e||typeof e!="object"||Array.isArray(e))return I("TL501",`Invalid GOOGLE_material_tsl on '${n}'.`);let c=e;if(c.version!==1)return I("TL501",`Unsupported TSL version on '${n}'.`);let i=[],m=new Map,d=c.uniforms;if(d!=null){if(typeof d!="object"||Array.isArray(d))return I("TL501",`Invalid TSL uniforms on '${n}'.`);let h=d,S=Object.keys(h).sort();if(S.length>16)return I("TL502",`TSL '${n}' exceeds MAX_TSL_UNIFORMS (16).`);for(let D=0;D<S.length;D++){let f=S[D],v=h[f],p=v&&!Array.isArray(v)?v.type:null,l=re(p)?ie(p,v.value):null;if(Ce.has(f)||!Fe.test(f)||!re(p)||!l)return I("TL501",`Invalid TSL uniform '${f}' on '${n}'.`);let a={name:f,slot:D,type:p,defaultVec4:l};i.push(a),m.set(f,a)}}let _=c.nodes;if(!Array.isArray(_)||_.length===0)return I("TL501",`Empty TSL nodes on '${n}'.`);if(_.length>128)return I("TL502",`TSL '${n}' exceeds MAX_TSL_NODES (128).`);let s=[],t=2166136261;for(let h of i)t=B(t,`U:${h.slot}:${h.type};`);for(let h=0;h<_.length;h++){let S=_[h];if(!S||typeof S!="object"||Array.isArray(S))return I("TL501",`Invalid TSL node ${h} on '${n}'.`);let D=S,f=D.op,v=D.type;if(f==="loop"||f==="while"||f==="for"||f==="call")return I("TL504",`Unsupported loop/call op '${f}' at node ${h} on '${n}'.`);if(typeof f!="string"||!re(v))return I("TL501",`Malformed TSL node ${h} on '${n}'.`);let p=f,l=D.args,a=[];if(l!==void 0){if(!Array.isArray(l)||l.length>4)return I("TL501",`Invalid args at node ${h} on '${n}'.`);for(let r of l){if(typeof r!="number"||!Number.isInteger(r)||r<0||r>=h)return I("TL501",`Cyclic or out-of-order arg at node ${h} on '${n}'.`);a.push(r)}}t=B(t,`N${h}:${p}:${v}:`);for(let r of a)t=B(t,`${r},`);let o=a.map(r=>s[r].type);if(p==="const"){let r=C(v),$=D.value;if(a.length!==0||!Array.isArray($)||$.length!==r)return I("TL501",`Invalid const node ${h} on '${n}'.`);let P=[];for(let T=0;T<r;T++){let V=Number($[T]);if(!Number.isFinite(V))return I("TL501",`Non-finite const at node ${h}.`);let A=Math.fround(V);P.push(A),t=We(t,A)}s.push({op:p,type:v,args:[],value:P})}else if(p==="builtin"){let r=D.name;if(a.length!==0||typeof r!="string"||Ve[r]!==v)return I("TL501",`Invalid builtin at node ${h}.`);t=B(t,r),s.push({op:p,type:v,args:[],builtin:r})}else if(p==="uniform"){let r=D.name,$=typeof r=="string"?m.get(r):void 0;if(a.length!==0||!$||$.type!==v)return I("TL501",`Invalid uniform ref at node ${h}.`);t=B(t,`S${$.slot}`),s.push({op:p,type:v,args:[],uniformSlot:$.slot})}else if(p==="swizzle"){let r=D.mask;if(a.length!==1||typeof r!="string"||!/^[xyzw]{1,4}$/.test(r)||ve(r.length)!==v)return I("TL501",`Invalid swizzle at node ${h}.`);let $=C(o[0]);for(let P=0;P<r.length;P++)if("xyzw".indexOf(r[P])>=$)return I("TL501",`Swizzle '.${r}' out of bounds at node ${h}.`);t=B(t,r),s.push({op:p,type:v,args:a,mask:r})}else if(p==="construct"){let r=C(v),$=o.reduce((P,T)=>P+C(T),0);if(a.length===0||!(a.length===1&&o[0]==="float")&&$!==r)return I("TL501",`Invalid construct at node ${h}.`);s.push({op:p,type:v,args:a})}else if(Ae.has(p)){if(a.length!==1||o[0]!==v)return I("TL501",`Type mismatch on '${p}' at node ${h}.`);s.push({op:p,type:v,args:a})}else if(je.has(p)){if((a.length===2?o[0]===o[1]?o[0]:o[0]==="float"?o[1]:o[1]==="float"?o[0]:null:null)!==v)return I("TL501",`Type mismatch on '${p}' at node ${h}.`);s.push({op:p,type:v,args:a})}else if(p==="step"){if(a.length!==2||o[0]!==o[1]&&o[0]!=="float"||v!==o[1])return I("TL501",`Invalid 'step' at node ${h}.`);s.push({op:p,type:v,args:a})}else if(p==="clamp"){if(!(a.length===3&&v===o[0]&&(o[1]===o[0]&&o[2]===o[0]||o[1]==="float"&&o[2]==="float")))return I("TL501",`Invalid 'clamp' at node ${h}.`);s.push({op:p,type:v,args:a})}else if(p==="mix"){if(!(a.length===3&&o[0]===o[1]&&v===o[0]&&(o[2]===o[0]||o[2]==="float")))return I("TL501",`Invalid 'mix' at node ${h}.`);s.push({op:p,type:v,args:a})}else if(p==="smoothstep"){if(!(a.length===3&&v===o[2]&&(o[0]===o[2]&&o[1]===o[2]||o[0]==="float"&&o[1]==="float")))return I("TL501",`Invalid 'smoothstep' at node ${h}.`);s.push({op:p,type:v,args:a})}else if(p==="dot"||p==="distance"){if(a.length!==2||o[0]!==o[1]||v!=="float")return I("TL501",`Invalid '${p}' at node ${h}.`);s.push({op:p,type:v,args:a})}else if(p==="length"){if(a.length!==1||v!=="float")return I("TL501",`Invalid 'length' at node ${h}.`);s.push({op:p,type:v,args:a})}else if(p==="reflect"){if(a.length!==2||o[0]!==o[1]||o[0]==="float"||v!==o[0])return I("TL501",`Invalid 'reflect' at node ${h}.`);s.push({op:p,type:v,args:a})}else if(p in me){let[r,$]=me[p];if(a.length!==r.length||v!==$||r.some((P,T)=>o[T]!==P))return I("TL501",`Invalid '${p}' at node ${h}.`);s.push({op:p,type:v,args:a})}else return I("TL501",`Unknown TSL op '${String(p)}' at node ${h}.`)}let x=c.outputs;if(!x||typeof x!="object"||Array.isArray(x))return I("TL501",`Missing TSL outputs on '${n}'.`);let u=x,N={},w=0;for(let h of Object.keys(u).sort()){let S=Ue[h];if(!S)return I("TL501",`Unknown TSL socket '${h}' on '${n}'.`);let D=u[h];if(D!=null){if(typeof D!="number"||!Number.isInteger(D)||D<0||D>=s.length||s[D].type!==S)return I("TL501",`Invalid TSL output '${h}' on '${n}'.`);N[h]=D,w++,t=B(t,`O:${h}=${D};`)}}if(w===0)return I("TL501",`No active TSL outputs on '${n}'.`);let g=new Uint8Array(s.length),k=new Uint8Array(s.length);for(let[h,S]of Object.entries(N))h==="position"?k[S]=1:g[S]=1;for(let h=s.length-1;h>=0;h--){if(g[h])for(let S of s[h].args)g[S]=1;if(k[h])for(let S of s[h].args)k[S]=1}let y=!1,L=!1;for(let h=0;h<s.length;h++){let S=s[h];if(k[h]&&(S.op==="builtin"&&S.builtin==="viewDirection"||S.op==="fresnel"||S.op==="bumpFromHeight"))return I("TL503",`Fragment-only TSL op '${S.builtin||S.op}' used in positionNode on '${n}'.`);(g[h]||k[h])&&(S.op==="builtin"&&S.builtin==="time"&&(y=!0),S.op==="bumpFromHeight"&&(L=!0))}return{ok:!0,graph:{nodes:s,outputs:N,uniformSlots:i,uniformSlotByName:m,hash:(t>>>0).toString(16).padStart(8,"0"),usesTime:y,usesVertexPosition:N.position!==void 0,usesDerivatives:L,fragmentReachable:g,vertexReachable:k}}}function W(e){if(!Number.isFinite(e)||e===0)return"0.0";let n=Math.fround(e),c=Math.abs(n);if(c>=1e-4&&c<1e6){let m=Number(n.toPrecision(7)).toString();return m.includes(".")?m:`${m}.0`}let i=n.toExponential(6).split("e");return`${i[0].replace(/(\.\d*?[1-9])0+$/,"$1").replace(/\.0+$/,".0")}e${i[1]}`}function ae(e,n,c){let i={div:!1,mod:c,pow:!1,clamp:!1,smooth:!1,atan2:!1,norm:!1,fresnel:!1,snoise2:!1,snoise3:!1,voronoi2:!1,checker:!1,triplanar:!1,hueShift:!1,bump:!1};for(let m=0;m<e.length;m++)if(n[m])switch(e[m].op){case"div":i.div=!0;break;case"mod":i.mod=!0;break;case"pow":i.pow=!0;break;case"clamp":i.clamp=!0;break;case"smoothstep":i.smooth=!0,i.div=!0,i.clamp=!0;break;case"atan2":i.atan2=!0;break;case"normalize":i.norm=!0;break;case"fresnel":i.fresnel=!0,i.pow=!0,i.norm=!0;break;case"simplexNoise2D":i.snoise2=!0;break;case"simplexNoise3D":i.snoise3=!0;break;case"voronoi2D":i.voronoi2=!0;break;case"checker":i.checker=!0,i.mod=!0;break;case"triplanar":i.triplanar=!0,i.snoise2=!0,i.pow=!0,i.norm=!0;break;case"hueShift":i.hueShift=!0;break;case"bumpFromHeight":i.bump=!0,i.norm=!0;break;default:break}return i}var Ee=["x","y","z","w"],$e=[[2,"vec2"],[3,"vec3"],[4,"vec4"]];function H(e,n){return $e.map(([c,i])=>{let m=Ee.slice(0,c).map(d=>n===2?`${e}(a.${d},b.${d})`:`${e}(a.${d},b.${d},c.${d})`).join(",");return n===2?`${i} ${e}(${i} a,${i} b){return ${i}(${m});}`:`${i} ${e}(${i} a,${i} b,${i} c){return ${i}(${m});}`})}function he(e,n){let c=[];return e.div&&c.push("float _tslDiv(float a,float b){return a/(abs(b)<1e-6?(b<0.0?-1e-6:1e-6):b);}",...H("_tslDiv",2)),e.mod&&c.push("float _tslMod(float a,float b){float d=abs(b)<1e-6?(b<0.0?-1e-6:1e-6):b;return a-d*floor(a/d);}",...H("_tslMod",2)),e.pow&&c.push("float _tslPow(float a,float b){float e=clamp(b,-64.0,64.0);return abs(e)<1e-8?1.0:(a<=0.0?0.0:pow(max(a,1e-8),e));}",...H("_tslPow",2)),e.clamp&&c.push("float _tslClamp(float a,float b,float c){return clamp(a,min(b,c),max(b,c));}",...H("_tslClamp",3)),e.smooth&&c.push("float _tslSmooth(float a,float b,float c){float t=_tslClamp(_tslDiv(c-a,b-a),0.0,1.0);return t*t*(3.0-2.0*t);}",...H("_tslSmooth",3)),e.atan2&&c.push("float _tslAtan2(float a,float b){return(abs(a)<1e-8&&abs(b)<1e-8)?0.0:atan(a,b);}",...H("_tslAtan2",2)),e.norm&&c.push("float _tslNorm(float v){return abs(v)>1e-6?sign(v):0.0;}",...$e.map(([,i])=>`${i} _tslNorm(${i} v){float l=length(v);return l>1e-6?v/l:${i}(0.0);}`)),(e.snoise2||e.snoise3||e.voronoi2)&&c.push("float _tslM289(float x){return x-floor(x*(1.0/289.0))*289.0;}","float _tslPerm(float x){return _tslM289((x*34.0+10.0)*x);}"),e.snoise2&&c.push("float _tslSNoise2(vec2 v){const float c0=0.21132487,c1=0.3660254,c2=-0.57735026,c3=0.024390243;float s=(v.x+v.y)*c1,ix=floor(v.x+s),iy=floor(v.y+s),t=(ix+iy)*c0,x0=v.x-ix+t,y0=v.y-iy+t,i1x=x0>y0?1.0:0.0,i1y=1.0-i1x,x1=x0+c0-i1x,y1=y0+c0-i1y,x2=x0+c2,y2=y0+c2;ix=_tslM289(ix);iy=_tslM289(iy);float p0=_tslPerm(_tslPerm(iy)+ix),p1=_tslPerm(_tslPerm(iy+i1y)+ix+i1x),p2=_tslPerm(_tslPerm(iy+1.0)+ix+1.0);vec3 m=max(0.5-vec3(x0*x0+y0*y0,x1*x1+y1*y1,x2*x2+y2*y2),0.0);m=m*m;m=m*m;vec3 gx=2.0*fract(vec3(p0,p1,p2)*c3)-1.0,hy=abs(gx)-0.5,ox=gx-floor(gx+0.5);m*=1.7928429-0.85373473*(ox*ox+hy*hy);return clamp(0.5+65.0*dot(m,ox*vec3(x0,x1,x2)+hy*vec3(y0,y1,y2)),0.0,1.0);}"),e.snoise3&&c.push("float _tslSNoise3(vec3 v){const float c0=0.16666667,c1=0.33333334,ns=0.14285715;float s=(v.x+v.y+v.z)*c1,i0=floor(v.x+s),i1=floor(v.y+s),i2=floor(v.z+s),t=(i0+i1+i2)*c0;vec3 p0=v-vec3(i0,i1,i2)+t,g=step(p0.yzx,p0.xyz),l=1.0-g.zxy,a1=min(g,l),a2=max(g,l),p1=p0-a1+c0,p2=p0-a2+c1,p3=p0-0.5;i0=_tslM289(i0);i1=_tslM289(i1);i2=_tslM289(i2);vec4 hs=vec4(_tslPerm(_tslPerm(_tslPerm(i2)+i1)+i0),_tslPerm(_tslPerm(_tslPerm(i2+a1.z)+i1+a1.y)+i0+a1.x),_tslPerm(_tslPerm(_tslPerm(i2+a2.z)+i1+a2.y)+i0+a2.x),_tslPerm(_tslPerm(_tslPerm(i2+1.0)+i1+1.0)+i0+1.0));float sum=0.0;for(int k=0;k<4;k++){float pk=hs[k],j=pk-49.0*floor(pk*ns*ns),x_=floor(j*ns),y_=floor(j-7.0*x_),gx=x_*(2.0/7.0)+(1.0/7.0)-1.0,gy=y_*(2.0/7.0)+(1.0/7.0)-1.0,gz=1.0-abs(gx)-abs(gy),sz=gz<0.0?1.0:0.0;gx-=sz*(gx>=0.0?1.0:-1.0);gy-=sz*(gy>=0.0?1.0:-1.0);float inv=1.7928429-0.85373473*(gx*gx+gy*gy+gz*gz);vec3 q=k==0?p0:(k==1?p1:(k==2?p2:p3));float m=max(0.6-dot(q,q),0.0);m=m*m;m=m*m;sum+=m*inv*(gx*q.x+gy*q.y+gz*q.z);}return clamp(0.5+21.0*sum,0.0,1.0);}"),e.voronoi2&&c.push("float _tslVoronoi2(vec2 u,float jit){vec2 n=floor(u),fr=u-n;float jt=clamp(jit,0.0,1.0),md=8.0;for(int dy=-1;dy<=1;dy++)for(int dx=-1;dx<=1;dx++){float qx=_tslM289(n.x+float(dx)),qy=_tslM289(n.y+float(dy)),hx=fract(_tslPerm(_tslPerm(qx)+qy)*(1.0/41.0)),hy=fract(_tslPerm(_tslPerm(qy+7.0)+qx+13.0)*(1.0/41.0));md=min(md,length(vec2(float(dx),float(dy))+0.5+(vec2(hx,hy)-0.5)*jt-fr));}return clamp(md,0.0,1.0);}"),e.checker&&c.push("float _tslChecker(vec2 p){return _tslMod(floor(p.x)+floor(p.y),2.0)>=1.0?1.0:0.0;}"),e.fresnel&&c.push("float _tslFresnel(float pw,vec3 nw,vec3 vd){return _tslPow(1.0-clamp(dot(_tslNorm(nw),_tslNorm(vd)),0.0,1.0),max(pw,1e-3));}"),e.triplanar&&c.push("float _tslTriplanar(vec3 p,float sh,vec3 nw){vec3 w=_tslPow(max(abs(_tslNorm(nw)),vec3(1e-4)),vec3(clamp(sh,0.5,16.0)));return(w.x*_tslSNoise2(p.yz)+w.y*_tslSNoise2(p.xz)+w.z*_tslSNoise2(p.xy))/max(w.x+w.y+w.z,1e-6);}"),e.hueShift&&c.push("vec3 _tslHueShift(vec3 rgb,float ang){const vec3 k=vec3(0.57735026);float c=cos(ang),s=sin(ang);return rgb*c+cross(k,rgb)*s+k*dot(k,rgb)*(1.0-c);}"),e.bump&&n&&c.push("vec3 _tslBump(float h,float st,vec3 nw,vec3 wp){vec3 n=_tslNorm(nw),dx=dFdx(wp),dy=dFdy(wp),r1=cross(dy,n),r2=cross(n,dx);float det=dot(dx,r1);return abs(det)<1e-8?n:_tslNorm(abs(det)*n-st*(det<0.0?-1.0:1.0)*(dFdx(h)*r1+dFdy(h)*r2));}"),c.join(`
|
|
17
|
+
`)}var qe={positionLocal:"_tslPL",positionWorld:"_tslPW",normalLocal:"_tslNL",normalWorld:"_tslNW",viewDirection:"_tslVD",uv:"_tslUV",vertexColor:"_tslVC",instanceIndex:"_tslII"};function ge(e,n){let c=[],i=(d,_)=>d==="float"?_:`${d}(${_})`,m=(d,_,s)=>_===s?d:`${s}(${d})`;for(let d=0;d<e.length;d++){if(!n[d])continue;let _=e[d],s=_.type,t=_.args[0]!==void 0?`_t${_.args[0]}`:"",x=_.args[1]!==void 0?`_t${_.args[1]}`:"",u=_.args[2]!==void 0?`_t${_.args[2]}`:"",N=_.args[0]!==void 0?e[_.args[0]].type:"float",w=_.args[1]!==void 0?e[_.args[1]].type:"float",g=_.args[2]!==void 0?e[_.args[2]].type:"float",k=i(s,"0.0");switch(_.op){case"const":{let y=(_.value??[0]).map(W);k=s==="float"?y[0]:`${s}(${y.join(",")})`;break}case"builtin":k=_.builtin==="time"?`_tslMod(uTslTime,${W(3600)})`:qe[_.builtin]??"0.0";break;case"uniform":{let y=`uTslParams[${_.uniformSlot??0}]`,L=C(s);k=L===4?y:`${y}.${"xyz".slice(0,L)}`;break}case"swizzle":k=N==="float"?i(s,t):`${t}.${_.mask}`;break;case"construct":k=s==="float"?t:`${s}(${_.args.map(y=>`_t${y}`).join(",")})`;break;case"neg":k=`(-${t})`;break;case"abs":case"sign":case"floor":case"ceil":case"fract":case"sin":case"cos":k=`${_.op}(${t})`;break;case"sqrt":k=`sqrt(max(${t},${i(s,"0.0")}))`;break;case"exp":k=`exp(clamp(${t},${i(s,"-80.0")},${i(s,"80.0")}))`;break;case"log":k=`log(max(${t},${i(s,"1.0e-8")}))`;break;case"normalize":k=`_tslNorm(${t})`;break;case"add":case"sub":case"mul":{let y=_.op==="add"?"+":_.op==="sub"?"-":"*";k=`(${m(t,N,s)}${y}${m(x,w,s)})`;break}case"div":case"mod":case"pow":case"atan2":{k=`${_.op==="div"?"_tslDiv":_.op==="mod"?"_tslMod":_.op==="pow"?"_tslPow":"_tslAtan2"}(${m(t,N,s)},${m(x,w,s)})`;break}case"min":case"max":case"step":k=`${_.op}(${m(t,N,s)},${m(x,w,s)})`;break;case"clamp":k=`_tslClamp(${m(t,N,s)},${m(x,w,s)},${m(u,g,s)})`;break;case"mix":k=`mix(${t},${x},${m(u,g,s)})`;break;case"smoothstep":k=`_tslSmooth(${m(t,N,s)},${m(x,w,s)},${m(u,g,s)})`;break;case"dot":k=N==="float"?`(${t}*${x})`:`dot(${t},${x})`;break;case"length":k=N==="float"?`abs(${t})`:`length(${t})`;break;case"distance":k=N==="float"?`abs(${t}-${x})`:`distance(${t},${x})`;break;case"cross":case"reflect":k=`${_.op}(${t},${x})`;break;case"fresnel":k=`_tslFresnel(${t},_tslNW,_tslVD)`;break;case"simplexNoise2D":k=`_tslSNoise2(${t})`;break;case"simplexNoise3D":k=`_tslSNoise3(${t})`;break;case"voronoi2D":k=`_tslVoronoi2(${t},${x})`;break;case"checker":k=`_tslChecker(${t})`;break;case"triplanar":k=`_tslTriplanar(${t},${x},_tslNW)`;break;case"hueShift":k=`_tslHueShift(${t},${x})`;break;case"bumpFromHeight":k=`_tslBump(${t},${x},_tslNW,_tslPW)`;break;default:break}c.push(`${s} _t${d}=${k};`)}return c.join(`
|
|
18
|
+
`)}function ye(e,n,c){let i=`uniform vec4 uTslParams[16];
|
|
19
|
+
uniform float uTslTime;`,m=e.fragmentReachable.some(t=>t===1),d=n.replace("out vec2 vUV;",`${i}
|
|
20
|
+
out vec2 vUV;
|
|
21
|
+
out vec3 vTslLP,vTslLN;
|
|
22
|
+
flat out float vTslII;`),_=d.includes("morphedPos")?"morphedPos":"aPosition";if(e.usesVertexPosition&&e.outputs.position!==void 0){let t=he(ae(e.nodes,e.vertexReachable,e.usesTime),!1),x=ge(e.nodes,e.vertexReachable),u=`${t}
|
|
23
|
+
vec3 _tslEvalPos(vec3 _tslPL,vec3 _tslNL,vec2 _tslUV,vec4 _tslVC,float _tslII,mat4 _m,mat3 _nm){
|
|
24
|
+
vec3 _tslPW=(_m*vec4(_tslPL,1.0)).xyz,_rn=_nm*_tslNL,_tslNW=length(_rn)>1e-5?normalize(_rn):vec3(0.0,1.0,0.0),_tslVD=vec3(0.0,0.0,1.0);
|
|
25
|
+
${x}
|
|
26
|
+
return _t${e.outputs.position};}`;d=d.replace("void main(){",`${u}
|
|
27
|
+
void main(){`);let N=`vec2 _tslScUV=uUvTransform.zw;if(_tslScUV.x==0.0&&_tslScUV.y==0.0)_tslScUV=vec2(1.0);vec2 _tslVtxUV=aUV*_tslScUV+uUvTransform.xy;
|
|
28
|
+
float _tslInstF=float(gl_InstanceID);vec3 _tslBaseP=${_},_tslNewP=_tslEvalPos(_tslBaseP,aNormal,_tslVtxUV,vInstTint,_tslInstF,uModel,uNormalMatrix),_tslNewN=aNormal;
|
|
29
|
+
if(length(aNormal)>1e-4){vec3 _n0=normalize(aNormal),_tg1=normalize(abs(_n0.y)<0.99?cross(vec3(0.0,1.0,0.0),_n0):cross(vec3(1.0,0.0,0.0),_n0)),_tg2=cross(_n0,_tg1),_cn=cross(_tslEvalPos(_tslBaseP+_tg1*1e-3,aNormal,_tslVtxUV,vInstTint,_tslInstF,uModel,uNormalMatrix)-_tslNewP,_tslEvalPos(_tslBaseP+_tg2*1e-3,aNormal,_tslVtxUV,vInstTint,_tslInstF,uModel,uNormalMatrix)-_tslNewP);if(length(_cn)>1e-10)_tslNewN=normalize(_cn);}
|
|
30
|
+
vTslLP=_tslNewP;vTslLN=_tslNewN;vTslII=_tslInstF;vec3 _rnDisp=uNormalMatrix*_tslNewN;vNormal=length(_rnDisp)>1e-5?normalize(_rnDisp):vec3(0.0);`;d.includes(`vec4 world=uModel*(skinMatrix*vec4(${_},1.0));`)?d=d.replace(`vec4 world=uModel*(skinMatrix*vec4(${_},1.0));`,`${N}
|
|
31
|
+
vec4 world=uModel*(skinMatrix*vec4(_tslNewP,1.0));`).replace(`gl_Position=uMVP*(skinMatrix*vec4(${_},1.0));`,"gl_Position=uMVP*(skinMatrix*vec4(_tslNewP,1.0));"):d=d.replace(`vec4 world=uModel*vec4(${_},1.0);`,`${N}
|
|
32
|
+
vec4 world=uModel*vec4(_tslNewP,1.0);`).replace(`gl_Position=uMVP*vec4(${_},1.0);`,"gl_Position=uMVP*vec4(_tslNewP,1.0);")}else d=d.replace("gl_Position=",`vTslLP=${_};vTslLN=aNormal;vTslII=float(gl_InstanceID);
|
|
33
|
+
gl_Position=`);let s=c.replace("in vec2 vUV;",`${i}
|
|
34
|
+
in vec2 vUV;
|
|
35
|
+
in vec3 vTslLP,vTslLN;
|
|
36
|
+
flat in float vTslII;`);if(m){let t=he(ae(e.nodes,e.fragmentReachable,e.usesTime),!0),x=ge(e.nodes,e.fragmentReachable);t&&(s=s.replace("void main(){",`${t}
|
|
37
|
+
void main(){`)),e.outputs.opacity!==void 0&&(s=s.replace("if(base.a<0.004)discard;","")),s=s.replace("if(length(vNormal)<1e-4&&uHasTexture==1){","if(false&&length(vNormal)<1e-4&&uHasTexture==1){");let u=[];if(e.outputs.color!==void 0&&u.push(`base.rgb=max(_t${e.outputs.color},vec3(0.0))*vInstTint.rgb;`),e.outputs.opacity!==void 0&&u.push(`base.a=clamp(_t${e.outputs.opacity},0.0,1.0)*vInstTint.a;`,"if(base.a<0.004)discard;"),e.outputs.emissive!==void 0&&u.push(`em=max(_t${e.outputs.emissive},vec3(0.0));`),e.outputs.roughness!==void 0&&u.push(`roughness=clamp(_t${e.outputs.roughness},0.04,1.0);`),e.outputs.metalness!==void 0&&u.push(`metallic=clamp(_t${e.outputs.metalness},0.0,1.0);`),e.outputs.normalScale!==void 0&&e.outputs.bump===void 0&&(s=s.replace("if(uHasNormalTexture==1){",`vec3 _tslBaseN=N;
|
|
38
|
+
if(uHasNormalTexture==1){`),u.push(`if(uHasNormalTexture==1){N=perturbNormal(_tslBaseN,V,vUV,uNormalTexture,max(_t${e.outputs.normalScale},0.0));}`)),e.outputs.bump!==void 0){let N=e.outputs.normalScale!==void 0?`max(_t${e.outputs.normalScale},0.0)`:"1.0";u.push(`{vec3 _bn=_t${e.outputs.bump};float _bl=length(_bn);if(_bl>1e-6){vec3 _bm=N+(_bn/_bl-N)*(${N});float _ml=length(_bm);if(_ml>1e-6)N=_bm/_ml;}}`)}s=s.replace("metallic=clamp(metallic,0.0,1.0);",`metallic=clamp(metallic,0.0,1.0);
|
|
39
|
+
{vec3 _tslPL=vTslLP,_tslPW=vWorldPos,_tslNL=length(vTslLN)>1e-5?normalize(vTslLN):vec3(0.0,1.0,0.0),_tslNW=N,_tslVD=V;vec2 _tslUV=vUV;vec4 _tslVC=vInstTint;float _tslII=vTslII;
|
|
40
|
+
${x}
|
|
41
|
+
${u.join(`
|
|
42
|
+
`)}}`)}return{vs:d,fs:s}}var Be={float:"float",vec2:"float2",vec3:"float3",vec4:"float4"},Oe={float:"float",vec2:"vector2",vec3:"vector3",vec4:"vector4"};function R(e){return Be[e]||"float"}function E(e){return Oe[e]||"float"}function J(e,n){if(n===1)return W(e[0]??0);let c=[];for(let i=0;i<n;i++)c.push(W(e[i]??0));return`(${c.join(", ")})`}function be(e,n,c=""){let i;if(e&&typeof e=="object"&&"fragmentReachable"in e)i=e;else{let f=Y(e,n);if(!f.ok)return null;i=f.graph}if(!i.fragmentReachable.some(f=>f===1))return null;let m=`/Root/Materials/${(n||"Material").replace(/[^A-Za-z0-9_]/g,"_").replace(/^([0-9])/,"_$1")||"Material"}/TSLGraph`,d=[],_=[],s=0;for(let f of i.uniformSlots){let v=R(f.type),p=C(f.type);_.push(`${v} inputs:u_${f.slot} = ${J(f.defaultVec4,p)}`)}let t=(f,v,p,l)=>(d.push(`def Shader "${f}"
|
|
43
|
+
{
|
|
44
|
+
uniform token info:id = "${v}"
|
|
45
|
+
${l.join(`
|
|
46
|
+
`)}${l.length>0?`
|
|
47
|
+
`:""}${p} outputs:out
|
|
48
|
+
}`),`${m}/${f}.outputs:out`),x=(f,v)=>{let p=C(f),l=`C_${s++}`;return t(l,`ND_constant_${E(f)}`,R(f),[`${R(f)} inputs:value = ${J(v,p)}`])},u=(f,v)=>{if(v==="float")return f;let p=C(v),l=`B_${s++}`,a=[];for(let o=1;o<=p;o++)a.push(`float inputs:in${o}.connect = <${f}>`);return t(l,`ND_combine${p}_${E(v)}`,R(v),a)},N=(f,v,p)=>v===p?f:u(f,p),w=(f,v,p)=>{if(v==="float")return f;let l=`E_${s++}`;return t(l,`ND_extract_${E(v)}`,"float",[`${R(v)} inputs:in.connect = <${f}>`,`int inputs:index = ${p}`])},g=new Map,k=f=>{let v=g.get(f);if(v)return v;let p="";switch(f){case"positionLocal":p=t("BI_posLocal","ND_position_vector3","float3",['string inputs:space = "object"']);break;case"positionWorld":p=t("BI_posWorld","ND_position_vector3","float3",['string inputs:space = "world"']);break;case"normalLocal":p=t("BI_normLocal","ND_normal_vector3","float3",['string inputs:space = "object"']);break;case"normalWorld":p=t("BI_normWorld","ND_normal_vector3","float3",['string inputs:space = "world"']);break;case"viewDirection":{let l=t("BI_rawView","ND_viewdirection_vector3","float3",['string inputs:space = "world"']);p=t("BI_viewDir","ND_multiply_vector3FA","float3",[`float3 inputs:in1.connect = <${l}>`,"float inputs:in2 = -1.0"]);break}case"uv":p=t("BI_uv","ND_texcoord_vector2","float2",["int inputs:index = 0"]);break;case"vertexColor":{let l=t("BI_vcCol","ND_geomcolor_color4","color4f",["int inputs:index = 0"]);p=t("BI_vcVec","ND_convert_color4_vector4","float4",[`color4f inputs:in.connect = <${l}>`]);break}case"instanceIndex":p=x("float",[0]);break;case"time":{let l=t("BI_rawTime","ND_time_float","float",[]);p=t("BI_time","ND_modulo_float","float",[`float inputs:in1.connect = <${l}>`,`float inputs:in2 = ${W(3600)}`]);break}default:p=x("float",[0]);break}return g.set(f,p),p},y=new Array(i.nodes.length).fill("");for(let f=0;f<i.nodes.length;f++){if(!i.fragmentReachable[f])continue;let v=i.nodes[f],p=v.type,l=R(p),a=E(p),o=C(p),r=v.args[0]!==void 0?y[v.args[0]]:"",$=v.args[1]!==void 0?y[v.args[1]]:"",P=v.args[2]!==void 0?y[v.args[2]]:"",T=v.args[0]!==void 0?i.nodes[v.args[0]].type:"float",V=v.args[1]!==void 0?i.nodes[v.args[1]].type:"float",A=v.args[2]!==void 0?i.nodes[v.args[2]].type:"float",M=`N${f}`;switch(v.op){case"const":y[f]=t(M,`ND_constant_${a}`,l,[`${l} inputs:value = ${J(v.value??[0],o)}`]);break;case"builtin":y[f]=k(v.builtin??"time");break;case"uniform":y[f]=`${m}.inputs:u_${v.uniformSlot??0}`;break;case"swizzle":{let z=v.mask??"x";if(T==="float")y[f]=u(r,p);else if(z.length===1)y[f]=w(r,T,"xyzw".indexOf(z[0]));else{let U=Array.from(z).map(b=>w(r,T,"xyzw".indexOf(b)));y[f]=t(M,`ND_combine${z.length}_${a}`,l,U.map((b,j)=>`float inputs:in${j+1}.connect = <${b}>`))}break}case"construct":{if(p==="float")y[f]=r;else if(v.args.length===1&&T==="float")y[f]=u(r,p);else{let z=[];for(let U of v.args){let b=i.nodes[U].type,j=y[U],F=C(b);if(F===1)z.push(j);else for(let O=0;O<F;O++)z.push(w(j,b,O))}y[f]=t(M,`ND_combine${o}_${a}`,l,z.slice(0,o).map((U,b)=>`float inputs:in${b+1}.connect = <${U}>`))}break}case"neg":{y[f]=t(M,`ND_multiply_${a}`,l,[`${l} inputs:in1.connect = <${r}>`,`${l} inputs:in2 = ${J([-1,-1,-1,-1],o)}`]);break}case"abs":case"sign":case"floor":case"ceil":case"sqrt":case"exp":case"sin":case"cos":case"normalize":{let z=v.op==="abs"?"absval":v.op;v.op==="normalize"&&p==="float"?y[f]=t(M,"ND_sign_float","float",[`float inputs:in.connect = <${r}>`]):y[f]=t(M,`ND_${z}_${a}`,l,[`${l} inputs:in.connect = <${r}>`]);break}case"log":y[f]=t(M,`ND_ln_${a}`,l,[`${l} inputs:in.connect = <${r}>`]);break;case"fract":{y[f]=t(M,`ND_modulo_${a}`,l,[`${l} inputs:in1.connect = <${r}>`,`${l} inputs:in2 = ${J([1,1,1,1],o)}`]);break}case"add":case"sub":case"mul":case"div":case"mod":case"pow":case"atan2":case"min":case"max":{let z={add:"add",sub:"subtract",mul:"multiply",div:"divide",mod:"modulo",pow:"power",atan2:"atan2",min:"min",max:"max"},U=N(r,T,p),b=N($,V,p);y[f]=t(M,`ND_${z[v.op]}_${a}`,l,[`${l} inputs:in1.connect = <${U}>`,`${l} inputs:in2.connect = <${b}>`]);break}case"step":{let z=N(r,T,p);if(p==="float")y[f]=t(M,"ND_ifgreater_float","float",[`float inputs:value1.connect = <${$}>`,`float inputs:value2.connect = <${z}>`,"float inputs:in1 = 1.0","float inputs:in2 = 0.0"]);else{let U=[];for(let b=0;b<o;b++){let j=w(z,p,b),F=w($,p,b);U.push(t(`S_${s++}`,"ND_ifgreater_float","float",[`float inputs:value1.connect = <${F}>`,`float inputs:value2.connect = <${j}>`,"float inputs:in1 = 1.0","float inputs:in2 = 0.0"]))}y[f]=t(M,`ND_combine${o}_${a}`,l,U.map((b,j)=>`float inputs:in${j+1}.connect = <${b}>`))}break}case"clamp":{let z=N($,V,p),U=N(P,A,p);y[f]=t(M,`ND_clamp_${a}`,l,[`${l} inputs:in.connect = <${r}>`,`${l} inputs:low.connect = <${z}>`,`${l} inputs:high.connect = <${U}>`]);break}case"mix":{let z=N(P,A,p);y[f]=t(M,`ND_mix_${a}`,l,[`${l} inputs:bg.connect = <${r}>`,`${l} inputs:fg.connect = <${$}>`,`${l} inputs:mix.connect = <${z}>`]);break}case"smoothstep":{let z=N(r,T,p),U=N($,V,p);y[f]=t(M,`ND_smoothstep_${a}`,l,[`${l} inputs:in.connect = <${P}>`,`${l} inputs:low.connect = <${z}>`,`${l} inputs:high.connect = <${U}>`]);break}case"dot":T==="float"?y[f]=t(M,"ND_multiply_float","float",[`float inputs:in1.connect = <${r}>`,`float inputs:in2.connect = <${$}>`]):y[f]=t(M,`ND_dotproduct_${E(T)}`,"float",[`${R(T)} inputs:in1.connect = <${r}>`,`${R(T)} inputs:in2.connect = <${$}>`]);break;case"length":T==="float"?y[f]=t(M,"ND_absval_float","float",[`float inputs:in.connect = <${r}>`]):y[f]=t(M,`ND_magnitude_${E(T)}`,"float",[`${R(T)} inputs:in.connect = <${r}>`]);break;case"distance":{let z=t(`D_${s++}`,`ND_subtract_${E(T)}`,R(T),[`${R(T)} inputs:in1.connect = <${r}>`,`${R(T)} inputs:in2.connect = <${$}>`]);y[f]=T==="float"?t(M,"ND_absval_float","float",[`float inputs:in.connect = <${z}>`]):t(M,`ND_magnitude_${E(T)}`,"float",[`${R(T)} inputs:in.connect = <${z}>`]);break}case"cross":y[f]=t(M,"ND_crossproduct_vector3","float3",[`float3 inputs:in1.connect = <${r}>`,`float3 inputs:in2.connect = <${$}>`]);break;case"reflect":if(p==="vec3")y[f]=t(M,"ND_reflect_vector3","float3",[`float3 inputs:in.connect = <${r}>`,`float3 inputs:normal.connect = <${$}>`]);else{let z=p==="float"?t(`RF_${s++}`,"ND_multiply_float","float",[`float inputs:in1.connect = <${$}>`,`float inputs:in2.connect = <${r}>`]):t(`RF_${s++}`,`ND_dotproduct_${a}`,"float",[`${l} inputs:in1.connect = <${$}>`,`${l} inputs:in2.connect = <${r}>`]),U=t(`RF_${s++}`,"ND_multiply_float","float",[`float inputs:in1.connect = <${z}>`,"float inputs:in2 = 2.0"]),b=t(`RF_${s++}`,`ND_multiply_${a}`,l,[`${l} inputs:in1.connect = <${$}>`,`${l} inputs:in2.connect = <${N(U,"float",p)}>`]);y[f]=t(M,`ND_subtract_${a}`,l,[`${l} inputs:in1.connect = <${r}>`,`${l} inputs:in2.connect = <${b}>`])}break;case"fresnel":{let z=k("normalWorld"),U=k("viewDirection"),b=t(`FR_${s++}`,"ND_dotproduct_vector3","float",[`float3 inputs:in1.connect = <${z}>`,`float3 inputs:in2.connect = <${U}>`]),j=t(`FR_${s++}`,"ND_clamp_float","float",[`float inputs:in.connect = <${b}>`,"float inputs:low = 0.0","float inputs:high = 1.0"]),F=t(`FR_${s++}`,"ND_subtract_float","float",["float inputs:in1 = 1.0",`float inputs:in2.connect = <${j}>`]);y[f]=t(M,"ND_power_float","float",[`float inputs:in1.connect = <${F}>`,`float inputs:in2.connect = <${r}>`]);break}case"simplexNoise2D":y[f]=t(M,"ND_noise2d_float","float",[`float2 inputs:texcoord.connect = <${r}>`,"float inputs:amplitude = 0.5","float inputs:pivot = 0.5"]);break;case"simplexNoise3D":y[f]=t(M,"ND_noise3d_float","float",[`float3 inputs:position.connect = <${r}>`,"float inputs:amplitude = 0.5","float inputs:pivot = 0.5"]);break;case"voronoi2D":y[f]=t(M,"ND_cellnoise2d_float","float",[`float2 inputs:texcoord.connect = <${r}>`]);break;case"checker":{let z=t(`CK_${s++}`,"ND_floor_vector2","float2",[`float2 inputs:in.connect = <${r}>`]),U=w(z,"vec2",0),b=w(z,"vec2",1),j=t(`CK_${s++}`,"ND_add_float","float",[`float inputs:in1.connect = <${U}>`,`float inputs:in2.connect = <${b}>`]),F=t(`CK_${s++}`,"ND_modulo_float","float",[`float inputs:in1.connect = <${j}>`,"float inputs:in2 = 2.0"]);y[f]=t(M,"ND_ifgreater_float","float",[`float inputs:value1.connect = <${F}>`,"float inputs:value2 = 0.999","float inputs:in1 = 1.0","float inputs:in2 = 0.0"]);break}case"triplanar":y[f]=t(M,"ND_noise3d_float","float",[`float3 inputs:position.connect = <${r}>`,"float inputs:amplitude = 0.5","float inputs:pivot = 0.5"]);break;case"hueShift":{let z=t(`HS_${s++}`,"ND_convert_vector3_color3","color3f",[`float3 inputs:in.connect = <${r}>`]),U=t(`HS_${s++}`,"ND_multiply_float","float",[`float inputs:in1.connect = <${$}>`,"float inputs:in2 = 0.15915494"]),b=t(`HS_${s++}`,"ND_combine3_vector3","float3",[`float inputs:in1.connect = <${U}>`,"float inputs:in2 = 1.0","float inputs:in3 = 1.0"]),j=t(`HS_${s++}`,"ND_hsvadjust_color3","color3f",[`color3f inputs:in.connect = <${z}>`,`float3 inputs:amount.connect = <${b}>`]);y[f]=t(M,"ND_convert_color3_vector3","float3",[`color3f inputs:in.connect = <${j}>`]);break}case"bumpFromHeight":{y[f]=t(M,"ND_heighttonormal_vector3","float3",[`float inputs:in.connect = <${r}>`,`float inputs:scale.connect = <${$}>`]);break}default:y[f]=x(p,[0,0,0,0]);break}}let L=[],h={};if(i.outputs.color!==void 0){let f=t("OUT_color","ND_convert_vector3_color3","color3f",[`float3 inputs:in.connect = <${y[i.outputs.color]}>`]);L.push(`color3f outputs:diffuseColor.connect = <${f}>`),h.diffuseColor=`${m}.outputs:diffuseColor`}if(i.outputs.emissive!==void 0){let f=t("OUT_emissive","ND_convert_vector3_color3","color3f",[`float3 inputs:in.connect = <${y[i.outputs.emissive]}>`]);L.push(`color3f outputs:emissiveColor.connect = <${f}>`),h.emissiveColor=`${m}.outputs:emissiveColor`}if(i.outputs.opacity!==void 0&&(L.push(`float outputs:opacity.connect = <${y[i.outputs.opacity]}>`),h.opacity=`${m}.outputs:opacity`),i.outputs.roughness!==void 0&&(L.push(`float outputs:roughness.connect = <${y[i.outputs.roughness]}>`),h.roughness=`${m}.outputs:roughness`),i.outputs.metalness!==void 0&&(L.push(`float outputs:metallic.connect = <${y[i.outputs.metalness]}>`),h.metallic=`${m}.outputs:metallic`),i.outputs.bump!==void 0&&(L.push(`normal3f outputs:normal.connect = <${y[i.outputs.bump]}>`),h.normal=`${m}.outputs:normal`),L.length===0)return null;let S=[["diffuseColor","color3f"],["metallic","float"],["roughness","float"],["opacity","float"],["emissiveColor","color3f"],["normal","normal3f"]],D=c;for(let[f,v]of S){let p=h[f];if(!p)continue;let l=`${v} inputs:${f}.connect = <${p}>`,a=new RegExp(`^${v}\\s+inputs:${f}[^
|
|
49
|
+
]*$`,"m");a.test(D)?D=D.replace(a,l):D+=`${D.endsWith(`
|
|
50
|
+
`)||!D?"":`
|
|
51
|
+
`}${l}
|
|
52
|
+
`}return{nodeGraphUsda:`def NodeGraph "TSLGraph"
|
|
53
|
+
{
|
|
54
|
+
${[..._,...L,...d].join(`
|
|
55
|
+
`)}
|
|
56
|
+
}
|
|
57
|
+
`,surfaceConnections:h,surfaceInputsUsda:D,usesTime:i.usesTime}}var He=272,Ge=["x","y","z","w"],xe=[[2,"vec2<f32>"],[3,"vec3<f32>"],[4,"vec4<f32>"]];function G(e,n){return xe.map(([c,i])=>{let m=Ge.slice(0,c).map(d=>n===2?`${e}1(a.${d},b.${d})`:`${e}1(a.${d},b.${d},c.${d})`).join(",");return n===2?`fn ${e}${c}(a:${i},b:${i})->${i}{return ${i}(${m});}`:`fn ${e}${c}(a:${i},b:${i},c:${i})->${i}{return ${i}(${m});}`})}function Ne(e,n){let c=[];return e.div&&c.push("fn _tslDiv1(a:f32,b:f32)->f32{return a/select(b,select(1e-6,-1e-6,b<0.0),abs(b)<1e-6);}",...G("_tslDiv",2)),e.mod&&c.push("fn _tslMod1(a:f32,b:f32)->f32{let d=select(b,select(1e-6,-1e-6,b<0.0),abs(b)<1e-6);return a-d*floor(a/d);}",...G("_tslMod",2)),e.pow&&c.push("fn _tslPow1(a:f32,b:f32)->f32{let e=clamp(b,-64.0,64.0);if(abs(e)<1e-8){return 1.0;}if(a<=0.0){return 0.0;}return pow(max(a,1e-8),e);}",...G("_tslPow",2)),e.clamp&&c.push("fn _tslClamp1(a:f32,b:f32,c:f32)->f32{return clamp(a,min(b,c),max(b,c));}",...G("_tslClamp",3)),e.smooth&&c.push("fn _tslSmooth1(a:f32,b:f32,c:f32)->f32{let t=_tslClamp1(_tslDiv1(c-a,b-a),0.0,1.0);return t*t*(3.0-2.0*t);}",...G("_tslSmooth",3)),e.atan2&&c.push("fn _tslAtan1(a:f32,b:f32)->f32{return select(atan2(a,b),0.0,abs(a)<1e-8&&abs(b)<1e-8);}",...G("_tslAtan",2)),e.norm&&c.push("fn _tslNorm1(v:f32)->f32{return select(0.0,sign(v),abs(v)>1e-6);}",...xe.map(([i,m])=>`fn _tslNorm${i}(v:${m})->${m}{let l=length(v);return select(${m}(0.0),v/l,l>1e-6);}`)),(e.snoise2||e.snoise3||e.voronoi2)&&c.push("fn _tslM289(x:f32)->f32{return x-floor(x*(1.0/289.0))*289.0;}","fn _tslPerm(x:f32)->f32{return _tslM289((x*34.0+10.0)*x);}"),e.snoise2&&c.push("fn _tslSNoise2(v:vec2<f32>)->f32{let c0=0.21132487;let c1=0.3660254;let c2=-0.57735026;let c3=0.024390243;let s=(v.x+v.y)*c1;var ix=floor(v.x+s);var iy=floor(v.y+s);let t=(ix+iy)*c0;let x0=v.x-ix+t;let y0=v.y-iy+t;let i1x=select(0.0,1.0,x0>y0);let i1y=1.0-i1x;let x1=x0+c0-i1x;let y1=y0+c0-i1y;let x2=x0+c2;let y2=y0+c2;ix=_tslM289(ix);iy=_tslM289(iy);let p0=_tslPerm(_tslPerm(iy)+ix);let p1=_tslPerm(_tslPerm(iy+i1y)+ix+i1x);let p2=_tslPerm(_tslPerm(iy+1.0)+ix+1.0);var m=max(vec3<f32>(0.5)-vec3<f32>(x0*x0+y0*y0,x1*x1+y1*y1,x2*x2+y2*y2),vec3<f32>(0.0));m=m*m;m=m*m;let gx=2.0*fract(vec3<f32>(p0,p1,p2)*c3)-vec3<f32>(1.0);let hy=abs(gx)-vec3<f32>(0.5);let ox=gx-floor(gx+vec3<f32>(0.5));m*=vec3<f32>(1.7928429)-0.85373473*(ox*ox+hy*hy);return clamp(0.5+65.0*dot(m,ox*vec3<f32>(x0,x1,x2)+hy*vec3<f32>(y0,y1,y2)),0.0,1.0);}"),e.snoise3&&c.push("fn _tslSNoise3(v:vec3<f32>)->f32{let c0=0.16666667;let c1=0.33333334;let ns=0.14285715;let s=(v.x+v.y+v.z)*c1;var i0=floor(v.x+s);var i1=floor(v.y+s);var i2=floor(v.z+s);let t=(i0+i1+i2)*c0;let p0=v-vec3<f32>(i0,i1,i2)+vec3<f32>(t);let g=step(p0.yzx,p0.xyz);let l=vec3<f32>(1.0)-g.zxy;let a1=min(g,l);let a2=max(g,l);let p1=p0-a1+vec3<f32>(c0);let p2=p0-a2+vec3<f32>(c1);let p3=p0-vec3<f32>(0.5);i0=_tslM289(i0);i1=_tslM289(i1);i2=_tslM289(i2);let hs=array<f32,4>(_tslPerm(_tslPerm(_tslPerm(i2)+i1)+i0),_tslPerm(_tslPerm(_tslPerm(i2+a1.z)+i1+a1.y)+i0+a1.x),_tslPerm(_tslPerm(_tslPerm(i2+a2.z)+i1+a2.y)+i0+a2.x),_tslPerm(_tslPerm(_tslPerm(i2+1.0)+i1+1.0)+i0+1.0));let qs=array<vec3<f32>,4>(p0,p1,p2,p3);var sum=0.0;for(var k=0;k<4;k=k+1){let pk=hs[k];let j=pk-49.0*floor(pk*ns*ns);let x_=floor(j*ns);let y_=floor(j-7.0*x_);var gx=x_*(2.0/7.0)+(1.0/7.0)-1.0;var gy=y_*(2.0/7.0)+(1.0/7.0)-1.0;let gz=1.0-abs(gx)-abs(gy);let sz=select(0.0,1.0,gz<0.0);gx-=sz*select(-1.0,1.0,gx>=0.0);gy-=sz*select(-1.0,1.0,gy>=0.0);let inv=1.7928429-0.85373473*(gx*gx+gy*gy+gz*gz);let q=qs[k];var m=max(0.6-dot(q,q),0.0);m=m*m;m=m*m;sum+=m*inv*(gx*q.x+gy*q.y+gz*q.z);}return clamp(0.5+21.0*sum,0.0,1.0);}"),e.voronoi2&&c.push("fn _tslVoronoi2(u:vec2<f32>,jit:f32)->f32{let n=floor(u);let fr=u-n;let jt=clamp(jit,0.0,1.0);var md=8.0;for(var dy=-1;dy<=1;dy=dy+1){for(var dx=-1;dx<=1;dx=dx+1){let qx=_tslM289(n.x+f32(dx));let qy=_tslM289(n.y+f32(dy));let hx=fract(_tslPerm(_tslPerm(qx)+qy)*(1.0/41.0));let hy=fract(_tslPerm(_tslPerm(qy+7.0)+qx+13.0)*(1.0/41.0));md=min(md,length(vec2<f32>(f32(dx),f32(dy))+vec2<f32>(0.5)+(vec2<f32>(hx,hy)-vec2<f32>(0.5))*jt-fr));}}return clamp(md,0.0,1.0);}"),e.checker&&c.push("fn _tslChecker(p:vec2<f32>)->f32{return select(0.0,1.0,_tslMod1(floor(p.x)+floor(p.y),2.0)>=1.0);}"),e.fresnel&&c.push("fn _tslFresnel(pw:f32,nw:vec3<f32>,vd:vec3<f32>)->f32{return _tslPow1(1.0-clamp(dot(_tslNorm3(nw),_tslNorm3(vd)),0.0,1.0),max(pw,1e-3));}"),e.triplanar&&c.push("fn _tslTriplanar(p:vec3<f32>,sh:f32,nw:vec3<f32>)->f32{let w=_tslPow3(max(abs(_tslNorm3(nw)),vec3<f32>(1e-4)),vec3<f32>(clamp(sh,0.5,16.0)));return(w.x*_tslSNoise2(p.yz)+w.y*_tslSNoise2(p.xz)+w.z*_tslSNoise2(p.xy))/max(w.x+w.y+w.z,1e-6);}"),e.hueShift&&c.push("fn _tslHueShift(rgb:vec3<f32>,ang:f32)->vec3<f32>{let k=vec3<f32>(0.57735026);let c=cos(ang);let s=sin(ang);return rgb*c+cross(k,rgb)*s+k*dot(k,rgb)*(1.0-c);}"),e.bump&&n&&c.push("fn _tslBump(dhx:f32,dhy:f32,st:f32,nw:vec3<f32>,dx:vec3<f32>,dy:vec3<f32>)->vec3<f32>{let n=_tslNorm3(nw);let r1=cross(dy,n);let r2=cross(n,dx);let det=dot(dx,r1);if(abs(det)<1e-8){return n;}return _tslNorm3(abs(det)*n-st*select(1.0,-1.0,det<0.0)*(dhx*r1+dhy*r2));}"),c.join(`
|
|
58
|
+
`)}var Xe={positionLocal:"_tslPL",positionWorld:"_tslPW",normalLocal:"_tslNL",normalWorld:"_tslNW",viewDirection:"_tslVD",uv:"_tslUV",vertexColor:"_tslVC",instanceIndex:"_tslII"};function ce(e,n){let c=[],i=_=>_==="float"?"f32":`${_}<f32>`,m=(_,s)=>_==="float"?s:`${_}<f32>(${s})`,d=(_,s,t)=>s===t?_:`${t}<f32>(${_})`;for(let _=0;_<e.length;_++){if(!n[_])continue;let s=e[_],t=s.type,x=C(t),u=s.args[0]!==void 0?`_t${s.args[0]}`:"",N=s.args[1]!==void 0?`_t${s.args[1]}`:"",w=s.args[2]!==void 0?`_t${s.args[2]}`:"",g=s.args[0]!==void 0?e[s.args[0]].type:"float",k=s.args[1]!==void 0?e[s.args[1]].type:"float",y=s.args[2]!==void 0?e[s.args[2]].type:"float",L=m(t,"0.0");switch(s.op){case"const":{let h=(s.value??[0]).map(W);L=t==="float"?h[0]:`${t}<f32>(${h.join(",")})`;break}case"builtin":L=s.builtin==="time"?`_tslMod1(tslU.timeAndPad.x,${W(3600)})`:Xe[s.builtin]??"0.0";break;case"uniform":{let h=`tslU.params[${s.uniformSlot??0}]`;L=x===4?h:`${h}.${"xyz".slice(0,x)}`;break}case"swizzle":L=g==="float"?m(t,u):`${u}.${s.mask}`;break;case"construct":L=t==="float"?u:`${t}<f32>(${s.args.map(h=>`_t${h}`).join(",")})`;break;case"neg":L=`(-${u})`;break;case"abs":case"sign":case"floor":case"ceil":case"fract":case"sin":case"cos":L=`${s.op}(${u})`;break;case"sqrt":L=`sqrt(max(${u},${m(t,"0.0")}))`;break;case"exp":L=`exp(clamp(${u},${m(t,"-80.0")},${m(t,"80.0")}))`;break;case"log":L=`log(max(${u},${m(t,"1.0e-8")}))`;break;case"normalize":L=`_tslNorm${x}(${u})`;break;case"add":case"sub":case"mul":{let h=s.op==="add"?"+":s.op==="sub"?"-":"*";L=`(${d(u,g,t)}${h}${d(N,k,t)})`;break}case"div":case"mod":case"pow":case"atan2":{L=`${s.op==="div"?"_tslDiv":s.op==="mod"?"_tslMod":s.op==="pow"?"_tslPow":"_tslAtan"}${x}(${d(u,g,t)},${d(N,k,t)})`;break}case"min":case"max":case"step":L=`${s.op}(${d(u,g,t)},${d(N,k,t)})`;break;case"clamp":L=`_tslClamp${x}(${d(u,g,t)},${d(N,k,t)},${d(w,y,t)})`;break;case"mix":L=`mix(${u},${N},${d(w,y,t)})`;break;case"smoothstep":L=`_tslSmooth${x}(${d(u,g,t)},${d(N,k,t)},${d(w,y,t)})`;break;case"dot":L=g==="float"?`(${u}*${N})`:`dot(${u},${N})`;break;case"length":L=g==="float"?`abs(${u})`:`length(${u})`;break;case"distance":L=g==="float"?`abs(${u}-${N})`:`distance(${u},${N})`;break;case"cross":case"reflect":L=`${s.op}(${u},${N})`;break;case"fresnel":L=`_tslFresnel(${u},_tslNW,_tslVD)`;break;case"simplexNoise2D":L=`_tslSNoise2(${u})`;break;case"simplexNoise3D":L=`_tslSNoise3(${u})`;break;case"voronoi2D":L=`_tslVoronoi2(${u},${N})`;break;case"checker":L=`_tslChecker(${u})`;break;case"triplanar":L=`_tslTriplanar(${u},${N},_tslNW)`;break;case"hueShift":L=`_tslHueShift(${u},${N})`;break;case"bumpFromHeight":L=`_tslBump(dpdx(${u}),-dpdy(${u}),${N},_tslNW,p1,p2)`;break;default:break}c.push(` let _t${_}:${i(t)}=${L};`)}return c.join(`
|
|
59
|
+
`)}function we(e,n,c){let i=`struct TslUniformBlock{params:array<vec4<f32>,16>,timeAndPad:vec4<f32>,};
|
|
60
|
+
`,m=new Uint8Array(e.nodes.length);for(let x=0;x<e.nodes.length;x++)m[x]=e.fragmentReachable[x]||e.vertexReachable[x];let d=e.fragmentReachable.some(x=>x===1),_=Ne(ae(e.nodes,m,e.usesTime),!0),s=n.replace("@group(1) @binding(4) var emTex: texture_2d<f32>;",`@group(1) @binding(4) var emTex: texture_2d<f32>;
|
|
61
|
+
${i}@group(2) @binding(0) var<uniform> tslU: TslUniformBlock;`).replace("@location(3) @interpolate(flat) instIdx: u32,",`@location(3) @interpolate(flat) instIdx: u32,
|
|
62
|
+
@location(4) localPos: vec3<f32>,
|
|
63
|
+
@location(5) localNorm: vec3<f32>,`).replace(`out.instIdx = instIdx;
|
|
64
|
+
return out;`,`out.instIdx = instIdx;
|
|
65
|
+
out.localPos = vec3<f32>(0.0);
|
|
66
|
+
out.localNorm = vec3<f32>(0.0, 1.0, 0.0);
|
|
67
|
+
return out;`);if(_&&(s=s.replace("@vertex",`${_}
|
|
68
|
+
@vertex`)),e.usesVertexPosition&&e.outputs.position!==void 0){let x=`fn _tslEvalPos(_tslPL:vec3<f32>,_tslNL:vec3<f32>,_tslUV:vec2<f32>,_tslVC:vec4<f32>,_tslII:f32,m:mat4x4<f32>)->vec3<f32>{
|
|
69
|
+
let _tslPW=(m*vec4<f32>(_tslPL,1.0)).xyz;let _rn=m[0].xyz*_tslNL.x+m[1].xyz*_tslNL.y+m[2].xyz*_tslNL.z;let _rl=length(_rn);
|
|
70
|
+
let _tslNW=select(vec3<f32>(0.0,1.0,0.0),_rn/_rl,_rl>1e-5);let _tslVD=vec3<f32>(0.0,0.0,1.0);
|
|
71
|
+
${ce(e.nodes,e.vertexReachable)}
|
|
72
|
+
return _t${e.outputs.position};}`;s=s.replace("@vertex",`${x}
|
|
73
|
+
@vertex`).replace("let world = m * vec4<f32>(aPos, 1.0);",`let _tslInstF=f32(instIdx);let _tslNewP=_tslEvalPos(aPos,aNormal,aUV,vec4<f32>(1.0),_tslInstF,m);var _tslNewN=aNormal;
|
|
74
|
+
if(length(aNormal)>1e-4){let _n0=normalize(aNormal);let _tg1=normalize(select(cross(vec3<f32>(1.0,0.0,0.0),_n0),cross(vec3<f32>(0.0,1.0,0.0),_n0),abs(_n0.y)<0.99));let _tg2=cross(_n0,_tg1);let _cn=cross(_tslEvalPos(aPos+_tg1*1e-3,aNormal,aUV,vec4<f32>(1.0),_tslInstF,m)-_tslNewP,_tslEvalPos(aPos+_tg2*1e-3,aNormal,aUV,vec4<f32>(1.0),_tslInstF,m)-_tslNewP);if(length(_cn)>1e-10){_tslNewN=normalize(_cn);}}
|
|
75
|
+
let world = m * vec4<f32>(_tslNewP, 1.0);`).replace(`rawN = (adj0 * aNormal.x + adj1 * aNormal.y + adj2 * aNormal.z) * sgn;
|
|
76
|
+
} else {
|
|
77
|
+
rawN = c0 * aNormal.x + c1 * aNormal.y + c2 * aNormal.z;`,`rawN = (adj0 * _tslNewN.x + adj1 * _tslNewN.y + adj2 * _tslNewN.z) * sgn;
|
|
78
|
+
} else {
|
|
79
|
+
rawN = c0 * _tslNewN.x + c1 * _tslNewN.y + c2 * _tslNewN.z;`).replace(`out.instIdx = instIdx;
|
|
80
|
+
return out;`,`out.instIdx = instIdx;
|
|
81
|
+
out.localPos = _tslNewP;
|
|
82
|
+
out.localNorm = _tslNewN;
|
|
83
|
+
return out;`)}else s=s.replace(`out.instIdx = instIdx;
|
|
84
|
+
return out;`,`out.instIdx = instIdx;
|
|
85
|
+
out.localPos = aPos;
|
|
86
|
+
out.localNorm = aNormal;
|
|
87
|
+
return out;`);if(d){let x=` let _nL=length(in.normal);var _nU:vec3<f32>;if(_nL<1e-4){let _fc=cross(p1,p2);let _fl=length(_fc);_nU=select(vec3<f32>(0.0,1.0,0.0),_fc/_fl,_fl>1e-8);}else{_nU=in.normal/_nL;}if(!frontFacing){_nU=-_nU;}
|
|
88
|
+
let _toC=sc.camAndExp.xyz-in.worldPos;let _cD=length(_toC);let _tslVD=select(vec3<f32>(0.0,0.0,1.0),_toC/_cD,_cD>1e-6);let _tslNW=_nU;
|
|
89
|
+
let _lnL=length(in.localNorm);let _tslNL=select(vec3<f32>(0.0,1.0,0.0),in.localNorm/_lnL,_lnL>1e-5);let _tslPL=in.localPos;let _tslPW=in.worldPos;let _tslUV=in.uv;let _tslVC=vec4<f32>(1.0);let _tslII=f32(in.instIdx);
|
|
90
|
+
${ce(e.nodes,e.fragmentReachable)}`;s=s.replace("let emTexel = textureSample(emTex, linSampler, in.uv);",`let emTexel = textureSample(emTex, linSampler, in.uv);
|
|
91
|
+
${x}`),e.outputs.opacity!==void 0&&(s=s.replace(`if (base.a < 0.004) {
|
|
92
|
+
discard;
|
|
93
|
+
}`,`base.a = clamp(_t${e.outputs.opacity}, 0.0, 1.0);
|
|
94
|
+
if (base.a < 0.004) {
|
|
95
|
+
discard;
|
|
96
|
+
}`)),s=s.replace("if (nInLen < 1e-4 && (pFlags & 2u) != 0u) {","if (false && nInLen < 1e-4 && (pFlags & 2u) != 0u) {"),e.outputs.normalScale!==void 0&&e.outputs.bump===void 0&&(s=s.replace("q = vec3<f32>(q.xy * p.aux.x, q.z);",`q = vec3<f32>(q.xy * max(_t${e.outputs.normalScale}, 0.0), q.z);`));let u=[];if(e.outputs.color!==void 0&&u.push(` base=vec4<f32>(max(_t${e.outputs.color},vec3<f32>(0.0)),base.a);`),e.outputs.emissive!==void 0&&u.push(` em=max(_t${e.outputs.emissive},vec3<f32>(0.0));`),e.outputs.roughness!==void 0&&u.push(` roughness=clamp(_t${e.outputs.roughness},0.04,1.0);`),e.outputs.metalness!==void 0&&u.push(` metallic=clamp(_t${e.outputs.metalness},0.0,1.0);`),e.outputs.bump!==void 0){let N=e.outputs.normalScale!==void 0?`max(_t${e.outputs.normalScale},0.0)`:"1.0";u.push(` let _bn=_t${e.outputs.bump};let _bl=length(_bn);if(_bl>1e-6){let _bm=N+(_bn/_bl-N)*(${N});let _ml=length(_bm);if(_ml>1e-6){N=_bm/_ml;}}`)}u.length>0&&(s=s.replace("metallic = clamp(metallic, 0.0, 1.0);",`metallic = clamp(metallic, 0.0, 1.0);
|
|
97
|
+
${u.join(`
|
|
98
|
+
`)}`))}let t=null;if(e.usesVertexPosition&&e.outputs.position!==void 0){let x=Ne(ae(e.nodes,e.vertexReachable,e.usesTime),!1),u=ce(e.nodes,e.vertexReachable);t=c.replace("@group(0) @binding(1) var<storage, read> parts: array<PartInstance>;",`@group(0) @binding(1) var<storage, read> parts: array<PartInstance>;
|
|
99
|
+
${i}@group(1) @binding(0) var<uniform> tslU: TslUniformBlock;
|
|
100
|
+
${x}`).replace("@location(1) aNormal: vec3<f32>,",`@location(1) aNormal: vec3<f32>,
|
|
101
|
+
@location(2) aUV: vec2<f32>,`).replace("var wp = m * vec4<f32>(aPos, 1.0);",`let _tslPL=aPos;let _tslPW=(m*vec4<f32>(aPos,1.0)).xyz;let _tslNL=aNormal;let _rn=m[0].xyz*aNormal.x+m[1].xyz*aNormal.y+m[2].xyz*aNormal.z;let _rl=length(_rn);let _tslNW=select(vec3<f32>(0.0,1.0,0.0),_rn/_rl,_rl>1e-5);let _tslVD=vec3<f32>(0.0,0.0,1.0);let _tslUV=aUV;let _tslVC=vec4<f32>(1.0);let _tslII=f32(instIdx);
|
|
102
|
+
${u}
|
|
103
|
+
var wp = m * vec4<f32>(_t${e.outputs.position}, 1.0);`)}return{hash:e.hash,pbrWgsl:s,shadowDepthWgsl:t}}var Q=new WeakMap,X=new WeakMap,ke=new WeakMap,ee=new Map,Ke=128;function Ze(e,n){let c=ke.get(n);return c||(c={paramsLoc:e.getUniformLocation(n,"uTslParams[0]")??e.getUniformLocation(n,"uTslParams"),timeLoc:e.getUniformLocation(n,"uTslTime")},ke.set(n,c)),c}function Ye(e,n){let c=new Float32Array(68);for(let i of n.uniformSlots){let m=i.slot*4;c[m]=i.defaultVec4[0],c[m+1]=i.defaultVec4[1],c[m+2]=i.defaultVec4[2],c[m+3]=i.defaultVec4[3]}return{name:e,hash:n.hash,graph:n,buf68:c,usesTime:n.usesTime,usesVertexPosition:n.usesVertexPosition,version:1}}function te(e,n){let c=new Map,i=new Map,m=!1,d=Array.isArray(n?.materials)?n.materials:[];for(let t=0;t<d.length;t++){let x=d[t],u=x?.extensions?.[Z];if(!u)continue;let N=typeof x.name=="string"&&x.name.length>0?x.name:`Material_${t}`,w=Y(u,N);if(!w.ok){console.warn(`[three-live] [${w.issue.code}] ${w.issue.message} Falling back to base PBR material.`);continue}let g=Ye(N,w.graph);c.set(t,g);let k=i.get(N)??[];k.push(g),i.set(N,k),w.graph.usesTime&&(m=!0)}let _={materialsByIndex:c,materialsByName:i,hasAnimatedTsl:m,elapsedSec:0};Q.set(e,_);let s=e.gl;return s&&typeof s=="object"&&X.set(s,_),_}function Te(e,n){let c=Q.get(e);if(!c){let i=n??e.gltf;if(!i)return null;c=te(e,i)}return c}function Je(e){if(e==null)return null;if(typeof e=="number"||Array.isArray(e)||e instanceof Float32Array)return e;if(typeof e=="object"){let n=e;if("r"in n&&"g"in n&&"b"in n)return[Number(n.r),Number(n.g),Number(n.b)];if("x"in n&&"y"in n&&"z"in n&&"w"in n)return[Number(n.x),Number(n.y),Number(n.z),Number(n.w)];if("x"in n&&"y"in n&&"z"in n)return[Number(n.x),Number(n.y),Number(n.z)];if("x"in n&&"y"in n)return[Number(n.x),Number(n.y)]}return null}function Le(e,n,c,i){if(!e||typeof e!="object")return!1;let m=e,d=m.viewer&&typeof m.viewer=="object"?m.viewer:e,_=Te(d);if(!_||_.materialsByIndex.size===0)return!1;let s=n==="*"||n===""?Array.from(_.materialsByIndex.values()):_.materialsByName.get(n)??[];if(s.length===0)return!1;let t=Je(i);if(t===null)return!1;let x=!1;for(let u of s){let N=u.graph.uniformSlotByName.get(c);if(!N)continue;let w=ie(N.type,t);if(!w)continue;let g=N.slot*4;u.buf68[g]=w[0],u.buf68[g+1]=w[1],u.buf68[g+2]=w[2],u.buf68[g+3]=w[3],u.version++,x=!0}return x&&d.animFrameId===null&&typeof d.render=="function"&&d.render(),x}function se(e){if(!e||typeof e!="object")return;let n=e;typeof n.setMaterialUniform!="function"&&(n.setMaterialUniform=(c,i,m)=>Le(e,c,i,m))}function Qe(){let e=le();ze(ne);let n=e.threeNamespace??(typeof globalThis<"u"?globalThis.THREE:void 0);n?ne(n,e):e.authorNamespace||(e.authorNamespace={}),e.tslWgslCompiler=(c,i,m)=>!c||typeof c!="object"?null:we(c,i,m),e.tslMaterialResolver=(c,i)=>i===void 0||i<0?null:Te(c)?.materialsByIndex.get(i)??null,e.tslUsdzSerializer=be}var Pe={name:"tsl",onRegister(){Qe()},onElementConnected(e){se(e),e?.viewer&&(se(e.viewer),e.viewer.gltf&&(te(e.viewer,e.viewer.gltf),e.viewer.recompileRenderProgram?.()))},onBuildPrimitiveVAO(e,n,c,i){let m=c?.material;if(typeof m!="number")return null;let d=i?.materials?.[m]?.extensions?.[Z];if(!d)return null;let _=Y(d,i?.materials?.[m]?.name||`Material_${m}`);return _.ok?{hash:_.graph.hash,materialIndex:m}:null},onResolveShader(e,n,c,i){let m=n?.extensions?.[Z];if(!m)return null;let d=Y(m,n?.name||"Material");if(!d.ok)return console.warn(`[three-live] [${d.issue.code}] ${d.issue.message} Falling back to base PBR shader.`),null;let _=`${d.graph.hash}|${c.length}|${i.length}|${i.slice(-64)}`,s=ee.get(_);if(!s){if(ee.size>=Ke){let t=ee.keys().next().value;t!==void 0&&ee.delete(t)}s=ye(d.graph,c,i),ee.set(_,s)}return s},onLoadModel(e,n){se(e),e?.container&&se(e.container),te(e,n).hasAnimatedTsl&&e&&e.animFrameId===null&&typeof e.start=="function"&&e.opts?.autoStart!==!1&&typeof requestAnimationFrame<"u"&&e.start()},onUpdate(e,n){let c=Q.get(e);if(!c||!c.hasAnimatedTsl)return;let i=Number.isFinite(n)&&n>0?Math.min(n,.25):0;c.elapsedSec=_e(c.elapsedSec+i,3600);for(let m of c.materialsByIndex.values())m.usesTime&&(m.buf68[64]=c.elapsedSec,m.version++)},onBeforeDrawPrimitive(e,n,c,i,m,d){let _=c?.materialIndex;if(typeof _!="number"||!d?.materials?.[_]?.extensions?.[Z])return;let s=Ze(e,n);if(!s.paramsLoc&&!s.timeLoc)return;let t=X.get(e);t||(t=te(e,d),X.set(e,t));let x=t.materialsByIndex.get(_);x&&(s.paramsLoc&&e.uniform4fv(s.paramsLoc,x.buf68,0,64),s.timeLoc&&e.uniform1f(s.timeLoc,x.buf68[64]))},onClearScene(e){Q.delete(e),e?.gl&&typeof e.gl=="object"&&X.delete(e.gl)},onContextRestored(e,n){if(n?.gltf){let c=te(n,n.gltf);e&&typeof e=="object"&&X.set(e,c)}},onDestroy(e,n){Q.delete(n),e&&typeof e=="object"&&X.delete(e)}};Me(Pe);export{Ie as MAX_TSL_NODES,Se as MAX_TSL_UNIFORMS,Z as TSL_EXTENSION_NAME,De as TSL_TIME_PERIOD_SEC,He as TSL_WGSL_UNIFORM_BYTES,ye as compileTslToGlsl,we as compileTslToWgsl,W as fmtShaderFloat,ne as installTslExtension,ie as packUniformVec4,be as serializeTslToUsda,Le as setMaterialUniform,Pe as tslPlugin,_e as tslSafeMod,C as tslTypeArity,ve as tslTypeFromArity,Y as validateTslGraph};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/var ThreeLiveTSL=(()=>{var ne=Object.defineProperty,Ie=Object.getOwnPropertyDescriptor,Se=Object.getOwnPropertyNames,De=Object.prototype.hasOwnProperty,Ve=(e,s)=>{for(var c in s)ne(e,c,{get:s[c],enumerable:!0})},Ue=(e,s,c,i)=>{if(s&&typeof s=="object"||typeof s=="function")for(let u of Se(s))!De.call(e,u)&&u!==c&&ne(e,u,{get:()=>s[u],enumerable:!(i=Ie(s,u))||i.enumerable});return e},Ae=e=>Ue(ne({},"__esModule",{value:!0}),e),fe={};Ve(fe,{MAX_TSL_NODES:()=>Ce,MAX_TSL_UNIFORMS:()=>Re,TSL_EXTENSION_NAME:()=>Z,TSL_TIME_PERIOD_SEC:()=>We,TSL_WGSL_UNIFORM_BYTES:()=>et,compileTslToGlsl:()=>ye,compileTslToWgsl:()=>Ne,fmtShaderFloat:()=>W,installTslExtension:()=>re,packUniformVec4:()=>ce,serializeTslToUsda:()=>ge,setMaterialUniform:()=>Te,tslPlugin:()=>Le,tslSafeMod:()=>me,tslTypeArity:()=>C,tslTypeFromArity:()=>_e,validateTslGraph:()=>K});var le="__THREE_LIVE_REGISTRY__";function oe(){let e=typeof globalThis<"u"?globalThis:{};if(!e[le]){let s={plugins:[],decoders:new Map};try{Object.defineProperty(e,le,{value:s,writable:!1,configurable:!1,enumerable:!1})}catch{e[le]=s}}return e[le]}var it=oe().plugins;function je(e){let s=oe();s.workerExtensionInstallers||(s.workerExtensionInstallers=[]),s.workerExtensionInstallers.includes(e)||s.workerExtensionInstallers.push(e)}function Fe(e){let s=oe().plugins;s.some(c=>c.name===e.name)||s.push(e),e.onRegister?.()}function re(e,s){if(!e||!e.MeshPhysicalMaterial)return null;if(s?.authorNamespace?.TSL&&s?.authorNamespace?.MeshStandardNodeMaterial)return{TSL:s.authorNamespace.TSL,MeshStandardNodeMaterial:s.authorNamespace.MeshStandardNodeMaterial,MeshPhysicalNodeMaterial:s.authorNamespace.MeshPhysicalNodeMaterial};let c=128,i=16,u=0,d=(o,a,l)=>{typeof e.warnUnsupported=="function"&&e.warnUnsupported(`${a} [${o}]`,l)},_=o=>o==="vec4"?4:o==="vec3"?3:o==="vec2"?2:1,n=o=>o===4?"vec4":o===3?"vec3":o===2?"vec2":"float",t=(o,a=0)=>{let l=Number(o);return Number.isFinite(l)?Math.fround(l):a},x=o=>!!(o&&o.isTSLNode===!0),f=(o,a=1)=>{if(x(o))return o;if(typeof o=="number"||typeof o=="boolean"){let l=t(Number(o),0);return a>1?new y("const",n(a),[],new Array(a).fill(l)):new y("const","float",[],[l])}if(typeof o=="string"){let l=new e.Color(o);return new y("const","vec3",[],[t(l.r,1),t(l.g,1),t(l.b,1)])}if(Array.isArray(o)||o instanceof Float32Array){let l=Math.max(1,Math.min(4,o.length)),r=[];for(let $=0;$<l;$++)r.push(t(o[$],0));return new y("const",n(l),[],r)}if(o&&typeof o=="object"){let l=o;if("isColor"in l||"r"in l&&"g"in l&&"b"in l)return new y("const","vec3",[],[t(l.r,1),t(l.g,1),t(l.b,1)]);if("x"in l&&"y"in l)return"z"in l&&"w"in l?new y("const","vec4",[],[t(l.x),t(l.y),t(l.z),t(l.w,1)]):"z"in l?new y("const","vec3",[],[t(l.x),t(l.y),t(l.z)]):new y("const","vec2",[],[t(l.x),t(l.y)])}return new y("const","float",[],[0])},N=(o,a)=>{let l=f(o),r=f(a);if(l._type===r._type)return[l,r,l._type];if(l._type==="float")return[l,r,r._type];if(r._type==="float")return[l,r,l._type];let $=_(l._type),P=_(r._type);if(P>$)r=r.swizzle("xyzw".slice(0,$));else{let T=[r];for(let V=P;V<$;V++)T.push(new y("const","float",[],[0]));r=new y("construct",l._type,T)}return[l,r,l._type]},w=(o,a)=>{let l=f(o);if(l._type===a)return l;let r=_(a),$=_(l._type);if(r===1)return l.swizzle("x");if($===1)return l._op==="const"&&l._val?new y("const",a,[],new Array(r).fill(l._val[0])):new y("construct",a,[l]);if($>r)return l.swizzle("xyzw".slice(0,r));let P=[l];for(let T=$;T<r;T++)P.push(new y("const","float",[],[T===3?1:0]));return new y("construct",a,P)};class y{constructor(a,l,r,$,P,T){this.isTSLNode=!0,this._op=a,this._type=l,this._args=r,$!==void 0&&(this._val=$),P!==void 0&&(this._name=P),T!==void 0&&(this._mask=T)}get nodeType(){return this._type}get value(){return this._uVal}set value(a){this._op==="uniform"&&(this._uVal=a)}swizzle(a){let l=String(a||"x").trim().replace(/[rgba]/g,$=>"xyzw"["rgba".indexOf($)]);if(!/^[xyzw]{1,4}$/.test(l))return d("TL501",`Invalid TSL swizzle '${a}'`,".x"),new y("const","float",[],[0]);let r=_(this._type);if(r===1&&l==="x")return this;for(let $=0;$<l.length;$++)if("xyzw".indexOf(l[$])>=r)return d("TL501",`TSL swizzle '.${a}' out of range`,"0"),new y("const",n(l.length),[],new Array(l.length).fill(0));return new y("swizzle",n(l.length),[this],void 0,void 0,l)}clamp(a=0,l=1){let r=f(a),$=f(l);return(r._type!=="float"||$._type!=="float")&&(r=w(r,this._type),$=w($,this._type)),new y("clamp",this._type,[this,r,$])}saturate(){return this.clamp(0,1)}oneMinus(){return f(1,_(this._type)).sub(this)}mix(a,l){let[r,$,P]=N(this,a),T=f(l);return T._type!=="float"&&T._type!==P&&(T=w(T,P)),new y("mix",P,[r,$,T])}step(a){let l=f(a);return this._type==="float"?new y("step",l._type,[this,l]):new y("step",this._type,[this,w(l,this._type)])}smoothstep(a,l){let r=f(l),$=this,P=f(a);return($._type!=="float"||P._type!=="float")&&($=w($,r._type),P=w(P,r._type)),new y("smoothstep",r._type,[$,P,r])}dot(a){let[l,r]=N(this,a);return new y("dot","float",[l,r])}cross(a){return new y("cross","vec3",[w(this,"vec3"),w(a,"vec3")])}length(){return new y("length","float",[this])}distance(a){let[l,r]=N(this,a);return new y("distance","float",[l,r])}reflect(a){let l=this._type==="float"?w(this,"vec3"):this;return new y("reflect",l._type,[l,w(a,l._type)])}remap(a,l,r,$){let P=this.sub(a).div(f(l).sub(a)).clamp(0,1);return f(r).mix($,P)}}let k=y.prototype;for(let[o,a]of[["x","x"],["y","y"],["z","z"],["w","w"],["r","x"],["g","y"],["b","z"],["a","w"],["xy","xy"],["xz","xz"],["yz","yz"],["xyz","xyz"],["rgb","xyz"],["xyzw","xyzw"],["rgba","xyzw"]])Object.defineProperty(k,o,{get(){return this.swizzle(a)}});let g=["add","sub","mul","div","mod","pow","atan2","min","max"];for(let o of g)k[o]=function(a){let[l,r,$]=N(this,a);return new y(o,$,[l,r])};let L=["neg","abs","sign","floor","ceil","fract","sqrt","exp","log","sin","cos","normalize"];for(let o of L)k[o]=function(){return new y(o,this._type,[this])};let h=(o,a)=>{let l=new y("builtin",a,[],void 0,o),r=()=>l;return Object.setPrototypeOf(r,k),Object.defineProperty(r,"length",{value:k.length,configurable:!0}),Object.assign(r,l),r},S=(o,a)=>{let l=_(o);if(a.length===0)return new y("const",o,[],new Array(l).fill(0));if(a.length===1)return w(a[0],o);let r=a.map(A=>f(A)),$=!0,P=[],T=0;for(let A of r)if(T+=_(A._type),A._op==="const"&&A._val)for(let M of A._val)P.push(M);else $=!1;if($&&P.length>=l)return new y("const",o,[],P.slice(0,l));if(T===l)return new y("construct",o,r);let V=[];for(let A of r){let M=_(A._type);for(let z=0;z<M&&V.length<l;z++)V.push(M===1?A:A.swizzle("xyzw"[z]))}for(;V.length<l;)V.push(new y("const","float",[],[V.length===3?1:0]));return new y("construct",o,V)},D=h("positionWorld","vec3"),p={float:(o=0)=>w(o,"float"),vec2:(...o)=>S("vec2",o),vec3:(...o)=>S("vec3",o),vec4:(...o)=>S("vec4",o),color:(o=16777215,a,l)=>{if(typeof o=="number"&&typeof a=="number"&&typeof l=="number"){let $=new e.Color(o,a,l);return new y("const","vec3",[],[t($.r,1),t($.g,1),t($.b,1)])}if(x(o))return w(o,"vec3");let r=new e.Color(o);return new y("const","vec3",[],[t(r.r,1),t(r.g,1),t(r.b,1)])},uniform:(o=0,a)=>{let l=f(o),r=typeof a=="string"&&a.trim().length>0?a.trim().replace(/[^A-Za-z0-9_]/g,"_").slice(0,63):`uParam_${u++}`;/^[0-9]/.test(r)&&(r="_"+r.slice(0,62)),(r==="__proto__"||r==="constructor"||r==="prototype")&&(r=`uParam_${u++}`);let $=new y("uniform",l._type,[],void 0,r);return $._uVal=o,$},positionLocal:h("positionLocal","vec3"),positionWorld:D,normalLocal:h("normalLocal","vec3"),normalWorld:h("normalWorld","vec3"),viewDirection:h("viewDirection","vec3"),uv:h("uv","vec2"),vertexColor:h("vertexColor","vec4"),instanceIndex:h("instanceIndex","float"),time:h("time","float"),clamp:(o,a=0,l=1)=>f(o).clamp(a,l),saturate:o=>f(o).saturate(),oneMinus:o=>f(o).oneMinus(),mix:(o,a,l)=>f(o).mix(a,l),step:(o,a)=>f(o).step(a),smoothstep:(o,a,l)=>f(o).smoothstep(a,l),dot:(o,a)=>f(o).dot(a),cross:(o,a)=>f(o).cross(a),length:o=>f(o).length(),distance:(o,a)=>f(o).distance(a),reflect:(o,a)=>f(o).reflect(a),remap:(o,a,l,r,$)=>f(o).remap(a,l,r,$),fresnel:(o=2)=>new y("fresnel","float",[w(o,"float")]),simplexNoise2D:o=>new y("simplexNoise2D","float",[w(o,"vec2")]),simplexNoise3D:o=>new y("simplexNoise3D","float",[w(o,"vec3")]),voronoi2D:(o,a=1)=>new y("voronoi2D","float",[w(o,"vec2"),w(a,"float")]),checker:o=>new y("checker","float",[w(o,"vec2")]),triplanar:(o=1,a=4)=>{let l=f(o),r=l._type==="vec3"?l:D.mul(w(l,"float"));return new y("triplanar","float",[r,w(a,"float")])},hueShift:(o,a)=>new y("hueShift","vec3",[w(o,"vec3"),w(a,"float")]),bumpFromHeight:(o,a=1)=>new y("bumpFromHeight","vec3",[w(o,"float"),w(a,"float")]),Loop:()=>(d("TL504","THREE.TSL.Loop","loop-free TSL constant"),new y("const","float",[],[0])),While:()=>(d("TL504","THREE.TSL.While","loop-free TSL constant"),new y("const","float",[],[0]))};for(let o of g)p[o]=(a,l)=>f(a)[o](l);for(let o of L)p[o]=a=>f(a)[o]();class v extends e.MeshPhysicalMaterial{get opacityNode(){return this._opacityNode}set opacityNode(a){this._opacityNode=a,a!=null&&!(Number(this.alphaTest)>0)&&(this.transparent=!0)}constructor(a={}){super(a),this._opacityNode=null,this.isMeshStandardNodeMaterial=!0,this.colorNode=a.colorNode??null,this.opacityNode=a.opacityNode??null,this.emissiveNode=a.emissiveNode??null,this.roughnessNode=a.roughnessNode??null,this.metalnessNode=a.metalnessNode??null,this.normalScaleNode=a.normalScaleNode??null,this.bumpNode=a.bumpNode??null,this.positionNode=a.positionNode??null}_serializeTslGraph(){let a=[["color",this.colorNode,"vec3"],["opacity",this.opacityNode,"float"],["emissive",this.emissiveNode,"vec3"],["roughness",this.roughnessNode,"float"],["metalness",this.metalnessNode,"float"],["normalScale",this.normalScaleNode,"float"],["bump",this.bumpNode,"vec3"],["position",this.positionNode,"vec3"]],l=[];for(let[b,j,F]of a)j!=null&&l.push([b,w(j,F)]);if(l.length===0)return null;let r=[],$={},P=new Map,T=new Set,V="",A="",M=(b,j)=>{if(V)return 0;if(!b||!b.isTSLNode)return V="TL501",A="Invalid non-TSL node in material socket",0;if(T.has(b))return V="TL501",A="Cyclic TSL node reference",0;if(j&&(b._op==="builtin"&&b._name==="viewDirection"||b._op==="fresnel"||b._op==="bumpFromHeight"))return V="TL503",A=`Fragment-only TSL op '${b._name||b._op}' used in positionNode`,0;T.add(b);let F=[];for(let q of b._args)if(F.push(M(q,j)),V)return T.delete(b),0;if(T.delete(b),b._op==="uniform"){let q=b._name||"uParam";if(q in $){if($[q].type!==b._type)return V="TL501",A=`Conflicting types for TSL uniform '${q}' (${$[q].type} vs ${b._type})`,0}else{if(Object.keys($).length>=i)return V="TL502",A=`TSL material exceeds MAX_TSL_UNIFORMS (${i})`,0;let Me=w(b._uVal??0,b._type)._val??[0];$[q]={type:b._type,value:b._type==="float"?Me[0]:Me}}}let X=`${b._op}|${b._type}|${F.join(",")}|${b._val?b._val.join(","):""}|${b._name??""}|${b._mask??""}`,Pe=P.get(X);if(Pe!==void 0)return Pe;if(r.length>=c)return V="TL502",A=`TSL material exceeds MAX_TSL_NODES (${c})`,0;let ze=r.length,te={op:b._op,type:b._type};return F.length>0&&(te.args=F),b._val&&(te.value=[...b._val]),b._name&&(te.name=b._name),b._mask&&(te.mask=b._mask),r.push(te),P.set(X,ze),ze},z={};for(let[b,j]of l)if(z[b]=M(j,b==="position"),V)return d(V,A,"base MeshStandardMaterial factors"),null;let U={version:1,nodes:r,outputs:z};return Object.keys($).length>0&&(U.uniforms=$),U}clone(){let a=new v;for(let l of["name","_em","roughness","metalness","emissiveIntensity","opacity","transparent","wireframe","side","doubleSided","map","normalMap","roughnessMap","metalnessMap","emissiveMap","aoMap","aoMapIntensity","bumpMap","bumpScale","clearcoat","clearcoatRoughness","sheen","sheenRoughness","ior","transmission","thickness","attenuationDistance","dispersion","iridescence","iridescenceIOR","colorNode","opacityNode","emissiveNode","roughnessNode","metalnessNode","normalScaleNode","bumpNode","positionNode"])a[l]=this[l];return a.color.copy(this.color),a.emissive.copy(this.emissive),a.normalScale.copy(this.normalScale),a.sheenColor.copy(this.sheenColor),a.attenuationColor.copy(this.attenuationColor),a}}let m={TSL:p,TSLNode:y,MeshStandardNodeMaterial:v,MeshPhysicalNodeMaterial:v};return s&&typeof s=="object"&&(s.authorNamespace||(s.authorNamespace={}),Object.assign(s.authorNamespace,m)),m}var Z="GOOGLE_material_tsl",Ce=128,Re=16,We=3600,Oe={positionLocal:"vec3",positionWorld:"vec3",normalLocal:"vec3",normalWorld:"vec3",viewDirection:"vec3",uv:"vec2",vertexColor:"vec4",instanceIndex:"float",time:"float"},Ee={color:"vec3",opacity:"float",emissive:"vec3",roughness:"float",metalness:"float",normalScale:"float",bump:"vec3",position:"vec3"},qe=new Set(["neg","abs","sign","floor","ceil","fract","sqrt","exp","log","sin","cos","normalize"]),Be=new Set(["add","sub","mul","div","mod","pow","atan2","min","max"]),pe={cross:[["vec3","vec3"],"vec3"],fresnel:[["float"],"float"],simplexNoise2D:[["vec2"],"float"],checker:[["vec2"],"float"],simplexNoise3D:[["vec3"],"float"],voronoi2D:[["vec2","float"],"float"],triplanar:[["vec3","float"],"float"],hueShift:[["vec3","float"],"vec3"],bumpFromHeight:[["float","float"],"vec3"]},He=/^[A-Za-z_][A-Za-z0-9_]{0,63}$/,Ge=new Set(["__proto__","constructor","prototype"]);function me(e,s){let c=Math.abs(s)<1e-6?s<0?-1e-6:1e-6:s;return e-c*Math.floor(e/c)}function C(e){return e==="vec4"?4:e==="vec3"?3:e==="vec2"?2:1}function _e(e){return e===1?"float":e===2?"vec2":e===3?"vec3":e===4?"vec4":null}function ie(e){return e==="float"||e==="vec2"||e==="vec3"||e==="vec4"}function I(e,s,c="Use loop-free THREE.TSL expressions on MeshStandardNodeMaterial (<= 128 nodes, <= 16 uniforms)."){return{ok:!1,issue:{code:e,message:s,hint:c}}}function E(e,s){for(let c=0;c<s.length;c++)e=Math.imul((e^s.charCodeAt(c))>>>0,16777619)>>>0;return e}var ve=new Float32Array(1),Xe=new Uint32Array(ve.buffer);function Ze(e,s){return ve[0]=s,Math.imul((e^Xe[0])>>>0,16777619)>>>0}function ce(e,s){let c=C(e);if(c===1){let u=typeof s=="number"?s:Array.isArray(s)&&s.length===1?Number(s[0]):NaN;return Number.isFinite(u)?[Math.fround(u),0,0,0]:null}if(!Array.isArray(s)&&!(s instanceof Float32Array)||s.length<c)return null;let i=[0,0,0,0];for(let u=0;u<c;u++){let d=Number(s[u]);if(!Number.isFinite(d))return null;i[u]=Math.fround(d)}return i}function K(e,s="Material"){if(!e||typeof e!="object"||Array.isArray(e))return I("TL501",`Invalid GOOGLE_material_tsl on '${s}'.`);let c=e;if(c.version!==1)return I("TL501",`Unsupported TSL version on '${s}'.`);let i=[],u=new Map,d=c.uniforms;if(d!=null){if(typeof d!="object"||Array.isArray(d))return I("TL501",`Invalid TSL uniforms on '${s}'.`);let h=d,S=Object.keys(h).sort();if(S.length>16)return I("TL502",`TSL '${s}' exceeds MAX_TSL_UNIFORMS (16).`);for(let D=0;D<S.length;D++){let p=S[D],v=h[p],m=v&&!Array.isArray(v)?v.type:null,o=ie(m)?ce(m,v.value):null;if(Ge.has(p)||!He.test(p)||!ie(m)||!o)return I("TL501",`Invalid TSL uniform '${p}' on '${s}'.`);let a={name:p,slot:D,type:m,defaultVec4:o};i.push(a),u.set(p,a)}}let _=c.nodes;if(!Array.isArray(_)||_.length===0)return I("TL501",`Empty TSL nodes on '${s}'.`);if(_.length>128)return I("TL502",`TSL '${s}' exceeds MAX_TSL_NODES (128).`);let n=[],t=2166136261;for(let h of i)t=E(t,`U:${h.slot}:${h.type};`);for(let h=0;h<_.length;h++){let S=_[h];if(!S||typeof S!="object"||Array.isArray(S))return I("TL501",`Invalid TSL node ${h} on '${s}'.`);let D=S,p=D.op,v=D.type;if(p==="loop"||p==="while"||p==="for"||p==="call")return I("TL504",`Unsupported loop/call op '${p}' at node ${h} on '${s}'.`);if(typeof p!="string"||!ie(v))return I("TL501",`Malformed TSL node ${h} on '${s}'.`);let m=p,o=D.args,a=[];if(o!==void 0){if(!Array.isArray(o)||o.length>4)return I("TL501",`Invalid args at node ${h} on '${s}'.`);for(let r of o){if(typeof r!="number"||!Number.isInteger(r)||r<0||r>=h)return I("TL501",`Cyclic or out-of-order arg at node ${h} on '${s}'.`);a.push(r)}}t=E(t,`N${h}:${m}:${v}:`);for(let r of a)t=E(t,`${r},`);let l=a.map(r=>n[r].type);if(m==="const"){let r=C(v),$=D.value;if(a.length!==0||!Array.isArray($)||$.length!==r)return I("TL501",`Invalid const node ${h} on '${s}'.`);let P=[];for(let T=0;T<r;T++){let V=Number($[T]);if(!Number.isFinite(V))return I("TL501",`Non-finite const at node ${h}.`);let A=Math.fround(V);P.push(A),t=Ze(t,A)}n.push({op:m,type:v,args:[],value:P})}else if(m==="builtin"){let r=D.name;if(a.length!==0||typeof r!="string"||Oe[r]!==v)return I("TL501",`Invalid builtin at node ${h}.`);t=E(t,r),n.push({op:m,type:v,args:[],builtin:r})}else if(m==="uniform"){let r=D.name,$=typeof r=="string"?u.get(r):void 0;if(a.length!==0||!$||$.type!==v)return I("TL501",`Invalid uniform ref at node ${h}.`);t=E(t,`S${$.slot}`),n.push({op:m,type:v,args:[],uniformSlot:$.slot})}else if(m==="swizzle"){let r=D.mask;if(a.length!==1||typeof r!="string"||!/^[xyzw]{1,4}$/.test(r)||_e(r.length)!==v)return I("TL501",`Invalid swizzle at node ${h}.`);let $=C(l[0]);for(let P=0;P<r.length;P++)if("xyzw".indexOf(r[P])>=$)return I("TL501",`Swizzle '.${r}' out of bounds at node ${h}.`);t=E(t,r),n.push({op:m,type:v,args:a,mask:r})}else if(m==="construct"){let r=C(v),$=l.reduce((P,T)=>P+C(T),0);if(a.length===0||!(a.length===1&&l[0]==="float")&&$!==r)return I("TL501",`Invalid construct at node ${h}.`);n.push({op:m,type:v,args:a})}else if(qe.has(m)){if(a.length!==1||l[0]!==v)return I("TL501",`Type mismatch on '${m}' at node ${h}.`);n.push({op:m,type:v,args:a})}else if(Be.has(m)){if((a.length===2?l[0]===l[1]?l[0]:l[0]==="float"?l[1]:l[1]==="float"?l[0]:null:null)!==v)return I("TL501",`Type mismatch on '${m}' at node ${h}.`);n.push({op:m,type:v,args:a})}else if(m==="step"){if(a.length!==2||l[0]!==l[1]&&l[0]!=="float"||v!==l[1])return I("TL501",`Invalid 'step' at node ${h}.`);n.push({op:m,type:v,args:a})}else if(m==="clamp"){if(!(a.length===3&&v===l[0]&&(l[1]===l[0]&&l[2]===l[0]||l[1]==="float"&&l[2]==="float")))return I("TL501",`Invalid 'clamp' at node ${h}.`);n.push({op:m,type:v,args:a})}else if(m==="mix"){if(!(a.length===3&&l[0]===l[1]&&v===l[0]&&(l[2]===l[0]||l[2]==="float")))return I("TL501",`Invalid 'mix' at node ${h}.`);n.push({op:m,type:v,args:a})}else if(m==="smoothstep"){if(!(a.length===3&&v===l[2]&&(l[0]===l[2]&&l[1]===l[2]||l[0]==="float"&&l[1]==="float")))return I("TL501",`Invalid 'smoothstep' at node ${h}.`);n.push({op:m,type:v,args:a})}else if(m==="dot"||m==="distance"){if(a.length!==2||l[0]!==l[1]||v!=="float")return I("TL501",`Invalid '${m}' at node ${h}.`);n.push({op:m,type:v,args:a})}else if(m==="length"){if(a.length!==1||v!=="float")return I("TL501",`Invalid 'length' at node ${h}.`);n.push({op:m,type:v,args:a})}else if(m==="reflect"){if(a.length!==2||l[0]!==l[1]||l[0]==="float"||v!==l[0])return I("TL501",`Invalid 'reflect' at node ${h}.`);n.push({op:m,type:v,args:a})}else if(m in pe){let[r,$]=pe[m];if(a.length!==r.length||v!==$||r.some((P,T)=>l[T]!==P))return I("TL501",`Invalid '${m}' at node ${h}.`);n.push({op:m,type:v,args:a})}else return I("TL501",`Unknown TSL op '${String(m)}' at node ${h}.`)}let x=c.outputs;if(!x||typeof x!="object"||Array.isArray(x))return I("TL501",`Missing TSL outputs on '${s}'.`);let f=x,N={},w=0;for(let h of Object.keys(f).sort()){let S=Ee[h];if(!S)return I("TL501",`Unknown TSL socket '${h}' on '${s}'.`);let D=f[h];if(D!=null){if(typeof D!="number"||!Number.isInteger(D)||D<0||D>=n.length||n[D].type!==S)return I("TL501",`Invalid TSL output '${h}' on '${s}'.`);N[h]=D,w++,t=E(t,`O:${h}=${D};`)}}if(w===0)return I("TL501",`No active TSL outputs on '${s}'.`);let y=new Uint8Array(n.length),k=new Uint8Array(n.length);for(let[h,S]of Object.entries(N))h==="position"?k[S]=1:y[S]=1;for(let h=n.length-1;h>=0;h--){if(y[h])for(let S of n[h].args)y[S]=1;if(k[h])for(let S of n[h].args)k[S]=1}let g=!1,L=!1;for(let h=0;h<n.length;h++){let S=n[h];if(k[h]&&(S.op==="builtin"&&S.builtin==="viewDirection"||S.op==="fresnel"||S.op==="bumpFromHeight"))return I("TL503",`Fragment-only TSL op '${S.builtin||S.op}' used in positionNode on '${s}'.`);(y[h]||k[h])&&(S.op==="builtin"&&S.builtin==="time"&&(g=!0),S.op==="bumpFromHeight"&&(L=!0))}return{ok:!0,graph:{nodes:n,outputs:N,uniformSlots:i,uniformSlotByName:u,hash:(t>>>0).toString(16).padStart(8,"0"),usesTime:g,usesVertexPosition:N.position!==void 0,usesDerivatives:L,fragmentReachable:y,vertexReachable:k}}}function W(e){if(!Number.isFinite(e)||e===0)return"0.0";let s=Math.fround(e),c=Math.abs(s);if(c>=1e-4&&c<1e6){let u=Number(s.toPrecision(7)).toString();return u.includes(".")?u:`${u}.0`}let i=s.toExponential(6).split("e");return`${i[0].replace(/(\.\d*?[1-9])0+$/,"$1").replace(/\.0+$/,".0")}e${i[1]}`}function se(e,s,c){let i={div:!1,mod:c,pow:!1,clamp:!1,smooth:!1,atan2:!1,norm:!1,fresnel:!1,snoise2:!1,snoise3:!1,voronoi2:!1,checker:!1,triplanar:!1,hueShift:!1,bump:!1};for(let u=0;u<e.length;u++)if(s[u])switch(e[u].op){case"div":i.div=!0;break;case"mod":i.mod=!0;break;case"pow":i.pow=!0;break;case"clamp":i.clamp=!0;break;case"smoothstep":i.smooth=!0,i.div=!0,i.clamp=!0;break;case"atan2":i.atan2=!0;break;case"normalize":i.norm=!0;break;case"fresnel":i.fresnel=!0,i.pow=!0,i.norm=!0;break;case"simplexNoise2D":i.snoise2=!0;break;case"simplexNoise3D":i.snoise3=!0;break;case"voronoi2D":i.voronoi2=!0;break;case"checker":i.checker=!0,i.mod=!0;break;case"triplanar":i.triplanar=!0,i.snoise2=!0,i.pow=!0,i.norm=!0;break;case"hueShift":i.hueShift=!0;break;case"bumpFromHeight":i.bump=!0,i.norm=!0;break;default:break}return i}var Ke=["x","y","z","w"],de=[[2,"vec2"],[3,"vec3"],[4,"vec4"]];function B(e,s){return de.map(([c,i])=>{let u=Ke.slice(0,c).map(d=>s===2?`${e}(a.${d},b.${d})`:`${e}(a.${d},b.${d},c.${d})`).join(",");return s===2?`${i} ${e}(${i} a,${i} b){return ${i}(${u});}`:`${i} ${e}(${i} a,${i} b,${i} c){return ${i}(${u});}`})}function $e(e,s){let c=[];return e.div&&c.push("float _tslDiv(float a,float b){return a/(abs(b)<1e-6?(b<0.0?-1e-6:1e-6):b);}",...B("_tslDiv",2)),e.mod&&c.push("float _tslMod(float a,float b){float d=abs(b)<1e-6?(b<0.0?-1e-6:1e-6):b;return a-d*floor(a/d);}",...B("_tslMod",2)),e.pow&&c.push("float _tslPow(float a,float b){float e=clamp(b,-64.0,64.0);return abs(e)<1e-8?1.0:(a<=0.0?0.0:pow(max(a,1e-8),e));}",...B("_tslPow",2)),e.clamp&&c.push("float _tslClamp(float a,float b,float c){return clamp(a,min(b,c),max(b,c));}",...B("_tslClamp",3)),e.smooth&&c.push("float _tslSmooth(float a,float b,float c){float t=_tslClamp(_tslDiv(c-a,b-a),0.0,1.0);return t*t*(3.0-2.0*t);}",...B("_tslSmooth",3)),e.atan2&&c.push("float _tslAtan2(float a,float b){return(abs(a)<1e-8&&abs(b)<1e-8)?0.0:atan(a,b);}",...B("_tslAtan2",2)),e.norm&&c.push("float _tslNorm(float v){return abs(v)>1e-6?sign(v):0.0;}",...de.map(([,i])=>`${i} _tslNorm(${i} v){float l=length(v);return l>1e-6?v/l:${i}(0.0);}`)),(e.snoise2||e.snoise3||e.voronoi2)&&c.push("float _tslM289(float x){return x-floor(x*(1.0/289.0))*289.0;}","float _tslPerm(float x){return _tslM289((x*34.0+10.0)*x);}"),e.snoise2&&c.push("float _tslSNoise2(vec2 v){const float c0=0.21132487,c1=0.3660254,c2=-0.57735026,c3=0.024390243;float s=(v.x+v.y)*c1,ix=floor(v.x+s),iy=floor(v.y+s),t=(ix+iy)*c0,x0=v.x-ix+t,y0=v.y-iy+t,i1x=x0>y0?1.0:0.0,i1y=1.0-i1x,x1=x0+c0-i1x,y1=y0+c0-i1y,x2=x0+c2,y2=y0+c2;ix=_tslM289(ix);iy=_tslM289(iy);float p0=_tslPerm(_tslPerm(iy)+ix),p1=_tslPerm(_tslPerm(iy+i1y)+ix+i1x),p2=_tslPerm(_tslPerm(iy+1.0)+ix+1.0);vec3 m=max(0.5-vec3(x0*x0+y0*y0,x1*x1+y1*y1,x2*x2+y2*y2),0.0);m=m*m;m=m*m;vec3 gx=2.0*fract(vec3(p0,p1,p2)*c3)-1.0,hy=abs(gx)-0.5,ox=gx-floor(gx+0.5);m*=1.7928429-0.85373473*(ox*ox+hy*hy);return clamp(0.5+65.0*dot(m,ox*vec3(x0,x1,x2)+hy*vec3(y0,y1,y2)),0.0,1.0);}"),e.snoise3&&c.push("float _tslSNoise3(vec3 v){const float c0=0.16666667,c1=0.33333334,ns=0.14285715;float s=(v.x+v.y+v.z)*c1,i0=floor(v.x+s),i1=floor(v.y+s),i2=floor(v.z+s),t=(i0+i1+i2)*c0;vec3 p0=v-vec3(i0,i1,i2)+t,g=step(p0.yzx,p0.xyz),l=1.0-g.zxy,a1=min(g,l),a2=max(g,l),p1=p0-a1+c0,p2=p0-a2+c1,p3=p0-0.5;i0=_tslM289(i0);i1=_tslM289(i1);i2=_tslM289(i2);vec4 hs=vec4(_tslPerm(_tslPerm(_tslPerm(i2)+i1)+i0),_tslPerm(_tslPerm(_tslPerm(i2+a1.z)+i1+a1.y)+i0+a1.x),_tslPerm(_tslPerm(_tslPerm(i2+a2.z)+i1+a2.y)+i0+a2.x),_tslPerm(_tslPerm(_tslPerm(i2+1.0)+i1+1.0)+i0+1.0));float sum=0.0;for(int k=0;k<4;k++){float pk=hs[k],j=pk-49.0*floor(pk*ns*ns),x_=floor(j*ns),y_=floor(j-7.0*x_),gx=x_*(2.0/7.0)+(1.0/7.0)-1.0,gy=y_*(2.0/7.0)+(1.0/7.0)-1.0,gz=1.0-abs(gx)-abs(gy),sz=gz<0.0?1.0:0.0;gx-=sz*(gx>=0.0?1.0:-1.0);gy-=sz*(gy>=0.0?1.0:-1.0);float inv=1.7928429-0.85373473*(gx*gx+gy*gy+gz*gz);vec3 q=k==0?p0:(k==1?p1:(k==2?p2:p3));float m=max(0.6-dot(q,q),0.0);m=m*m;m=m*m;sum+=m*inv*(gx*q.x+gy*q.y+gz*q.z);}return clamp(0.5+21.0*sum,0.0,1.0);}"),e.voronoi2&&c.push("float _tslVoronoi2(vec2 u,float jit){vec2 n=floor(u),fr=u-n;float jt=clamp(jit,0.0,1.0),md=8.0;for(int dy=-1;dy<=1;dy++)for(int dx=-1;dx<=1;dx++){float qx=_tslM289(n.x+float(dx)),qy=_tslM289(n.y+float(dy)),hx=fract(_tslPerm(_tslPerm(qx)+qy)*(1.0/41.0)),hy=fract(_tslPerm(_tslPerm(qy+7.0)+qx+13.0)*(1.0/41.0));md=min(md,length(vec2(float(dx),float(dy))+0.5+(vec2(hx,hy)-0.5)*jt-fr));}return clamp(md,0.0,1.0);}"),e.checker&&c.push("float _tslChecker(vec2 p){return _tslMod(floor(p.x)+floor(p.y),2.0)>=1.0?1.0:0.0;}"),e.fresnel&&c.push("float _tslFresnel(float pw,vec3 nw,vec3 vd){return _tslPow(1.0-clamp(dot(_tslNorm(nw),_tslNorm(vd)),0.0,1.0),max(pw,1e-3));}"),e.triplanar&&c.push("float _tslTriplanar(vec3 p,float sh,vec3 nw){vec3 w=_tslPow(max(abs(_tslNorm(nw)),vec3(1e-4)),vec3(clamp(sh,0.5,16.0)));return(w.x*_tslSNoise2(p.yz)+w.y*_tslSNoise2(p.xz)+w.z*_tslSNoise2(p.xy))/max(w.x+w.y+w.z,1e-6);}"),e.hueShift&&c.push("vec3 _tslHueShift(vec3 rgb,float ang){const vec3 k=vec3(0.57735026);float c=cos(ang),s=sin(ang);return rgb*c+cross(k,rgb)*s+k*dot(k,rgb)*(1.0-c);}"),e.bump&&s&&c.push("vec3 _tslBump(float h,float st,vec3 nw,vec3 wp){vec3 n=_tslNorm(nw),dx=dFdx(wp),dy=dFdy(wp),r1=cross(dy,n),r2=cross(n,dx);float det=dot(dx,r1);return abs(det)<1e-8?n:_tslNorm(abs(det)*n-st*(det<0.0?-1.0:1.0)*(dFdx(h)*r1+dFdy(h)*r2));}"),c.join(`
|
|
17
|
+
`)}var Ye={positionLocal:"_tslPL",positionWorld:"_tslPW",normalLocal:"_tslNL",normalWorld:"_tslNW",viewDirection:"_tslVD",uv:"_tslUV",vertexColor:"_tslVC",instanceIndex:"_tslII"};function he(e,s){let c=[],i=(d,_)=>d==="float"?_:`${d}(${_})`,u=(d,_,n)=>_===n?d:`${n}(${d})`;for(let d=0;d<e.length;d++){if(!s[d])continue;let _=e[d],n=_.type,t=_.args[0]!==void 0?`_t${_.args[0]}`:"",x=_.args[1]!==void 0?`_t${_.args[1]}`:"",f=_.args[2]!==void 0?`_t${_.args[2]}`:"",N=_.args[0]!==void 0?e[_.args[0]].type:"float",w=_.args[1]!==void 0?e[_.args[1]].type:"float",y=_.args[2]!==void 0?e[_.args[2]].type:"float",k=i(n,"0.0");switch(_.op){case"const":{let g=(_.value??[0]).map(W);k=n==="float"?g[0]:`${n}(${g.join(",")})`;break}case"builtin":k=_.builtin==="time"?`_tslMod(uTslTime,${W(3600)})`:Ye[_.builtin]??"0.0";break;case"uniform":{let g=`uTslParams[${_.uniformSlot??0}]`,L=C(n);k=L===4?g:`${g}.${"xyz".slice(0,L)}`;break}case"swizzle":k=N==="float"?i(n,t):`${t}.${_.mask}`;break;case"construct":k=n==="float"?t:`${n}(${_.args.map(g=>`_t${g}`).join(",")})`;break;case"neg":k=`(-${t})`;break;case"abs":case"sign":case"floor":case"ceil":case"fract":case"sin":case"cos":k=`${_.op}(${t})`;break;case"sqrt":k=`sqrt(max(${t},${i(n,"0.0")}))`;break;case"exp":k=`exp(clamp(${t},${i(n,"-80.0")},${i(n,"80.0")}))`;break;case"log":k=`log(max(${t},${i(n,"1.0e-8")}))`;break;case"normalize":k=`_tslNorm(${t})`;break;case"add":case"sub":case"mul":{let g=_.op==="add"?"+":_.op==="sub"?"-":"*";k=`(${u(t,N,n)}${g}${u(x,w,n)})`;break}case"div":case"mod":case"pow":case"atan2":{k=`${_.op==="div"?"_tslDiv":_.op==="mod"?"_tslMod":_.op==="pow"?"_tslPow":"_tslAtan2"}(${u(t,N,n)},${u(x,w,n)})`;break}case"min":case"max":case"step":k=`${_.op}(${u(t,N,n)},${u(x,w,n)})`;break;case"clamp":k=`_tslClamp(${u(t,N,n)},${u(x,w,n)},${u(f,y,n)})`;break;case"mix":k=`mix(${t},${x},${u(f,y,n)})`;break;case"smoothstep":k=`_tslSmooth(${u(t,N,n)},${u(x,w,n)},${u(f,y,n)})`;break;case"dot":k=N==="float"?`(${t}*${x})`:`dot(${t},${x})`;break;case"length":k=N==="float"?`abs(${t})`:`length(${t})`;break;case"distance":k=N==="float"?`abs(${t}-${x})`:`distance(${t},${x})`;break;case"cross":case"reflect":k=`${_.op}(${t},${x})`;break;case"fresnel":k=`_tslFresnel(${t},_tslNW,_tslVD)`;break;case"simplexNoise2D":k=`_tslSNoise2(${t})`;break;case"simplexNoise3D":k=`_tslSNoise3(${t})`;break;case"voronoi2D":k=`_tslVoronoi2(${t},${x})`;break;case"checker":k=`_tslChecker(${t})`;break;case"triplanar":k=`_tslTriplanar(${t},${x},_tslNW)`;break;case"hueShift":k=`_tslHueShift(${t},${x})`;break;case"bumpFromHeight":k=`_tslBump(${t},${x},_tslNW,_tslPW)`;break;default:break}c.push(`${n} _t${d}=${k};`)}return c.join(`
|
|
18
|
+
`)}function ye(e,s,c){let i=`uniform vec4 uTslParams[16];
|
|
19
|
+
uniform float uTslTime;`,u=e.fragmentReachable.some(t=>t===1),d=s.replace("out vec2 vUV;",`${i}
|
|
20
|
+
out vec2 vUV;
|
|
21
|
+
out vec3 vTslLP,vTslLN;
|
|
22
|
+
flat out float vTslII;`),_=d.includes("morphedPos")?"morphedPos":"aPosition";if(e.usesVertexPosition&&e.outputs.position!==void 0){let t=$e(se(e.nodes,e.vertexReachable,e.usesTime),!1),x=he(e.nodes,e.vertexReachable),f=`${t}
|
|
23
|
+
vec3 _tslEvalPos(vec3 _tslPL,vec3 _tslNL,vec2 _tslUV,vec4 _tslVC,float _tslII,mat4 _m,mat3 _nm){
|
|
24
|
+
vec3 _tslPW=(_m*vec4(_tslPL,1.0)).xyz,_rn=_nm*_tslNL,_tslNW=length(_rn)>1e-5?normalize(_rn):vec3(0.0,1.0,0.0),_tslVD=vec3(0.0,0.0,1.0);
|
|
25
|
+
${x}
|
|
26
|
+
return _t${e.outputs.position};}`;d=d.replace("void main(){",`${f}
|
|
27
|
+
void main(){`);let N=`vec2 _tslScUV=uUvTransform.zw;if(_tslScUV.x==0.0&&_tslScUV.y==0.0)_tslScUV=vec2(1.0);vec2 _tslVtxUV=aUV*_tslScUV+uUvTransform.xy;
|
|
28
|
+
float _tslInstF=float(gl_InstanceID);vec3 _tslBaseP=${_},_tslNewP=_tslEvalPos(_tslBaseP,aNormal,_tslVtxUV,vInstTint,_tslInstF,uModel,uNormalMatrix),_tslNewN=aNormal;
|
|
29
|
+
if(length(aNormal)>1e-4){vec3 _n0=normalize(aNormal),_tg1=normalize(abs(_n0.y)<0.99?cross(vec3(0.0,1.0,0.0),_n0):cross(vec3(1.0,0.0,0.0),_n0)),_tg2=cross(_n0,_tg1),_cn=cross(_tslEvalPos(_tslBaseP+_tg1*1e-3,aNormal,_tslVtxUV,vInstTint,_tslInstF,uModel,uNormalMatrix)-_tslNewP,_tslEvalPos(_tslBaseP+_tg2*1e-3,aNormal,_tslVtxUV,vInstTint,_tslInstF,uModel,uNormalMatrix)-_tslNewP);if(length(_cn)>1e-10)_tslNewN=normalize(_cn);}
|
|
30
|
+
vTslLP=_tslNewP;vTslLN=_tslNewN;vTslII=_tslInstF;vec3 _rnDisp=uNormalMatrix*_tslNewN;vNormal=length(_rnDisp)>1e-5?normalize(_rnDisp):vec3(0.0);`;d.includes(`vec4 world=uModel*(skinMatrix*vec4(${_},1.0));`)?d=d.replace(`vec4 world=uModel*(skinMatrix*vec4(${_},1.0));`,`${N}
|
|
31
|
+
vec4 world=uModel*(skinMatrix*vec4(_tslNewP,1.0));`).replace(`gl_Position=uMVP*(skinMatrix*vec4(${_},1.0));`,"gl_Position=uMVP*(skinMatrix*vec4(_tslNewP,1.0));"):d=d.replace(`vec4 world=uModel*vec4(${_},1.0);`,`${N}
|
|
32
|
+
vec4 world=uModel*vec4(_tslNewP,1.0);`).replace(`gl_Position=uMVP*vec4(${_},1.0);`,"gl_Position=uMVP*vec4(_tslNewP,1.0);")}else d=d.replace("gl_Position=",`vTslLP=${_};vTslLN=aNormal;vTslII=float(gl_InstanceID);
|
|
33
|
+
gl_Position=`);let n=c.replace("in vec2 vUV;",`${i}
|
|
34
|
+
in vec2 vUV;
|
|
35
|
+
in vec3 vTslLP,vTslLN;
|
|
36
|
+
flat in float vTslII;`);if(u){let t=$e(se(e.nodes,e.fragmentReachable,e.usesTime),!0),x=he(e.nodes,e.fragmentReachable);t&&(n=n.replace("void main(){",`${t}
|
|
37
|
+
void main(){`)),e.outputs.opacity!==void 0&&(n=n.replace("if(base.a<0.004)discard;","")),n=n.replace("if(length(vNormal)<1e-4&&uHasTexture==1){","if(false&&length(vNormal)<1e-4&&uHasTexture==1){");let f=[];if(e.outputs.color!==void 0&&f.push(`base.rgb=max(_t${e.outputs.color},vec3(0.0))*vInstTint.rgb;`),e.outputs.opacity!==void 0&&f.push(`base.a=clamp(_t${e.outputs.opacity},0.0,1.0)*vInstTint.a;`,"if(base.a<0.004)discard;"),e.outputs.emissive!==void 0&&f.push(`em=max(_t${e.outputs.emissive},vec3(0.0));`),e.outputs.roughness!==void 0&&f.push(`roughness=clamp(_t${e.outputs.roughness},0.04,1.0);`),e.outputs.metalness!==void 0&&f.push(`metallic=clamp(_t${e.outputs.metalness},0.0,1.0);`),e.outputs.normalScale!==void 0&&e.outputs.bump===void 0&&(n=n.replace("if(uHasNormalTexture==1){",`vec3 _tslBaseN=N;
|
|
38
|
+
if(uHasNormalTexture==1){`),f.push(`if(uHasNormalTexture==1){N=perturbNormal(_tslBaseN,V,vUV,uNormalTexture,max(_t${e.outputs.normalScale},0.0));}`)),e.outputs.bump!==void 0){let N=e.outputs.normalScale!==void 0?`max(_t${e.outputs.normalScale},0.0)`:"1.0";f.push(`{vec3 _bn=_t${e.outputs.bump};float _bl=length(_bn);if(_bl>1e-6){vec3 _bm=N+(_bn/_bl-N)*(${N});float _ml=length(_bm);if(_ml>1e-6)N=_bm/_ml;}}`)}n=n.replace("metallic=clamp(metallic,0.0,1.0);",`metallic=clamp(metallic,0.0,1.0);
|
|
39
|
+
{vec3 _tslPL=vTslLP,_tslPW=vWorldPos,_tslNL=length(vTslLN)>1e-5?normalize(vTslLN):vec3(0.0,1.0,0.0),_tslNW=N,_tslVD=V;vec2 _tslUV=vUV;vec4 _tslVC=vInstTint;float _tslII=vTslII;
|
|
40
|
+
${x}
|
|
41
|
+
${f.join(`
|
|
42
|
+
`)}}`)}return{vs:d,fs:n}}var Je={float:"float",vec2:"float2",vec3:"float3",vec4:"float4"},Qe={float:"float",vec2:"vector2",vec3:"vector3",vec4:"vector4"};function R(e){return Je[e]||"float"}function O(e){return Qe[e]||"float"}function Y(e,s){if(s===1)return W(e[0]??0);let c=[];for(let i=0;i<s;i++)c.push(W(e[i]??0));return`(${c.join(", ")})`}function ge(e,s,c=""){let i;if(e&&typeof e=="object"&&"fragmentReachable"in e)i=e;else{let p=K(e,s);if(!p.ok)return null;i=p.graph}if(!i.fragmentReachable.some(p=>p===1))return null;let u=`/Root/Materials/${(s||"Material").replace(/[^A-Za-z0-9_]/g,"_").replace(/^([0-9])/,"_$1")||"Material"}/TSLGraph`,d=[],_=[],n=0;for(let p of i.uniformSlots){let v=R(p.type),m=C(p.type);_.push(`${v} inputs:u_${p.slot} = ${Y(p.defaultVec4,m)}`)}let t=(p,v,m,o)=>(d.push(`def Shader "${p}"
|
|
43
|
+
{
|
|
44
|
+
uniform token info:id = "${v}"
|
|
45
|
+
${o.join(`
|
|
46
|
+
`)}${o.length>0?`
|
|
47
|
+
`:""}${m} outputs:out
|
|
48
|
+
}`),`${u}/${p}.outputs:out`),x=(p,v)=>{let m=C(p),o=`C_${n++}`;return t(o,`ND_constant_${O(p)}`,R(p),[`${R(p)} inputs:value = ${Y(v,m)}`])},f=(p,v)=>{if(v==="float")return p;let m=C(v),o=`B_${n++}`,a=[];for(let l=1;l<=m;l++)a.push(`float inputs:in${l}.connect = <${p}>`);return t(o,`ND_combine${m}_${O(v)}`,R(v),a)},N=(p,v,m)=>v===m?p:f(p,m),w=(p,v,m)=>{if(v==="float")return p;let o=`E_${n++}`;return t(o,`ND_extract_${O(v)}`,"float",[`${R(v)} inputs:in.connect = <${p}>`,`int inputs:index = ${m}`])},y=new Map,k=p=>{let v=y.get(p);if(v)return v;let m="";switch(p){case"positionLocal":m=t("BI_posLocal","ND_position_vector3","float3",['string inputs:space = "object"']);break;case"positionWorld":m=t("BI_posWorld","ND_position_vector3","float3",['string inputs:space = "world"']);break;case"normalLocal":m=t("BI_normLocal","ND_normal_vector3","float3",['string inputs:space = "object"']);break;case"normalWorld":m=t("BI_normWorld","ND_normal_vector3","float3",['string inputs:space = "world"']);break;case"viewDirection":{let o=t("BI_rawView","ND_viewdirection_vector3","float3",['string inputs:space = "world"']);m=t("BI_viewDir","ND_multiply_vector3FA","float3",[`float3 inputs:in1.connect = <${o}>`,"float inputs:in2 = -1.0"]);break}case"uv":m=t("BI_uv","ND_texcoord_vector2","float2",["int inputs:index = 0"]);break;case"vertexColor":{let o=t("BI_vcCol","ND_geomcolor_color4","color4f",["int inputs:index = 0"]);m=t("BI_vcVec","ND_convert_color4_vector4","float4",[`color4f inputs:in.connect = <${o}>`]);break}case"instanceIndex":m=x("float",[0]);break;case"time":{let o=t("BI_rawTime","ND_time_float","float",[]);m=t("BI_time","ND_modulo_float","float",[`float inputs:in1.connect = <${o}>`,`float inputs:in2 = ${W(3600)}`]);break}default:m=x("float",[0]);break}return y.set(p,m),m},g=new Array(i.nodes.length).fill("");for(let p=0;p<i.nodes.length;p++){if(!i.fragmentReachable[p])continue;let v=i.nodes[p],m=v.type,o=R(m),a=O(m),l=C(m),r=v.args[0]!==void 0?g[v.args[0]]:"",$=v.args[1]!==void 0?g[v.args[1]]:"",P=v.args[2]!==void 0?g[v.args[2]]:"",T=v.args[0]!==void 0?i.nodes[v.args[0]].type:"float",V=v.args[1]!==void 0?i.nodes[v.args[1]].type:"float",A=v.args[2]!==void 0?i.nodes[v.args[2]].type:"float",M=`N${p}`;switch(v.op){case"const":g[p]=t(M,`ND_constant_${a}`,o,[`${o} inputs:value = ${Y(v.value??[0],l)}`]);break;case"builtin":g[p]=k(v.builtin??"time");break;case"uniform":g[p]=`${u}.inputs:u_${v.uniformSlot??0}`;break;case"swizzle":{let z=v.mask??"x";if(T==="float")g[p]=f(r,m);else if(z.length===1)g[p]=w(r,T,"xyzw".indexOf(z[0]));else{let U=Array.from(z).map(b=>w(r,T,"xyzw".indexOf(b)));g[p]=t(M,`ND_combine${z.length}_${a}`,o,U.map((b,j)=>`float inputs:in${j+1}.connect = <${b}>`))}break}case"construct":{if(m==="float")g[p]=r;else if(v.args.length===1&&T==="float")g[p]=f(r,m);else{let z=[];for(let U of v.args){let b=i.nodes[U].type,j=g[U],F=C(b);if(F===1)z.push(j);else for(let X=0;X<F;X++)z.push(w(j,b,X))}g[p]=t(M,`ND_combine${l}_${a}`,o,z.slice(0,l).map((U,b)=>`float inputs:in${b+1}.connect = <${U}>`))}break}case"neg":{g[p]=t(M,`ND_multiply_${a}`,o,[`${o} inputs:in1.connect = <${r}>`,`${o} inputs:in2 = ${Y([-1,-1,-1,-1],l)}`]);break}case"abs":case"sign":case"floor":case"ceil":case"sqrt":case"exp":case"sin":case"cos":case"normalize":{let z=v.op==="abs"?"absval":v.op;v.op==="normalize"&&m==="float"?g[p]=t(M,"ND_sign_float","float",[`float inputs:in.connect = <${r}>`]):g[p]=t(M,`ND_${z}_${a}`,o,[`${o} inputs:in.connect = <${r}>`]);break}case"log":g[p]=t(M,`ND_ln_${a}`,o,[`${o} inputs:in.connect = <${r}>`]);break;case"fract":{g[p]=t(M,`ND_modulo_${a}`,o,[`${o} inputs:in1.connect = <${r}>`,`${o} inputs:in2 = ${Y([1,1,1,1],l)}`]);break}case"add":case"sub":case"mul":case"div":case"mod":case"pow":case"atan2":case"min":case"max":{let z={add:"add",sub:"subtract",mul:"multiply",div:"divide",mod:"modulo",pow:"power",atan2:"atan2",min:"min",max:"max"},U=N(r,T,m),b=N($,V,m);g[p]=t(M,`ND_${z[v.op]}_${a}`,o,[`${o} inputs:in1.connect = <${U}>`,`${o} inputs:in2.connect = <${b}>`]);break}case"step":{let z=N(r,T,m);if(m==="float")g[p]=t(M,"ND_ifgreater_float","float",[`float inputs:value1.connect = <${$}>`,`float inputs:value2.connect = <${z}>`,"float inputs:in1 = 1.0","float inputs:in2 = 0.0"]);else{let U=[];for(let b=0;b<l;b++){let j=w(z,m,b),F=w($,m,b);U.push(t(`S_${n++}`,"ND_ifgreater_float","float",[`float inputs:value1.connect = <${F}>`,`float inputs:value2.connect = <${j}>`,"float inputs:in1 = 1.0","float inputs:in2 = 0.0"]))}g[p]=t(M,`ND_combine${l}_${a}`,o,U.map((b,j)=>`float inputs:in${j+1}.connect = <${b}>`))}break}case"clamp":{let z=N($,V,m),U=N(P,A,m);g[p]=t(M,`ND_clamp_${a}`,o,[`${o} inputs:in.connect = <${r}>`,`${o} inputs:low.connect = <${z}>`,`${o} inputs:high.connect = <${U}>`]);break}case"mix":{let z=N(P,A,m);g[p]=t(M,`ND_mix_${a}`,o,[`${o} inputs:bg.connect = <${r}>`,`${o} inputs:fg.connect = <${$}>`,`${o} inputs:mix.connect = <${z}>`]);break}case"smoothstep":{let z=N(r,T,m),U=N($,V,m);g[p]=t(M,`ND_smoothstep_${a}`,o,[`${o} inputs:in.connect = <${P}>`,`${o} inputs:low.connect = <${z}>`,`${o} inputs:high.connect = <${U}>`]);break}case"dot":T==="float"?g[p]=t(M,"ND_multiply_float","float",[`float inputs:in1.connect = <${r}>`,`float inputs:in2.connect = <${$}>`]):g[p]=t(M,`ND_dotproduct_${O(T)}`,"float",[`${R(T)} inputs:in1.connect = <${r}>`,`${R(T)} inputs:in2.connect = <${$}>`]);break;case"length":T==="float"?g[p]=t(M,"ND_absval_float","float",[`float inputs:in.connect = <${r}>`]):g[p]=t(M,`ND_magnitude_${O(T)}`,"float",[`${R(T)} inputs:in.connect = <${r}>`]);break;case"distance":{let z=t(`D_${n++}`,`ND_subtract_${O(T)}`,R(T),[`${R(T)} inputs:in1.connect = <${r}>`,`${R(T)} inputs:in2.connect = <${$}>`]);g[p]=T==="float"?t(M,"ND_absval_float","float",[`float inputs:in.connect = <${z}>`]):t(M,`ND_magnitude_${O(T)}`,"float",[`${R(T)} inputs:in.connect = <${z}>`]);break}case"cross":g[p]=t(M,"ND_crossproduct_vector3","float3",[`float3 inputs:in1.connect = <${r}>`,`float3 inputs:in2.connect = <${$}>`]);break;case"reflect":if(m==="vec3")g[p]=t(M,"ND_reflect_vector3","float3",[`float3 inputs:in.connect = <${r}>`,`float3 inputs:normal.connect = <${$}>`]);else{let z=m==="float"?t(`RF_${n++}`,"ND_multiply_float","float",[`float inputs:in1.connect = <${$}>`,`float inputs:in2.connect = <${r}>`]):t(`RF_${n++}`,`ND_dotproduct_${a}`,"float",[`${o} inputs:in1.connect = <${$}>`,`${o} inputs:in2.connect = <${r}>`]),U=t(`RF_${n++}`,"ND_multiply_float","float",[`float inputs:in1.connect = <${z}>`,"float inputs:in2 = 2.0"]),b=t(`RF_${n++}`,`ND_multiply_${a}`,o,[`${o} inputs:in1.connect = <${$}>`,`${o} inputs:in2.connect = <${N(U,"float",m)}>`]);g[p]=t(M,`ND_subtract_${a}`,o,[`${o} inputs:in1.connect = <${r}>`,`${o} inputs:in2.connect = <${b}>`])}break;case"fresnel":{let z=k("normalWorld"),U=k("viewDirection"),b=t(`FR_${n++}`,"ND_dotproduct_vector3","float",[`float3 inputs:in1.connect = <${z}>`,`float3 inputs:in2.connect = <${U}>`]),j=t(`FR_${n++}`,"ND_clamp_float","float",[`float inputs:in.connect = <${b}>`,"float inputs:low = 0.0","float inputs:high = 1.0"]),F=t(`FR_${n++}`,"ND_subtract_float","float",["float inputs:in1 = 1.0",`float inputs:in2.connect = <${j}>`]);g[p]=t(M,"ND_power_float","float",[`float inputs:in1.connect = <${F}>`,`float inputs:in2.connect = <${r}>`]);break}case"simplexNoise2D":g[p]=t(M,"ND_noise2d_float","float",[`float2 inputs:texcoord.connect = <${r}>`,"float inputs:amplitude = 0.5","float inputs:pivot = 0.5"]);break;case"simplexNoise3D":g[p]=t(M,"ND_noise3d_float","float",[`float3 inputs:position.connect = <${r}>`,"float inputs:amplitude = 0.5","float inputs:pivot = 0.5"]);break;case"voronoi2D":g[p]=t(M,"ND_cellnoise2d_float","float",[`float2 inputs:texcoord.connect = <${r}>`]);break;case"checker":{let z=t(`CK_${n++}`,"ND_floor_vector2","float2",[`float2 inputs:in.connect = <${r}>`]),U=w(z,"vec2",0),b=w(z,"vec2",1),j=t(`CK_${n++}`,"ND_add_float","float",[`float inputs:in1.connect = <${U}>`,`float inputs:in2.connect = <${b}>`]),F=t(`CK_${n++}`,"ND_modulo_float","float",[`float inputs:in1.connect = <${j}>`,"float inputs:in2 = 2.0"]);g[p]=t(M,"ND_ifgreater_float","float",[`float inputs:value1.connect = <${F}>`,"float inputs:value2 = 0.999","float inputs:in1 = 1.0","float inputs:in2 = 0.0"]);break}case"triplanar":g[p]=t(M,"ND_noise3d_float","float",[`float3 inputs:position.connect = <${r}>`,"float inputs:amplitude = 0.5","float inputs:pivot = 0.5"]);break;case"hueShift":{let z=t(`HS_${n++}`,"ND_convert_vector3_color3","color3f",[`float3 inputs:in.connect = <${r}>`]),U=t(`HS_${n++}`,"ND_multiply_float","float",[`float inputs:in1.connect = <${$}>`,"float inputs:in2 = 0.15915494"]),b=t(`HS_${n++}`,"ND_combine3_vector3","float3",[`float inputs:in1.connect = <${U}>`,"float inputs:in2 = 1.0","float inputs:in3 = 1.0"]),j=t(`HS_${n++}`,"ND_hsvadjust_color3","color3f",[`color3f inputs:in.connect = <${z}>`,`float3 inputs:amount.connect = <${b}>`]);g[p]=t(M,"ND_convert_color3_vector3","float3",[`color3f inputs:in.connect = <${j}>`]);break}case"bumpFromHeight":{g[p]=t(M,"ND_heighttonormal_vector3","float3",[`float inputs:in.connect = <${r}>`,`float inputs:scale.connect = <${$}>`]);break}default:g[p]=x(m,[0,0,0,0]);break}}let L=[],h={};if(i.outputs.color!==void 0){let p=t("OUT_color","ND_convert_vector3_color3","color3f",[`float3 inputs:in.connect = <${g[i.outputs.color]}>`]);L.push(`color3f outputs:diffuseColor.connect = <${p}>`),h.diffuseColor=`${u}.outputs:diffuseColor`}if(i.outputs.emissive!==void 0){let p=t("OUT_emissive","ND_convert_vector3_color3","color3f",[`float3 inputs:in.connect = <${g[i.outputs.emissive]}>`]);L.push(`color3f outputs:emissiveColor.connect = <${p}>`),h.emissiveColor=`${u}.outputs:emissiveColor`}if(i.outputs.opacity!==void 0&&(L.push(`float outputs:opacity.connect = <${g[i.outputs.opacity]}>`),h.opacity=`${u}.outputs:opacity`),i.outputs.roughness!==void 0&&(L.push(`float outputs:roughness.connect = <${g[i.outputs.roughness]}>`),h.roughness=`${u}.outputs:roughness`),i.outputs.metalness!==void 0&&(L.push(`float outputs:metallic.connect = <${g[i.outputs.metalness]}>`),h.metallic=`${u}.outputs:metallic`),i.outputs.bump!==void 0&&(L.push(`normal3f outputs:normal.connect = <${g[i.outputs.bump]}>`),h.normal=`${u}.outputs:normal`),L.length===0)return null;let S=[["diffuseColor","color3f"],["metallic","float"],["roughness","float"],["opacity","float"],["emissiveColor","color3f"],["normal","normal3f"]],D=c;for(let[p,v]of S){let m=h[p];if(!m)continue;let o=`${v} inputs:${p}.connect = <${m}>`,a=new RegExp(`^${v}\\s+inputs:${p}[^
|
|
49
|
+
]*$`,"m");a.test(D)?D=D.replace(a,o):D+=`${D.endsWith(`
|
|
50
|
+
`)||!D?"":`
|
|
51
|
+
`}${o}
|
|
52
|
+
`}return{nodeGraphUsda:`def NodeGraph "TSLGraph"
|
|
53
|
+
{
|
|
54
|
+
${[..._,...L,...d].join(`
|
|
55
|
+
`)}
|
|
56
|
+
}
|
|
57
|
+
`,surfaceConnections:h,surfaceInputsUsda:D,usesTime:i.usesTime}}var et=272,tt=["x","y","z","w"],be=[[2,"vec2<f32>"],[3,"vec3<f32>"],[4,"vec4<f32>"]];function H(e,s){return be.map(([c,i])=>{let u=tt.slice(0,c).map(d=>s===2?`${e}1(a.${d},b.${d})`:`${e}1(a.${d},b.${d},c.${d})`).join(",");return s===2?`fn ${e}${c}(a:${i},b:${i})->${i}{return ${i}(${u});}`:`fn ${e}${c}(a:${i},b:${i},c:${i})->${i}{return ${i}(${u});}`})}function xe(e,s){let c=[];return e.div&&c.push("fn _tslDiv1(a:f32,b:f32)->f32{return a/select(b,select(1e-6,-1e-6,b<0.0),abs(b)<1e-6);}",...H("_tslDiv",2)),e.mod&&c.push("fn _tslMod1(a:f32,b:f32)->f32{let d=select(b,select(1e-6,-1e-6,b<0.0),abs(b)<1e-6);return a-d*floor(a/d);}",...H("_tslMod",2)),e.pow&&c.push("fn _tslPow1(a:f32,b:f32)->f32{let e=clamp(b,-64.0,64.0);if(abs(e)<1e-8){return 1.0;}if(a<=0.0){return 0.0;}return pow(max(a,1e-8),e);}",...H("_tslPow",2)),e.clamp&&c.push("fn _tslClamp1(a:f32,b:f32,c:f32)->f32{return clamp(a,min(b,c),max(b,c));}",...H("_tslClamp",3)),e.smooth&&c.push("fn _tslSmooth1(a:f32,b:f32,c:f32)->f32{let t=_tslClamp1(_tslDiv1(c-a,b-a),0.0,1.0);return t*t*(3.0-2.0*t);}",...H("_tslSmooth",3)),e.atan2&&c.push("fn _tslAtan1(a:f32,b:f32)->f32{return select(atan2(a,b),0.0,abs(a)<1e-8&&abs(b)<1e-8);}",...H("_tslAtan",2)),e.norm&&c.push("fn _tslNorm1(v:f32)->f32{return select(0.0,sign(v),abs(v)>1e-6);}",...be.map(([i,u])=>`fn _tslNorm${i}(v:${u})->${u}{let l=length(v);return select(${u}(0.0),v/l,l>1e-6);}`)),(e.snoise2||e.snoise3||e.voronoi2)&&c.push("fn _tslM289(x:f32)->f32{return x-floor(x*(1.0/289.0))*289.0;}","fn _tslPerm(x:f32)->f32{return _tslM289((x*34.0+10.0)*x);}"),e.snoise2&&c.push("fn _tslSNoise2(v:vec2<f32>)->f32{let c0=0.21132487;let c1=0.3660254;let c2=-0.57735026;let c3=0.024390243;let s=(v.x+v.y)*c1;var ix=floor(v.x+s);var iy=floor(v.y+s);let t=(ix+iy)*c0;let x0=v.x-ix+t;let y0=v.y-iy+t;let i1x=select(0.0,1.0,x0>y0);let i1y=1.0-i1x;let x1=x0+c0-i1x;let y1=y0+c0-i1y;let x2=x0+c2;let y2=y0+c2;ix=_tslM289(ix);iy=_tslM289(iy);let p0=_tslPerm(_tslPerm(iy)+ix);let p1=_tslPerm(_tslPerm(iy+i1y)+ix+i1x);let p2=_tslPerm(_tslPerm(iy+1.0)+ix+1.0);var m=max(vec3<f32>(0.5)-vec3<f32>(x0*x0+y0*y0,x1*x1+y1*y1,x2*x2+y2*y2),vec3<f32>(0.0));m=m*m;m=m*m;let gx=2.0*fract(vec3<f32>(p0,p1,p2)*c3)-vec3<f32>(1.0);let hy=abs(gx)-vec3<f32>(0.5);let ox=gx-floor(gx+vec3<f32>(0.5));m*=vec3<f32>(1.7928429)-0.85373473*(ox*ox+hy*hy);return clamp(0.5+65.0*dot(m,ox*vec3<f32>(x0,x1,x2)+hy*vec3<f32>(y0,y1,y2)),0.0,1.0);}"),e.snoise3&&c.push("fn _tslSNoise3(v:vec3<f32>)->f32{let c0=0.16666667;let c1=0.33333334;let ns=0.14285715;let s=(v.x+v.y+v.z)*c1;var i0=floor(v.x+s);var i1=floor(v.y+s);var i2=floor(v.z+s);let t=(i0+i1+i2)*c0;let p0=v-vec3<f32>(i0,i1,i2)+vec3<f32>(t);let g=step(p0.yzx,p0.xyz);let l=vec3<f32>(1.0)-g.zxy;let a1=min(g,l);let a2=max(g,l);let p1=p0-a1+vec3<f32>(c0);let p2=p0-a2+vec3<f32>(c1);let p3=p0-vec3<f32>(0.5);i0=_tslM289(i0);i1=_tslM289(i1);i2=_tslM289(i2);let hs=array<f32,4>(_tslPerm(_tslPerm(_tslPerm(i2)+i1)+i0),_tslPerm(_tslPerm(_tslPerm(i2+a1.z)+i1+a1.y)+i0+a1.x),_tslPerm(_tslPerm(_tslPerm(i2+a2.z)+i1+a2.y)+i0+a2.x),_tslPerm(_tslPerm(_tslPerm(i2+1.0)+i1+1.0)+i0+1.0));let qs=array<vec3<f32>,4>(p0,p1,p2,p3);var sum=0.0;for(var k=0;k<4;k=k+1){let pk=hs[k];let j=pk-49.0*floor(pk*ns*ns);let x_=floor(j*ns);let y_=floor(j-7.0*x_);var gx=x_*(2.0/7.0)+(1.0/7.0)-1.0;var gy=y_*(2.0/7.0)+(1.0/7.0)-1.0;let gz=1.0-abs(gx)-abs(gy);let sz=select(0.0,1.0,gz<0.0);gx-=sz*select(-1.0,1.0,gx>=0.0);gy-=sz*select(-1.0,1.0,gy>=0.0);let inv=1.7928429-0.85373473*(gx*gx+gy*gy+gz*gz);let q=qs[k];var m=max(0.6-dot(q,q),0.0);m=m*m;m=m*m;sum+=m*inv*(gx*q.x+gy*q.y+gz*q.z);}return clamp(0.5+21.0*sum,0.0,1.0);}"),e.voronoi2&&c.push("fn _tslVoronoi2(u:vec2<f32>,jit:f32)->f32{let n=floor(u);let fr=u-n;let jt=clamp(jit,0.0,1.0);var md=8.0;for(var dy=-1;dy<=1;dy=dy+1){for(var dx=-1;dx<=1;dx=dx+1){let qx=_tslM289(n.x+f32(dx));let qy=_tslM289(n.y+f32(dy));let hx=fract(_tslPerm(_tslPerm(qx)+qy)*(1.0/41.0));let hy=fract(_tslPerm(_tslPerm(qy+7.0)+qx+13.0)*(1.0/41.0));md=min(md,length(vec2<f32>(f32(dx),f32(dy))+vec2<f32>(0.5)+(vec2<f32>(hx,hy)-vec2<f32>(0.5))*jt-fr));}}return clamp(md,0.0,1.0);}"),e.checker&&c.push("fn _tslChecker(p:vec2<f32>)->f32{return select(0.0,1.0,_tslMod1(floor(p.x)+floor(p.y),2.0)>=1.0);}"),e.fresnel&&c.push("fn _tslFresnel(pw:f32,nw:vec3<f32>,vd:vec3<f32>)->f32{return _tslPow1(1.0-clamp(dot(_tslNorm3(nw),_tslNorm3(vd)),0.0,1.0),max(pw,1e-3));}"),e.triplanar&&c.push("fn _tslTriplanar(p:vec3<f32>,sh:f32,nw:vec3<f32>)->f32{let w=_tslPow3(max(abs(_tslNorm3(nw)),vec3<f32>(1e-4)),vec3<f32>(clamp(sh,0.5,16.0)));return(w.x*_tslSNoise2(p.yz)+w.y*_tslSNoise2(p.xz)+w.z*_tslSNoise2(p.xy))/max(w.x+w.y+w.z,1e-6);}"),e.hueShift&&c.push("fn _tslHueShift(rgb:vec3<f32>,ang:f32)->vec3<f32>{let k=vec3<f32>(0.57735026);let c=cos(ang);let s=sin(ang);return rgb*c+cross(k,rgb)*s+k*dot(k,rgb)*(1.0-c);}"),e.bump&&s&&c.push("fn _tslBump(dhx:f32,dhy:f32,st:f32,nw:vec3<f32>,dx:vec3<f32>,dy:vec3<f32>)->vec3<f32>{let n=_tslNorm3(nw);let r1=cross(dy,n);let r2=cross(n,dx);let det=dot(dx,r1);if(abs(det)<1e-8){return n;}return _tslNorm3(abs(det)*n-st*select(1.0,-1.0,det<0.0)*(dhx*r1+dhy*r2));}"),c.join(`
|
|
58
|
+
`)}var lt={positionLocal:"_tslPL",positionWorld:"_tslPW",normalLocal:"_tslNL",normalWorld:"_tslNW",viewDirection:"_tslVD",uv:"_tslUV",vertexColor:"_tslVC",instanceIndex:"_tslII"};function ue(e,s){let c=[],i=_=>_==="float"?"f32":`${_}<f32>`,u=(_,n)=>_==="float"?n:`${_}<f32>(${n})`,d=(_,n,t)=>n===t?_:`${t}<f32>(${_})`;for(let _=0;_<e.length;_++){if(!s[_])continue;let n=e[_],t=n.type,x=C(t),f=n.args[0]!==void 0?`_t${n.args[0]}`:"",N=n.args[1]!==void 0?`_t${n.args[1]}`:"",w=n.args[2]!==void 0?`_t${n.args[2]}`:"",y=n.args[0]!==void 0?e[n.args[0]].type:"float",k=n.args[1]!==void 0?e[n.args[1]].type:"float",g=n.args[2]!==void 0?e[n.args[2]].type:"float",L=u(t,"0.0");switch(n.op){case"const":{let h=(n.value??[0]).map(W);L=t==="float"?h[0]:`${t}<f32>(${h.join(",")})`;break}case"builtin":L=n.builtin==="time"?`_tslMod1(tslU.timeAndPad.x,${W(3600)})`:lt[n.builtin]??"0.0";break;case"uniform":{let h=`tslU.params[${n.uniformSlot??0}]`;L=x===4?h:`${h}.${"xyz".slice(0,x)}`;break}case"swizzle":L=y==="float"?u(t,f):`${f}.${n.mask}`;break;case"construct":L=t==="float"?f:`${t}<f32>(${n.args.map(h=>`_t${h}`).join(",")})`;break;case"neg":L=`(-${f})`;break;case"abs":case"sign":case"floor":case"ceil":case"fract":case"sin":case"cos":L=`${n.op}(${f})`;break;case"sqrt":L=`sqrt(max(${f},${u(t,"0.0")}))`;break;case"exp":L=`exp(clamp(${f},${u(t,"-80.0")},${u(t,"80.0")}))`;break;case"log":L=`log(max(${f},${u(t,"1.0e-8")}))`;break;case"normalize":L=`_tslNorm${x}(${f})`;break;case"add":case"sub":case"mul":{let h=n.op==="add"?"+":n.op==="sub"?"-":"*";L=`(${d(f,y,t)}${h}${d(N,k,t)})`;break}case"div":case"mod":case"pow":case"atan2":{L=`${n.op==="div"?"_tslDiv":n.op==="mod"?"_tslMod":n.op==="pow"?"_tslPow":"_tslAtan"}${x}(${d(f,y,t)},${d(N,k,t)})`;break}case"min":case"max":case"step":L=`${n.op}(${d(f,y,t)},${d(N,k,t)})`;break;case"clamp":L=`_tslClamp${x}(${d(f,y,t)},${d(N,k,t)},${d(w,g,t)})`;break;case"mix":L=`mix(${f},${N},${d(w,g,t)})`;break;case"smoothstep":L=`_tslSmooth${x}(${d(f,y,t)},${d(N,k,t)},${d(w,g,t)})`;break;case"dot":L=y==="float"?`(${f}*${N})`:`dot(${f},${N})`;break;case"length":L=y==="float"?`abs(${f})`:`length(${f})`;break;case"distance":L=y==="float"?`abs(${f}-${N})`:`distance(${f},${N})`;break;case"cross":case"reflect":L=`${n.op}(${f},${N})`;break;case"fresnel":L=`_tslFresnel(${f},_tslNW,_tslVD)`;break;case"simplexNoise2D":L=`_tslSNoise2(${f})`;break;case"simplexNoise3D":L=`_tslSNoise3(${f})`;break;case"voronoi2D":L=`_tslVoronoi2(${f},${N})`;break;case"checker":L=`_tslChecker(${f})`;break;case"triplanar":L=`_tslTriplanar(${f},${N},_tslNW)`;break;case"hueShift":L=`_tslHueShift(${f},${N})`;break;case"bumpFromHeight":L=`_tslBump(dpdx(${f}),-dpdy(${f}),${N},_tslNW,p1,p2)`;break;default:break}c.push(` let _t${_}:${i(t)}=${L};`)}return c.join(`
|
|
59
|
+
`)}function Ne(e,s,c){let i=`struct TslUniformBlock{params:array<vec4<f32>,16>,timeAndPad:vec4<f32>,};
|
|
60
|
+
`,u=new Uint8Array(e.nodes.length);for(let x=0;x<e.nodes.length;x++)u[x]=e.fragmentReachable[x]||e.vertexReachable[x];let d=e.fragmentReachable.some(x=>x===1),_=xe(se(e.nodes,u,e.usesTime),!0),n=s.replace("@group(1) @binding(4) var emTex: texture_2d<f32>;",`@group(1) @binding(4) var emTex: texture_2d<f32>;
|
|
61
|
+
${i}@group(2) @binding(0) var<uniform> tslU: TslUniformBlock;`).replace("@location(3) @interpolate(flat) instIdx: u32,",`@location(3) @interpolate(flat) instIdx: u32,
|
|
62
|
+
@location(4) localPos: vec3<f32>,
|
|
63
|
+
@location(5) localNorm: vec3<f32>,`).replace(`out.instIdx = instIdx;
|
|
64
|
+
return out;`,`out.instIdx = instIdx;
|
|
65
|
+
out.localPos = vec3<f32>(0.0);
|
|
66
|
+
out.localNorm = vec3<f32>(0.0, 1.0, 0.0);
|
|
67
|
+
return out;`);if(_&&(n=n.replace("@vertex",`${_}
|
|
68
|
+
@vertex`)),e.usesVertexPosition&&e.outputs.position!==void 0){let x=`fn _tslEvalPos(_tslPL:vec3<f32>,_tslNL:vec3<f32>,_tslUV:vec2<f32>,_tslVC:vec4<f32>,_tslII:f32,m:mat4x4<f32>)->vec3<f32>{
|
|
69
|
+
let _tslPW=(m*vec4<f32>(_tslPL,1.0)).xyz;let _rn=m[0].xyz*_tslNL.x+m[1].xyz*_tslNL.y+m[2].xyz*_tslNL.z;let _rl=length(_rn);
|
|
70
|
+
let _tslNW=select(vec3<f32>(0.0,1.0,0.0),_rn/_rl,_rl>1e-5);let _tslVD=vec3<f32>(0.0,0.0,1.0);
|
|
71
|
+
${ue(e.nodes,e.vertexReachable)}
|
|
72
|
+
return _t${e.outputs.position};}`;n=n.replace("@vertex",`${x}
|
|
73
|
+
@vertex`).replace("let world = m * vec4<f32>(aPos, 1.0);",`let _tslInstF=f32(instIdx);let _tslNewP=_tslEvalPos(aPos,aNormal,aUV,vec4<f32>(1.0),_tslInstF,m);var _tslNewN=aNormal;
|
|
74
|
+
if(length(aNormal)>1e-4){let _n0=normalize(aNormal);let _tg1=normalize(select(cross(vec3<f32>(1.0,0.0,0.0),_n0),cross(vec3<f32>(0.0,1.0,0.0),_n0),abs(_n0.y)<0.99));let _tg2=cross(_n0,_tg1);let _cn=cross(_tslEvalPos(aPos+_tg1*1e-3,aNormal,aUV,vec4<f32>(1.0),_tslInstF,m)-_tslNewP,_tslEvalPos(aPos+_tg2*1e-3,aNormal,aUV,vec4<f32>(1.0),_tslInstF,m)-_tslNewP);if(length(_cn)>1e-10){_tslNewN=normalize(_cn);}}
|
|
75
|
+
let world = m * vec4<f32>(_tslNewP, 1.0);`).replace(`rawN = (adj0 * aNormal.x + adj1 * aNormal.y + adj2 * aNormal.z) * sgn;
|
|
76
|
+
} else {
|
|
77
|
+
rawN = c0 * aNormal.x + c1 * aNormal.y + c2 * aNormal.z;`,`rawN = (adj0 * _tslNewN.x + adj1 * _tslNewN.y + adj2 * _tslNewN.z) * sgn;
|
|
78
|
+
} else {
|
|
79
|
+
rawN = c0 * _tslNewN.x + c1 * _tslNewN.y + c2 * _tslNewN.z;`).replace(`out.instIdx = instIdx;
|
|
80
|
+
return out;`,`out.instIdx = instIdx;
|
|
81
|
+
out.localPos = _tslNewP;
|
|
82
|
+
out.localNorm = _tslNewN;
|
|
83
|
+
return out;`)}else n=n.replace(`out.instIdx = instIdx;
|
|
84
|
+
return out;`,`out.instIdx = instIdx;
|
|
85
|
+
out.localPos = aPos;
|
|
86
|
+
out.localNorm = aNormal;
|
|
87
|
+
return out;`);if(d){let x=` let _nL=length(in.normal);var _nU:vec3<f32>;if(_nL<1e-4){let _fc=cross(p1,p2);let _fl=length(_fc);_nU=select(vec3<f32>(0.0,1.0,0.0),_fc/_fl,_fl>1e-8);}else{_nU=in.normal/_nL;}if(!frontFacing){_nU=-_nU;}
|
|
88
|
+
let _toC=sc.camAndExp.xyz-in.worldPos;let _cD=length(_toC);let _tslVD=select(vec3<f32>(0.0,0.0,1.0),_toC/_cD,_cD>1e-6);let _tslNW=_nU;
|
|
89
|
+
let _lnL=length(in.localNorm);let _tslNL=select(vec3<f32>(0.0,1.0,0.0),in.localNorm/_lnL,_lnL>1e-5);let _tslPL=in.localPos;let _tslPW=in.worldPos;let _tslUV=in.uv;let _tslVC=vec4<f32>(1.0);let _tslII=f32(in.instIdx);
|
|
90
|
+
${ue(e.nodes,e.fragmentReachable)}`;n=n.replace("let emTexel = textureSample(emTex, linSampler, in.uv);",`let emTexel = textureSample(emTex, linSampler, in.uv);
|
|
91
|
+
${x}`),e.outputs.opacity!==void 0&&(n=n.replace(`if (base.a < 0.004) {
|
|
92
|
+
discard;
|
|
93
|
+
}`,`base.a = clamp(_t${e.outputs.opacity}, 0.0, 1.0);
|
|
94
|
+
if (base.a < 0.004) {
|
|
95
|
+
discard;
|
|
96
|
+
}`)),n=n.replace("if (nInLen < 1e-4 && (pFlags & 2u) != 0u) {","if (false && nInLen < 1e-4 && (pFlags & 2u) != 0u) {"),e.outputs.normalScale!==void 0&&e.outputs.bump===void 0&&(n=n.replace("q = vec3<f32>(q.xy * p.aux.x, q.z);",`q = vec3<f32>(q.xy * max(_t${e.outputs.normalScale}, 0.0), q.z);`));let f=[];if(e.outputs.color!==void 0&&f.push(` base=vec4<f32>(max(_t${e.outputs.color},vec3<f32>(0.0)),base.a);`),e.outputs.emissive!==void 0&&f.push(` em=max(_t${e.outputs.emissive},vec3<f32>(0.0));`),e.outputs.roughness!==void 0&&f.push(` roughness=clamp(_t${e.outputs.roughness},0.04,1.0);`),e.outputs.metalness!==void 0&&f.push(` metallic=clamp(_t${e.outputs.metalness},0.0,1.0);`),e.outputs.bump!==void 0){let N=e.outputs.normalScale!==void 0?`max(_t${e.outputs.normalScale},0.0)`:"1.0";f.push(` let _bn=_t${e.outputs.bump};let _bl=length(_bn);if(_bl>1e-6){let _bm=N+(_bn/_bl-N)*(${N});let _ml=length(_bm);if(_ml>1e-6){N=_bm/_ml;}}`)}f.length>0&&(n=n.replace("metallic = clamp(metallic, 0.0, 1.0);",`metallic = clamp(metallic, 0.0, 1.0);
|
|
97
|
+
${f.join(`
|
|
98
|
+
`)}`))}let t=null;if(e.usesVertexPosition&&e.outputs.position!==void 0){let x=xe(se(e.nodes,e.vertexReachable,e.usesTime),!1),f=ue(e.nodes,e.vertexReachable);t=c.replace("@group(0) @binding(1) var<storage, read> parts: array<PartInstance>;",`@group(0) @binding(1) var<storage, read> parts: array<PartInstance>;
|
|
99
|
+
${i}@group(1) @binding(0) var<uniform> tslU: TslUniformBlock;
|
|
100
|
+
${x}`).replace("@location(1) aNormal: vec3<f32>,",`@location(1) aNormal: vec3<f32>,
|
|
101
|
+
@location(2) aUV: vec2<f32>,`).replace("var wp = m * vec4<f32>(aPos, 1.0);",`let _tslPL=aPos;let _tslPW=(m*vec4<f32>(aPos,1.0)).xyz;let _tslNL=aNormal;let _rn=m[0].xyz*aNormal.x+m[1].xyz*aNormal.y+m[2].xyz*aNormal.z;let _rl=length(_rn);let _tslNW=select(vec3<f32>(0.0,1.0,0.0),_rn/_rl,_rl>1e-5);let _tslVD=vec3<f32>(0.0,0.0,1.0);let _tslUV=aUV;let _tslVC=vec4<f32>(1.0);let _tslII=f32(instIdx);
|
|
102
|
+
${f}
|
|
103
|
+
var wp = m * vec4<f32>(_t${e.outputs.position}, 1.0);`)}return{hash:e.hash,pbrWgsl:n,shadowDepthWgsl:t}}var J=new WeakMap,G=new WeakMap,we=new WeakMap,Q=new Map,ot=128;function st(e,s){let c=we.get(s);return c||(c={paramsLoc:e.getUniformLocation(s,"uTslParams[0]")??e.getUniformLocation(s,"uTslParams"),timeLoc:e.getUniformLocation(s,"uTslTime")},we.set(s,c)),c}function at(e,s){let c=new Float32Array(68);for(let i of s.uniformSlots){let u=i.slot*4;c[u]=i.defaultVec4[0],c[u+1]=i.defaultVec4[1],c[u+2]=i.defaultVec4[2],c[u+3]=i.defaultVec4[3]}return{name:e,hash:s.hash,graph:s,buf68:c,usesTime:s.usesTime,usesVertexPosition:s.usesVertexPosition,version:1}}function ee(e,s){let c=new Map,i=new Map,u=!1,d=Array.isArray(s?.materials)?s.materials:[];for(let t=0;t<d.length;t++){let x=d[t],f=x?.extensions?.[Z];if(!f)continue;let N=typeof x.name=="string"&&x.name.length>0?x.name:`Material_${t}`,w=K(f,N);if(!w.ok){console.warn(`[three-live] [${w.issue.code}] ${w.issue.message} Falling back to base PBR material.`);continue}let y=at(N,w.graph);c.set(t,y);let k=i.get(N)??[];k.push(y),i.set(N,k),w.graph.usesTime&&(u=!0)}let _={materialsByIndex:c,materialsByName:i,hasAnimatedTsl:u,elapsedSec:0};J.set(e,_);let n=e.gl;return n&&typeof n=="object"&&G.set(n,_),_}function ke(e,s){let c=J.get(e);if(!c){let i=s??e.gltf;if(!i)return null;c=ee(e,i)}return c}function nt(e){if(e==null)return null;if(typeof e=="number"||Array.isArray(e)||e instanceof Float32Array)return e;if(typeof e=="object"){let s=e;if("r"in s&&"g"in s&&"b"in s)return[Number(s.r),Number(s.g),Number(s.b)];if("x"in s&&"y"in s&&"z"in s&&"w"in s)return[Number(s.x),Number(s.y),Number(s.z),Number(s.w)];if("x"in s&&"y"in s&&"z"in s)return[Number(s.x),Number(s.y),Number(s.z)];if("x"in s&&"y"in s)return[Number(s.x),Number(s.y)]}return null}function Te(e,s,c,i){if(!e||typeof e!="object")return!1;let u=e,d=u.viewer&&typeof u.viewer=="object"?u.viewer:e,_=ke(d);if(!_||_.materialsByIndex.size===0)return!1;let n=s==="*"||s===""?Array.from(_.materialsByIndex.values()):_.materialsByName.get(s)??[];if(n.length===0)return!1;let t=nt(i);if(t===null)return!1;let x=!1;for(let f of n){let N=f.graph.uniformSlotByName.get(c);if(!N)continue;let w=ce(N.type,t);if(!w)continue;let y=N.slot*4;f.buf68[y]=w[0],f.buf68[y+1]=w[1],f.buf68[y+2]=w[2],f.buf68[y+3]=w[3],f.version++,x=!0}return x&&d.animFrameId===null&&typeof d.render=="function"&&d.render(),x}function ae(e){if(!e||typeof e!="object")return;let s=e;typeof s.setMaterialUniform!="function"&&(s.setMaterialUniform=(c,i,u)=>Te(e,c,i,u))}function rt(){let e=oe();je(re);let s=e.threeNamespace??(typeof globalThis<"u"?globalThis.THREE:void 0);s?re(s,e):e.authorNamespace||(e.authorNamespace={}),e.tslWgslCompiler=(c,i,u)=>!c||typeof c!="object"?null:Ne(c,i,u),e.tslMaterialResolver=(c,i)=>i===void 0||i<0?null:ke(c)?.materialsByIndex.get(i)??null,e.tslUsdzSerializer=ge}var Le={name:"tsl",onRegister(){rt()},onElementConnected(e){ae(e),e?.viewer&&(ae(e.viewer),e.viewer.gltf&&(ee(e.viewer,e.viewer.gltf),e.viewer.recompileRenderProgram?.()))},onBuildPrimitiveVAO(e,s,c,i){let u=c?.material;if(typeof u!="number")return null;let d=i?.materials?.[u]?.extensions?.[Z];if(!d)return null;let _=K(d,i?.materials?.[u]?.name||`Material_${u}`);return _.ok?{hash:_.graph.hash,materialIndex:u}:null},onResolveShader(e,s,c,i){let u=s?.extensions?.[Z];if(!u)return null;let d=K(u,s?.name||"Material");if(!d.ok)return console.warn(`[three-live] [${d.issue.code}] ${d.issue.message} Falling back to base PBR shader.`),null;let _=`${d.graph.hash}|${c.length}|${i.length}|${i.slice(-64)}`,n=Q.get(_);if(!n){if(Q.size>=ot){let t=Q.keys().next().value;t!==void 0&&Q.delete(t)}n=ye(d.graph,c,i),Q.set(_,n)}return n},onLoadModel(e,s){ae(e),e?.container&&ae(e.container),ee(e,s).hasAnimatedTsl&&e&&e.animFrameId===null&&typeof e.start=="function"&&e.opts?.autoStart!==!1&&typeof requestAnimationFrame<"u"&&e.start()},onUpdate(e,s){let c=J.get(e);if(!c||!c.hasAnimatedTsl)return;let i=Number.isFinite(s)&&s>0?Math.min(s,.25):0;c.elapsedSec=me(c.elapsedSec+i,3600);for(let u of c.materialsByIndex.values())u.usesTime&&(u.buf68[64]=c.elapsedSec,u.version++)},onBeforeDrawPrimitive(e,s,c,i,u,d){let _=c?.materialIndex;if(typeof _!="number"||!d?.materials?.[_]?.extensions?.[Z])return;let n=st(e,s);if(!n.paramsLoc&&!n.timeLoc)return;let t=G.get(e);t||(t=ee(e,d),G.set(e,t));let x=t.materialsByIndex.get(_);x&&(n.paramsLoc&&e.uniform4fv(n.paramsLoc,x.buf68,0,64),n.timeLoc&&e.uniform1f(n.timeLoc,x.buf68[64]))},onClearScene(e){J.delete(e),e?.gl&&typeof e.gl=="object"&&G.delete(e.gl)},onContextRestored(e,s){if(s?.gltf){let c=ee(s,s.gltf);e&&typeof e=="object"&&G.set(e,c)}},onDestroy(e,s){J.delete(s),e&&typeof e=="object"&&G.delete(e)}};return Fe(Le),Ae(fe)})();
|