UGUI ScrollView中显示模型和特效
游戏开发中有时候会遇到在UI上显示模型和特效的需求,这次需要在ScrollView上显示。我们使用UGUI的Screen Space - Camera模式,修改模型和特效的layer使之显示在UI上面,但是会遇到ScrollView的mask无法剔除模型和特效的问题。
解决思路:计算出ScrollView的显示矩形框的世界坐标和尺寸,传给模型和特效的shader,在shader里判断这个像素的世界坐标是否超过矩形框,来判断是否显示图像。
C#代码部分:
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class ScrollViewModelAndEffectClip : MonoBehaviour
{ public RectTransform scrollViewArea;
float mWidth, mHeight, m_canvasScale;
Canvas mCanvas;
public Vector4 area;
List<Material> modelMaterials = new List<Material>();
// Start is called before the first frame update
void Start()
{ var renderers=GetComponentsInChildren<Renderer>();
for(int i = ; i < renderers.Length; i++)
{
//Debug.LogError(renderers[i]);
modelMaterials.AddRange(renderers[i].GetMaterials());
}
for(int i = ; i < modelMaterials.Count; i++)
{
modelMaterials[i].shader = Shader.Find(modelMaterials[i].shader.name + "ScrollViewClip");
//Debug.LogError(modelMaterials[i].shader.name + "ScrollViewClip");
} mCanvas = GetComponentInParent<Canvas>();
GetArea();
} // Update is called once per frame
void Update()
{ } public void GetArea()
{
m_canvasScale = mCanvas.transform.localScale.x;
mWidth = scrollViewArea.rect.width * m_canvasScale;
mHeight = scrollViewArea.rect.height * m_canvasScale; area = CalculateArea(scrollViewArea.position, scrollViewArea.pivot); for (int i = ; i < modelMaterials.Count; i++)
{
//Debug.LogError(area);
modelMaterials[i].SetVector("_Area", area);
} } Vector4 CalculateArea(Vector3 position,Vector2 pivot)
{
return new Vector4()
{
x = position.x - mWidth*pivot.x,
y = position.y - mHeight * pivot.y,
z = position.x + mWidth *(- pivot.x),
w = position.y + mHeight * ( - pivot.y)
};
} }
接下来是Shader:
Shader "SonicDance/AddtiveScrollViewClip"
{
Properties
{
_TintColor ("Tint Color", Color) = (0.5,0.5,0.5,0.5)
_MainTex ("Particle Texture", 2D) = "white" {}
_InvFade ("Soft Particles Factor", Range(0.01,3.0)) = 1.0
//Clip新增
_Area("Area", Vector) = (,,,)
//
} Category
{
SubShader
{
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
Blend SrcAlpha OneMinusSrcAlpha
ColorMask RGB
Cull Off
Lighting Off
ZWrite Off Pass { CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#pragma multi_compile_particles
#pragma multi_compile_fog #include "UnityCG.cginc" struct appdata_t
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float4 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID }; struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float4 texcoord : TEXCOORD0;
float4 worldPos:TEXCOORD3;
UNITY_FOG_COORDS()
#ifdef SOFTPARTICLES_ON
float4 projPos : TEXCOORD2;
#endif
UNITY_VERTEX_OUTPUT_STEREO }; uniform sampler2D _MainTex;
uniform fixed4 _TintColor;
uniform float4 _MainTex_ST;
uniform sampler2D_float _CameraDepthTexture;
uniform float _InvFade;
//Clip新增
float4 _Area;
// v2f vert ( appdata_t v )
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); v.vertex.xyz += float3( , , ) ;
o.vertex = UnityObjectToClipPos(v.vertex);
o.worldPos= mul(unity_ObjectToWorld, v.vertex);
#ifdef SOFTPARTICLES_ON
o.projPos = ComputeScreenPos (o.vertex);
COMPUTE_EYEDEPTH(o.projPos.z);
#endif
o.color = v.color;
o.texcoord = v.texcoord;
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
} fixed4 frag ( v2f i ) : SV_Target
{ //新增,判断顶点坐标是否在裁剪框内 bool inArea = i.worldPos.x >= _Area.x && i.worldPos.x <= _Area.z && i.worldPos.y >= _Area.y && i.worldPos.y <= _Area.w;
if (!inArea)
discard;
//----end---- #ifdef SOFTPARTICLES_ON
float sceneZ = LinearEyeDepth (SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
float partZ = i.projPos.z;
float fade = saturate (_InvFade * (sceneZ-partZ));
i.color.a *= fade;
#endif fixed4 col = 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord.xy*_MainTex_ST.xy + _MainTex_ST.zw );
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
CustomEditor "ASEMaterialInspector"
}
这个方法有一个问题就是,需要复制一份显示的模型和特效的shader,并且增加上面shader中注释的部分,如果模型和特效的shader多种多样的话,那工作量就比较大,需要其他的方法。
UGUI ScrollView中显示模型和特效的更多相关文章
- Android 6.0+ RecyclerView嵌套在ScrollView中显示不全
ScrollView嵌套RecyclerView在Android6.0以下能正常显示,但是在6.0以上就会出现RecyclerView显示不全的bug.尝试多种方法之后终于找到解决办法,特在此记录下. ...
- Unity NGUI和UGUI与模型、特效的层级关系
目录 1.介绍两大UI插件NGUI和UGUI 2.unity渲染顺序控制方式 3.NGUI的控制 4.UGUI的控制 5.模型深度的控制 6.粒子特效深度控制 7.NGUI与模型和粒子特效穿插层级管理 ...
- 解决在ScrollView中套用ListView显示不正常
最近在设计Android程序时,因为需要在ScrollView中添加一个ListView列表来显示一些信息.刚开始并没有想太多,但添加进去后才发现ListView不论怎样定义都只能显示一行,显示效果很 ...
- Android--------解决ScrollView中嵌套ExpandableListView显示不全的问题
##***Scrollview和ExpandableListView嵌套显示不全*** - 当我们将ExpandableListView嵌套在Scrollview之中时,我们必须要确保我们的不居中Ex ...
- ScrollView中嵌套ListView时,listview高度显示的问题
方法一:直接更改listview的控件高度,动态获取(根据条目和每个条目的高度获取) 前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个Lis ...
- Android 如何在ScrollView中嵌套ListView
前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...
- Android 设置ListView不可滚动 及在ScrollView中不可滚动的设置
http://m.blog.csdn.net/blog/yusewuhen/43706169 转载请注明出处: http://blog.csdn.net/androiddevelop/article/ ...
- Android实战技巧:如何在ScrollView中嵌套ListView
前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...
- Android -- 在ScrollView中嵌套ListView
在做一个工程,这个工程的布局可以相当的复杂,最外面是ScrollView,在ScrollView里面有两个Listview,这下好了,布局出来了,放在机子上跑,卡得想死有木有,信息乱跑乱出现,表示非常 ...
随机推荐
- poj 1787 Charlie's Change (多重背包可作完全背包)
Charlie's Change Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3792 Accepted: 1144 ...
- pytorch 状态字典:state_dict 模型和参数保存
pytorch 中的 state_dict 是一个简单的python的字典对象,将每一层与它的对应参数建立映射关系.(如model的每一层的weights及偏置等等) (注意,只有那些参数可以训练的l ...
- Lifecycle mapping "org.eclipse.m2e.jdt.JarLifecycleMapping" Eclipse Maven报错
eclipse 项目中报错Lifecycle mapping "org.eclipse.m2e.jdt.JarLifecycleMapping" is not available, ...
- 1134 最长上升子序列 (序列型 DP)
思路: 由于一般的动态规划时间复杂度是O(n^2)(哈哈哈哈 第一次用的就是这个!)用在这里由于n最大为50000 所以会超时 到这里我们可以用一个数组来动态维护这个最长上升的子序列,将你要输入的子序 ...
- LightOJ - 1265 Island of Survival (概率dp)
You are in a reality show, and the show is way too real that they threw into an island. Only two kin ...
- dotnet 通过 WMI 获取设备厂商
本文告诉大家如何通过 WMI 获取设备厂商 通过 Win32_ComputerSystem 可以获取电脑系统信息 通过下面代码可以获取 机器型号 和 制造厂商 var mc = "Win32 ...
- SmartAssembly 使用方法
SmartAssembly加壳工具,我还真的是不太喜欢给自己的程序加壳,觉得开源才是王道,但是没办法工作需要,需要有个加壳后与加壳前的对比,好吧谁叫咱只是程序员呢. 开始埋头苦干,找了半天也没找到合适 ...
- C++,Windows/MFC_中L和_T()之区别
字符串前面加L表示该字符串是Unicode字符串._T是一个宏,如果项目使用了Unicode字符集(定义了UNICODE宏),则自动在字符串前面加上L,否则字符串不变.因此,Visual C++里边定 ...
- python基础试题(一)
1.执行 Python 脚本的两种方式 1.python 进入解释器 2.python 1.py 执行文件 limux里 ./1.py 2.简述位.字节的关系 8位1个字节.计算机处理以字节为单位,存 ...
- iOS-NSNotificationCenter通知原理解析
一.基本概念 NSNotification和NSNotificationCenter是使用观察者模式来实现的用于跨层传递消息. NSNotificationCenter采用单例模式. 二.基本实现 通 ...