博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于Unity ParticleSystem的一些"冷"知识
阅读量:4225 次
发布时间:2019-05-26

本文共 8085 字,大约阅读时间需要 26 分钟。

  目前的游戏开发中,粒子系统的使用想必是标配了,Unity自然也提供了相应的解决方案:ParticleSystem,网上对ParticleSystem的介绍也非常之多(譬如,,还有),虽然相关的资料教程不少,但多是一些如何使用之类的常规信息,对于一些虽然有些“生冷”,但仍然可能遇到的粒子知识,网上涉及的资源便不多了,本篇就随便讲讲自己遇到的几个ParticleSystem的“冷”知识~

 

  1. 如何在EditorPlayback粒子效果?

 

  Unity本身自带ParticleEditor,正常情况下选中所编辑的粒子即可Playback,但是当我们切换选择到其他GameObject的时候,之前粒子的Playback便停止了,这在某些时候可能会给我们开发带来不便,考虑你基于Unity开发了一个表现预览器,预览器里显示的内容可能包括角色模型,技能特效等等各种内容,如果我们在编辑器中选择了某个GameObject节点,我们的期望自然是在视图中可以看到挂接在该GameObject下的所有显示效果,其中几乎肯定会有粒子特效,如果你简单的尝试调用一下ParticleSystemPlay接口,就会发现在Editor模式下粒子并不会如期的进行Playback……

  

  一个Workaround的方案是仅在UnityEditorPlay模式下才提供粒子的Playback,这在一般情况下是可以接受的,但是算不上一个很好的方案,更好的解决方法还是实现Editor模式下的粒子Playback功能,能办到吗?

 

  其实是可以的,其中的关键是用好接口~

 

  简单看下该接口的API说明:

 

  Fastforwards the particle system by simulating particles over given period of time, then pauses it.

 

  该接口能够模拟粒子快进一段指定时间并停止粒子,Nice,如果我们在Editor下的Update中持续对该接口进行调用,并传递deltaTime作为时间参数,那么粒子便能在EditorPlayback起来了!

 

  简单看下示例代码:

// desc simple particle playback window// maintainer hugoyuusing System;using System.Collections.Generic;using UnityEngine;using UnityEditor;public class ParticlePlaybackWindow : EditorWindow{    // implementation details here    // Called multiple times per second on all visible windows.    void Update()    {        if (m_playbackState == PlaybackState.Playing)        {            if (m_particlePlayback.HaveParticleSystems())            {                // simulate particles                if (m_lastUpdateTime <= 0)                {                    m_lastUpdateTime = Time.realtimeSinceStartup;                }                m_particlePlayback.Update(Time.realtimeSinceStartup - m_lastUpdateTime);                m_lastUpdateTime = Time.realtimeSinceStartup;                // repaint views                EditorUtil.RepaintView();            }        }    }		// implementation details here	}

  其中的核心代码便是这句

 

  m_particlePlayback.Update(Time.realtimeSinceStartup - m_lastUpdateTime);

 

  简单看下ParticlePlayback的实现:

// desc simple particle play back implementation// maintainer hugoyuusing System.Collections.Generic;using System.Collections.ObjectModel;using UnityEngine;using UnityEditor;class ParticlePlayback{    // implementation details here    public void Update(float deltaTime)    {        for (int i = 0; i < m_particleSystems.Count; ++i)        {            // NOTE need false params here since default params are all true            m_particleSystems[i].Simulate(deltaTime, false, false);        }    }    // implementation details here}

2. 粒子可以Scale吗?

 

  对于很早便开始接触Unity的朋友,对于这个问题的回答可能是否定的,至少在运行模式下是否定的,不过这个观点已经过时了,现在我们已经可以很方便的缩放ParticleSystem了,至于如何做,容我细细道来~

 

  Unity5.3版本之前,ParticleSystem并不能很好的受Transform的缩放影响,为了达到Scale的目的,一个方法是根据Scale动态的改变ParticleSystem的各项粒子属性,譬如大小、速度等等,但是由于粒子的不少属性并没有暴露给脚本使用,导致必须使用SerializedObject来进行反射获取,这也进一步导致该方法只能在编辑器环境下使用,网上相关的资料不少也是该方法的不同实现(譬如和),简单列下示例代码:

void ScaleShurikenSystems(float scaleFactor){#if UNITY_EDITOR 		//get all shuriken systems we need to do scaling on		ParticleSystem[] systems = GetComponentsInChildren
(); for (int index = 0; index < systems.Length; index++) { ParticleSystem system = systems[index]; system.startSpeed *= scaleFactor; system.startSize *= scaleFactor; system.gravityModifier *= scaleFactor; //some variables cannot be accessed through regular script, we will acces them through a serialized object SerializedObject so = new SerializedObject(system); //unity 4.0 and onwards will already do this one for us#if UNITY_3_5 so.FindProperty("ShapeModule.radius").floatValue *= scaleFactor; so.FindProperty("ShapeModule.boxX").floatValue *= scaleFactor; so.FindProperty("ShapeModule.boxY").floatValue *= scaleFactor; so.FindProperty("ShapeModule.boxZ").floatValue *= scaleFactor;#endif so.FindProperty("VelocityModule.x.scalar").floatValue *= scaleFactor; so.FindProperty("VelocityModule.y.scalar").floatValue *= scaleFactor; so.FindProperty("VelocityModule.z.scalar").floatValue *= scaleFactor; so.FindProperty("ClampVelocityModule.magnitude.scalar").floatValue *= scaleFactor; so.FindProperty("ClampVelocityModule.x.scalar").floatValue *= scaleFactor; so.FindProperty("ClampVelocityModule.y.scalar").floatValue *= scaleFactor; so.FindProperty("ClampVelocityModule.z.scalar").floatValue *= scaleFactor; so.FindProperty("ForceModule.x.scalar").floatValue *= scaleFactor; so.FindProperty("ForceModule.y.scalar").floatValue *= scaleFactor; so.FindProperty("ForceModule.z.scalar").floatValue *= scaleFactor; so.FindProperty("ColorBySpeedModule.range").vector2Value *= scaleFactor; so.FindProperty("SizeBySpeedModule.range").vector2Value *= scaleFactor; so.FindProperty("RotationBySpeedModule.range").vector2Value *= scaleFactor; so.ApplyModifiedProperties(); }#endif}

  另外的一种缩放粒子的方式是使用定制的Shader,这种方法便不存在只能在Editor下使用的限制了,但是需要为所有粒子使用到的Shader扩展Scale功能,感觉上也是略有不便,网上也有不少相关的介绍,譬如和,有兴趣的朋友可以细致看看,在此便不列代码了。

 

  Unity5.3之后,ParticleSystem为脚本导出了相关的属性接口,并且很好的支持了TransformScale变换,相关的介绍可以看和(还有个小讨论),同样的,我们仍然可以沿用之前改变粒子属性的方法来达到缩放的目的,只是这次我们不再需要SerializedObject了,自然代码也“清新”了许多,上有段示例代码,大概贴一下:

static void ScaleSystem(ParticleSystem particles, float scale, bool scalePosition, ParticleScalerOptions options = null){    if (options == null) { options = defaultOptions; }	if (scalePosition) { particles.transform.localPosition *= scale; }	particles.startSize *= scale;	particles.gravityModifier *= scale;	particles.startSpeed *= scale;	if (options.shape)	{		var shape = particles.shape;		shape.radius *= scale;		shape.box = shape.box * scale;	}	if (options.velocity) 	{		var vel = particles.velocityOverLifetime;		vel.x = ScaleMinMaxCurve(vel.x, scale);		vel.y = ScaleMinMaxCurve(vel.y, scale);		vel.z = ScaleMinMaxCurve(vel.z, scale);	}	if (options.clampVelocity) 	{		var clampVel = particles.limitVelocityOverLifetime;		clampVel.limitX = ScaleMinMaxCurve(clampVel.limitX, scale);		clampVel.limitY = ScaleMinMaxCurve(clampVel.limitY, scale);		clampVel.limitZ = ScaleMinMaxCurve(clampVel.limitZ, scale);	}	if (options.force) 	{		var force = particles.forceOverLifetime;		force.x = ScaleMinMaxCurve(force.x, scale);		force.y = ScaleMinMaxCurve(force.y, scale);		force.z = ScaleMinMaxCurve(force.z, scale);	}}

  不过个人感觉更简洁的方式,还是使用ParticleSystemScalingMode.Hierarchy,简单设置一下粒子的scalingModeHierarchy,之后便可以简单的调整Hierarchy层级的Transform缩放了,如果使用ParticleSystemScalingMode.Local,缩放粒子也并不困难,概念上和Hierarchy也是类似的,大概贴下代码:

static float GetScaleInternal(ParticleSystem particleSystem){	Debug.Assert(particleSystem != null);	var localScale = particleSystem.transform.localScale;	// NOTE now we assume local scale component values are always same, pay attention	Debug.Assert(MathUtil.IsEqual(localScale.x, localScale.y, localScale.z));	return localScale.x;}static void ScaleInternal(ParticleSystem particleSystem, float scale){	Debug.Assert(particleSystem != null);	// set scaling mode and adjust local scale and gravity	particleSystem.scalingMode = ParticleSystemScalingMode.Local;	var oldScale = GetScaleInternal(particleSystem);	particleSystem.transform.localScale = Vector3.one * scale;	// NOTE now we assume gravityModifier is always sync local scale value, pay attention	particleSystem.gravityModifier *= (scale / oldScale);}public static void Scale(ParticleSystem particleSystem, float scale, bool includeChildren){	if (particleSystem != null)	{		ScaleInternal(particleSystem, scale);		if (includeChildren)		{			var childrenParticleSystems = particleSystem.GetComponentsInChildren
(); for (int i = 0; i < childrenParticleSystems.Length; ++i) { if (childrenParticleSystems[i] != particleSystem) { ScaleInternal(childrenParticleSystems[i], scale); } } } }}

  至于ParticleSystemScalingMode.Shape,该模式下,TransformScale只会影响粒子的Shape大小,并不会影响诸如粒子大小速度之类的其他属性~

 

  最后贴张测试图,从左到右的三个ParticleSystem的属性都是相同的,差别仅在scalingMode上,第一个为Hierarchy,第二为Local,第三为Shape ~

  现在我们尝试缩放粒子,第一和第三都仅是调整TransformScale数值,第二个我们通过改变粒子属性值来进行缩放,结果如下:

  

  可以看到,第一个粒子“完美”的被放大了,第三个粒子确实只有EmitShape被放大了,至于第二个粒子感觉可能有些奇怪,似乎是部分被放大的感觉,其实是因为我们使用了改变粒子属性的方法,遂而导致旧粒子不会受到缩放影响,于是产生了上述效果~

  OK,粒子系统的“冷”知识大概就讲这么多吧,下次再见了~

你可能感兴趣的文章
python使用lxml解析命名空间xml
查看>>
cookie和session区别详解
查看>>
程序员失业第一步?斯坦福研究员用AI从编译器反馈中学习改Bug
查看>>
原创 | 电视广告流量预测中的“常识”陷阱,你掉进去了吗?
查看>>
DeepMind发布最新《神经网络中持续学习》综述论文!
查看>>
本科三篇顶会一作、超算竞赛冠军,2020清华本科特奖结果出炉
查看>>
多语言互通:谷歌发布实体检索模型,涵盖超过100种语言和2000万个实体
查看>>
你的房东可能正用AI筛查你的犯罪记录,决定要不要租房给你
查看>>
工业图像异常检测最新研究总结(2019-2020)
查看>>
独家 | 用归纳偏置来增强你的模型性能
查看>>
CoRL 2020奖项公布,斯坦福获最佳论文奖,华为等摘得最佳系统论文奖
查看>>
独家 | 这可能会引领通用AI的下一个重大突破
查看>>
学习人必看!空军老兵自学编程,仅隔一年成为国土安全部的数据库分析师
查看>>
皱眉细节完美复刻,阿尔伯塔大学团队标星2.5K的项目生成超逼真的肖像画
查看>>
微软亚研院副院长周明:从语言智能到代码智能
查看>>
不到1000行代码,GitHub 1400星,天才黑客开源深度学习框架tinygrad
查看>>
Nature:科研PUA太严重,过半博士后打算逃离
查看>>
数据科学很性感?不,其实它非常枯燥!
查看>>
李宏毅强化学习完整笔记!开源项目《LeeDeepRL-Notes》发布
查看>>
赵劲松:预知潜在风险,做化工安全科技创新的引领者
查看>>