Skip to content

Instantly share code, notes, and snippets.

Last active Aug 26, 2020
What would you like to do?
Script for saving Flag shader params in props. (Added modless shader parameter saving method by boformer.)
// The script will not work properly if you have no texture imported.
// The vertex paint of the mesh controls the amount of movement:
// white means no movement and black means maximum movement.
// If you're using multiple "flag" textures in one prop, (like vanilla flags)
// the flag must be entirely black and everything else should be white,
// and you can't use the strength variable, otherwise the UV mapping will break.
// The flag should be UV mapped onto the flag on the bottom right of the texture.
// If you're using just one texture,
// you can use shades of gray to control which parts move more or less,
// and also use the strength variable to reduce the movement.
// LOD also moves, so it should be treated the same way as the main model.
// (added modless shader parameter saving method by boformer)
// Flag grid on the texture: leave at defaults if you're not using multiple flag textures in one prop
var columns = 1; // amount of vertical columns
var rows = 1; // amount of horizontal rows
Vector2 topLeft = new Vector2(0, 0); // top left corner x,y position (measured from top left) in pixels
Vector2 bottomRight = new Vector2(0, 0); // bottom right corner x,y position (measured from top left) in pixels
// Flag movement:
var strength = 1.0f; // movement strength (0.0 - 1.0)
// can't be used if you're using multiple flag textures in one prop
Vector3 axis = new Vector4(0.0f, 1.0f, 0.0f); // movement axis
Vector3 pivot = new Vector4(0.0f, 0.0f, 0.0f); // pivot point of the flag, where it is "attached"
var asset = ToolsModifierControl.toolController.m_editPrefabInfo as PropInfo;
var shader = Shader.Find("Custom/Props/Prop/Flag");
asset.m_material.shader = shader; if(asset.m_lodMaterial) asset.m_lodMaterial.shader = shader;
var tex = asset.m_material.GetTexture("_MainTex");
var x = - ((bottomRight.x - topLeft.x) / columns) / tex.width; var y = ((bottomRight.y - topLeft.y) / rows) / tex.height;
Vector4 a = new Vector4(axis.x, axis.y, axis.z, 0f); Vector4 p = new Vector4(pivot.x, pivot.y, pivot.z, 0f);
Vector4 t = new Vector4(columns, rows, x, y); asset.m_material.SetVector("_RollParams3", t);
asset.m_material.SetVector("_RollLocation0", p); asset.m_material.SetVector("_RollParams0", a);
if(asset.m_lodMaterial) { asset.m_lodMaterial.SetVector("_RollParams3", t);
asset.m_lodMaterial.SetVector("_RollLocation0", p); asset.m_lodMaterial.SetVector("_RollParams0", a); }
if(asset.m_lodMaterialCombined) { asset.m_lodMaterialCombined.SetVector("_RollParams3", t);
asset.m_lodMaterialCombined.SetVector("_RollLocation0", p); asset.m_lodMaterialCombined.SetVector("_RollParams0", a); }
Vector3[] v = asset.m_mesh.vertices; var c = asset.m_mesh.colors; Color[] nc = new Color[v.Length];
if (asset.m_mesh.colors.Length != 0) for (int i = 0; i < v.Length; i++) nc[i] = new Color(c[i].r, c[i].g, 1-((1-c[i].r)*strength), 1f);
asset.m_mesh.colors = nc;
if (asset.m_lodMesh) if (asset.m_lodMesh.colors.Length != 0) { v = asset.m_lodMesh.vertices; c = asset.m_lodMesh.colors; nc = new Color[v.Length];
for (int i = 0; i < v.Length; i++) nc[i] = new Color(c[i].r, c[i].g, 1-((1-c[i].r)*strength), 1f);
asset.m_lodMesh.colors = nc; }
var vectorProps = Type.GetType("ColossalFramework.Packaging.ShaderUtil, ColossalManaged").GetField("vectorProps").GetValue(null) as string[];
vectorProps[4] = "_RollParams3"; vectorProps[5] = "_RollLocation0"; vectorProps[6] = "_RollParams0";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment