Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BallisticNG.Gamemodes;
- using BallisticNG.RaceUI;
- using Battlehub.Utils;
- using GameData;
- using GameData.Constants;
- using UnityEngine.SceneManagement;
- using Settings;
- using UnityEngine;
- using System.Collections;
- using System.Linq;
- using System.IO;
- using BallisticSource.Mods;
- namespace BallisticNG.ShieldCustomizer
- {
- /* This is a simple example mod which allows you to customize the visual appearance of ingame shields.
- everything has been commented for educational purposes.*/
- public class ShieldCustomizerRegister : ModRegister
- {
- public override void OnRegistered()
- {
- GameObject customShield = new GameObject("[ Custom Shield ]");
- customShield.AddComponent<CustomShield>();
- Object.DontDestroyOnLoad(customShield);
- }
- }
- public class CustomShield : MonoBehaviour
- {
- /*---Variables---*/
- public bool ModEnabled = true;
- public bool UseTeamColor = false;
- public bool UseHorizontalUvs = false;
- public Color CustomShieldColor = new Color(1.0f, 0.5f, 0.0f, 1.0f);
- private void Start()
- {
- // load the settings to create the empty ini if this mod is running for the first time
- LoadSettings();
- // register the scene loaded delegate so we can perform operations whenever a scene is loaded
- SceneManager.sceneLoaded += OnSceneLoaded;
- }
- private void OnSceneLoaded(Scene scene, LoadSceneMode loadMode)
- {
- // if there isn't a race manager then the loaded scene isn't a track
- if (!FindObjectOfType<RaceManager>()) return;
- // if the mod is disabled then do nothing
- if (!ModEnabled) return;
- LoadSettings();
- StartCoroutine(SetupShields());
- }
- private IEnumerator SetupShields()
- {
- // wait a frame so the ships can be spawned
- yield return new WaitForEndOfFrame();
- // iterate through each ship and apply the changes we want
- for (int i = 0; i < Ships.LoadedShips.Count; ++i)
- {
- ShipRefs r = Ships.LoadedShips[i];
- // apply color, this is the easy part
- r.Effects.TargetShieldColor = UseTeamColor ? r.Settings.REF_ENGINECOL_BRIGHT : CustomShieldColor;
- // apply horizontal uvs, this is where it gets harder
- if (UseHorizontalUvs)
- {
- // to make life easier, we want to temporarily move the ship so we want to store the current position first
- Vector3 pos = r.T.position;
- Quaternion rot = r.T.rotation;
- // now we can move it to a location where we can work on the verts
- r.T.position = new Vector3(0.0f, 100.0f, 0.0f);
- r.T.rotation = Quaternion.identity;
- // first off grab the shield mesh
- MeshFilter mf = r.Settings.REF_SHIELD.GetComponent<MeshFilter>();
- Mesh m = mf.mesh;
- // we need the verts and uvs to apply the changes we want
- Vector3[] verts = m.vertices;
- Vector2[] uvs = new Vector2[verts.Length];
- // first we want to calculate the bounds for each vert in world space
- Vector3 minBounds = new Vector3(Mathf.Infinity, Mathf.Infinity, Mathf.Infinity), maxBounds = new Vector3(-Mathf.Infinity, -Mathf.Infinity, -Mathf.Infinity);
- Vector3[] worldVerts = new Vector3[verts.Length]; // we are going to need to transform the ship verts into world space - the default teams were rotated in Unity so this corrects that
- Transform shipTransform = r.Settings.REF_MESH.transform; // small optimization, prevents GetComponent<Transform> on each local-world space transformation
- for (int j = 0; j < verts.Length; ++j)
- {
- worldVerts[j] = shipTransform.TransformPoint(verts[j]); // transform the current vert into world space
- // what we're doing here is comparing the position of verts to what is initially an infinetly large position away from the ship and then increasing
- // the bounds until it reaches the furthest vert in each axis on the oposite side of the ship the min/max bounds start at
- // x bounds
- if (worldVerts[j].x > maxBounds.x) maxBounds.x = worldVerts[j].x;
- if (worldVerts[j].x < minBounds.x) minBounds.x = worldVerts[j].x;
- // y bounds
- if (worldVerts[j].y > maxBounds.y) maxBounds.y = worldVerts[j].y;
- if (worldVerts[j].y < minBounds.y) minBounds.y = worldVerts[j].y;
- // z bounds
- if (worldVerts[j].z > maxBounds.z) maxBounds.z = worldVerts[j].z;
- if (worldVerts[j].z < minBounds.z) minBounds.z = worldVerts[j].z;
- }
- // now that we know the bounds we can normalize them and use them to generate the uvs
- for (int j = 0; j < verts.Length; ++j)
- {
- // normalize the bounds
- Vector3 boundsNorm = new Vector3
- (
- (worldVerts[j].x - Mathf.Abs(minBounds.x)) / (maxBounds.x - Mathf.Abs(minBounds.x)),
- (worldVerts[j].y - Mathf.Abs(minBounds.y)) / (maxBounds.y - Mathf.Abs(minBounds.y)),
- (worldVerts[j].z - Mathf.Abs(minBounds.z)) / (maxBounds.z - Mathf.Abs(minBounds.z))
- );
- // create UV
- uvs[j] = new Vector2(0.0f, boundsNorm.z * 0.1f);
- }
- // apply uvs back to mesh
- m.uv = uvs;
- // finally, put the ship back to where it was originally
- r.T.position = pos;
- r.T.rotation = rot;
- }
- }
- }
- private void LoadSettings()
- {
- // make sure the target directory exists
- string path = GameData.System.Directories.UserData + "ini/mods/";
- if (!Directory.Exists(path)) Directory.CreateDirectory(path);
- path += "shieldcustomizer.ini";
- // if the settings ini doesn't exist then create it
- if (!File.Exists(path)) CreateSettings(path);
- // open a new ini parser
- INIParser ini = new INIParser();
- ini.Open(path);
- // load settings from the parser
- ModEnabled = ini.ReadValue("Settings", "Mod Enabled", ModEnabled);
- UseTeamColor = ini.ReadValue("Settings", "Use Team Color", UseTeamColor);
- UseHorizontalUvs = ini.ReadValue("Settings", "Use Horizontal Uvs", UseHorizontalUvs);
- CustomShieldColor.r = (float)ini.ReadValue("Settings", "Custom Shield Color R", CustomShieldColor.r);
- CustomShieldColor.g = (float)ini.ReadValue("Settings", "Custom Shield Color G", CustomShieldColor.g);
- CustomShieldColor.b = (float)ini.ReadValue("Settings", "Custom Shield Color B", CustomShieldColor.b);
- CustomShieldColor.a = (float)ini.ReadValue("Settings", "Custom Shield Color A", CustomShieldColor.a);
- // close the ini parser
- ini.Close();
- }
- private void CreateSettings(string path)
- {
- INIParser ini = new INIParser();
- ini.Open(path);
- ini.WriteValue("Settings", "Mod Enabled", ModEnabled);
- ini.WriteValue("Settings", "Use Team Color", UseTeamColor);
- ini.WriteValue("Settings", "Use Horizontal Uvs", UseHorizontalUvs);
- ini.WriteValue("Settings", "Custom Shield Color R", CustomShieldColor.r);
- ini.WriteValue("Settings", "Custom Shield Color G", CustomShieldColor.g);
- ini.WriteValue("Settings", "Custom Shield Color B", CustomShieldColor.b);
- ini.WriteValue("Settings", "Custom Shield Color A", CustomShieldColor.a);
- ini.Close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement