Unity 通过代码简单实现文理的灰化显示

本文介绍了一种在Unity中实现模型或纹理灰化显示的方法,通过脚本操作纹理颜色通道来达到灰化效果,适用于资源未加载时的预览。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.可以用于纹理的处理,也可用于模型显示的处理(比如某件准备或者服饰未获取的时候,灰化显示)

 线上对比图:

                     

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TextureGrey : MonoBehaviour
{
    [SerializeField]
    UITexture tex = null;

    Texture2D tex2D = null;
    Vector3 grey = new Vector3(0.299f, 0.587f, 0.114f);  //灰化Shader中的灰度值,具体含义目前不是太清楚

    void Start()
    {
        tex2D = tex.mainTexture as Texture2D;
    }

    void Update()
    {
        if (Input.GetKeyUp(KeyCode.A))
        {
            //灰化
            Vector3 tempV3 = Vector3.zero;
            if (null != tex2D)
            {
                Color[] cols = tex2D.GetPixels();
                for (int i = 0, iMax = cols.Length; i < iMax; ++i)
                {
                    tempV3.Set(cols[i].r, cols[i].g, cols[i].b);
                    float dot = Vector3.Dot(tempV3, grey);
                    cols[i] = new Color(dot, dot, dot, cols[i].a);
                }
                tex2D = new Texture2D(tex2D.width, tex2D.height, TextureFormat.ARGB32, false);
                tex2D.SetPixels(cols);
                tex2D.Apply(false);         //实际上传改变后的像素数据到显卡上
                tex.mainTexture = tex2D as Texture;
                
            }
        }
    }
}

  

转载于:https://www.cnblogs.com/luguoshuai/p/7826148.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值