You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity2D中如何运行时生成程序化纸张纹理并应用于2D平面?

Unity2D运行时生成程序化纸张纹理的可行方案

方案1:手动创建Texture2D生成基础纸张纹理

  • 核心逻辑:通过Texture2D类创建空白纹理,遍历像素添加随机噪点、边缘泛黄等纸张特征,最终赋值给2D平面的SpriteRenderer或RawImage。
  • 实现步骤:
    1. 初始化指定尺寸的Texture2D实例,设置合适的纹理格式
    2. 遍历每个像素,先设置米白/浅黄的基础底色,再叠加随机灰度噪点模拟纸张纤维
    3. 可选添加边缘暗化效果,模拟纸张自然老化的边角
    4. 调用Apply()提交像素修改,将纹理绑定到目标2D对象
  • 代码示例:
using UnityEngine;

public class PaperTextureGenerator : MonoBehaviour
{
    public int textureSize = 512;
    public SpriteRenderer targetRenderer;

    void Start()
    {
        Texture2D paperTexture = GenerateBasicPaper();
        targetRenderer.sprite = Sprite.Create(paperTexture, new Rect(0,0,textureSize,textureSize), new Vector2(0.5f,0.5f));
    }

    private Texture2D GenerateBasicPaper()
    {
        Texture2D tex = new Texture2D(textureSize, textureSize, TextureFormat.RGBA32, false);
        tex.filterMode = FilterMode.Bilinear;
        Color baseColor = new Color(0.95f, 0.92f, 0.85f);
        float edgeDarkenRange = 0.1f * textureSize;

        for (int x = 0; x < textureSize; x++)
        {
            for (int y = 0; y < textureSize; y++)
            {
                Color pixel = baseColor;
                // 添加随机纤维噪点
                float noise = Random.Range(-0.05f, 0.05f);
                pixel += new Color(noise, noise, noise);
                // 边缘暗化
                float edgeDistance = Mathf.Min(x, y, textureSize - x, textureSize - y);
                if (edgeDistance < edgeDarkenRange)
                {
                    float darkenRate = 1 - (edgeDistance / edgeDarkenRange);
                    pixel *= 1 - darkenRate * 0.3f;
                }
                tex.SetPixel(x, y, pixel);
            }
        }
        tex.Apply();
        return tex;
    }
}

方案2:Shader实时渲染程序化纸张效果

  • 核心逻辑:无需生成纹理,直接在Shader中通过噪声函数模拟纸张褶皱、纤维,性能更优且支持动态调整参数。
  • 实现步骤:
    1. 创建Unlit类型Shader,在片元着色器中实现Simplex/Perlin噪声
    2. 叠加底色、噪点、UV扭曲效果模拟纸张质感
    3. 将Shader赋值给2D平面的材质,运行时可通过材质面板调整参数
  • 简化Shader片元着色器示例:
fixed4 frag (v2f i) : SV_Target
{
    fixed4 baseColor = fixed4(0.95, 0.92, 0.85, 1.0);
    // 小尺度噪声模拟纤维
    float fiberNoise = snoise(i.uv * 50.0) * 0.05;
    // 大尺度噪声模拟褶皱扭曲
    float warpNoise = snoise(i.uv * 10.0) * 0.01;
    fixed2 warpedUV = i.uv + fixed2(warpNoise, warpNoise);
    // 边缘暗化
    float edgeFactor = smoothstep(0.05, 0.1, min(i.uv.x, i.uv.y, 1-i.uv.x, 1-i.uv.y));
    baseColor *= edgeFactor * 0.3 + 0.7;
    // 叠加噪点
    baseColor.rgb += fiberNoise;
    return baseColor;
}

注:需在Shader中实现snoise(Simplex Noise)函数,可直接复用公开的噪声实现代码。

方案3:结合多层噪声生成逼真纸张纹理

  • 核心逻辑:用多层不同尺度的Perlin Noise叠加,模拟纸张的褶皱、污渍、纹理不均匀等细节,效果更自然。
  • 实现步骤:
    1. 利用Unity内置Mathf.PerlinNoise或第三方噪声库(如FastNoiseLite)
    2. 大尺度噪声模拟褶皱,小尺度噪声模拟纤维,再随机添加局部污渍
    3. 将噪声值转换为颜色,生成Texture2D并应用
  • 代码示例(基于Mathf.PerlinNoise):
private Texture2D GenerateRealisticPaper()
{
    Texture2D tex = new Texture2D(textureSize, textureSize, TextureFormat.RGBA32, false);
    Color baseColor = new Color(0.93f, 0.90f, 0.82f);

    for (int x = 0; x < textureSize; x++)
    {
        for (int y = 0; y < textureSize; y++)
        {
            float u = (float)x / textureSize * 10;
            float v = (float)y / textureSize * 10;

            // 多层噪声叠加
            float foldNoise = Mathf.PerlinNoise(u, v) * 0.1f;
            float fiberNoise = Mathf.PerlinNoise(u * 50, v * 50) * 0.03f;
            float unevenNoise = Mathf.PerlinNoise(u * 2, v * 2) * 0.05f;

            float totalNoise = foldNoise + fiberNoise + unevenNoise;
            Color pixel = baseColor + new Color(totalNoise, totalNoise, totalNoise);

            // 随机添加污渍
            if (Random.value < 0.001f)
            {
                pixel *= Random.Range(0.7f, 0.9f);
            }

            tex.SetPixel(x, y, pixel);
        }
    }
    tex.Apply();
    return tex;
}

内容的提问来源于stack exchange,提问作者Vallith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 06:06:30