Add texture to particle effects

This commit is contained in:
Ammar Askar 2023-08-02 13:24:33 -04:00
parent 8cd32935be
commit d31cda97ad

View file

@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenTS2.Common;
using OpenTS2.Content;
using OpenTS2.Content.DBPF;
using OpenTS2.Content.DBPF.Effects;
using OpenTS2.Content.DBPF.Scenegraph;
using OpenTS2.Engine;
using OpenTS2.Files;
using OpenTS2.Files.Formats.DBPF;
using UnityEngine;
@ -14,6 +17,7 @@ namespace OpenTS2.Scenes
public class EffectsManager : MonoBehaviour
{
private EffectsAsset _effects;
private List<Material> _particleMaterials = new List<Material>();
private void Start()
{
@ -28,6 +32,15 @@ namespace OpenTS2.Scenes
emissionModule.enabled = false;
}
// Clean up the materials we created.
private void OnDestroy()
{
foreach(var mat in _particleMaterials)
{
mat.Free();
}
}
public void StartEffect(string effectName)
{
var unityParticleSystem = GetComponent<ParticleSystem>();
@ -50,15 +63,36 @@ namespace OpenTS2.Scenes
unityParticleSystem.Play(withChildren:true);
}
private static GameObject CreateForParticleEffect(EffectDescription description, ParticleEffect effect)
private GameObject CreateForParticleEffect(EffectDescription description, ParticleEffect effect)
{
var particleObject = new GameObject(description.EffectName, typeof(ParticleSystem));
var system = particleObject.GetComponent<ParticleSystem>();
system.Stop(withChildren:true, ParticleSystemStopBehavior.StopEmittingAndClear);
var emission = system.emission;
var emissionRateOverTime = emission.rateOverTime;
emissionRateOverTime.curve = effect.Emission.RateCurve.ToUnityCurve();
var main = system.main;
main.duration = effect.Life.Life[0];
if (effect.Drawing.MaterialName != "")
{
var textureAsset = ContentProvider.Get().GetAsset<ScenegraphTextureAsset>(new ResourceKey(
$"{effect.Drawing.MaterialName}_txtr", GroupIDs.Scenegraph, TypeIDs.SCENEGRAPH_TXTR));
// TODO: temporarily using the standard shader here.
var material = new Material(Shader.Find("OpenTS2/StandardMaterial/AlphaBlended"))
{
mainTexture = textureAsset.GetSelectedImageAsUnityTexture(ContentProvider.Get())
};
_particleMaterials.Add(material);
system.GetComponent<Renderer>().material = material;
Debug.Log($"material: {effect.Drawing.MaterialName}");
}
return particleObject;
}
}