Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script for saving Flag shader params in props.
// 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.
// 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 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];
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) { 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; }
asset.m_mesh.name="FlagShader "+axis.x.ToString("R")+" "+axis.y.ToString("R")+" "+axis.z.ToString("R")+" "+pivot.x.ToString("R")+" "+pivot.y.ToString("R")+" "+pivot.z.ToString("R")+" "+((float)columns).ToString("R")+" "+((float)rows).ToString("R")+" "+x.ToString("R")+" "+y.ToString("R")+" ";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment