Skip to content

Instantly share code, notes, and snippets.

@ronyx69
Last active June 8, 2019 15:24
Show Gist options
  • Save ronyx69/e2f574d1cec4c666719e9642f08e8e9d to your computer and use it in GitHub Desktop.
Save ronyx69/e2f574d1cec4c666719e9642f08e8e9d to your computer and use it in GitHub Desktop.
Script for saving Flag shader params in vehicle sub meshes. Still requires the Flag Params mod.
// for vehicles, vertex colors get generated and overwritten on import
// therefore you need to import the rotors sub mesh as a prop, save, reload
// and then import the vehicle sub mesh,
// and copy the vertex paint from the prop to the vehicle sub mesh using this script:
var subMesh = 1; // vehicle sub mesh id, starting from 1
var asset2 = PrefabCollection<PropInfo>.FindLoaded("filename.Asset Name_Data"); // CHANGE TO PROP NAME
var asset = ToolsModifierControl.toolController.m_editPrefabInfo as VehicleInfo;
Color[] colors = new Color[asset2.m_mesh.vertices.Length];
for (int i = 0; i < asset2.m_mesh.vertices.Length; i++) colors[i] = asset2.m_mesh.colors[i];
asset.m_subMeshes[subMesh].m_subInfo.m_mesh.colors = colors;
//
// 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.
// (In this case for vehicles, the flag texture will change as it drives lol)
//
// 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.
var subMesh = 1; // vehicle sub mesh id, starting from 1
// FLAG GRID: leave at defaults if you're not using multiple flag textures in one prop
var columns = 1; // amount vertical columns
var rows = 1; // amount of horizontal rows
Vector2 topLeft = new Vector2(0, 0); // top left corner x,y position in pixels
Vector2 bottomRight = new Vector2(0, 0); // bottom right corner x,y position in pixels
// FLAG MOVEMENT:
var strength = 0.1f; // 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 VehicleInfo).m_subMeshes[subMesh].m_subInfo;
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);
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;
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