mirror of
https://github.com/PorktoberRevolution/ReStocked
synced 2024-09-01 17:34:42 +00:00
Add plugin with material modifier
ModuleRestockMaterialModifier allows materials to be modified. Currently only supports swapping out the shader and changing texture properties. Changes apply to the whole part (can be changed in the future).
This commit is contained in:
70
Source/Restock/ModuleRestockModifyMaterials.cs
Normal file
70
Source/Restock/ModuleRestockModifyMaterials.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Restock
|
||||
{
|
||||
public class ModuleRestockModifyMaterials : PartModule
|
||||
{
|
||||
public override void OnLoad(ConfigNode node)
|
||||
{
|
||||
base.OnLoad(node);
|
||||
|
||||
if (HighLogic.LoadedSceneIsEditor || HighLogic.LoadedSceneIsFlight) return;
|
||||
|
||||
Transform modelTransform = part.partTransform.Find("model");
|
||||
|
||||
Renderer[] renderers = modelTransform.GetComponentsInChildren<Renderer>();
|
||||
|
||||
if (modelTransform == null)
|
||||
{
|
||||
Debug.LogError("Couldn't find model transform");
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (ConfigNode node2 in node.GetNodes("MATERIAL"))
|
||||
{
|
||||
string newShaderName = node2.GetValue("shaderName");
|
||||
Shader newShader = Shader.Find(newShaderName);
|
||||
|
||||
if (newShader == null)
|
||||
{
|
||||
Debug.LogError($"Can't find shader {newShaderName}");
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (Renderer renderer in renderers)
|
||||
{
|
||||
renderer.material.shader = newShader;
|
||||
}
|
||||
|
||||
foreach (ConfigNode node3 in node2.GetNodes("TEXTURE_PROPERTY"))
|
||||
{
|
||||
string name = node3.GetValue("name");
|
||||
string textureUrl = node3.GetValue("textureUrl");
|
||||
bool normalMapToggle = false;
|
||||
|
||||
if (node3.GetValue("isNormalMap") is string normalMapToggleString)
|
||||
{
|
||||
normalMapToggle = bool.Parse(normalMapToggleString);
|
||||
}
|
||||
|
||||
GameDatabase.TextureInfo textureInfo = GameDatabase.Instance.GetTextureInfo(textureUrl);
|
||||
|
||||
if (textureInfo == null)
|
||||
{
|
||||
Debug.LogError($"Cannot find texture: {textureUrl}");
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (Renderer renderer in renderers)
|
||||
{
|
||||
renderer.material.SetTexture(name, normalMapToggle ? textureInfo.normalMap : textureInfo.texture);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isEnabled = false;
|
||||
moduleIsEnabled = false;
|
||||
}
|
||||
}
|
||||
}
|
38
Source/Restock/Properties/AssemblyInfo.cs
Normal file
38
Source/Restock/Properties/AssemblyInfo.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("Restock")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("Porktober Revolution")]
|
||||
[assembly: AssemblyProduct("Restock")]
|
||||
[assembly: AssemblyCopyright("Copyright © Porktober Revolution 2018")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("0a087745-0e2b-4d11-9431-c2d4191dd510")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("0.1.0.0")]
|
||||
[assembly: AssemblyFileVersion("0.1.0.0")]
|
||||
|
||||
[assembly: KSPAssembly("Restock", 0, 1, 0)]
|
51
Source/Restock/Restock.csproj
Normal file
51
Source/Restock/Restock.csproj
Normal file
@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{0A087745-0E2B-4D11-9431-C2D4191DD510}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Restock</RootNamespace>
|
||||
<AssemblyName>Restock</AssemblyName>
|
||||
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Assembly-CSharp">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="UnityEngine">
|
||||
<Private>False</Private>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ModuleRestockModifyMaterials.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<PropertyGroup>
|
||||
<PostBuildEvent>sh -e -c "cp -v '$(TargetPath)' '$(SolutionDir)/../Distribution/Restock/GameData/ReStock/Plugins'"</PostBuildEvent>
|
||||
</PropertyGroup>
|
||||
</Project>
|
Reference in New Issue
Block a user