Unity4 Particle Scaler粒子缩放

转自: http://fenglee.com/blog/html/293.html


你只需要,把这个行为绑定到需要的粒子系统上。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class ParticleScaling : MonoBehaviour
{
public void OnWillRenderObject()
{
//GetComponent().material.SetVector(“_Center”, center);《————-OLD WAY
GetComponent().renderer.material.SetVector(“_Center”, transform.position);
GetComponent().renderer.material.SetVector(“_Scaling”, transform.lossyScale);
GetComponent().renderer.material.SetMatrix(“_Camera”, Camera.current.worldToCameraMatrix);
GetComponent().renderer.material.SetMatrix(“_CameraInv”, Camera.current.worldToCameraMatrix.inverse);
}
}

并且,将需要用到的shader(可以到这里下载到http://unity3d.com/unity/download/archive),拿出来,单独修改一份。
比如说,我用了particle alpha blended这个shader 修改的地方就是
修改前:

float4 _MainTex_ST;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
#ifdef SOFTPARTICLES_ON
o.projPos = ComputeScreenPos (o.vertex);
COMPUTE_EYEDEPTH(o.projPos.z);
#endif
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}

修改后:

float4 _MainTex_ST;
// new 4 lines
float3 _Scaling;
float4 _Center;
float4x4 _Camera;
float4x4 _CameraInv;
v2f vert (appdata_t v)
{
v2f o;
if(_Scaling.x != 0){
// START OF CHANGE
float4 objV = mul(_CameraInv, v.vertex);
objV.xyz -= _Center;
objV.xyz = float3(_Scaling.x * objV.x, _Scaling.y * objV.y, _Scaling.z * objV.z);
objV.xyz += _Center;
o.vertex = mul(_Camera, objV);
// END OF CHANGE
o.vertex = mul(UNITY_MATRIX_MVP, o.vertex);
}else{
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
}
#ifdef SOFTPARTICLES_ON
o.projPos = ComputeScreenPos (o.vertex);
COMPUTE_EYEDEPTH(o.projPos.z);
#endif
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}

注意不同的地方。

这样,使用这个修改后的Shader和行为绑定脚本。就可以达到缩放的目的了。这样假如使用了缩放的shader,但没有绑定行为脚本的话,也不会出错(为了节省材质球=。=)。

转自 http://fenglee.com/blog/html/293.html
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值