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,但没有绑定行为脚本的话,也不会出错(为了节省材质球=。=)。