Files
PrinceOfGlory/Assets/JGW/Script/MipmapLevelController.cs
kridoo ac041525cc 1
2026-01-12 09:39:28 +08:00

114 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class MipmapLevelController : MonoBehaviour
{
[Range(-3, 3)]
public float mipMapBias = 0f;
private float lastMipMapBias = 0f;
private List<Renderer> allRenderers = new List<Renderer>();
void OnEnable()
{
CollectAllRenderers();
ApplyMipMapBias();
}
void OnDisable()
{
// 恢复原始设置
ResetMipMapBias();
}
void Update()
{
// 检查MipMap偏移是否发生变化
if (mipMapBias != lastMipMapBias)
{
ApplyMipMapBias();
lastMipMapBias = mipMapBias;
}
}
void CollectAllRenderers()
{
allRenderers.Clear();
// 获取场景中所有的渲染器
Renderer[] renderers = FindObjectsOfType<Renderer>(true);
foreach (Renderer renderer in renderers)
{
allRenderers.Add(renderer);
}
//Debug.Log($"收集到 {allRenderers.Count} 个渲染器");
}
void ApplyMipMapBias()
{
foreach (Renderer renderer in allRenderers)
{
if (renderer == null) continue;
Material[] materials = renderer.sharedMaterials;
for (int i = 0; i < materials.Length; i++)
{
if (materials[i] == null) continue;
foreach (string texName in materials[i].GetTexturePropertyNames())
{
if (materials[i].HasProperty(texName))
{
Texture texture = materials[i].GetTexture(texName);
if (texture != null)
{
texture.mipMapBias = mipMapBias;
}
}
}
}
}
//Debug.Log($"已应用MipMap偏移{mipMapBias}");
}
void ResetMipMapBias()
{
foreach (Renderer renderer in allRenderers)
{
if (renderer == null) continue;
Material[] materials = renderer.sharedMaterials;
for (int i = 0; i < materials.Length; i++)
{
if (materials[i] == null) continue;
foreach (string texName in materials[i].GetTexturePropertyNames())
{
if (materials[i].HasProperty(texName))
{
Texture texture = materials[i].GetTexture(texName);
if (texture != null)
{
texture.mipMapBias = 0f;
}
}
}
}
}
}
[ContextMenu("刷新渲染器列表")]
public void RefreshRenderers()
{
CollectAllRenderers();
ApplyMipMapBias();
}
}