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,这下好了,布局出来了,放在机子上跑,卡得想死有木有,信息乱跑乱出现,表示非常 ...
随机推荐
- Activity学习(二):Activity的启动模式(转载)
在Android中每个界面都是一个Activity,切换界面操作其实是多个不同Activity之间的实例化操作.在Android中Activity的启动模式决定了Activity的启动运行方式. An ...
- tp5 thinkphp5 多表关联查询 join查询
model下: $res = \think\Db::name('article') ->alias("a") //取一个别名 ->join('admin ad','a. ...
- java List接口中常用类
Vector:线程安全,但速度慢,已被ArrayList替代. ArrayList:线程不安全,查询速度快. LinkedList:链表结构,增删速度快.取出List集合中元素的方式: get(int ...
- SVN:符号
问号:未纳入SVN控制之下的新文件感叹号:已经修改过还没上传的文件红叉:准备删除尚未提交绿勾:从服务器上取下来没修改过的文件黄色感叹号:发生冲突蓝色加号:准备加到服务器上还没提交
- 51nod1370 排列与操作
性质:最终值域相同的一定是连续一段 花费最小?一定是值域个数个!并且当最后为i的数恰好只有i一个位置的时候,肯定选择不动,少花费一个 所以,我们考虑:每个最终方案在花费最小的方案下恰好被统计一次! 而 ...
- 人脸检测MTCNN的训练过程(PRO网络)
以下学习均由此:https://github.com/AITTSMD/MTCNN-Tensorflow 数据集 WIDER Face for face detection and Celeba for ...
- 2018-10-22-win10-uwp-自定义控件入门
title author date CreateTime categories win10 uwp 自定义控件入门 lindexi 2018-10-22 09:47:54 +0800 2018-10- ...
- dotnet core 使用 CoreRT 将程序编译为 Native 程序
现在微软有一个开源项目 CoreRT 能通过将托管的 .NET Core 编译为单个无依赖的 Native 程序 这个项目现在还没发布,但是能尝试使用,可以带来很多的性能提升 使用 CoreRT 发布 ...
- poll 和 select 底层的数据结构
poll 和 select 系统调用的真正实现是相当地简单, 对那些感兴趣于它如何工作的人; epoll 更加复杂一点但是建立在同样的机制上. 无论何时用户应用程序调用 poll, select, 或 ...
- es6笔记 day3---对象简介语法以及对象新增
以前的老写法↓ 新写法来了↓ 提示:千万不要手贱,在里面去用箭头函数!!! -------------------------------------------------------------- ...