unity3d/用户自由改变背景色
通过鼠标单击(或触摸点)发射射线,获取贴图上的某点的像素值,因为Texture2D也给我们提供 GetPixel API,同时碰触会给我们返回这个textureCoord值,也就是图形学里已经经过设备坐标化后的纹理坐标。但是要获取正确的贴图坐标颜色值,还必须要分别在U、V坐标上乘以贴图的宽和高(刚开始这个可让我debug了个够啊),同时我们的贴图必须设置成可读写的(默认是不支持的),还有就是贴图格式设置成RAGB32的。代码如下:(同时自己也PS合成了一张色彩原理贴图,共享在我的贴图相册)
using UnityEngine;
using System.Collections;public class ColorSelect : MonoBehaviour {
public Camera cam;
void Start()
{
if(cam == null)
{
cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
}
}
void Update ()
{
// Only when we press the mouse
if (Input.touchCount < 1) //应用于手机触摸版
return;
/*if(!Input.GetMouseButton(0)) //应用于PC或WEB版的鼠标单击
return;*/// Only if we hit something, do we continue
//Vector3 TouchPosition = new Vector3(Input.GetTouch(0).position.x,Input.GetTouch(0).position.y,0); //应用鼠标
Vector3 TouchPosition = Input.mousePosition; //触摸
RaycastHit hit = new RaycastHit();
if (!Physics.Raycast (cam.ScreenPointToRay(TouchPosition),out hit))
return;
// Just in case, also make sure the collider also has a renderer
// material and texture. Also we should ignore primitive colliders.
Renderer renderer = hit.collider.renderer;
if(renderer == null)
return;
//Debug.Log("throught renderer~");
MeshCollider meshCollider = hit.collider as MeshCollider;
if(meshCollider == null)
return;
//Debug.Log("throught mesh collider");
if (renderer.sharedMaterial == null ||
renderer.sharedMaterial.mainTexture == null)
return;
//Debug.Log("done~");
// Now draw a pixel where we hit the object
Texture2D tex = renderer.material.mainTexture as Texture2D;
//Debug.Log("Get Texture:"+tex.name);
Vector2 pixelUV = hit.textureCoord;
//Debug.Log("pixelUV:"+pixelUV);
pixelUV.x *= tex.width;
pixelUV.y *= tex.height;
Color color = tex.GetPixel((int)pixelUV.x,(int)pixelUV.y);
//Debug.Log("color:"+color);
cam.backgroundColor = color;
}
}
unity3d/用户自由改变背景色相关推荐
- css选中后的背景,css怎么实现鼠标选中文字后改变背景色
css中可以使用"::selection"选择器来实现鼠标选中文字后改变背景色效果,只需给文字元素E添加"E::selection{background: 背景色值;}& ...
- SecureCR 改变背景色和文字颜色
SecureCR 改变背景色和文字颜色 1.打开SecureCR链接Linux服务器,Options->Session Options->Emulation->Terminal 选择 ...
- QT QTreeWidget 行切换时改变背景色
文章目录 QT QTreeWidget 行切换时改变背景色 1.关联信号槽. 2.写信号槽函数: QT QTreeWidget 行切换时改变背景色 1.关联信号槽. 当QtreeWidget 的选中状 ...
- html未点击背景 点击背景,在AngularJs中点击状态如何改变背景色
下面我就为大家分享一篇AngularJs点击状态值改变背景色的实例,具有很好的参考价值,希望对大家有所帮助. 实例如下://更改边框颜色的代码 $("#shname").css({ ...
- html input 的value变颜色,vue里input根据value改变背景色的实例
这篇文章主要为大家详细介绍了vue里input根据value改变背景色的实例,具有一定的参考价值,可以用来参考一下. 感兴趣的小伙伴,下面一起跟随512笔记的小编两巴掌来看看吧! 1.首先定义两个不同 ...
- css3 两种背景色,CSS3 / 实例改变背景色和位置 - 汇智网
实例 实例改变背景色和位置 @keyframes myfirst { 0% {background: red; left:0px; top:0px;} 25% {background: yellow; ...
- element若依 菜单点击改变背景色
element&若依 菜单点击改变背景色 element只提供了激活时候改变字体的颜色 找到激活的菜单查看代码 从#app开始的,只能整个复写,加上激活的class: is-active &l ...
- vue里input根据value改变背景色
vue里input根据value改变背景色 1.首先定义两个不同的class .null-input .el-input__inner { background-color: rgba(255, 25 ...
- Opengl入门-glfw3创建窗口改变背景色
文章目录 一.目标 二.结果 三.具体步骤 下载 一.目标 opengl windows环境 glfw3创建窗口改变背景色 二.结果 三.具体步骤 1 glfw是窗口库需要头文件,依赖路径,调用依赖 ...
最新文章
- matlab GUI 实现按钮切换plot页面(含clear功能)
- spring框架使用Quartz执行定时任务实例详解
- 沈阳药科大学计算机基础网址,2016年沈阳药科大学药学信息学1016计算机技术基础之C程序设计复试笔试最后押题五套卷...
- 我也聊聊串口通信协议:用户层通信协议的编制
- vue全局注册组件实例
- python process 函数_Python Process创建进程的2种方法详解
- ubuntu下安装chrome浏览器和flash插件
- 马云谈区块链:不可能也不应该用来一夜暴富
- Codeforces Round #563 (Div. 2) A. Ehab Fails to Be Thanos
- 如何定制 Calico 网络 Policy - 每天5分钟玩转 Docker 容器技术(70)
- python可视化(十种常用图)
- 基于单片机的自动追日系统设计_电机太阳论文,关于基于Atmega32的主动式太阳能追日系统相关参考文献资料-免费论文范文...
- 行式 Excel 文件去重
- 虚幻引擎UE编辑器卡顿问题
- python中的join是什么意思_python里join是什么意思
- Java 线程池常见误区
- 极光推送报错time_to_live value should be a non-negative integertime_to_live value should be a non-negativ
- 拉丁超立方抽样的Python实现
- android移植wifi驱动流程porting
- 数据分析 常用的数据指标