unity2d使用shader创建缩放时重复原始大小的循环图像.docx

上传人:b****1 文档编号:1825676 上传时间:2022-10-24 格式:DOCX 页数:4 大小:17.74KB
下载 相关 举报
unity2d使用shader创建缩放时重复原始大小的循环图像.docx_第1页
第1页 / 共4页
unity2d使用shader创建缩放时重复原始大小的循环图像.docx_第2页
第2页 / 共4页
unity2d使用shader创建缩放时重复原始大小的循环图像.docx_第3页
第3页 / 共4页
unity2d使用shader创建缩放时重复原始大小的循环图像.docx_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

unity2d使用shader创建缩放时重复原始大小的循环图像.docx

《unity2d使用shader创建缩放时重复原始大小的循环图像.docx》由会员分享,可在线阅读,更多相关《unity2d使用shader创建缩放时重复原始大小的循环图像.docx(4页珍藏版)》请在冰豆网上搜索。

unity2d使用shader创建缩放时重复原始大小的循环图像.docx

unity2d使用shader创建缩放时重复原始大小的循环图像

unity2D使用shader创建缩放时重复原始大小的循环图像

在制作游戏时,经常需要用到将较小的重复循环的纹理图像拼成一个大图,比如地面上的尖刺,或是墙面砖块背景。

遇到这种问题时通常的处理手段是像tilemap那样用单张纹理图片作为一个tile(unity中一般用sprite),将多个tile拼接起来形成一张大图。

例如有一张256x256的无缝墙面纹理,我希望用这张图铺满一个512x512大小的墙面,512x512的墙面正好需要4个256x256的tile来拼接,拼接完成时如图1所示。

图1但是这样处理要求拼接后的图像大小的宽和高需要分别为原图宽和高的整数倍,这样比较好处理。

为了尽量满足这个要求,通常会将用作tile的图片做的非常小,这样可以满足尽量多的拼接图片不同大小的情况。

有没有一种更灵活的方式来处理这种问题,只用一个sprite就可以做出任意大小的墙面呢?

那必须有啊!

其实我们可以借助shader来完成这样一个效果。

在opengl中有一个参数叫做GL_TEXTURE_WRAP,就是在纹理超出边界怎么处理。

有一种模式是GL_REPEAT,就是可以将纹理进行重复。

受到这个启发,我们是不是也可以利用这种重复模式,来实现缩放sprite时,纹理的大小不变,多出的部分则自动用重复的纹理进行铺满呢?

当然可以!

只不过会麻烦一些......unity中,纹理有一个wrapmode属性,可以设置成repeat或是clamp,其中repeat就是我们想要的重复模式。

但是unity2d会自动将导入的纹理转换成sprite类型纹理,在sprite类型纹理的属性中,我们无法调整纹理的wrapmode属性所以我们首先需要将导入的纹理变成texture类型,如图2所示。

修改完之后记得点apply进行保存。

图2之后我们在unity编辑器新建一个sprite,这是我们发现新建的sprite不能直接使用我们修改的纹理了,所以我们需要通过脚本来用纹理生成一个sprite对象,赋给新建的sprite。

像这样:

[csharp]viewplaincopy<divstyle="text-align:

left;"><spanstyle="font-family:

Arial,Helvetica,sans-serif;"><spanstyle="white-space:

pre"></span>//将你的纹理<spanstyle="font-size:

18px;white-space:

pre;background-color:

rgb(240,240,240);">YourTextureForSprite变成sprite</span></span></div><divstyle="text-align:

left;"><spanstyle="font-family:

Arial,Helvetica,sans-serif;"><spanstyle="white-space:

pre"></span>Spritesprite=Sprite.Create((Texture2D)YourTextureForSprite,newRect(0,0,<spanstyle="font-size:

18px;white-space:

pre;background-color:

rgb(240,240,240);">YourTextureForSprite</span>.width,<spanstyle="font-size:

18px;white-space:

pre;background-color:

rgb(240,240,240);">YourTextureForSprite</span>.height),newVector2(0.5f,0.5f));</span></div><divstyle="text-align:

left;"></div><divstyle="text-align:

left;"></div><divstyle="text-align:

left;"><spanstyle="font-family:

Arial,Helvetica,sans-serif;"><spanstyle="white-space:

pre"></span>GetComponent<SpriteRenderer>().sprite=sprite;</span></div>

有了纹理,下一步我们希望sprite在缩放时并不改变原始纹理的大小,而是将纹理进行重复。

默认的shader是无法完成这项工作的,这时候就需要我们自己去写一个shader了。

我们需要将sprite的scale值告诉shader,以便进行处理。

这里我将unity自带的默认的sprite

shader改写了一下,代码如下:

[plain]viewplaincopyShader"Custom/RepeatShader"{<spanstyle="white-space:

pre"></span>Properties<spanstyle="white-space:

pre"></span>{<spanstyle="white-space:

pre"></span>[PerRendererData]_MainTex("SpriteTexture",2D)="white"{}<spanstyle="white-space:

pre"></span>_Color("Tint",Color)=(1,1,1,1)<spanstyle="white-space:

pre"></span>[MaterialToggle]PixelSnap("Pixelsnap",Float)=0<spanstyle="white-space:

pre"></span>_ScaleX("ScaleX",Float)=1<spanstyle="white-space:

pre"></span>_ScaleY("ScaleY",Float)=1<spanstyle="white-space:

pre"></span>}<spanstyle="white-space:

pre"></span>SubShader<spanstyle="white-space:

pre"></span>{<spanstyle="white-space:

pre"></span>Tags<spanstyle="white-space:

pre"></span>{<spanstyle="white-space:

pre"></span>"Queue"="Transparent"<spanstyle="white-space:

pre"></span>"IgnoreProjector"="True"<spanstyle="white-space:

pre"></span>"RenderType"="Transparent"<spanstyle="white-space:

pre"></span>"PreviewType"="Plane"<spanstyle="white-space:

pre"></span>"CanUseSpriteAtlas"="True"<spanstyle="white-space:

pre"></span>}<spanstyle="white-space:

pre"></span>CullOff<spanstyle="white-space:

pre"></span>LightingOff<spanstyle="white-space:

pre"></span>ZWriteOff<spanstyle="white-space:

pre"></span>Fog{ModeOff}<spanstyle="white-space:

pre"></span>BlendOneOneMinusSrcAlpha<spanstyle="white-space:

pre"></span>Pass<spanstyle="white-space:

pre"></span>{<spanstyle="white-space:

pre"></span>CGPROGRAM<spanstyle="white-space:

pre"></span>#pragmavertexvert<spanstyle="white-space:

pre"></span>#pragmafragmentfrag<spanstyle="white-space:

pre"></span>#pragmamulti_compileDUMMYPIXELSNAP_ON<spanstyle="white-space:

pre"></span>#include"UnityCG.cginc"<spanstyle="white-space:

pre"></span>structappdata_t<spanstyle="white-space:

pre"></span>{<spanstyle="white-space:

pre"></span>float4vertex:

POSITION;<spanstyle="white-space:

pre"></span>float4color:

COLOR;<spanstyle="white-space:

pre"></span>float2texcoord:

TEXCOORD0;<spanstyle="white-space:

pre"></span>};<spanstyle="white-space:

pre"></span>structv2f<spanstyle="white-space:

pre"></span>{<spanstyle="white-space:

pre"></span>float4vertex:

SV_POSITION;<

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 天文地理

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1