Unity 3D涂色
来源:互联网 发布:c语言源文件名有那些 编辑:程序博客网 时间:2024/06/10 09:42
最近一直研究3D 涂色方面的东东 其实所为的3d模型上的涂色 最终结果都涂在纹理贴图上面
上面的鱼和方块都涂上了红色
源代码如下
// Update is called once per frame
void Update () {
if (Input.GetMouseButton (0)) { //点击鼠标 移动鼠标 开始涂色
Ray lRay = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit lHit;
if (Physics.Raycast (lRay, out lHit,100f,1<<LayerMask.NameToLayer("Game"))) {
MeshCollider meshCollider = lHit.transform.GetComponent<MeshCollider> ();
MeshRenderer lRender = lHit.transform.GetComponent<MeshRenderer> ();
if (lRender) {
Texture2D lTexture = lRender.sharedMaterial.mainTexture as Texture2D;
Vector2 pixelUV = lHit.textureCoord;
pixelUV.x *= lTexture.width;
pixelUV.y *= lTexture.height; //得到射线点在2d纹理图片上的坐标
Draw (pixelUV, lTexture);
} else {
SkinnedMeshRenderer render = lHit.transform.GetComponent<SkinnedMeshRenderer> ();
Texture2D lTexture = render.sharedMaterial.mainTexture as Texture2D;
Vector2 pixelUV = lHit.textureCoord;
pixelUV.x *= lTexture.width;
pixelUV.y *= lTexture.height;
Draw (pixelUV, lTexture);
}
}
}
}
// 修改纹理上对应点的颜色
public void Draw(Vector2 pPoint,Texture2D pTexture)
{
Rect lRect = new Rect (0,0,pTexture.width,pTexture.height);
pPoint -= new Vector2 (brush.width/2,brush.height/2);
Debug.Log (pPoint.ToString());
int lX = Mathf.FloorToInt (pPoint.x);
int lY = Mathf.FloorToInt (pPoint.y);
for (int i = 0; i < brush.width; i++) { //Brush 是画笔的纹理 (Texture2D)
for (int j = 0; j < brush.height; j++) {
Vector2 lPosition = new Vector2 (lX+i,lY+j);
if (lRect.Contains (lPosition) && brush.GetPixel (i, j).a > 0.8f) {
pTexture.SetPixel(lX+i,lY+j,Color.red);
}
}
}
pTexture.Apply();
}
- Unity 3D涂色
- Unity 3D
- unity 3d 注意事项
- Unity 3d 摄像头
- Unity 3D 使用
- Unity 3D--PlayerPrefs
- Unity 3d
- Unity 3D俄罗斯方块
- unity 3d 多开
- UNITY 3D笔记
- Unity 3D接触
- unity 3d
- Unity 3D简介
- Unity 3D 琐记
- unity 3d 视频
- Unity 3D 光源
- Unity 3D FingerID
- 再议Unity 3D
- 查询 数据平均值 和重复记录
- JavaScript中的函数参数详解
- apex:actionRegion
- Android OkHttp(三)源码解析
- React Native 学习笔记十八(关于样式 补充)
- Unity 3D涂色
- 深度学习中的数学与技巧(11):dropout原理解读
- 如何通过JS调用某段SQL语句
- python(十一)上:RabbitMQ 使用详细介绍
- liinx下 安装phpstudy
- pthon--字符串拼接
- 初级dba阅读书籍
- hibernate分页查询
- 深度学习中的数学与技巧(8):矩阵及其变换、特征值与特征向量的物理意义