当前位置:网站首页>Unity Shader学习(七)纹理图像的简单使用
Unity Shader学习(七)纹理图像的简单使用
2022-08-02 06:04:00 【ToDoNothing】
今天我们来了解一下怎么使用贴图,把贴图渲染出来
先上代码:
Shader "Unlit/shader9"
{
///鼠标移动正方形
Properties
{
_MainTex("Main Texture",2D)="white"{
}
}
SubShader
{
Tags {
"RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f{
float4 vertex:SV_POSITION;
float4 position:TEXCOORD1;
float2 uv:TEXCOORD;
float4 screenPos:TEXCOORD2;
};
v2f vert(appdata_base v){
v2f o;
o.vertex=UnityObjectToClipPos(v.vertex);
o.position=v.vertex;
o.uv=v.texcoord;
o.screenPos=ComputeScreenPos(o.vertex);
return o;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
fixed3 color=tex2D(_MainTex,i.uv).rgb;
return fixed4(color,1.0);
}
ENDCG
}
}
}
其实也就是获取到贴图的颜色信息,再直接输出,此处用到tex2D函数,传入两个参数
sampler2D:贴图信息
uv:UV值 如果uv值为(0,0),则返回左下角的像素,如果uv值为(1,1),则返回右上角的像素,如果uv值为(0.5,0.5),则返回中间像素
在代码中我们剥离了alpha值,只取了rgb三种颜色
如果们需要把图像左右翻转或倒过来,可以通过操作uv的坐标值来实现,如下是左右翻过来
float2 uv=float2(1-i.uv.x,i.uv.y);
fixed3 color=tex2D(_MainTex,uv).rgb;
return fixed4(color,1.0);
上下翻转:
float2 uv=float2(i.uv.x,1-i.uv.y);
fixed3 color=tex2D(_MainTex,uv).rgb;
return fixed4(color,1.0);
旋转90°
Shader "Unlit/shader9"
{
///鼠标移动正方形
Properties
{
_MainTex("Main Texture",2D)="white"{
}
}
SubShader
{
Tags {
"RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f{
float4 vertex:SV_POSITION;
float4 position:TEXCOORD1;
float2 uv:TEXCOORD;
float4 screenPos:TEXCOORD2;
};
v2f vert(appdata_base v){
v2f o;
o.vertex=UnityObjectToClipPos(v.vertex);
o.position=v.vertex;
o.uv=v.texcoord;
o.screenPos=ComputeScreenPos(o.vertex);
return o;
}
float2 rotate(float2 pt,float theta,float aspect){
float c=cos(theta);
float s=sin(theta);
float2x2 mat=float2x2(c,s,-s,c);
pt.y/=aspect;
pt=mul(pt,mat);
pt.y*=aspect;
return pt;
}
sampler2D _MainTex;
fixed4 frag (v2f i) : SV_Target
{
float center=0.5;
float2 uv=rotate(i.uv-center,UNITY_HALF_PI,2.0/1.5)+center;
fixed3 color;
if(uv.x<0||uv.x>1||uv.y<0||uv.y>1){
color=fixed3(0,0,0);
}else{
color=tex2D(_MainTex,uv).rgb;
}
return fixed4(color,1.0);
}
ENDCG
}
}
}
旋转过程中添加了一个rotate函数,该函数主要是通过矩阵实现旋转相应的度数theta,aspect参数主要是处理拉伸的情况。
在旋转之后,会多出相同的画面,这里进行了判断处理,多出的部分变成黑色
边栏推荐
- View source and switch mirrors in two ways: npm and nrm
- MySQL Advanced Study Notes
- Nacos installation configuration and single-machine deployment tutorial
- MySQL - Multi-table query and case detailed explanation
- 宝塔+FastAdmin 404 Not Found
- Node installation and environment variable configuration
- Technology empowers Lhasa's "lungs", Huawei helps Lalu Wetland Smart Management to protect lucid waters and lush mountains
- mysql高阶语句(一)
- Kingdee International: Lost in half a year and last year, how does the business model of frantically burning money continue
- A detailed introduction to the deployment and usage of the Nacos registry
猜你喜欢
解决:- SPY: No data found for this date range, symbol may be delisted报错
The nacos source code can not find the istio package
(Notes are not completed) [Graph Theory] Traversal of graphs
CAT1 4G+以太网开发板腾讯云手机微信小程序显示温度和下发控制
有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
typescript ‘props‘ is declared but its value is never read 解决办法
The installation of NPM, CNPM
宝塔+FastAdmin 404 Not Found
MySQL Advanced SQL Statements
Nodejs安装教程
随机推荐
Leetcode Weekly 304
[数据集][VOC]眼睛佩戴数据集VOC格式6000张
MySQL高阶---存储引擎、索引、锁
Toolbox App 1.25 新功能一览 | 版本更新
BGP+MPLS Comprehensive Experiment
HCIP 第二天
node安装及环境配置
View source and switch mirrors in two ways: npm and nrm
文件上传漏洞(二)
Xgboost报错ValueError:无效的形状:标签(1650 2)
odoo field 设置匿名函数domain
返回文件名问题
[npm install error report collection] - npm ERR! code ENOTEMPTY npm ERR! syscall rmdir
Launch Space on-premises deployment (local) Beta!
Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"
享年94岁,图灵奖得主、计算复杂性理论先驱Juris Hartmanis逝世
数据库概论之MySQL表的增删改查1
DNS resolution process
MySQL (3)
两篇不错的php debug教程