Unity打包圖集還是有一些坑的,至于圖集SpriteAtlas是什么請參考我之前寫的文章:【Sprite Atlas】Unity新圖集系統SpriteAtlas超詳細使用教程_spriteatlas 使用-CSDN博客
問題:
今天碰到的問題是,shader繪制的時候,因為打包圖集后,MainTexture是圖集的圖片,所以shader渲染就錯誤了。
非圖集是這樣顯示的,正常的一個地塊。
打包完圖集后,發現這個MainTexture是整個圖集的圖片
導致顯示就錯亂了,如下圖。
正常的顯示是這樣的。
問題所在:
原因就是打包圖集后傳入給shader的uv變了,本來只有圖片的時候,uv就是0-1的本地uv值,現在素材換成一張更大的圖了,導致uv采樣就出現了問題。
解決方法:
將圖片本身的uv信息傳給shader,做一個計算轉換即可。
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Sprites;
using UnityEngine;public class StaticGroundObject : GroundObject
{public GameObject notCellObjectShow;private SpriteRenderer spriteRenderer;private const string _UVRangeName = "_UVRange";protected override void OnStart(){base.OnStart();SetSortingOrder();spriteRenderer = GetComponentInChildren<SpriteRenderer>();UpdateSpriteRenderer();}protected override void OnEnable(){base.OnEnable();}//修改也要void UpdateSpriteRenderer(){if (spriteRenderer != null){Sprite sprite = spriteRenderer.sprite;Vector2 texelSize = sprite.texture.texelSize;Rect rect = sprite.textureRect;Vector4 uvRemap = new(rect.x * texelSize.x,rect.y * texelSize.y,rect.width * texelSize.x,rect.height * texelSize.y);// 將UV值傳遞給材質Material material = spriteRenderer.material;if (material != null){// 確保shader中有對應的屬性if (material.HasProperty(_UVRangeName)){material.SetVector(_UVRangeName, uvRemap);Debug.Log("UV值已成功寫入shader");}else{Debug.LogError("shader缺少必要的屬性,請確保shader中定義了_UV1和_UV2屬性");}}else{Debug.LogError("renderer的材質為空");}}}public override void SetNotCellObjectShow(GameObject notCellObjectShow){base.SetNotCellObjectShow(notCellObjectShow);this.notCellObjectShow = notCellObjectShow;}protected override void CheckRandomPrefabs(bool isShow){if (notCellObjectShow != null){//上面沒有東西,而且沒有混合到其他格子上if (ParentGround == null && isShow){notCellObjectShow.SetActive(true);}else{notCellObjectShow.SetActive(false);}}}private void SetSortingOrder(){var ground = GetComponentsInChildren<SpriteRenderer>(true)[0];var groundName = ground.name;var lastIndex = groundName.LastIndexOf('_');var secondLastIndex = groundName.LastIndexOf('_', lastIndex - 1);int.TryParse(groundName.Substring(secondLastIndex + 1, lastIndex - secondLastIndex - 1), out int result);ground.sortingOrder -= result;}protected override void SetBlendMaskValue(Texture2D newBlendMask, int newRotateMaskValue, Texture2D mainBlendTex,Texture2D blendMaskChamfer, Dictionary<int, float> _offsetList){// base.SetBlendMaskValue(newBlendMask, newRotateMaskValue, mainBlendTex, blendMaskChamfer, offsetList);//獲得要渲染的對象if (mainBlends.Count == 0){mainBlends = GetComponentsInChildren<Renderer>().ToList();}if (mainBlends.Count == 0){Debug.LogError("mainBlends.Count==0");return;}//設置遮罩貼圖this.blendMask = newBlendMask;this.rotateMaskValue = newRotateMaskValue;this.mainBlendTex = mainBlendTex;this.blendMaskChamfer = blendMaskChamfer;//設置偏移值this.blendOffsetTargetList = _offsetList;UpdateMaterial();UpdateSpriteRenderer();}protected override void SetBlendOffsetList(Dictionary<int, float> _offsetList){// base.SetBlendOffsetList(_offsetList);blendOffsetTargetList = _offsetList;UpdateMaterial();UpdateSpriteRenderer();}
}public class MaterialCacheTool
{public static Dictionary<string, Material> materialCache = new Dictionary<string, Material>();public static Material CheckMaterial(string materialKey, Renderer defaultRenderer, Action<Material> onInit){Material checkMaterial = null;if (materialCache.ContainsKey(materialKey)){checkMaterial = materialCache[materialKey];}else{var material = new Material(defaultRenderer.sharedMaterial);material.name = materialKey;onInit?.Invoke(material);materialCache.Add(materialKey, material);checkMaterial = material;}return checkMaterial;}
}
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'Shader "Shader/GroundObjectV3_Code"
{Properties{[NoScaleOffset]_MaskMap("MaskMap", 2D) = "white" {}[NoScaleOffset]_MainTex("MainTex", 2D) = "white" {}[NoScaleOffset]_Chamfer("Chamfer", 2D) = "black" {}_RotateMask("RotateMask",Range(0, 360)) = 0_OffsetUp("OffsetUp", Range(0, 1)) = 0_OffsetDown("OffsetDown", Range(0, 1)) = 0_OffsetLeft("OffsetLeft", Range(0, 1)) = 0_OffsetRight("OffsetRight", Range(0, 1)) = 0_OffsetUpLeft("OffsetUpLeft", Range(0, 1)) = 0_OffsetUpRight("OffsetUpRight", Range(0, 1)) = 0_OffsetDownLeft("OffsetDownLeft", Range(0, 1)) = 0_OffsetDownRight("OffsetDownRight", Range(0, 1)) = 0//相反_IsInversion("IsInversion", Float) = 0[NoScaleOffset]_BlendMaskMap("BlendMaskMap", 2D) = "black" {}[NoScaleOffset]_BlendMainTex("BlendMainTex", 2D) = "white" {}[NoScaleOffset]_BlendChamfer("_BlendChamfer", 2D) = "black" {}_BlendOffsetUp("BlendOffsetUp", Range(0, 1)) = 0_BlendOffsetDown("BlendOffsetDown", Range(0, 1)) = 0_BlendOffsetLeft("BlendOffsetLeft", Range(0, 1)) = 0_BlendOffsetRight("BlendOffsetRight", Range(0, 1)) = 0_BlendOffsetUpLeft("BlendOffsetUpLeft", Range(0, 1)) = 0_BlendOffsetUpRight("BlendOffsetUpRight", Range(0, 1)) = 0_BlendOffsetDownLeft("BlendOffsetDownLeft", Range(0, 1)) = 0_BlendOffsetDownRight("BlendOffsetDownRight", Range(0, 1)) = 0[HideInInspector]_QueueOffset("_QueueOffset", Float) = 0[HideInInspector]_QueueControl("_QueueControl", Float) = -1[HideInInspector][NoScaleOffset]unity_Lightmaps("unity_Lightmaps", 2DArray) = "" {}[HideInInspector][NoScaleOffset]unity_LightmapsInd("unity_LightmapsInd", 2DArray) = "" {}[HideInInspector][NoScaleOffset]unity_ShadowMasks("unity_ShadowMasks", 2DArray) = "" {}_OffsetFactor("OffsetFactor", Float) = 0_OffsetUnits("OffsetUnits", Float) = 0[ToggleUI] _ReceiveShadows("Receive Shadows", Float) = 1.0[ToggleUI] _CastShadows("Cast Shadows", Float) = 1.0//陰影模糊_ShadowBlur("ShadowBlur", Float) = 0.0_Cull("__cull", Float) = 2.0// 新增距離縮放因子屬性_DistanceScaleFactor("Distance Scale Factor", Range(0.1, 100.0)) = 60.0//補充UV_UVRange("UV Range", Vector) = (0, 0, 1, 1)}SubShader{Tags{"RenderPipeline"="UniversalPipeline""RenderType"="Opaque""UniversalMaterialType" = "Lit""Queue"="AlphaTest""DisableBatching"="LODFading""ShaderGraphShader"="true""ShaderGraphTargetId"="UniversalLitSubTarget""EnableGPUInstancing" = "true"}LOD 100Pass{// Render StateCull BackZTest LEqualZWrite OnBlend One ZeroAlphaToMask Onoffset [_OffsetFactor] , [_OffsetUnits]HLSLPROGRAM#pragma vertex vert#pragma fragment frag#pragma multi_compile_fog// #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" // #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"#include "EgoLight.hlsl"#pragma multi_compile _ _MAIN_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE _MAIN_LIGHT_SHADOWS_SCREENstruct appdata{float4 vertex : POSITION;float2 uv : TEXCOORD0;//光照計算float4 positionOS : POSITION;float4 normalOS : NORMAL;};struct v2f{float2 uv : TEXCOORD0;// UNITY_FOG_COORDS(1)float4 vertex : SV_POSITION;float3 worldPos : TEXCOORD1;//光照計算float3 viewDirWS : TEXCOORD2;float3 normalWS : TEXCOORD3;// 新增距離變量float distanceToCamera : TEXCOORD4;float prevLod : TEXCOORD5; // 新增變量存儲上一次的LODfloat2 uv2:TEXCOORD6;};sampler2D _MaskMap;float4 _MaskMap_ST;sampler2D _MainTex;float4 _MainTex_ST;sampler2D _Chamfer;float4 _Chamfer_ST;float _RotateMask;float _OffsetUp;float _OffsetDown;float _OffsetLeft;float _OffsetRight;float _OffsetUpLeft;float _OffsetUpRight;float _OffsetDownLeft;float _OffsetDownRight;float _IsInversion;//混合剔除的參數sampler2D _BlendMaskMap;float4 _BlendMaskMap_ST;sampler2D _BlendMainTex;float4 _BlendMainTex_ST;sampler2D _BlendChamfer;float4 _BlendChamfer_ST;float _BlendOffsetUp;float _BlendOffsetDown;float _BlendOffsetLeft;float _BlendOffsetRight;float _BlendOffsetUpLeft;float _BlendOffsetUpRight;float _BlendOffsetDownLeft;float _BlendOffsetDownRight;float4 _GlobalShadowColor;// 新增距離縮放因子屬性變量,放在HLSL代碼的合適位置,這里在函數外部聲明float _DistanceScaleFactor;float4 _UVRange;v2f vert (appdata v){v2f o;o.uv = TRANSFORM_TEX(v.uv, _MaskMap);float2 spriteRectPos = _UVRange.xy;float2 spriteRectSize = _UVRange.zw;float2 localUV = (v.uv - spriteRectPos) / spriteRectSize;o.uv2 = localUV;o.vertex = TransformObjectToHClip(v.vertex);//UnityObjectToClipPos //o.vertex = mul(UNITY_MATRIX_MVP,v.vertex); o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;// 計算物體到攝像機的距離o.distanceToCamera = distance(mul(unity_ObjectToWorld, v.vertex).xyz, _WorldSpaceCameraPos.xyz);o.prevLod = 0; // 初始化//光照計算VertexPositionInputs positionInputs = GetVertexPositionInputs(v.positionOS.xyz);VertexNormalInputs normalInputs = GetVertexNormalInputs(v.normalOS.xyz);o.viewDirWS = GetCameraPositionWS() - positionInputs.positionWS;o.normalWS = normalInputs.normalWS;return o;}float2 GetMaskUV(float2 uv){float2 maskUV=uv;maskUV -= 0.5;// 旋轉 45 度(π/4 弧度)float angle = radians(_RotateMask);float cosAngle = cos(angle);float sinAngle = sin(angle);float2x2 rotationMatrix = float2x2(cosAngle, -sinAngle, sinAngle, cosAngle);maskUV = mul(rotationMatrix, maskUV);// 平移回原來的坐標maskUV += 0.5;return maskUV;}//全部的Offsetfloat GetColorMask(sampler2D colorMask, sampler2D colorChamfer,float2 uv, float _OffsetUp, float _OffsetDown, float _OffsetLeft, float _OffsetRight, float _OffsetUpLeft, float _OffsetUpRight, float _OffsetDownLeft, float _OffsetDownRight,float _lod){float offsetMax = 0.5;float2 offsets[8] = {float2(0, -_OffsetUp * offsetMax),float2(0, _OffsetDown * offsetMax),float2(_OffsetLeft * offsetMax, 0),float2(-_OffsetRight * offsetMax, 0),float2(_OffsetUpLeft * offsetMax, -_OffsetUpLeft * offsetMax),float2(-_OffsetUpRight * offsetMax, -_OffsetUpRight * offsetMax),float2(_OffsetDownLeft * offsetMax, _OffsetDownLeft * offsetMax),float2(-_OffsetDownRight * offsetMax, _OffsetDownRight * offsetMax)};float alpha = 0;alpha += tex2Dlod(colorMask, float4(GetMaskUV(uv),0,_lod)).r;// 循環采樣偏移紋理//for (int i = 0; i < 8; i++) {// alpha += tex2Dlod(colorMask, float4(GetMaskUV(uv + offsets[i]),0,_lod)).r;//}alpha += tex2Dlod(colorMask, float4(GetMaskUV(uv + offsets[0]),0,_lod)).r;alpha += tex2Dlod(colorMask, float4(GetMaskUV(uv + offsets[1]),0,_lod)).r;alpha += tex2Dlod(colorMask, float4(GetMaskUV(uv + offsets[2]),0,_lod)).r;alpha += tex2Dlod(colorMask, float4(GetMaskUV(uv + offsets[3]),0,_lod)).r;alpha += tex2Dlod(colorMask, float4(GetMaskUV(uv + offsets[4]),0,_lod)).r;alpha += tex2Dlod(colorMask, float4(GetMaskUV(uv + offsets[5]),0,_lod)).r;alpha += tex2Dlod(colorMask, float4(GetMaskUV(uv + offsets[6]),0,_lod)).r;alpha += tex2Dlod(colorMask, float4(GetMaskUV(uv + offsets[7]),0,_lod)).r;// 采樣倒角紋理alpha += tex2Dlod(colorChamfer, float4(uv + float2( _OffsetLeft * offsetMax, -_OffsetUp * offsetMax),0,_lod)).r;alpha += tex2Dlod(colorChamfer, float4(uv + float2(-_OffsetRight * offsetMax, -_OffsetUp * offsetMax),0,_lod)).r;alpha += tex2Dlod(colorChamfer, float4(uv + float2( _OffsetLeft * offsetMax, _OffsetDown * offsetMax),0,_lod)).r;alpha += tex2Dlod(colorChamfer, float4(uv + float2(-_OffsetRight * offsetMax, _OffsetDown * offsetMax),0,_lod)).r;// 透明度限制在0-1之間alpha = clamp(alpha, 0, 1); return alpha;}float4 frag (v2f i) : SV_Target{float2 maskUV = i.uv2; float lod = i.distanceToCamera / _DistanceScaleFactor;lod = clamp(lod, 0, 5); float mainAlpha = GetColorMask( _MaskMap, _Chamfer, maskUV, _OffsetUp, _OffsetDown, _OffsetLeft, _OffsetRight, _OffsetUpLeft, _OffsetUpRight, _OffsetDownLeft, _OffsetDownRight,lod);// 計算主紋理的UV偏移float2 mainTexUV = TRANSFORM_TEX(i.uv, _MainTex).xy;float4 mainColor = tex2Dlod(_MainTex, float4(mainTexUV, 0, lod));//tex2D(_MainTex, TRANSFORM_TEX(i.uv, _MainTex));mainAlpha *= mainColor.a;float f = lerp(1.01,0.99,_IsInversion);//1.01;//maskUV縮放變大一點點maskUV -= 0.5;maskUV *=lerp(1.01,0.99,_IsInversion); // 1.005;maskUV += 0.5;//各個邊移動多一丟丟// _BlendOffsetUp += float2(0,f);// _BlendOffsetDown += float2(0,-f);// _BlendOffsetLeft += float2(-f,0);// _BlendOffsetRight += float2(f,0);// _BlendOffsetUpLeft += float2(-f,f);// _BlendOffsetUpRight += float2(f,f);// _BlendOffsetDownLeft += float2(-f,-f);// _BlendOffsetDownRight += float2(f,-f);float blendAlpha = GetColorMask( _BlendMaskMap, _BlendChamfer, maskUV, _BlendOffsetUp, _BlendOffsetDown, _BlendOffsetLeft, _BlendOffsetRight, _BlendOffsetUpLeft, _BlendOffsetUpRight, _BlendOffsetDownLeft, _BlendOffsetDownRight,lod);float4 blendColor = tex2Dlod(_BlendMainTex, float4(TRANSFORM_TEX(i.uv, _BlendMainTex),0,lod));blendAlpha*=blendColor.a;float alpha = mainAlpha - blendAlpha;alpha = lerp(alpha,1-alpha,_IsInversion); // 根據距離調整紋理采樣的LOD// mainColor = tex2Dlod(_MainTex, float4(TRANSFORM_TEX(i.uv, _MainTex).xy, 0, lod));// blendColor = tex2Dlod(_BlendMainTex, float4(TRANSFORM_TEX(i.uv, _BlendMainTex).xy, 0, lod));float _Cutoff = 0.8;float3 color = CheckColor(i.worldPos,i.normalWS,_GlobalShadowColor,mainColor.rgb);clip(alpha - _Cutoff);return float4(color.rgb, alpha);}ENDHLSL}}
}
參考這個鏈接會解釋的更加詳細點:
Local UVs for Sprites in Sprite Sheet/Atlas | Cyanilux