// ---- Created with 3Dmigoto v1.3.16 on Mon Feb 16 11:25:54 2026
Texture2D<float4> t1 : register(t1);
Texture2D<float4> t0 : register(t0);
SamplerState s1_s : register(s1);
SamplerState s0_s : register(s0);
cbuffer cb3 : register(b3) { float4 cb3[21]; }
cbuffer cb2 : register(b2) { float4 cb2[112]; }
cbuffer cb1 : register(b1) { float4 cb1[5]; }
cbuffer cb0 : register(b0) { float4 cb0[44]; }

// 3Dmigoto declarations
#define cmp -
Texture1D<float4> IniParams : register(t120);

void main(
  float4 v0 : SV_POSITION0,
  float4 v1 : COLOR0,
  float4 v2 : TEXCOORD0,
  float2 v3 : texcoord4,
  float2 w3 : TEXCOORD6,
  float4 v4 : TEXCOORD5,
  float4 v5 : TEXCOORD7,
  float2 v6 : TEXCOORD8,
  out float4 o0 : SV_TARGET0)
{
  const float4 icb[] = { { 1.000000, 0, 0, 0},
                              { 0, 1.000000, 0, 0},
                              { 0, 0, 1.000000, 0},
                              { 0, 0, 0, 1.000000} };
  float4 r0,r1,r2,r3;

  // [变量] 原始颜色备份
  float3 rawTextureColor;

  r0.x = cmp(0 != cb3[20].x);
  if (r0.x != 0) {
    r0.x = cmp(cb3[20].y < 0.949999988);
    if (r0.x != 0) {
      r0.xy = v4.yx / v4.ww;
      r0.xy = cb1[4].yx * r0.xy;
      r0.xy = (uint2)r0.xy;
      r0.xy = (int2)r0.xy & int2(3,3);
      r1.x = dot(float4(1,13,4,16), icb[r0.y+0].xyzw);
      r1.y = dot(float4(9,5,12,8), icb[r0.y+0].xyzw);
      r1.z = dot(float4(3,15,2,14), icb[r0.y+0].xyzw);
      r1.w = dot(float4(11,7,10,6), icb[r0.y+0].xyzw);
      r0.x = dot(r1.xyzw, icb[r0.x+0].xyzw);
      r0.y = 17 + -r0.x;
      r0.x = cb0[38].x ? r0.y : r0.x;
      r0.x = cb3[20].y * 17 + -r0.x;
      r0.x = -0.00999999978 + r0.x;
    } else {
      r0.x = 1;
    }
    r0.x = min(1, r0.x);
  } else {
    r0.x = 1;
  }
  r0.x = cmp(r0.x < 0);
  if (r0.x != 0) discard;

  // 1. 采样原始贴图
  r0.xyzw = t0.Sample(s0_s, v2.xy).xyzw;

  // 黑边修复

  // 纯黑剔除 (Blackness Test)
  // 如果贴图本身不是透明的，而是用黑色代表镂空，这一步会生效。
  // 计算亮度总和
  float brightness = r0.r + r0.g + r0.b;

  // 阈值设定为 0.05。
  // 意思是：如果 (R+G+B) 加起来还不到 0.05 (非常黑)，就视为透明区域剔除。
  if (brightness < 0.05) discard;

  // 备份通过测试的颜色
  rawTextureColor = r0.xyz;

  r1.x = cmp(cb3[20].z == 0.000000);
  if (r1.x != 0) {
    r1.xyzw = t1.Sample(s1_s, v2.zw).xyzw;
    r1.x = dot(r1.xyz, cb3[5].xyz);
    r1.x = cb3[11].z + r1.x;
    r1.x = r1.x * v3.x + cb3[10].z;
    r1.y = 1 + -cb3[10].w;
    r1.x = r1.x + -r1.y;
    r1.y = cb3[10].w + -r1.y;
    r1.y = 1 / r1.y;
    r1.x = saturate(r1.x * r1.y);
  } else {
    r1.y = cb3[10].z + v2.z;
    r1.z = 1 + -cb3[10].w;
    r1.y = r1.y + -r1.z;
    r1.z = cb3[10].w + -r1.z;
    r1.z = 1 / r1.z;
    r1.y = saturate(r1.y * r1.z);
    r1.zw = cmp(cb3[20].zz == float2(1,2));
    r1.w = r1.w ? 1.000000 : 0;
    r1.x = r1.z ? r1.y : r1.w;
  }
  r2.xyzw = v1.xyzw * r0.xyzw;
  r2.w = saturate(r2.w);
  r1.yzw = saturate(cb3[3].www + r0.xyz);
  r3.x = dot(r0.xyz, cb3[3].xyz);
  r3.w = saturate(v1.w * r3.x);
  r3.xyz = v1.xyz * r1.yzw;
  r1.yz = cmp(asint(cb3[15].xx) == int2(1,2));
  r1.w = saturate(dot(cb3[4].xyzw, r0.xyzw));
  r0.x = dot(cb3[3].xyzw, r0.xyzw);
  r0.w = saturate(v1.w * r0.x);
  r0.xyz = v1.xyz * r1.www;
  r0.xyzw = r1.zzzz ? r0.xyzw : v1.xyzw;
  r0.xyzw = r1.yyyy ? r3.xyzw : r0.xyzw;
  r0.xyzw = cb3[15].xxxx ? r0.xyzw : r2.xyzw;

  // --- [樱花滤镜 + 橙色捕获] ---

  // 1. 樱花粉色
  float luma = dot(r0.xyz, float3(0.299, 0.587, 0.114));
  float3 sakuraColor = luma * float3(1.8, 0.8, 1.1);

  // 2. 智能判定 (橙色捕获版)
  // 条件A: 只要红色的 1.25 倍 > 绿色，就认为是黄/橙/红
  bool isWarm = (rawTextureColor.r * 1.25) >= rawTextureColor.g;

  // 条件B: 排除冷色 (降低红蓝比要求，允许反光)
  bool isNotCold = rawTextureColor.r >= (rawTextureColor.b * 1.2);

  // 条件C: 亮度要求 (降低要求，允许暗橙色)
  bool isBright = rawTextureColor.r >= 0.10;

  if (isWarm && isNotCold && isBright) {
      r0.xyz = sakuraColor;
  }
  // -------------------------

  r0.w = r0.w * r1.x;
  r1.x = cmp(0.5 < cb3[14].y);
  r1.y = 1 + -cb3[14].z;
  r1.z = cmp(0 >= cb3[14].w);
  r1.z = r1.z ? 1 : cb3[14].w;
  r1.w = 1 + -r1.y;
  r1.y = v0.z * r1.z + -r1.y;
  r1.z = 1 / r1.w;
  r1.y = saturate(r1.y * r1.z);
  r1.z = r1.y * -2 + 3;
  r1.y = r1.y * r1.y;
  r1.y = -r1.z * r1.y + 1;
  r1.y = r1.y * r0.w;
  r0.w = r1.x ? r1.y : r0.w;
  r1.x = 1 + -cb3[13].x;
  r0.w = r1.x * r0.w;
  r1.xy = -cb0[43].yz * cb2[1].xy + float2(1,1);
  r0.xyz = r1.xxx * r0.xyz;
  r0.xyz = r0.xyz * r1.yyy;
  r0.xyz = cb2[111].www * r0.xyz;
  r1.x = max(r0.x, r0.y);
  r1.x = max(r1.x, r0.z);
  r1.x = r1.x * r0.w;
  r1.x = max(0.00999999978, r1.x);
  r0.xyz = r0.xyz / r1.xxx;
  r1.y = cmp(cb2[79].x < r1.x);
  r1.z = -cb2[79].x + cb2[78].w;
  r1.w = -cb2[79].x + r1.x;
  r2.x = 1 / r1.z;
  r1.w = saturate(r2.x * r1.w);
  r2.x = r1.w * -2 + 3;
  r1.w = r1.w * r1.w;
  r1.w = r2.x * r1.w;
  r1.z = r1.w * r1.z + cb2[79].x;
  r1.z = r1.z + -r1.x;
  r1.z = cb2[79].y * r1.z + r1.x;
  r1.x = r1.y ? r1.z : r1.x;
  o0.xyz = r1.xxx * r0.xyz;
  o0.w = r0.w;
  return;
}
