zoukankan      html  css  js  c++  java
  • unity中获取模型对应的贴图中的像素点,修改颜色

    /*
      获取模型对应的贴图中的像素点,修改颜色
    */
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class DrowLine : MonoBehaviour 
    {
        public GameObject m_obj;
        private Texture2D m_tex;
        public Color m_color;
        public int size = 3;
        private Color[] m_textureColorsStart;
        void Start () 
        {
            m_tex = m_obj.GetComponent<MeshRenderer>().material.mainTexture as Texture2D;
            //从纹理中获取像素颜色
            m_textureColorsStart = m_tex.GetPixels();
            Debug.Log(m_tex.name);
        }
    
    
        void Update()
        {
    
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Input.GetMouseButton(0))
            {
                if (Physics.Raycast(ray, out hit))
                {
                    //在碰撞位置处的UV纹理坐标。
                    Vector2 pixelUV = hit.textureCoord;
                    //以像素为单位的纹理宽度
                    pixelUV.x *= m_tex.width;
                    pixelUV.y *= m_tex.height;
                    //贴图UV坐标以右上角为原点
                    for (float i = pixelUV.x - 1; i < pixelUV.x + size; i++)
                    {
                        for (float j = pixelUV.y - 1; j < pixelUV.y + size; j++)
                        {
                            m_tex.SetPixel((int)i, (int)j, m_color);
                        }
                    }
                    Debug.Log(pixelUV);
                    m_tex.Apply();
                }
            }
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                //还原
                m_tex.SetPixels(m_textureColorsStart);
                m_tex.Apply();
            }
    
    
            //在处理鼠标按下的记录下位置,抬起的时候记录下位置,取2个位置中间的位置发射射线
            //if (Input.GetMouseButtonDown(0))
            //{
                
            //}
            //if (Input.GetMouseButtonUp(0))
            //{
    
            //}
        }
    }
  • 相关阅读:
    rhel5.4 x64安装apache http server2.2.27,并创建自动启服务
    解决BEA-000438 Unable to load performance pack.
    为aix系统挂载iso镜像-命令
    IBM小型机创建RAID
    linux下使用parted工具划分大于2T的分区
    linux下的SElinux
    日立HDS AMS2100存储的调试
    rhel创建yum源
    rfid安全简介
    安卓模拟器抓包
  • 原文地址:https://www.cnblogs.com/nanyang0310/p/9259579.html
Copyright © 2011-2022 走看看