Unity开发--(三)Shader编程:写一个shader让图
2016-11-01 本文已影响1257人
元宇宙协会
原因
面试了一家VR房地产公司,给了几张图片,要求做出VR漫游效果。通过3个小时研究,我发现问题还是在shader这一块,故贴出代码,如果其他小朋友遇到就可以尝试一下,下面的方法
问题:
将图片直接扔到材质球中,结果并不能很好的显示,而且场景光线很暗。原有的光线并不能很好的展示。
怎么做?
首先将素材打开
![](https://img.haomeiwen.com/i642887/fd7855571f4a4c37.png)
![](https://img.haomeiwen.com/i642887/ab81c44b3e7641e4.png)
其次拖入工程,选取下面的模式
![](https://img.haomeiwen.com/i642887/02177082b6f5911e.png)
创建一个材质球
![](https://img.haomeiwen.com/i642887/7b69f02c33d80736.png)
建立一个shader
![](https://img.haomeiwen.com/i642887/5630cfbe9e8a3ef1.png)
shader代码
如果看不懂,请看我之前的两篇博客。就可以看明白下面写的什么了。
Shader "Custom/SYShader" {
Properties
{
_Color("Color", Color) = (1,1,1,1)
_MainTex("Texture", 2D) = "white"{}
}
SubShader
{
//Ambient pass
Pass
{
Name "BASE"
Tags{ "LightMode" = "Always" /* Upgrade NOTE: changed from PixelOrNone to Always */ }
Color[_PPLAmbient]
SetTexture[_BumpMap]
{
constantColor(.5,.5,.5)
combine constant lerp(texture) previous
}
SetTexture[_MainTex]
{
constantColor[_Color]
Combine texture * previous DOUBLE, texture *constant
}
}
//Vertex lights
Pass{
Name "BASE"
Tags{ "LightMode" = "Vertex" }
Material
{
Diffuse[_Color]
Emission[_PPLAmbient]
Shininess[_Shininess]
Specular[_SpecColor]
}
SeparateSpecular On
Lighting On
cull off
SetTexture[_BumpMap]
{
constantColor(.5,.5,.5)
combine constant lerp(texture) previous
}
SetTexture[_MainTex]
{
Combine texture *previous DOUBLE, texture *primary
}
}
}
FallBack "Diffuse", 1
}
}
再创建一个Sphere物体,放大10倍,最后将材质球应用就Ok了!
![](https://img.haomeiwen.com/i642887/f91577db1087dbe8.png)