Skip to content

Commit fa8dc78

Browse files
committed
particles: tracers, effect Render binding, spritecard attribute and bounds fixes
- util.ParticleTracer / util.ParticleTracerEx, the client "ParticleTracer" effect, GetTracerOrigin (with the SWEP GetTracerOrigin hook) and FX_TracerSound - IntersectRayWithRay and CalcDistanceSqrToLineSegment - CNewParticleEffect:Render() - GL46: remap texcoord 6/7 to the wrinkle/specular locations to stay within 16 vertex attributes, and only bind flex streams when TC4/TC5 are unused; spritecard shader locations updated to match - ParticleCollection: fix the tint blue channel, initialize the constant normal attribute, and match GMod's RecomputeBounds (skip screen space effects, SSE min/max NaN semantics, summation order) - ParticleSystemDefinition: parse the "normal" constant
1 parent fe449ec commit fa8dc78

13 files changed

Lines changed: 300 additions & 29 deletions

File tree

‎Game.Assets/sdn/shaders/spritecard_gl460.vs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ layout(location = 13) in vec2 v_CornerID;
1313
layout(location = 14) in vec4 v_TexCoord2;
1414
#if DUALSEQUENCE
1515
layout(location = 15) in vec4 v_Seq2TexCoord0;
16-
layout(location = 16) in vec4 v_Seq2TexCoord1;
17-
layout(location = 17) in vec4 v_Parms1;
16+
layout(location = 6) in vec4 v_Seq2TexCoord1;
17+
layout(location = 3) in vec4 v_Parms1;
1818
#endif
1919

2020
layout(std140, binding = 0) uniform source_matrices {

‎Game.Client/C_BaseCombatWeapon.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public bool IsCarriedByLocalPlayer() {
5454

5555
public bool ShouldDrawUsingViewModel() => IsCarriedByLocalPlayer() && !C_BasePlayer.ShouldDrawLocalPlayer();
5656

57+
public virtual bool GetTracerOrigin(ref System.Numerics.Vector3 origin) => false;
58+
5759
public void Redraw() {
5860
// if (clientMode.ShouldDrawCrosshair()) todo
5961
DrawCrosshair();

‎Game.Client/FX_Tracer.cs‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using Game.Shared;
2+
3+
using Source;
4+
using Source.Common;
5+
using Source.Common.Audio;
6+
using Source.Common.Engine;
7+
using Source.Common.Mathematics;
8+
using Source.Common.SoundEmitterSystem;
9+
10+
using System.Numerics;
11+
12+
namespace Game.Client;
13+
14+
public static class FX_Tracer
15+
{
16+
static float NextWhizTime;
17+
18+
public static Vector3 GetTracerOrigin(EffectData data) {
19+
Vector3 vecStart = data.Start;
20+
int attachment = data.AttachmentIndex;
21+
22+
if ((data.Flags & TRACER_FLAG_USEATTACHMENT) == 0)
23+
return vecStart;
24+
25+
IClientRenderable? renderable = cl_entitylist.GetClientRenderableFromHandle(data.Entity);
26+
if (renderable == null)
27+
return vecStart;
28+
29+
C_BaseEntity? ent = C_BaseEntity.Instance(data.Entity);
30+
if (ent != null && ent.IsDormant())
31+
return vecStart;
32+
33+
if (ent is C_BaseCombatWeapon wpn) {
34+
if (wpn.GetTracerOrigin(ref vecStart))
35+
return vecStart;
36+
37+
C_BasePlayer? player = ToBasePlayer(wpn.GetOwner());
38+
C_BaseViewModel? viewModel = player?.GetViewModel(0);
39+
if (viewModel != null && wpn.ShouldDrawUsingViewModel())
40+
renderable = viewModel;
41+
}
42+
43+
if (renderable.GetAttachment(attachment, out Vector3 attachOrigin, out _))
44+
vecStart = attachOrigin;
45+
46+
return vecStart;
47+
}
48+
49+
public static void FX_TracerSound(in Vector3 start, in Vector3 end, int tracerType, ReadOnlySpan<char> customSoundName = default) {
50+
Vector3 vecListenOrigin = MainViewOrigin();
51+
ReadOnlySpan<char> soundName;
52+
float whizDistSqr;
53+
float minWhizTime = 0.1f;
54+
float maxWhizTime = 0.1f;
55+
56+
switch (tracerType) {
57+
case TRACER_TYPE_DEFAULT: {
58+
soundName = "Bullets.DefaultNearmiss";
59+
whizDistSqr = 24.0f * 24.0f;
60+
61+
Ray bullet = default;
62+
bullet.Init(start, end);
63+
64+
Vector3 vecLower = vecListenOrigin;
65+
vecLower.Z -= 24.0f;
66+
Ray listener = default;
67+
listener.Init(vecListenOrigin, vecLower);
68+
69+
CollisionUtils.IntersectRayWithRay(bullet, listener, out _, out float t);
70+
if (t <= 0.0f)
71+
t = 0.0f;
72+
if (1.0f <= t)
73+
t = 1.0f;
74+
vecListenOrigin.Z -= t * 24.0f;
75+
}
76+
break;
77+
case TRACER_TYPE_GUNSHIP:
78+
soundName = "Bullets.GunshipNearmiss";
79+
whizDistSqr = 72.0f * 72.0f;
80+
break;
81+
case TRACER_TYPE_STRIDER:
82+
soundName = "Bullets.StriderNearmiss";
83+
whizDistSqr = 72.0f * 72.0f;
84+
break;
85+
case TRACER_TYPE_WATERBULLET:
86+
soundName = "Underwater.BulletImpact";
87+
whizDistSqr = 48.0f * 48.0f;
88+
minWhizTime = 0.3f;
89+
maxWhizTime = 0.6f;
90+
break;
91+
default:
92+
return;
93+
}
94+
95+
if (!customSoundName.IsEmpty)
96+
soundName = customSoundName;
97+
98+
if ((float)(NextWhizTime - gpGlobals.CurTime) > 0.0f)
99+
return;
100+
101+
if (MathLib.CalcDistanceSqrToLineSegment(vecListenOrigin, start, end) >= whizDistSqr)
102+
return;
103+
104+
SoundParameters parms = new();
105+
if (C_BaseEntity.GetParametersForSound(soundName, ref parms, null)) {
106+
Vector3 shotDir = end - start;
107+
MathLib.VectorNormalize(ref shotDir);
108+
109+
LocalPlayerFilter filter = new();
110+
enginesound.EmitSound(filter, EngineSoundGlobals.SOUND_FROM_WORLD, (int)SoundEntityChannel.Static, ((Span<char>)parms.SoundName).SliceNullTerminatedString(),
111+
parms.Volume, AttenuationValues.SNDLVL_TO_ATTN(parms.SoundLevel), 0, parms.Pitch, 0, start, shotDir, null, true);
112+
}
113+
114+
NextWhizTime = (float)(gpGlobals.CurTime + random.RandomFloat(minWhizTime, maxWhizTime));
115+
}
116+
117+
static void ParticleTracerCallback(EffectData data) {
118+
C_BasePlayer? player = C_BasePlayer.GetLocalPlayer();
119+
if (player == null)
120+
return;
121+
122+
Vector3 vecStart = GetTracerOrigin(data);
123+
Vector3 vecEnd = data.Origin;
124+
125+
C_BaseEntity? entity = C_BaseEntity.Instance(data.Entity);
126+
int entIndex = entity != null ? entity.EntIndex() : -1;
127+
if (entIndex != 0 && entIndex == player.EntIndex()) {
128+
engine.GetViewAngles(out QAngle vangles);
129+
MathLib.AngleVectors(vangles, out _, out Vector3 vright, out _);
130+
131+
MathLib.VectorMA(data.Start, 4, vright, out vecStart);
132+
vecStart.Z -= 0.5f;
133+
}
134+
135+
Vector3 vecToEnd = vecEnd - vecStart;
136+
MathLib.VectorNormalize(ref vecToEnd);
137+
MathLib.VectorAngles(vecToEnd, out QAngle vecAngles);
138+
DispatchParticleEffect(data.HitBox, vecStart, vecEnd, vecAngles, entity);
139+
140+
if ((data.Flags & TRACER_FLAG_WHIZ) != 0)
141+
FX_TracerSound(vecStart, vecEnd, TRACER_TYPE_DEFAULT);
142+
}
143+
144+
static readonly ClientEffectRegistration ClientEffectReg_ParticleTracerCallback = new("ParticleTracer", ParticleTracerCallback);
145+
}

‎Game.Client/GarrysMod/LuaParticleEffect.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Game.Shared;
22

33
using Source.Common.GarrysMod.Lua;
4+
using Source.Common.MaterialSystem;
45
using Source.Common.Mathematics;
56
using Source.Particles;
67

@@ -263,6 +264,16 @@ static int CNewParticleEffect__GetAutoUpdateBBox(ILuaInterface lua) {
263264
return 1;
264265
}
265266

267+
[LuaMethod]
268+
static int CNewParticleEffect__Render(ILuaInterface lua) {
269+
CNewParticleEffect? effect = Get_ValidEffect(lua);
270+
if (effect == null)
271+
return 0;
272+
using MatRenderContextPtr renderContext = new(materials);
273+
effect.Render(renderContext.Context, effect.IsTwoPass(), null);
274+
return 0;
275+
}
276+
266277
[LuaMethod]
267278
static int CNewParticleEffect__StartEmission(ILuaInterface lua) {
268279
CNewParticleEffect? effect = Get_ValidEffect(lua);

‎Game.Client/HLClient.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public static void DLLInit(IServiceCollection services) {
5757
services.AddSingleton<ViewportClientSystem>();
5858
services.AddSingleton<IViewRender>(x => x.GetRequiredService<ViewRender>());
5959

60+
RuntimeHelpers.RunClassConstructor(typeof(FX_Tracer).TypeHandle);
61+
6062
#if GMOD_DLL
6163
// Dumb hack, but whatever
6264
RuntimeHelpers.RunClassConstructor(typeof(GModWeaponParse).TypeHandle);

‎Game.Shared/GarrysMod/LuaUtil.cs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,28 @@ static int PrecacheSound(ILuaInterface lua) {
204204
[LuaFunction]
205205
static int NetworkStringToID(string name) => NetworkString.Get(name);
206206

207+
[LuaFunction]
208+
static int ParticleTracer(ILuaInterface lua) {
209+
bool whiz = lua.GetBool(4);
210+
Vector3 end = LuaVector.Get_Vector(3);
211+
Vector3 start = LuaVector.Get_Vector(2);
212+
string? name = lua.CheckString(1);
213+
Game.Util.ParticleTracer(name, start, end, 0, 0, whiz);
214+
return 0;
215+
}
216+
217+
[LuaFunction]
218+
static int ParticleTracerEx(ILuaInterface lua) {
219+
bool whiz = lua.GetBool(4);
220+
int attachment = (int)lua.CheckNumber(6);
221+
int entIndex = (int)lua.CheckNumber(5);
222+
Vector3 end = LuaVector.Get_Vector(3);
223+
Vector3 start = LuaVector.Get_Vector(2);
224+
string? name = lua.CheckString(1);
225+
Game.Util.ParticleTracer(name, start, end, entIndex, attachment, whiz);
226+
return 0;
227+
}
228+
207229
#endif
208230

209231
[LuaFunction]

‎Game.Shared/GarrysMod/WeaponSWEP.cs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,18 @@ public override void Drop(in Vector3 velocity) {
376376
public override ReadOnlySpan<char> GetName() => ScriptedEntity.GetScriptName();
377377

378378
public void RenderScreen() => ScriptedEntity.CallFunction((int)LUA_POOLEDSTRING.RenderScreen, false);
379+
380+
public override bool GetTracerOrigin(ref Vector3 origin) {
381+
if (!ScriptedEntity.StartFunction((int)LUA_POOLEDSTRING.GetTracerOrigin) || !ScriptedEntity.Call(0, 1))
382+
return false;
383+
384+
Source.Common.GarrysMod.Lua.ILuaObject ret = g_Lua!.GetReturn(0);
385+
if (!ret.isVector())
386+
return false;
387+
388+
origin = ret.GetVector();
389+
return true;
390+
}
379391
#endif
380392

381393
public override int GetSlot() {

‎Game.Shared/ParticleParse.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public static int GetAttachTypeFromString(ReadOnlySpan<char> str) {
148148
public const int PARTICLE_DISPATCH_FROM_ENTITY = 1 << 0;
149149
public const int PARTICLE_DISPATCH_RESET_PARTICLES = 1 << 1;
150150

151-
static int ParticleSystemIndex(ReadOnlySpan<char> particleName) {
151+
public static int ParticleSystemIndex(ReadOnlySpan<char> particleName) {
152152
#if CLIENT_DLL
153153
return Game.Client.HLClient.GetParticleSystemIndex(particleName);
154154
#else

‎Game.Shared/UtilShared.cs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ public static void UTIL_StringToIntArray(Span<int> vec, int count, ReadOnlySpan<
174174
vec[j] = atoi(pString[start..pos]);
175175
}
176176
}
177+
178+
public const int TRACER_FLAG_WHIZ = 0x0001;
179+
public const int TRACER_FLAG_USEATTACHMENT = 0x0002;
180+
public const int TRACER_DONT_USE_ATTACHMENT = -1;
181+
182+
public const int TRACER_TYPE_DEFAULT = 0x00000001;
183+
public const int TRACER_TYPE_GUNSHIP = 0x00000002;
184+
public const int TRACER_TYPE_STRIDER = 0x00000004;
185+
public const int TRACER_TYPE_GAUSS = 0x00000008;
186+
public const int TRACER_TYPE_WATERBULLET = 0x00000010;
177187
}
178188

179189
public static partial class Util
@@ -184,6 +194,30 @@ public static partial class Util
184194
#endif
185195
static readonly ConVar developer = new("developer", "0", 0, "Set developer message level"); // developer mode
186196

197+
public static void ParticleTracer(ReadOnlySpan<char> tracerEffectName, in Vector3 vecStart, in Vector3 vecEnd, int entIndex, int attachment, bool whiz) {
198+
Game.Shared.EffectData data = new() {
199+
Start = vecStart,
200+
Origin = vecEnd,
201+
Scale = 0,
202+
HitBox = Game.Shared.ParticleParse.ParticleSystemIndex(tracerEffectName)
203+
};
204+
#if CLIENT_DLL
205+
data.Entity = cl_entitylist.EntIndexToHandle(entIndex);
206+
#else
207+
data.EntIndex = entIndex;
208+
#endif
209+
210+
if (whiz)
211+
data.Flags |= TRACER_FLAG_WHIZ;
212+
213+
if (attachment != TRACER_DONT_USE_ATTACHMENT) {
214+
data.Flags |= TRACER_FLAG_USEATTACHMENT;
215+
data.AttachmentIndex = attachment;
216+
}
217+
218+
DispatchEffect("ParticleTracer", data);
219+
}
220+
187221
public static Contents PointContents(in Vector3 vec) => enginetrace.GetPointContents(vec, out _);
188222

189223
// UTIL_StringToColor32: parses "r g b a" into a color.

‎Source.Common/CollisionUtils.cs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,37 @@ public static void Collision_ClearTrace(in Vector3 rayStart, in Vector3 rayDelta
2828
trace.Contents = 0;
2929
}
3030

31+
public static bool IntersectRayWithRay(in Ray ray0, in Ray ray1, out float t0, out float t1) {
32+
Vector3 v0 = ray0.Delta;
33+
Vector3 v1 = ray1.Delta;
34+
MathLib.VectorNormalize(ref v0);
35+
MathLib.VectorNormalize(ref v1);
36+
37+
float cx = v1.Z * v0.Y - v1.Y * v0.Z;
38+
float cy = v1.X * v0.Z - v0.X * v1.Z;
39+
float cz = v0.X * v1.Y - v1.X * v0.Y;
40+
float lenSq = cy * cy + cx * cx + cz * cz;
41+
if (lenSq == 0.0f) {
42+
t0 = 0.0f;
43+
t1 = 0.0f;
44+
return false;
45+
}
46+
47+
float inv = 1.0f / lenSq;
48+
float dx = ray1.Start.X - ray0.Start.X;
49+
float dy = ray1.Start.Y - ray0.Start.Y;
50+
float dz = ray1.Start.Z - ray0.Start.Z;
51+
float ax = dz * cy - dy * cz;
52+
float ay = dx * cz - dz * cx;
53+
float az = dy * cx - dx * cy;
54+
t1 = (ay * v0.Y + ax * v0.X + az * v0.Z) * inv;
55+
t0 = (ay * v1.Y + ax * v1.X + az * v1.Z) * inv;
56+
57+
return v0.X * t0 + ray0.Start.X == v1.X * t1 + ray1.Start.X
58+
&& v0.Y * t0 + ray0.Start.Y == v1.Y * t1 + ray1.Start.Y
59+
&& v0.Z * t0 + ray0.Start.Z == v1.Z * t1 + ray1.Start.Z;
60+
}
61+
3162
public static bool IntersectRayWithBox(in Vector3 rayStart, in Vector3 rayDelta, in Vector3 boxMins, in Vector3 boxMaxs, float tolerance, out BoxTraceInfo trace) {
3263
int i;
3364
float d1, d2;

0 commit comments

Comments
 (0)