diff --git a/Assets/Gothic-Core/Scripts/Domain/Meshes/Builder/VobPfxMeshBuilder.cs b/Assets/Gothic-Core/Scripts/Domain/Meshes/Builder/VobPfxMeshBuilder.cs index 946aa346a..f8e6f11d4 100644 --- a/Assets/Gothic-Core/Scripts/Domain/Meshes/Builder/VobPfxMeshBuilder.cs +++ b/Assets/Gothic-Core/Scripts/Domain/Meshes/Builder/VobPfxMeshBuilder.cs @@ -34,6 +34,8 @@ private void ApplyGothicShapeAndDirection(ZenKit.Daedalus.ParticleEffectInstance if (pfx.DirModeS.EqualsIgnoreCase("DIR")) { + var toVelocitySpace = GetVelocityRotation(pfx.DirForS.EqualsIgnoreCase("WORLD"), pfxGo, + out var velocitySpace); var minSpeed = Mathf.Max(0f, (pfx.VelAvg - pfx.VelVar) * _velocityScale); var maxSpeed = Mathf.Max(0f, (pfx.VelAvg + pfx.VelVar) * _velocityScale); var min = Vector3.positiveInfinity; @@ -45,8 +47,8 @@ private void ApplyGothicShapeAndDirection(ZenKit.Daedalus.ParticleEffectInstance var heading = (pfx.DirAngleHead + headStep * pfx.DirAngleHeadVar) * Mathf.Deg2Rad; var elevation = Mathf.Clamp(pfx.DirAngleElev + elevStep * pfx.DirAngleElevVar, -90f, 90f) * Mathf.Deg2Rad; - var sample = new Vector3(Mathf.Cos(elevation) * Mathf.Sin(heading), Mathf.Sin(elevation), - Mathf.Cos(elevation) * Mathf.Cos(heading)); + var sample = toVelocitySpace * new Vector3(Mathf.Cos(elevation) * Mathf.Sin(heading), + Mathf.Sin(elevation), Mathf.Cos(elevation) * Mathf.Cos(heading)); foreach (var speed in new[] { minSpeed, maxSpeed }) { min = Vector3.Min(min, sample * speed); @@ -59,9 +61,7 @@ private void ApplyGothicShapeAndDirection(ZenKit.Daedalus.ParticleEffectInstance main.startSpeed = 0f; var velocity = particleSystem.velocityOverLifetime; velocity.enabled = true; - velocity.space = pfx.DirForS.EqualsIgnoreCase("WORLD") - ? ParticleSystemSimulationSpace.World - : ParticleSystemSimulationSpace.Local; + velocity.space = velocitySpace; velocity.x = new ParticleSystem.MinMaxCurve(min.x, max.x); velocity.y = new ParticleSystem.MinMaxCurve(min.y, max.y); velocity.z = new ParticleSystem.MinMaxCurve(min.z, max.z); @@ -80,6 +80,27 @@ private void ApplyGothicShapeAndDirection(ZenKit.Daedalus.ParticleEffectInstance Convert.ToBoolean(pfx.ShpScaleIsLooping), Convert.ToBoolean(pfx.ShpScaleIsSmooth)); } + /// + /// The space of the velocity module and the rotation that takes a direction there from its frame (the world or + /// the emitter). With World simulation a Local velocity is turned with the emitter's current rotation every + /// frame - a turning emitter (the grindstone) curved particles that are already flying. So the direction is + /// taken into the world once, with the emitter's rotation at creation (dirFor OBJECT: the frame a particle + /// starts in). Limitation: an emitter that turns after it was built (reparented, a turning mover, a swung + /// torch) keeps the direction it had then - Unity has no per-particle frame for velocity over lifetime. + /// Effects that follow their emitter keep both Local (the direction turns with it, like before). + /// + private Quaternion GetVelocityRotation(bool isWorldDir, GameObject pfxGo, + out ParticleSystemSimulationSpace space) + { + if (_isFollowingEmitter) + { + space = isWorldDir ? ParticleSystemSimulationSpace.World : ParticleSystemSimulationSpace.Local; + return Quaternion.identity; + } + space = ParticleSystemSimulationSpace.World; + return isWorldDir ? Quaternion.identity : pfxGo.transform.rotation; + } + private const float _velocityScale = 10f; // VELO particles slower than this (m/s) are plain billboards. private const float _minStretchSpeed = 0.1f; @@ -99,7 +120,8 @@ private void CreateSubEmitter(ZenKit.Daedalus.ParticleEffectInstance pfx, GameOb _subEmitterDepth++; try { - var child = _pfxMeshService.CreateVobPfx(childName, parent: pfxGo, destroyAfterPlay: _destroyAfterPlay); + var child = _pfxMeshService.CreateVobPfx(childName, parent: pfxGo, destroyAfterPlay: _destroyAfterPlay, + isFollowingEmitter: _isFollowingEmitter); var childRoot = child != null ? child.transform.parent : null; if (childRoot != null && childRoot.gameObject != pfxGo) childRoot.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity); @@ -132,6 +154,7 @@ private void CreateSubEmitter(ZenKit.Daedalus.ParticleEffectInstance pfx, GameOb private string _visualName; private bool _destroyAfterPlay; + private bool _isFollowingEmitter; public void SetPfxData(string visualName) @@ -144,6 +167,15 @@ public void SetDestroyAfterPlay(bool destroyAfterPlay) _destroyAfterPlay = destroyAfterPlay; } + /// + /// An effect that sticks to a moving body (a spell's glow in a hand, an aura on an NPC): its particles move and + /// turn with the emitter (Local simulation). Otherwise they live in the world once emitted. + /// + public void SetFollowingEmitter(bool isFollowingEmitter) + { + _isFollowingEmitter = isFollowingEmitter; + } + /// /// Spell/visual effects: looping ones emit their full ppsValue (the /100 was tuned for world fires). /// @@ -215,12 +247,13 @@ public override GameObject Build() // Always disable Unity's built-in gravity - use Force over Lifetime instead mainModule.gravityModifier = 0f; - // Simulation space from FOR switches (prefer dir FOR if present) - var simFor = (pfx.DirForS ?? pfx.ShpForS)?.ToUpperInvariant(); - if (simFor == "WORLD") - mainModule.simulationSpace = ParticleSystemSimulationSpace.World; - else - mainModule.simulationSpace = ParticleSystemSimulationSpace.Local; + // Like the engine, an emitted particle lives in the world: it doesn't move or turn with its emitter + // afterwards. dirFor/shpFor only say in which frame a particle starts (velocity module below). Local + // simulation carried the sparks along with the turning grindstone - a line instead of a falling shower. + // Effects stuck to a moving body (a glow in a hand, an aura) move with it (SetFollowingEmitter). + mainModule.simulationSpace = _isFollowingEmitter + ? ParticleSystemSimulationSpace.Local + : ParticleSystemSimulationSpace.World; // Start size from visSizeStart (cm->m). Allow end scale via Size over Lifetime below. if (!string.IsNullOrEmpty(pfx.VisSizeStartS) && pfx.VisSizeStartS != "=") @@ -287,6 +320,9 @@ public override GameObject Build() if (gravity.Length == 3) { forceModule.enabled = true; + // flyGravity is a world vector (down is -Y): in the emitter's frame a turned emitter (a grindstone, + // a spell in the hand) pulled its particles sideways. + forceModule.space = ParticleSystemSimulationSpace.World; forceModule.x = gravityX; forceModule.y = gravityY; forceModule.z = gravityZ; @@ -497,6 +533,12 @@ public override GameObject Build() shapeModule.alignToDirection = false; // Don't align to direction for blood splatter } + // Only a requested position. Quaternion's == / != compare by dot product (default != default is true) - + // Equals() compares the components. Before the velocity: it takes the emitter's rotation into the world. + var hasRotation = !RootRotation.Equals(default(Quaternion)); + if (RootPosition != Vector3.zero || hasRotation) + RootGo.transform.SetPositionAndRotation(RootPosition, hasRotation ? RootRotation : Quaternion.identity); + ApplyGothicShapeAndDirection(pfx, particleSystem, pfxGo); // Velocity over Lifetime module (for directional spread and TARGET) @@ -521,12 +563,15 @@ public override GameObject Build() { var target = new Vector3(tx / 100f, ty / 100f, tz / 100f); var origin = Vector3.zero; // assuming emitter origin in Local space by default - var dir = (target - origin).normalized; + // The target is in the emitter's frame (as before) - into the world with the emitter's + // rotation at creation (GetVelocityRotation). + var toVelocitySpace = GetVelocityRotation(false, pfxGo, out var velocitySpace); + var dir = toVelocitySpace * (target - origin).normalized; // Use average speed var speed = Mathf.Max(0f, pfx.VelAvg); velocityModule.enabled = true; - velocityModule.space = ParticleSystemSimulationSpace.Local; + velocityModule.space = velocitySpace; velocityModule.x = new ParticleSystem.MinMaxCurve(dir.x * speed); velocityModule.y = new ParticleSystem.MinMaxCurve(dir.y * speed); velocityModule.z = new ParticleSystem.MinMaxCurve(dir.z * speed); @@ -589,12 +634,6 @@ public override GameObject Build() } } - // Only a requested position. Quaternion's == / != compare by dot product (default != default is true) - - // Equals() compares the components. - var hasRotation = !RootRotation.Equals(default(Quaternion)); - if (RootPosition != Vector3.zero || hasRotation) - RootGo.transform.SetPositionAndRotation(RootPosition, hasRotation ? RootRotation : Quaternion.identity); - particleSystem.Play(); CreateSubEmitter(pfx, pfxGo); diff --git a/Assets/Gothic-Core/Scripts/Domain/Npc/Actions/AnimationActions/AiDrawWeapon.cs b/Assets/Gothic-Core/Scripts/Domain/Npc/Actions/AnimationActions/AiDrawWeapon.cs index b4eab5fd7..942721541 100644 --- a/Assets/Gothic-Core/Scripts/Domain/Npc/Actions/AnimationActions/AiDrawWeapon.cs +++ b/Assets/Gothic-Core/Scripts/Domain/Npc/Actions/AnimationActions/AiDrawWeapon.cs @@ -113,16 +113,20 @@ private void SpawnSpellVfx() if (NpcContainer.ActiveSpellVfxGo != null) Object.Destroy(NpcContainer.ActiveSpellVfxGo); var pfxName = $"MFX_{mfxName.ToUpper()}_INIT"; - NpcContainer.ActiveSpellVfxGo = _meshService.CreateVobPfx(pfxName, parent: handGo); + // The glow stays in the hand (follows the emitter) - in the world it streaked behind the mage's arm. + NpcContainer.ActiveSpellVfxGo = _meshService.CreateVobPfx(pfxName, parent: handGo, + isFollowingEmitter: true); if (NpcContainer.ActiveSpellVfxGo == null) { pfxName = "MFX_FIREBALL_INIT"; // G2 fire fallback - NpcContainer.ActiveSpellVfxGo = _meshService.CreateVobPfx(pfxName, parent: handGo); + NpcContainer.ActiveSpellVfxGo = _meshService.CreateVobPfx(pfxName, parent: handGo, + isFollowingEmitter: true); } if (NpcContainer.ActiveSpellVfxGo == null) { pfxName = "MFX_FIREBOLT_INIT"; // G1 fire fallback - NpcContainer.ActiveSpellVfxGo = _meshService.CreateVobPfx(pfxName, parent: handGo); + NpcContainer.ActiveSpellVfxGo = _meshService.CreateVobPfx(pfxName, parent: handGo, + isFollowingEmitter: true); } // Force looping so the glow stays on hand until undraw/attack diff --git a/Assets/Gothic-Core/Scripts/Services/Meshes/MeshService.cs b/Assets/Gothic-Core/Scripts/Services/Meshes/MeshService.cs index 96562553f..228e944a4 100644 --- a/Assets/Gothic-Core/Scripts/Services/Meshes/MeshService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Meshes/MeshService.cs @@ -211,10 +211,12 @@ public GameObject CreateVobPfx(IVirtualObject vob, Vector3 position = default, Q } public GameObject CreateVobPfx(string pfxName, Vector3 position = default, Quaternion rotation = default, - GameObject parent = null, bool destroyAfterPlay = false, bool isFullRate = false) + GameObject parent = null, bool destroyAfterPlay = false, bool isFullRate = false, + bool isFollowingEmitter = false) { var vobPfxBuilder = new VobPfxMeshBuilder().Inject(); vobPfxBuilder.SetFullRate(isFullRate); + vobPfxBuilder.SetFollowingEmitter(isFollowingEmitter); vobPfxBuilder.SetGameObject(null, pfxName); vobPfxBuilder.SetParent(parent); vobPfxBuilder.SetRootPosAndRot(position, rotation); diff --git a/Assets/Gothic-Core/Scripts/Services/Meshes/ParticleService.cs b/Assets/Gothic-Core/Scripts/Services/Meshes/ParticleService.cs index 296c62b1e..7401bde50 100644 --- a/Assets/Gothic-Core/Scripts/Services/Meshes/ParticleService.cs +++ b/Assets/Gothic-Core/Scripts/Services/Meshes/ParticleService.cs @@ -90,8 +90,10 @@ private void PlayVisualFx(string fxName, Vector3 position, Transform follow, boo anchor.transform.SetPositionAndRotation(position, Quaternion.identity); if (follow != null) anchor.transform.SetParent(follow, true); + // An effect that follows someone (sleep over the head, an aura, the hit effect on an NPC) moves with + // him - in the world it trailed behind a walking NPC. var pfxGo = _meshService.CreateVobPfx(fx.VisNameS, parent: anchor, destroyAfterPlay: !isLooping, - isFullRate: true); + isFullRate: true, isFollowingEmitter: follow != null); if (pfxGo == null) { UnityEngine.Object.Destroy(anchor); diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellEffect.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellEffect.cs index c5f32f4ff..60579b3de 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellEffect.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellEffect.cs @@ -40,7 +40,8 @@ public static GameObject Create(ItemInstance item, GameObject parent, DaedalusVm { if (string.IsNullOrEmpty(pfxName)) continue; - var leaf = meshService.CreateVobPfx(pfxName, parent: parent); + // Held in the hand or the ring: follows it (PrepareHeldEffect). + var leaf = meshService.CreateVobPfx(pfxName, parent: parent, isFollowingEmitter: true); if (leaf == null) continue; @@ -79,11 +80,12 @@ public static void PrepareHeldEffect(GameObject effect, bool isGothicMotion) main.loop = true; // Default (Local) ignores the parent's scale - the invest level size and the ring's fit wouldn't show. main.scalingMode = ParticleSystemScalingMode.Hierarchy; + // Moving with the hand/ring, not left behind in the world - in both modes (the builder simulates in + // World: the ring's standing glow smeared into a streak when the hand moved). + main.simulationSpace = ParticleSystemSimulationSpace.Local; var emission = ps.emission; if (isGothicMotion) { - // Moving with the hand/ring, not left behind in the world. - main.simulationSpace = ParticleSystemSimulationSpace.Local; if (emission.rateOverTime.constantMax < _minEmissionRate) emission.rateOverTime = _minEmissionRate; } diff --git a/Assets/Gothic-VR/Scripts/Adapters/Vob/VobItem/VRSpellFx.cs b/Assets/Gothic-VR/Scripts/Adapters/Vob/VobItem/VRSpellFx.cs index ba14a048c..6ef730ab4 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Vob/VobItem/VRSpellFx.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Vob/VobItem/VRSpellFx.cs @@ -144,7 +144,8 @@ public void SpawnHandVfx(VrHandSide handSide, string pfxNameOverride = null) return; } var pfxName = pfxNameOverride ?? $"MFX_{mfxName.ToUpper()}_INIT"; - var pfxLeaf = _meshService.CreateVobPfx(pfxName, parent: handGo); + // The charge stays in the hand (follows the emitter, PrepareHeldEffect below). + var pfxLeaf = _meshService.CreateVobPfx(pfxName, parent: handGo, isFollowingEmitter: true); // The spell's own VISUALFX visual before the generic fallbacks: MFX_ICEWAVE_INIT doesn't exist, // spellFX_Icewave uses MFX_IceCUBE_INIT (a fireball was shown in the hand). if (pfxLeaf == null && pfxNameOverride == null) @@ -162,7 +163,7 @@ public void SpawnHandVfx(VrHandSide handSide, string pfxNameOverride = null) if (string.IsNullOrEmpty(visName)) continue; pfxName = visName; - pfxLeaf = _meshService.CreateVobPfx(pfxName, parent: handGo); + pfxLeaf = _meshService.CreateVobPfx(pfxName, parent: handGo, isFollowingEmitter: true); if (pfxLeaf != null) break; } @@ -170,12 +171,12 @@ public void SpawnHandVfx(VrHandSide handSide, string pfxNameOverride = null) if (pfxLeaf == null) { pfxName = "MFX_FIREBALL_INIT"; - pfxLeaf = _meshService.CreateVobPfx(pfxName, parent: handGo); + pfxLeaf = _meshService.CreateVobPfx(pfxName, parent: handGo, isFollowingEmitter: true); } if (pfxLeaf == null) { pfxName = "MFX_FIREBOLT_INIT"; - pfxLeaf = _meshService.CreateVobPfx(pfxName, parent: handGo); + pfxLeaf = _meshService.CreateVobPfx(pfxName, parent: handGo, isFollowingEmitter: true); } if (pfxLeaf != null) @@ -522,7 +523,8 @@ public void ToggleLightSpell() _stationaryLightsService.ClaimHeroLight(lightGo); stationaryLight.Init(); - _meshService.CreateVobPfx("MFX_LIGHT_INIT", parent: lightGo); + // The light's glow moves with the hero - in the world it trailed behind him. + _meshService.CreateVobPfx("MFX_LIGHT_INIT", parent: lightGo, isFollowingEmitter: true); hero.ActiveLightGo = lightGo; Logger.Log("[VRSpellFx] Light spell ON", LogCat.VR);