Ai_Assistant/client/node_modules/@pixiv/three-vrm-materials-mtoon/lib/three-vrm-materials-mtoon.min.cjs
2026-05-24 13:31:30 +02:00

925 lines
45 KiB
JavaScript

/*! (c) 2019-2025 pixiv Inc. - https://github.com/pixiv/three-vrm/blob/release/LICENSE */
"use strict";var N=Object.create;var h=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames,x=Object.getOwnPropertySymbols,D=Object.getPrototypeOf,b=Object.prototype.hasOwnProperty,V=Object.prototype.propertyIsEnumerable;var L=(a,i,e)=>i in a?h(a,i,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[i]=e,S=(a,i)=>{for(var e in i||(i={}))b.call(i,e)&&L(a,e,i[e]);if(x)for(var e of x(i))V.call(i,e)&&L(a,e,i[e]);return a};var W=(a,i)=>{for(var e in i)h(a,e,{get:i[e],enumerable:!0})},U=(a,i,e,t)=>{if(i&&typeof i=="object"||typeof i=="function")for(let r of w(i))!b.call(a,r)&&r!==e&&h(a,r,{get:()=>i[r],enumerable:!(t=F(i,r))||t.enumerable});return a};var p=(a,i,e)=>(e=a!=null?N(D(a)):{},U(i||!a||!a.__esModule?h(e,"default",{value:a,enumerable:!0}):e,a)),G=a=>U(h({},"__esModule",{value:!0}),a);var m=(a,i,e)=>new Promise((t,r)=>{var n=l=>{try{u(e.next(l))}catch(d){r(d)}},s=l=>{try{u(e.throw(l))}catch(d){r(d)}},u=l=>l.done?t(l.value):Promise.resolve(l.value).then(n,s);u((e=e.apply(a,i)).next())});var Y={};W(Y,{MToonMaterial:()=>c,MToonMaterialDebugMode:()=>v,MToonMaterialLoaderPlugin:()=>M,MToonMaterialOutlineWidthMode:()=>T});module.exports=G(Y);var _=p(require("three"),1);var H=p(require("three"),1);var A=p(require("three"),1),B={"":3e3,srgb:3001};function y(a,i){parseInt(A.REVISION,10)>=152?a.colorSpace=i:a.encoding=B[i]}var E=class{get pending(){return Promise.all(this._pendings)}constructor(i,e){this._parser=i,this._materialParams=e,this._pendings=[]}assignPrimitive(i,e){e!=null&&(this._materialParams[i]=e)}assignColor(i,e,t){e!=null&&(this._materialParams[i]=new H.Color().fromArray(e),t&&this._materialParams[i].convertSRGBToLinear())}assignTexture(i,e,t){return m(this,null,function*(){let r=m(this,null,function*(){e!=null&&(yield this._parser.assignTexture(this._materialParams,i,e),t&&y(this._materialParams[i],"srgb"))});return this._pendings.push(r),r})}assignTextureByIndex(i,e,t){return m(this,null,function*(){return this.assignTexture(i,e!=null?{index:e}:void 0,t)})}};var o=p(require("three"),1);var O=`// #define PHONG
varying vec3 vViewPosition;
#ifndef FLAT_SHADED
varying vec3 vNormal;
#endif
#include <common>
// #include <uv_pars_vertex>
#ifdef MTOON_USE_UV
varying vec2 vUv;
// COMPAT: pre-r151 uses a common uvTransform
#if THREE_VRM_THREE_REVISION < 151
uniform mat3 uvTransform;
#endif
#endif
// #include <uv2_pars_vertex>
// COMAPT: pre-r151 uses uv2 for lightMap and aoMap
#if THREE_VRM_THREE_REVISION < 151
#if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )
attribute vec2 uv2;
varying vec2 vUv2;
uniform mat3 uv2Transform;
#endif
#endif
// #include <displacementmap_pars_vertex>
// #include <envmap_pars_vertex>
#include <color_pars_vertex>
#include <fog_pars_vertex>
#include <morphtarget_pars_vertex>
#include <skinning_pars_vertex>
#include <shadowmap_pars_vertex>
#include <logdepthbuf_pars_vertex>
#include <clipping_planes_pars_vertex>
#ifdef USE_OUTLINEWIDTHMULTIPLYTEXTURE
uniform sampler2D outlineWidthMultiplyTexture;
uniform mat3 outlineWidthMultiplyTextureUvTransform;
#endif
uniform float outlineWidthFactor;
void main() {
// #include <uv_vertex>
#ifdef MTOON_USE_UV
// COMPAT: pre-r151 uses a common uvTransform
#if THREE_VRM_THREE_REVISION >= 151
vUv = uv;
#else
vUv = ( uvTransform * vec3( uv, 1 ) ).xy;
#endif
#endif
// #include <uv2_vertex>
// COMAPT: pre-r151 uses uv2 for lightMap and aoMap
#if THREE_VRM_THREE_REVISION < 151
#if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )
vUv2 = ( uv2Transform * vec3( uv2, 1 ) ).xy;
#endif
#endif
#include <color_vertex>
#include <beginnormal_vertex>
#include <morphnormal_vertex>
#include <skinbase_vertex>
#include <skinnormal_vertex>
// we need this to compute the outline properly
objectNormal = normalize( objectNormal );
#include <defaultnormal_vertex>
#ifndef FLAT_SHADED // Normal computed with derivatives when FLAT_SHADED
vNormal = normalize( transformedNormal );
#endif
#include <begin_vertex>
#include <morphtarget_vertex>
#include <skinning_vertex>
// #include <displacementmap_vertex>
#include <project_vertex>
#include <logdepthbuf_vertex>
#include <clipping_planes_vertex>
vViewPosition = - mvPosition.xyz;
#ifdef OUTLINE
float worldNormalLength = length( transformedNormal );
vec3 outlineOffset = outlineWidthFactor * worldNormalLength * objectNormal;
#ifdef USE_OUTLINEWIDTHMULTIPLYTEXTURE
vec2 outlineWidthMultiplyTextureUv = ( outlineWidthMultiplyTextureUvTransform * vec3( vUv, 1 ) ).xy;
float outlineTex = texture2D( outlineWidthMultiplyTexture, outlineWidthMultiplyTextureUv ).g;
outlineOffset *= outlineTex;
#endif
#ifdef OUTLINE_WIDTH_SCREEN
outlineOffset *= vViewPosition.z / projectionMatrix[ 1 ].y;
#endif
gl_Position = projectionMatrix * modelViewMatrix * vec4( outlineOffset + transformed, 1.0 );
gl_Position.z += 1E-6 * gl_Position.w; // anti-artifact magic
#endif
#include <worldpos_vertex>
// #include <envmap_vertex>
#include <shadowmap_vertex>
#include <fog_vertex>
}`;var C=`// #define PHONG
uniform vec3 litFactor;
uniform float opacity;
uniform vec3 shadeColorFactor;
#ifdef USE_SHADEMULTIPLYTEXTURE
uniform sampler2D shadeMultiplyTexture;
uniform mat3 shadeMultiplyTextureUvTransform;
#endif
uniform float shadingShiftFactor;
uniform float shadingToonyFactor;
#ifdef USE_SHADINGSHIFTTEXTURE
uniform sampler2D shadingShiftTexture;
uniform mat3 shadingShiftTextureUvTransform;
uniform float shadingShiftTextureScale;
#endif
uniform float giEqualizationFactor;
uniform vec3 parametricRimColorFactor;
#ifdef USE_RIMMULTIPLYTEXTURE
uniform sampler2D rimMultiplyTexture;
uniform mat3 rimMultiplyTextureUvTransform;
#endif
uniform float rimLightingMixFactor;
uniform float parametricRimFresnelPowerFactor;
uniform float parametricRimLiftFactor;
#ifdef USE_MATCAPTEXTURE
uniform vec3 matcapFactor;
uniform sampler2D matcapTexture;
uniform mat3 matcapTextureUvTransform;
#endif
uniform vec3 emissive;
uniform float emissiveIntensity;
uniform vec3 outlineColorFactor;
uniform float outlineLightingMixFactor;
#ifdef USE_UVANIMATIONMASKTEXTURE
uniform sampler2D uvAnimationMaskTexture;
uniform mat3 uvAnimationMaskTextureUvTransform;
#endif
uniform float uvAnimationScrollXOffset;
uniform float uvAnimationScrollYOffset;
uniform float uvAnimationRotationPhase;
#include <common>
#include <packing>
#include <dithering_pars_fragment>
#include <color_pars_fragment>
// #include <uv_pars_fragment>
#if ( defined( MTOON_USE_UV ) && !defined( MTOON_UVS_VERTEX_ONLY ) )
varying vec2 vUv;
#endif
// #include <uv2_pars_fragment>
// COMAPT: pre-r151 uses uv2 for lightMap and aoMap
#if THREE_VRM_THREE_REVISION < 151
#if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )
varying vec2 vUv2;
#endif
#endif
#include <map_pars_fragment>
#ifdef USE_MAP
uniform mat3 mapUvTransform;
#endif
// #include <alphamap_pars_fragment>
#include <alphatest_pars_fragment>
#include <aomap_pars_fragment>
// #include <lightmap_pars_fragment>
#include <emissivemap_pars_fragment>
#ifdef USE_EMISSIVEMAP
uniform mat3 emissiveMapUvTransform;
#endif
// #include <envmap_common_pars_fragment>
// #include <envmap_pars_fragment>
// #include <cube_uv_reflection_fragment>
#include <fog_pars_fragment>
// #include <bsdfs>
// COMPAT: pre-r151 doesn't have BRDF_Lambert in <common>
#if THREE_VRM_THREE_REVISION < 151
vec3 BRDF_Lambert( const in vec3 diffuseColor ) {
return RECIPROCAL_PI * diffuseColor;
}
#endif
#include <lights_pars_begin>
#include <normal_pars_fragment>
// #include <lights_phong_pars_fragment>
varying vec3 vViewPosition;
struct MToonMaterial {
vec3 diffuseColor;
vec3 shadeColor;
float shadingShift;
};
float linearstep( float a, float b, float t ) {
return clamp( ( t - a ) / ( b - a ), 0.0, 1.0 );
}
/**
* Convert NdotL into toon shading factor using shadingShift and shadingToony
*/
float getShading(
const in float dotNL,
const in float shadow,
const in float shadingShift
) {
float shading = dotNL;
shading = shading + shadingShift;
shading = linearstep( -1.0 + shadingToonyFactor, 1.0 - shadingToonyFactor, shading );
shading *= shadow;
return shading;
}
/**
* Mix diffuseColor and shadeColor using shading factor and light color
*/
vec3 getDiffuse(
const in MToonMaterial material,
const in float shading,
in vec3 lightColor
) {
#ifdef DEBUG_LITSHADERATE
return vec3( BRDF_Lambert( shading * lightColor ) );
#endif
vec3 col = lightColor * BRDF_Lambert( mix( material.shadeColor, material.diffuseColor, shading ) );
// The "comment out if you want to PBR absolutely" line
#ifdef V0_COMPAT_SHADE
col = min( col, material.diffuseColor );
#endif
return col;
}
// COMPAT: pre-r156 uses a struct GeometricContext
#if THREE_VRM_THREE_REVISION >= 157
void RE_Direct_MToon( const in IncidentLight directLight, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in MToonMaterial material, const in float shadow, inout ReflectedLight reflectedLight ) {
float dotNL = clamp( dot( geometryNormal, directLight.direction ), -1.0, 1.0 );
vec3 irradiance = directLight.color;
// directSpecular will be used for rim lighting, not an actual specular
reflectedLight.directSpecular += irradiance;
irradiance *= dotNL;
float shading = getShading( dotNL, shadow, material.shadingShift );
// toon shaded diffuse
reflectedLight.directDiffuse += getDiffuse( material, shading, directLight.color );
}
void RE_IndirectDiffuse_MToon( const in vec3 irradiance, const in vec3 geometryPosition, const in vec3 geometryNormal, const in vec3 geometryViewDir, const in vec3 geometryClearcoatNormal, const in MToonMaterial material, inout ReflectedLight reflectedLight ) {
// indirect diffuse will use diffuseColor, no shadeColor involved
reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
// directSpecular will be used for rim lighting, not an actual specular
reflectedLight.directSpecular += irradiance;
}
#else
void RE_Direct_MToon( const in IncidentLight directLight, const in GeometricContext geometry, const in MToonMaterial material, const in float shadow, inout ReflectedLight reflectedLight ) {
float dotNL = clamp( dot( geometry.normal, directLight.direction ), -1.0, 1.0 );
vec3 irradiance = directLight.color;
// directSpecular will be used for rim lighting, not an actual specular
reflectedLight.directSpecular += irradiance;
irradiance *= dotNL;
float shading = getShading( dotNL, shadow, material.shadingShift );
// toon shaded diffuse
reflectedLight.directDiffuse += getDiffuse( material, shading, directLight.color );
}
void RE_IndirectDiffuse_MToon( const in vec3 irradiance, const in GeometricContext geometry, const in MToonMaterial material, inout ReflectedLight reflectedLight ) {
// indirect diffuse will use diffuseColor, no shadeColor involved
reflectedLight.indirectDiffuse += irradiance * BRDF_Lambert( material.diffuseColor );
// directSpecular will be used for rim lighting, not an actual specular
reflectedLight.directSpecular += irradiance;
}
#endif
#define RE_Direct RE_Direct_MToon
#define RE_IndirectDiffuse RE_IndirectDiffuse_MToon
#define Material_LightProbeLOD( material ) (0)
#include <shadowmap_pars_fragment>
// #include <bumpmap_pars_fragment>
// #include <normalmap_pars_fragment>
#ifdef USE_NORMALMAP
uniform sampler2D normalMap;
uniform mat3 normalMapUvTransform;
uniform vec2 normalScale;
#endif
// COMPAT: pre-r151
// USE_NORMALMAP_OBJECTSPACE used to be OBJECTSPACE_NORMALMAP in pre-r151
#if defined( USE_NORMALMAP_OBJECTSPACE ) || defined( OBJECTSPACE_NORMALMAP )
uniform mat3 normalMatrix;
#endif
// COMPAT: pre-r151
// USE_NORMALMAP_TANGENTSPACE used to be TANGENTSPACE_NORMALMAP in pre-r151
#if ! defined ( USE_TANGENT ) && ( defined ( USE_NORMALMAP_TANGENTSPACE ) || defined ( TANGENTSPACE_NORMALMAP ) )
// Per-Pixel Tangent Space Normal Mapping
// http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html
// three-vrm specific change: it requires \`uv\` as an input in order to support uv scrolls
// Temporary compat against shader change @ Three.js r126, r151
#if THREE_VRM_THREE_REVISION >= 151
mat3 getTangentFrame( vec3 eye_pos, vec3 surf_norm, vec2 uv ) {
vec3 q0 = dFdx( eye_pos.xyz );
vec3 q1 = dFdy( eye_pos.xyz );
vec2 st0 = dFdx( uv.st );
vec2 st1 = dFdy( uv.st );
vec3 N = surf_norm;
vec3 q1perp = cross( q1, N );
vec3 q0perp = cross( N, q0 );
vec3 T = q1perp * st0.x + q0perp * st1.x;
vec3 B = q1perp * st0.y + q0perp * st1.y;
float det = max( dot( T, T ), dot( B, B ) );
float scale = ( det == 0.0 ) ? 0.0 : inversesqrt( det );
return mat3( T * scale, B * scale, N );
}
#else
vec3 perturbNormal2Arb( vec2 uv, vec3 eye_pos, vec3 surf_norm, vec3 mapN, float faceDirection ) {
vec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );
vec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );
vec2 st0 = dFdx( uv.st );
vec2 st1 = dFdy( uv.st );
vec3 N = normalize( surf_norm );
vec3 q1perp = cross( q1, N );
vec3 q0perp = cross( N, q0 );
vec3 T = q1perp * st0.x + q0perp * st1.x;
vec3 B = q1perp * st0.y + q0perp * st1.y;
// three-vrm specific change: Workaround for the issue that happens when delta of uv = 0.0
// TODO: Is this still required? Or shall I make a PR about it?
if ( length( T ) == 0.0 || length( B ) == 0.0 ) {
return surf_norm;
}
float det = max( dot( T, T ), dot( B, B ) );
float scale = ( det == 0.0 ) ? 0.0 : faceDirection * inversesqrt( det );
return normalize( T * ( mapN.x * scale ) + B * ( mapN.y * scale ) + N * mapN.z );
}
#endif
#endif
// #include <specularmap_pars_fragment>
#include <logdepthbuf_pars_fragment>
#include <clipping_planes_pars_fragment>
// == post correction ==========================================================
void postCorrection() {
#include <tonemapping_fragment>
#include <colorspace_fragment>
#include <fog_fragment>
#include <premultiplied_alpha_fragment>
#include <dithering_fragment>
}
// == main procedure ===========================================================
void main() {
#include <clipping_planes_fragment>
vec2 uv = vec2(0.5, 0.5);
#if ( defined( MTOON_USE_UV ) && !defined( MTOON_UVS_VERTEX_ONLY ) )
uv = vUv;
float uvAnimMask = 1.0;
#ifdef USE_UVANIMATIONMASKTEXTURE
vec2 uvAnimationMaskTextureUv = ( uvAnimationMaskTextureUvTransform * vec3( uv, 1 ) ).xy;
uvAnimMask = texture2D( uvAnimationMaskTexture, uvAnimationMaskTextureUv ).b;
#endif
float uvRotCos = cos( uvAnimationRotationPhase * uvAnimMask );
float uvRotSin = sin( uvAnimationRotationPhase * uvAnimMask );
uv = mat2( uvRotCos, -uvRotSin, uvRotSin, uvRotCos ) * ( uv - 0.5 ) + 0.5;
uv = uv + vec2( uvAnimationScrollXOffset, uvAnimationScrollYOffset ) * uvAnimMask;
#endif
#ifdef DEBUG_UV
gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 );
#if ( defined( MTOON_USE_UV ) && !defined( MTOON_UVS_VERTEX_ONLY ) )
gl_FragColor = vec4( uv, 0.0, 1.0 );
#endif
return;
#endif
vec4 diffuseColor = vec4( litFactor, opacity );
ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );
vec3 totalEmissiveRadiance = emissive * emissiveIntensity;
#include <logdepthbuf_fragment>
// #include <map_fragment>
#ifdef USE_MAP
vec2 mapUv = ( mapUvTransform * vec3( uv, 1 ) ).xy;
vec4 sampledDiffuseColor = texture2D( map, mapUv );
#ifdef DECODE_VIDEO_TEXTURE
sampledDiffuseColor = vec4( mix( pow( sampledDiffuseColor.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), sampledDiffuseColor.rgb * 0.0773993808, vec3( lessThanEqual( sampledDiffuseColor.rgb, vec3( 0.04045 ) ) ) ), sampledDiffuseColor.w );
#endif
diffuseColor *= sampledDiffuseColor;
#endif
// #include <color_fragment>
#if ( defined( USE_COLOR ) && !defined( IGNORE_VERTEX_COLOR ) )
diffuseColor.rgb *= vColor;
#endif
// #include <alphamap_fragment>
#include <alphatest_fragment>
// #include <specularmap_fragment>
// #include <normal_fragment_begin>
float faceDirection = gl_FrontFacing ? 1.0 : -1.0;
#ifdef FLAT_SHADED
vec3 fdx = dFdx( vViewPosition );
vec3 fdy = dFdy( vViewPosition );
vec3 normal = normalize( cross( fdx, fdy ) );
#else
vec3 normal = normalize( vNormal );
#ifdef DOUBLE_SIDED
normal *= faceDirection;
#endif
#endif
#ifdef USE_NORMALMAP
vec2 normalMapUv = ( normalMapUvTransform * vec3( uv, 1 ) ).xy;
#endif
#ifdef USE_NORMALMAP_TANGENTSPACE
#ifdef USE_TANGENT
mat3 tbn = mat3( normalize( vTangent ), normalize( vBitangent ), normal );
#else
mat3 tbn = getTangentFrame( - vViewPosition, normal, normalMapUv );
#endif
#if defined( DOUBLE_SIDED ) && ! defined( FLAT_SHADED )
tbn[0] *= faceDirection;
tbn[1] *= faceDirection;
#endif
#endif
#ifdef USE_CLEARCOAT_NORMALMAP
#ifdef USE_TANGENT
mat3 tbn2 = mat3( normalize( vTangent ), normalize( vBitangent ), normal );
#else
mat3 tbn2 = getTangentFrame( - vViewPosition, normal, vClearcoatNormalMapUv );
#endif
#if defined( DOUBLE_SIDED ) && ! defined( FLAT_SHADED )
tbn2[0] *= faceDirection;
tbn2[1] *= faceDirection;
#endif
#endif
// non perturbed normal for clearcoat among others
vec3 nonPerturbedNormal = normal;
#ifdef OUTLINE
normal *= -1.0;
#endif
// #include <normal_fragment_maps>
// COMPAT: pre-r151
// USE_NORMALMAP_OBJECTSPACE used to be OBJECTSPACE_NORMALMAP in pre-r151
#if defined( USE_NORMALMAP_OBJECTSPACE ) || defined( OBJECTSPACE_NORMALMAP )
normal = texture2D( normalMap, normalMapUv ).xyz * 2.0 - 1.0; // overrides both flatShading and attribute normals
#ifdef FLIP_SIDED
normal = - normal;
#endif
#ifdef DOUBLE_SIDED
normal = normal * faceDirection;
#endif
normal = normalize( normalMatrix * normal );
// COMPAT: pre-r151
// USE_NORMALMAP_TANGENTSPACE used to be TANGENTSPACE_NORMALMAP in pre-r151
#elif defined( USE_NORMALMAP_TANGENTSPACE ) || defined( TANGENTSPACE_NORMALMAP )
vec3 mapN = texture2D( normalMap, normalMapUv ).xyz * 2.0 - 1.0;
mapN.xy *= normalScale;
// COMPAT: pre-r151
#if THREE_VRM_THREE_REVISION >= 151 || defined( USE_TANGENT )
normal = normalize( tbn * mapN );
#else
normal = perturbNormal2Arb( uv, -vViewPosition, normal, mapN, faceDirection );
#endif
#endif
// #include <emissivemap_fragment>
#ifdef USE_EMISSIVEMAP
vec2 emissiveMapUv = ( emissiveMapUvTransform * vec3( uv, 1 ) ).xy;
totalEmissiveRadiance *= texture2D( emissiveMap, emissiveMapUv ).rgb;
#endif
#ifdef DEBUG_NORMAL
gl_FragColor = vec4( 0.5 + 0.5 * normal, 1.0 );
return;
#endif
// -- MToon: lighting --------------------------------------------------------
// accumulation
// #include <lights_phong_fragment>
MToonMaterial material;
material.diffuseColor = diffuseColor.rgb;
material.shadeColor = shadeColorFactor;
#ifdef USE_SHADEMULTIPLYTEXTURE
vec2 shadeMultiplyTextureUv = ( shadeMultiplyTextureUvTransform * vec3( uv, 1 ) ).xy;
material.shadeColor *= texture2D( shadeMultiplyTexture, shadeMultiplyTextureUv ).rgb;
#endif
#if ( defined( USE_COLOR ) && !defined( IGNORE_VERTEX_COLOR ) )
material.shadeColor.rgb *= vColor;
#endif
material.shadingShift = shadingShiftFactor;
#ifdef USE_SHADINGSHIFTTEXTURE
vec2 shadingShiftTextureUv = ( shadingShiftTextureUvTransform * vec3( uv, 1 ) ).xy;
material.shadingShift += texture2D( shadingShiftTexture, shadingShiftTextureUv ).r * shadingShiftTextureScale;
#endif
// #include <lights_fragment_begin>
// MToon Specific changes:
// Since we want to take shadows into account of shading instead of irradiance,
// we had to modify the codes that multiplies the results of shadowmap into color of direct lights.
// COMPAT: pre-r156 uses a struct GeometricContext
#if THREE_VRM_THREE_REVISION >= 157
vec3 geometryPosition = - vViewPosition;
vec3 geometryNormal = normal;
vec3 geometryViewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
vec3 geometryClearcoatNormal;
#ifdef USE_CLEARCOAT
geometryClearcoatNormal = clearcoatNormal;
#endif
#else
GeometricContext geometry;
geometry.position = - vViewPosition;
geometry.normal = normal;
geometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );
#ifdef USE_CLEARCOAT
geometry.clearcoatNormal = clearcoatNormal;
#endif
#endif
IncidentLight directLight;
// since these variables will be used in unrolled loop, we have to define in prior
float shadow;
#if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )
PointLight pointLight;
#if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0
PointLightShadow pointLightShadow;
#endif
#pragma unroll_loop_start
for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
pointLight = pointLights[ i ];
// COMPAT: pre-r156 uses a struct GeometricContext
#if THREE_VRM_THREE_REVISION >= 157
getPointLightInfo( pointLight, geometryPosition, directLight );
#else
getPointLightInfo( pointLight, geometry, directLight );
#endif
shadow = 1.0;
#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )
pointLightShadow = pointLightShadows[ i ];
// COMPAT: pre-r166
// r166 introduced shadowIntensity
#if THREE_VRM_THREE_REVISION >= 166
shadow = all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowIntensity, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
#else
shadow = all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;
#endif
#endif
// COMPAT: pre-r156 uses a struct GeometricContext
#if THREE_VRM_THREE_REVISION >= 157
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, shadow, reflectedLight );
#else
RE_Direct( directLight, geometry, material, shadow, reflectedLight );
#endif
}
#pragma unroll_loop_end
#endif
#if ( NUM_SPOT_LIGHTS > 0 ) && defined( RE_Direct )
SpotLight spotLight;
#if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0
SpotLightShadow spotLightShadow;
#endif
#pragma unroll_loop_start
for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
spotLight = spotLights[ i ];
// COMPAT: pre-r156 uses a struct GeometricContext
#if THREE_VRM_THREE_REVISION >= 157
getSpotLightInfo( spotLight, geometryPosition, directLight );
#else
getSpotLightInfo( spotLight, geometry, directLight );
#endif
shadow = 1.0;
#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )
spotLightShadow = spotLightShadows[ i ];
// COMPAT: pre-r166
// r166 introduced shadowIntensity
#if THREE_VRM_THREE_REVISION >= 166
shadow = all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowIntensity, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;
#else
shadow = all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;
#endif
#endif
// COMPAT: pre-r156 uses a struct GeometricContext
#if THREE_VRM_THREE_REVISION >= 157
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, shadow, reflectedLight );
#else
RE_Direct( directLight, geometry, material, shadow, reflectedLight );
#endif
}
#pragma unroll_loop_end
#endif
#if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct )
DirectionalLight directionalLight;
#if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
DirectionalLightShadow directionalLightShadow;
#endif
#pragma unroll_loop_start
for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
directionalLight = directionalLights[ i ];
// COMPAT: pre-r156 uses a struct GeometricContext
#if THREE_VRM_THREE_REVISION >= 157
getDirectionalLightInfo( directionalLight, directLight );
#else
getDirectionalLightInfo( directionalLight, geometry, directLight );
#endif
shadow = 1.0;
#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )
directionalLightShadow = directionalLightShadows[ i ];
// COMPAT: pre-r166
// r166 introduced shadowIntensity
#if THREE_VRM_THREE_REVISION >= 166
shadow = all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowIntensity, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
#else
shadow = all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;
#endif
#endif
// COMPAT: pre-r156 uses a struct GeometricContext
#if THREE_VRM_THREE_REVISION >= 157
RE_Direct( directLight, geometryPosition, geometryNormal, geometryViewDir, geometryClearcoatNormal, material, shadow, reflectedLight );
#else
RE_Direct( directLight, geometry, material, shadow, reflectedLight );
#endif
}
#pragma unroll_loop_end
#endif
// #if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea )
// RectAreaLight rectAreaLight;
// #pragma unroll_loop_start
// for ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {
// rectAreaLight = rectAreaLights[ i ];
// RE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );
// }
// #pragma unroll_loop_end
// #endif
#if defined( RE_IndirectDiffuse )
vec3 iblIrradiance = vec3( 0.0 );
vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );
// COMPAT: pre-r156 uses a struct GeometricContext
// COMPAT: pre-r156 doesn't have a define USE_LIGHT_PROBES
#if THREE_VRM_THREE_REVISION >= 157
#if defined( USE_LIGHT_PROBES )
irradiance += getLightProbeIrradiance( lightProbe, geometryNormal );
#endif
#else
irradiance += getLightProbeIrradiance( lightProbe, geometry.normal );
#endif
#if ( NUM_HEMI_LIGHTS > 0 )
#pragma unroll_loop_start
for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
// COMPAT: pre-r156 uses a struct GeometricContext
#if THREE_VRM_THREE_REVISION >= 157
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometryNormal );
#else
irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );
#endif
}
#pragma unroll_loop_end
#endif
#endif
// #if defined( RE_IndirectSpecular )
// vec3 radiance = vec3( 0.0 );
// vec3 clearcoatRadiance = vec3( 0.0 );
// #endif
#include <lights_fragment_maps>
#include <lights_fragment_end>
// modulation
#include <aomap_fragment>
vec3 col = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse;
#ifdef DEBUG_LITSHADERATE
gl_FragColor = vec4( col, diffuseColor.a );
postCorrection();
return;
#endif
// -- MToon: rim lighting -----------------------------------------
vec3 viewDir = normalize( vViewPosition );
#ifndef PHYSICALLY_CORRECT_LIGHTS
reflectedLight.directSpecular /= PI;
#endif
vec3 rimMix = mix( vec3( 1.0 ), reflectedLight.directSpecular, 1.0 );
vec3 rim = parametricRimColorFactor * pow( saturate( 1.0 - dot( viewDir, normal ) + parametricRimLiftFactor ), parametricRimFresnelPowerFactor );
#ifdef USE_MATCAPTEXTURE
{
vec3 x = normalize( vec3( viewDir.z, 0.0, -viewDir.x ) );
vec3 y = cross( viewDir, x ); // guaranteed to be normalized
vec2 sphereUv = 0.5 + 0.5 * vec2( dot( x, normal ), -dot( y, normal ) );
sphereUv = ( matcapTextureUvTransform * vec3( sphereUv, 1 ) ).xy;
vec3 matcap = texture2D( matcapTexture, sphereUv ).rgb;
rim += matcapFactor * matcap;
}
#endif
#ifdef USE_RIMMULTIPLYTEXTURE
vec2 rimMultiplyTextureUv = ( rimMultiplyTextureUvTransform * vec3( uv, 1 ) ).xy;
rim *= texture2D( rimMultiplyTexture, rimMultiplyTextureUv ).rgb;
#endif
col += rimMix * rim;
// -- MToon: Emission --------------------------------------------------------
col += totalEmissiveRadiance;
// #include <envmap_fragment>
// -- Almost done! -----------------------------------------------------------
#if defined( OUTLINE )
col = outlineColorFactor.rgb * mix( vec3( 1.0 ), col, outlineLightingMixFactor );
#endif
#ifdef OPAQUE
diffuseColor.a = 1.0;
#endif
gl_FragColor = vec4( col, diffuseColor.a );
postCorrection();
}
`;var v={None:"none",Normal:"normal",LitShadeRate:"litShadeRate",UV:"uv"};var T={None:"none",WorldCoordinates:"worldCoordinates",ScreenCoordinates:"screenCoordinates"};var P=p(require("three"),1),z={3e3:"",3001:"srgb"};function g(a){return parseInt(P.REVISION,10)>=152?a.colorSpace:z[a.encoding]}var c=class extends o.ShaderMaterial{constructor(e={}){var t;super({vertexShader:O,fragmentShader:C});this.uvAnimationScrollXSpeedFactor=0;this.uvAnimationScrollYSpeedFactor=0;this.uvAnimationRotationSpeedFactor=0;this.fog=!0;this.normalMapType=o.TangentSpaceNormalMap;this._ignoreVertexColor=!0;this._v0CompatShade=!1;this._debugMode=v.None;this._outlineWidthMode=T.None;this._isOutline=!1;e.transparentWithZWrite&&(e.depthWrite=!0),delete e.transparentWithZWrite,e.fog=!0,e.lights=!0,e.clipping=!0,this.uniforms=o.UniformsUtils.merge([o.UniformsLib.common,o.UniformsLib.normalmap,o.UniformsLib.emissivemap,o.UniformsLib.fog,o.UniformsLib.lights,{litFactor:{value:new o.Color(1,1,1)},mapUvTransform:{value:new o.Matrix3},colorAlpha:{value:1},normalMapUvTransform:{value:new o.Matrix3},shadeColorFactor:{value:new o.Color(0,0,0)},shadeMultiplyTexture:{value:null},shadeMultiplyTextureUvTransform:{value:new o.Matrix3},shadingShiftFactor:{value:0},shadingShiftTexture:{value:null},shadingShiftTextureUvTransform:{value:new o.Matrix3},shadingShiftTextureScale:{value:1},shadingToonyFactor:{value:.9},giEqualizationFactor:{value:.9},matcapFactor:{value:new o.Color(1,1,1)},matcapTexture:{value:null},matcapTextureUvTransform:{value:new o.Matrix3},parametricRimColorFactor:{value:new o.Color(0,0,0)},rimMultiplyTexture:{value:null},rimMultiplyTextureUvTransform:{value:new o.Matrix3},rimLightingMixFactor:{value:1},parametricRimFresnelPowerFactor:{value:5},parametricRimLiftFactor:{value:0},emissive:{value:new o.Color(0,0,0)},emissiveIntensity:{value:1},emissiveMapUvTransform:{value:new o.Matrix3},outlineWidthMultiplyTexture:{value:null},outlineWidthMultiplyTextureUvTransform:{value:new o.Matrix3},outlineWidthFactor:{value:0},outlineColorFactor:{value:new o.Color(0,0,0)},outlineLightingMixFactor:{value:1},uvAnimationMaskTexture:{value:null},uvAnimationMaskTextureUvTransform:{value:new o.Matrix3},uvAnimationScrollXOffset:{value:0},uvAnimationScrollYOffset:{value:0},uvAnimationRotationPhase:{value:0}},(t=e.uniforms)!=null?t:{}]),this.setValues(e),this._uploadUniformsWorkaround(),this.customProgramCacheKey=()=>[...Object.entries(this._generateDefines()).map(([r,n])=>`${r}:${n}`),this.matcapTexture?`matcapTextureColorSpace:${g(this.matcapTexture)}`:"",this.shadeMultiplyTexture?`shadeMultiplyTextureColorSpace:${g(this.shadeMultiplyTexture)}`:"",this.rimMultiplyTexture?`rimMultiplyTextureColorSpace:${g(this.rimMultiplyTexture)}`:""].join(","),this.onBeforeCompile=r=>{let n=parseInt(o.REVISION,10),s=Object.entries(S(S({},this._generateDefines()),this.defines)).filter(([u,l])=>!!l).map(([u,l])=>`#define ${u} ${l}`).join(`
`)+`
`;r.vertexShader=s+r.vertexShader,r.fragmentShader=s+r.fragmentShader,n<154&&(r.fragmentShader=r.fragmentShader.replace("#include <colorspace_fragment>","#include <encodings_fragment>"))}}get color(){return this.uniforms.litFactor.value}set color(e){this.uniforms.litFactor.value=e}get map(){return this.uniforms.map.value}set map(e){this.uniforms.map.value=e}get normalMap(){return this.uniforms.normalMap.value}set normalMap(e){this.uniforms.normalMap.value=e}get normalScale(){return this.uniforms.normalScale.value}set normalScale(e){this.uniforms.normalScale.value=e}get emissive(){return this.uniforms.emissive.value}set emissive(e){this.uniforms.emissive.value=e}get emissiveIntensity(){return this.uniforms.emissiveIntensity.value}set emissiveIntensity(e){this.uniforms.emissiveIntensity.value=e}get emissiveMap(){return this.uniforms.emissiveMap.value}set emissiveMap(e){this.uniforms.emissiveMap.value=e}get shadeColorFactor(){return this.uniforms.shadeColorFactor.value}set shadeColorFactor(e){this.uniforms.shadeColorFactor.value=e}get shadeMultiplyTexture(){return this.uniforms.shadeMultiplyTexture.value}set shadeMultiplyTexture(e){this.uniforms.shadeMultiplyTexture.value=e}get shadingShiftFactor(){return this.uniforms.shadingShiftFactor.value}set shadingShiftFactor(e){this.uniforms.shadingShiftFactor.value=e}get shadingShiftTexture(){return this.uniforms.shadingShiftTexture.value}set shadingShiftTexture(e){this.uniforms.shadingShiftTexture.value=e}get shadingShiftTextureScale(){return this.uniforms.shadingShiftTextureScale.value}set shadingShiftTextureScale(e){this.uniforms.shadingShiftTextureScale.value=e}get shadingToonyFactor(){return this.uniforms.shadingToonyFactor.value}set shadingToonyFactor(e){this.uniforms.shadingToonyFactor.value=e}get giEqualizationFactor(){return this.uniforms.giEqualizationFactor.value}set giEqualizationFactor(e){this.uniforms.giEqualizationFactor.value=e}get matcapFactor(){return this.uniforms.matcapFactor.value}set matcapFactor(e){this.uniforms.matcapFactor.value=e}get matcapTexture(){return this.uniforms.matcapTexture.value}set matcapTexture(e){this.uniforms.matcapTexture.value=e}get parametricRimColorFactor(){return this.uniforms.parametricRimColorFactor.value}set parametricRimColorFactor(e){this.uniforms.parametricRimColorFactor.value=e}get rimMultiplyTexture(){return this.uniforms.rimMultiplyTexture.value}set rimMultiplyTexture(e){this.uniforms.rimMultiplyTexture.value=e}get rimLightingMixFactor(){return this.uniforms.rimLightingMixFactor.value}set rimLightingMixFactor(e){this.uniforms.rimLightingMixFactor.value=e}get parametricRimFresnelPowerFactor(){return this.uniforms.parametricRimFresnelPowerFactor.value}set parametricRimFresnelPowerFactor(e){this.uniforms.parametricRimFresnelPowerFactor.value=e}get parametricRimLiftFactor(){return this.uniforms.parametricRimLiftFactor.value}set parametricRimLiftFactor(e){this.uniforms.parametricRimLiftFactor.value=e}get outlineWidthMultiplyTexture(){return this.uniforms.outlineWidthMultiplyTexture.value}set outlineWidthMultiplyTexture(e){this.uniforms.outlineWidthMultiplyTexture.value=e}get outlineWidthFactor(){return this.uniforms.outlineWidthFactor.value}set outlineWidthFactor(e){this.uniforms.outlineWidthFactor.value=e}get outlineColorFactor(){return this.uniforms.outlineColorFactor.value}set outlineColorFactor(e){this.uniforms.outlineColorFactor.value=e}get outlineLightingMixFactor(){return this.uniforms.outlineLightingMixFactor.value}set outlineLightingMixFactor(e){this.uniforms.outlineLightingMixFactor.value=e}get uvAnimationMaskTexture(){return this.uniforms.uvAnimationMaskTexture.value}set uvAnimationMaskTexture(e){this.uniforms.uvAnimationMaskTexture.value=e}get uvAnimationScrollXOffset(){return this.uniforms.uvAnimationScrollXOffset.value}set uvAnimationScrollXOffset(e){this.uniforms.uvAnimationScrollXOffset.value=e}get uvAnimationScrollYOffset(){return this.uniforms.uvAnimationScrollYOffset.value}set uvAnimationScrollYOffset(e){this.uniforms.uvAnimationScrollYOffset.value=e}get uvAnimationRotationPhase(){return this.uniforms.uvAnimationRotationPhase.value}set uvAnimationRotationPhase(e){this.uniforms.uvAnimationRotationPhase.value=e}get ignoreVertexColor(){return this._ignoreVertexColor}set ignoreVertexColor(e){this._ignoreVertexColor=e,this.needsUpdate=!0}get v0CompatShade(){return this._v0CompatShade}set v0CompatShade(e){this._v0CompatShade=e,this.needsUpdate=!0}get debugMode(){return this._debugMode}set debugMode(e){this._debugMode=e,this.needsUpdate=!0}get outlineWidthMode(){return this._outlineWidthMode}set outlineWidthMode(e){this._outlineWidthMode=e,this.needsUpdate=!0}get isOutline(){return this._isOutline}set isOutline(e){this._isOutline=e,this.needsUpdate=!0}get isMToonMaterial(){return!0}update(e){this._uploadUniformsWorkaround(),this._updateUVAnimation(e)}copy(e){return super.copy(e),this.map=e.map,this.normalMap=e.normalMap,this.emissiveMap=e.emissiveMap,this.shadeMultiplyTexture=e.shadeMultiplyTexture,this.shadingShiftTexture=e.shadingShiftTexture,this.matcapTexture=e.matcapTexture,this.rimMultiplyTexture=e.rimMultiplyTexture,this.outlineWidthMultiplyTexture=e.outlineWidthMultiplyTexture,this.uvAnimationMaskTexture=e.uvAnimationMaskTexture,this.normalMapType=e.normalMapType,this.uvAnimationScrollXSpeedFactor=e.uvAnimationScrollXSpeedFactor,this.uvAnimationScrollYSpeedFactor=e.uvAnimationScrollYSpeedFactor,this.uvAnimationRotationSpeedFactor=e.uvAnimationRotationSpeedFactor,this.ignoreVertexColor=e.ignoreVertexColor,this.v0CompatShade=e.v0CompatShade,this.debugMode=e.debugMode,this.outlineWidthMode=e.outlineWidthMode,this.isOutline=e.isOutline,this.needsUpdate=!0,this}_updateUVAnimation(e){this.uniforms.uvAnimationScrollXOffset.value+=e*this.uvAnimationScrollXSpeedFactor,this.uniforms.uvAnimationScrollYOffset.value+=e*this.uvAnimationScrollYSpeedFactor,this.uniforms.uvAnimationRotationPhase.value+=e*this.uvAnimationRotationSpeedFactor,this.uniforms.alphaTest.value=this.alphaTest,this.uniformsNeedUpdate=!0}_uploadUniformsWorkaround(){this.uniforms.opacity.value=this.opacity,this._updateTextureMatrix(this.uniforms.map,this.uniforms.mapUvTransform),this._updateTextureMatrix(this.uniforms.normalMap,this.uniforms.normalMapUvTransform),this._updateTextureMatrix(this.uniforms.emissiveMap,this.uniforms.emissiveMapUvTransform),this._updateTextureMatrix(this.uniforms.shadeMultiplyTexture,this.uniforms.shadeMultiplyTextureUvTransform),this._updateTextureMatrix(this.uniforms.shadingShiftTexture,this.uniforms.shadingShiftTextureUvTransform),this._updateTextureMatrix(this.uniforms.matcapTexture,this.uniforms.matcapTextureUvTransform),this._updateTextureMatrix(this.uniforms.rimMultiplyTexture,this.uniforms.rimMultiplyTextureUvTransform),this._updateTextureMatrix(this.uniforms.outlineWidthMultiplyTexture,this.uniforms.outlineWidthMultiplyTextureUvTransform),this._updateTextureMatrix(this.uniforms.uvAnimationMaskTexture,this.uniforms.uvAnimationMaskTextureUvTransform),this.uniformsNeedUpdate=!0}_generateDefines(){let e=parseInt(o.REVISION,10),t=this.outlineWidthMultiplyTexture!==null,r=this.map!==null||this.normalMap!==null||this.emissiveMap!==null||this.shadeMultiplyTexture!==null||this.shadingShiftTexture!==null||this.rimMultiplyTexture!==null||this.uvAnimationMaskTexture!==null;return{THREE_VRM_THREE_REVISION:e,OUTLINE:this._isOutline,MTOON_USE_UV:t||r,MTOON_UVS_VERTEX_ONLY:t&&!r,V0_COMPAT_SHADE:this._v0CompatShade,USE_SHADEMULTIPLYTEXTURE:this.shadeMultiplyTexture!==null,USE_SHADINGSHIFTTEXTURE:this.shadingShiftTexture!==null,USE_MATCAPTEXTURE:this.matcapTexture!==null,USE_RIMMULTIPLYTEXTURE:this.rimMultiplyTexture!==null,USE_OUTLINEWIDTHMULTIPLYTEXTURE:this._isOutline&&this.outlineWidthMultiplyTexture!==null,USE_UVANIMATIONMASKTEXTURE:this.uvAnimationMaskTexture!==null,IGNORE_VERTEX_COLOR:this._ignoreVertexColor===!0,DEBUG_NORMAL:this._debugMode==="normal",DEBUG_LITSHADERATE:this._debugMode==="litShadeRate",DEBUG_UV:this._debugMode==="uv",OUTLINE_WIDTH_SCREEN:this._isOutline&&this._outlineWidthMode===T.ScreenCoordinates}}_updateTextureMatrix(e,t){e.value&&(e.value.matrixAutoUpdate&&e.value.updateMatrix(),t.value.copy(e.value.matrix))}};var q=new Set(["1.0","1.0-beta"]),f=class f{get name(){return f.EXTENSION_NAME}constructor(i,e={}){var t,r,n,s;this.parser=i,this.materialType=(t=e.materialType)!=null?t:c,this.renderOrderOffset=(r=e.renderOrderOffset)!=null?r:0,this.v0CompatShade=(n=e.v0CompatShade)!=null?n:!1,this.debugMode=(s=e.debugMode)!=null?s:"none",this._mToonMaterialSet=new Set}beforeRoot(){return m(this,null,function*(){this._removeUnlitExtensionIfMToonExists()})}afterRoot(i){return m(this,null,function*(){i.userData.vrmMToonMaterials=Array.from(this._mToonMaterialSet)})}getMaterialType(i){return this._getMToonExtension(i)?this.materialType:null}extendMaterialParams(i,e){let t=this._getMToonExtension(i);return t?this._extendMaterialParams(t,e):null}loadMesh(i){return m(this,null,function*(){var u;let e=this.parser,r=(u=e.json.meshes)==null?void 0:u[i];if(r==null)throw new Error(`MToonMaterialLoaderPlugin: Attempt to use meshes[${i}] of glTF but the mesh doesn't exist`);let n=r.primitives,s=yield e.loadMesh(i);if(n.length===1){let l=s,d=n[0].material;d!=null&&this._setupPrimitive(l,d)}else{let l=s;for(let d=0;d<n.length;d++){let I=l.children[d],R=n[d].material;R!=null&&this._setupPrimitive(I,R)}}return s})}_removeUnlitExtensionIfMToonExists(){let t=this.parser.json.materials;t==null||t.map((r,n)=>{var u;this._getMToonExtension(n)&&((u=r.extensions)!=null&&u.KHR_materials_unlit)&&delete r.extensions.KHR_materials_unlit})}_getMToonExtension(i){var u,l;let r=(u=this.parser.json.materials)==null?void 0:u[i];if(r==null){console.warn(`MToonMaterialLoaderPlugin: Attempt to use materials[${i}] of glTF but the material doesn't exist`);return}let n=(l=r.extensions)==null?void 0:l[f.EXTENSION_NAME];if(n==null)return;let s=n.specVersion;if(!q.has(s)){console.warn(`MToonMaterialLoaderPlugin: Unknown ${f.EXTENSION_NAME} specVersion "${s}"`);return}return n}_extendMaterialParams(i,e){return m(this,null,function*(){var r;delete e.metalness,delete e.roughness;let t=new E(this.parser,e);t.assignPrimitive("transparentWithZWrite",i.transparentWithZWrite),t.assignColor("shadeColorFactor",i.shadeColorFactor),t.assignTexture("shadeMultiplyTexture",i.shadeMultiplyTexture,!0),t.assignPrimitive("shadingShiftFactor",i.shadingShiftFactor),t.assignTexture("shadingShiftTexture",i.shadingShiftTexture,!0),t.assignPrimitive("shadingShiftTextureScale",(r=i.shadingShiftTexture)==null?void 0:r.scale),t.assignPrimitive("shadingToonyFactor",i.shadingToonyFactor),t.assignPrimitive("giEqualizationFactor",i.giEqualizationFactor),t.assignColor("matcapFactor",i.matcapFactor),t.assignTexture("matcapTexture",i.matcapTexture,!0),t.assignColor("parametricRimColorFactor",i.parametricRimColorFactor),t.assignTexture("rimMultiplyTexture",i.rimMultiplyTexture,!0),t.assignPrimitive("rimLightingMixFactor",i.rimLightingMixFactor),t.assignPrimitive("parametricRimFresnelPowerFactor",i.parametricRimFresnelPowerFactor),t.assignPrimitive("parametricRimLiftFactor",i.parametricRimLiftFactor),t.assignPrimitive("outlineWidthMode",i.outlineWidthMode),t.assignPrimitive("outlineWidthFactor",i.outlineWidthFactor),t.assignTexture("outlineWidthMultiplyTexture",i.outlineWidthMultiplyTexture,!1),t.assignColor("outlineColorFactor",i.outlineColorFactor),t.assignPrimitive("outlineLightingMixFactor",i.outlineLightingMixFactor),t.assignTexture("uvAnimationMaskTexture",i.uvAnimationMaskTexture,!1),t.assignPrimitive("uvAnimationScrollXSpeedFactor",i.uvAnimationScrollXSpeedFactor),t.assignPrimitive("uvAnimationScrollYSpeedFactor",i.uvAnimationScrollYSpeedFactor),t.assignPrimitive("uvAnimationRotationSpeedFactor",i.uvAnimationRotationSpeedFactor),t.assignPrimitive("v0CompatShade",this.v0CompatShade),t.assignPrimitive("debugMode",this.debugMode),yield t.pending})}_setupPrimitive(i,e){let t=this._getMToonExtension(e);if(t){let r=this._parseRenderOrder(t);i.renderOrder=r+this.renderOrderOffset,this._generateOutline(i),this._addToMaterialSet(i);return}}_shouldGenerateOutline(i){return typeof i.outlineWidthMode=="string"&&i.outlineWidthMode!=="none"&&typeof i.outlineWidthFactor=="number"&&i.outlineWidthFactor>0}_generateOutline(i){let e=i.material;if(!(e instanceof _.Material)||!this._shouldGenerateOutline(e))return;i.material=[e];let t=e.clone();t.name+=" (Outline)",t.isOutline=!0,t.side=_.BackSide,i.material.push(t);let r=i.geometry,n=r.index?r.index.count:r.attributes.position.count/3;r.addGroup(0,n,0),r.addGroup(0,n,1)}_addToMaterialSet(i){let e=i.material,t=new Set;Array.isArray(e)?e.forEach(r=>t.add(r)):t.add(e);for(let r of t)this._mToonMaterialSet.add(r)}_parseRenderOrder(i){var t;return(i.transparentWithZWrite?0:19)+((t=i.renderQueueOffsetNumber)!=null?t:0)}};f.EXTENSION_NAME="VRMC_materials_mtoon";var M=f;