mirror of
https://github.com/drewcassidy/KSP-Conformal-Decals.git
synced 2024-09-01 18:23:54 +00:00
Add projection logic
This commit is contained in:
parent
bd1a588ff7
commit
1287e729eb
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
<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')"/>
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
@ -10,7 +10,7 @@
|
|||||||
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
|
||||||
<FileAlignment>512</FileAlignment>
|
<FileAlignment>512</FileAlignment>
|
||||||
<Deterministic>true</Deterministic>
|
<Deterministic>true</Deterministic>
|
||||||
<LangVersion>7.3</LangVersion>
|
<LangVersion>8</LangVersion>
|
||||||
<RootNamespace>ConformalDecals</RootNamespace>
|
<RootNamespace>ConformalDecals</RootNamespace>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
@ -37,24 +37,25 @@
|
|||||||
<Reference Include="Shabby, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
<Reference Include="Shabby, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||||
<HintPath>dlls/Shabby.dll</HintPath>
|
<HintPath>dlls/Shabby.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="System"/>
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Core"/>
|
<Reference Include="System.Core" />
|
||||||
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
|
||||||
<HintPath>dlls/UnityEngine.dll</HintPath>
|
<HintPath>dlls/UnityEngine.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="MaterialModifiers\ColorMaterialProperty.cs"/>
|
<Compile Include="MaterialModifiers\ColorMaterialProperty.cs" />
|
||||||
<Compile Include="MaterialModifiers\FloatMaterialProperty.cs"/>
|
<Compile Include="MaterialModifiers\FloatMaterialProperty.cs" />
|
||||||
<Compile Include="MaterialModifiers\MaterialProperty.cs"/>
|
<Compile Include="MaterialModifiers\MaterialProperty.cs" />
|
||||||
<Compile Include="MaterialModifiers\MaterialPropertyCollection.cs"/>
|
<Compile Include="MaterialModifiers\MaterialPropertyCollection.cs" />
|
||||||
<Compile Include="MaterialModifiers\TextureMaterialProperty.cs"/>
|
<Compile Include="MaterialModifiers\TextureMaterialProperty.cs" />
|
||||||
<Compile Include="ModuleConformalDecal.cs"/>
|
<Compile Include="ProjectionTarget.cs" />
|
||||||
<Compile Include="Logging.cs"/>
|
<Compile Include="ModuleConformalDecal.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs"/>
|
<Compile Include="Logging.cs" />
|
||||||
<Compile Include="OrientedBounds.cs" />
|
<Compile Include="OrientedBounds.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<PostBuildEvent>sh -e -c "cp -v '$(TargetPath)' '$(SolutionDir)/Releases/ConformalDecals/Gamedata'"</PostBuildEvent>
|
<PostBuildEvent>sh -e -c "cp -v '$(TargetPath)' '$(SolutionDir)/Releases/ConformalDecals/Gamedata'"</PostBuildEvent>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
@ -4,32 +4,39 @@ using UnityEngine;
|
|||||||
|
|
||||||
namespace ConformalDecals.MaterialModifiers {
|
namespace ConformalDecals.MaterialModifiers {
|
||||||
public class MaterialPropertyCollection {
|
public class MaterialPropertyCollection {
|
||||||
public Shader ShaderRef { get; }
|
public TextureMaterialProperty MainTextureProperty { get; }
|
||||||
public TextureMaterialProperty MainTextureMaterial { get; }
|
|
||||||
|
public Material ParsedMaterial { get; }
|
||||||
|
|
||||||
public bool UseBaseNormal { get; }
|
public bool UseBaseNormal { get; }
|
||||||
|
|
||||||
private List<MaterialProperty> _materialModifiers;
|
private readonly List<MaterialProperty> _materialProperties;
|
||||||
private List<TextureMaterialProperty> _texturePropertyMaterialModifiers;
|
private readonly List<TextureMaterialProperty> _textureMaterialProperties;
|
||||||
|
|
||||||
|
public String BaseNormalSrc { get; }
|
||||||
|
public String BaseNormalDest { get; }
|
||||||
|
|
||||||
|
private const string _normalTextureName = "_BumpMap";
|
||||||
|
|
||||||
public MaterialPropertyCollection(ConfigNode node, PartModule module) {
|
public MaterialPropertyCollection(ConfigNode node, PartModule module) {
|
||||||
_materialModifiers = new List<MaterialProperty>();
|
|
||||||
_texturePropertyMaterialModifiers = new List<TextureMaterialProperty>();
|
|
||||||
|
|
||||||
var shaderString = node.GetValue("shader");
|
// Initialize fields
|
||||||
|
_materialProperties = new List<MaterialProperty>();
|
||||||
|
_textureMaterialProperties = new List<TextureMaterialProperty>();
|
||||||
|
|
||||||
if (shaderString == null)
|
// Get shader
|
||||||
throw new FormatException("Missing shader name in material");
|
var shaderString = node.GetValue("shader") ?? throw new FormatException("Missing shader name in material");
|
||||||
|
|
||||||
if (shaderString == string.Empty)
|
var shaderRef = Shabby.Shabby.FindShader(shaderString);
|
||||||
throw new FormatException("Empty shader name in material");
|
|
||||||
|
|
||||||
|
// note to self: null coalescing does not work on UnityEngine classes
|
||||||
|
if (shaderRef == null) {
|
||||||
|
throw new FormatException($"Shader not found: '{shaderString}'");
|
||||||
|
}
|
||||||
|
|
||||||
//TODO: USE SHABBY PROVIDED METHOD HERE INSTEAD
|
ParsedMaterial = new Material(shaderRef);
|
||||||
ShaderRef = Shader.Find(shaderString);
|
|
||||||
|
|
||||||
if (ShaderRef == null) throw new FormatException($"Shader not found: {shaderString}");
|
|
||||||
|
|
||||||
|
// Get useBaseNormal value
|
||||||
var useBaseNormalString = node.GetValue("useBaseNormal");
|
var useBaseNormalString = node.GetValue("useBaseNormal");
|
||||||
|
|
||||||
if (useBaseNormalString != null) {
|
if (useBaseNormalString != null) {
|
||||||
@ -44,6 +51,11 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
UseBaseNormal = false;
|
UseBaseNormal = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get basenormal source and destination property names
|
||||||
|
BaseNormalSrc = node.GetValue("baseNormalSource") ?? _normalTextureName;
|
||||||
|
BaseNormalDest = node.GetValue("baseNormalDestination") ?? _normalTextureName;
|
||||||
|
|
||||||
|
// Parse all materialProperties
|
||||||
foreach (ConfigNode propertyNode in node.nodes) {
|
foreach (ConfigNode propertyNode in node.nodes) {
|
||||||
try {
|
try {
|
||||||
MaterialProperty property;
|
MaterialProperty property;
|
||||||
@ -60,26 +72,27 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
property = new TextureMaterialProperty(propertyNode);
|
property = new TextureMaterialProperty(propertyNode);
|
||||||
var textureModifier = (TextureMaterialProperty) property;
|
var textureModifier = (TextureMaterialProperty) property;
|
||||||
if (textureModifier.IsMain) {
|
if (textureModifier.IsMain) {
|
||||||
if (MainTextureMaterial == null) {
|
if (MainTextureProperty == null) {
|
||||||
MainTextureMaterial = textureModifier;
|
MainTextureProperty = textureModifier;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// multiple textures have been marked as main!
|
// multiple textures have been marked as main!
|
||||||
// non-fatal issue, ignore this one and keep using current main texture
|
// non-fatal issue, ignore this one and keep using current main texture
|
||||||
module.LogWarning(
|
module.LogWarning(
|
||||||
$"Material texture property {textureModifier.TextureUrl} is marked as main, but material already has a main texture! \n" +
|
$"Material texture property {textureModifier.TextureUrl} is marked as main, but material already has a main texture! \n" +
|
||||||
$"Defaulting to {MainTextureMaterial.TextureUrl}");
|
$"Defaulting to {MainTextureProperty.TextureUrl}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_texturePropertyMaterialModifiers.Add(textureModifier);
|
_textureMaterialProperties.Add(textureModifier);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new FormatException($"Invalid property type '{propertyNode.name}' in material");
|
throw new FormatException($"Invalid property type '{propertyNode.name}' in material");
|
||||||
}
|
}
|
||||||
|
|
||||||
_materialModifiers.Add(property);
|
_materialProperties.Add(property);
|
||||||
|
property.Modify(ParsedMaterial);
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
@ -89,5 +102,13 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void UpdateMaterial(Vector2 scale) {
|
||||||
|
foreach (var textureProperty in _textureMaterialProperties) {
|
||||||
|
textureProperty.UpdateScale(ParsedMaterial, scale);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,15 +5,17 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
public class TextureMaterialProperty : MaterialProperty {
|
public class TextureMaterialProperty : MaterialProperty {
|
||||||
public string TextureUrl { get; }
|
public string TextureUrl { get; }
|
||||||
public Texture2D TextureRef { get; }
|
public Texture2D TextureRef { get; }
|
||||||
|
|
||||||
private Vector2 _textureOffset;
|
|
||||||
private Vector2 _textureScale;
|
|
||||||
|
|
||||||
public bool IsNormal { get; }
|
public bool IsNormal { get; }
|
||||||
public bool IsMain { get; }
|
public bool IsMain { get; }
|
||||||
public bool AutoScale { get; }
|
public bool AutoScale { get; }
|
||||||
|
|
||||||
public Rect TileRect { get; }
|
public Rect TileRect { get; }
|
||||||
|
|
||||||
|
public float AspectRatio => TileRect.height / TileRect.width;
|
||||||
|
|
||||||
|
private readonly Vector2 _textureOffset;
|
||||||
|
private readonly Vector2 _textureScale;
|
||||||
|
|
||||||
public TextureMaterialProperty(ConfigNode node) : base(node) {
|
public TextureMaterialProperty(ConfigNode node) : base(node) {
|
||||||
TextureUrl = node.GetValue("textureURL");
|
TextureUrl = node.GetValue("textureURL");
|
||||||
@ -28,9 +30,9 @@ namespace ConformalDecals.MaterialModifiers {
|
|||||||
if (TextureRef == null)
|
if (TextureRef == null)
|
||||||
throw new Exception($"Cannot get texture from texture info '{TextureUrl}' isNormalMap = {IsNormal}");
|
throw new Exception($"Cannot get texture from texture info '{TextureUrl}' isNormalMap = {IsNormal}");
|
||||||
|
|
||||||
IsNormal = ParsePropertyBool(node, "isNormalMap", true, false);
|
IsNormal = ParsePropertyBool(node, "isNormalMap", true);
|
||||||
IsMain = ParsePropertyBool(node, "isMain", true, false);
|
IsMain = ParsePropertyBool(node, "isMain", true);
|
||||||
AutoScale = ParsePropertyBool(node, "autoScale", true, false);
|
AutoScale = ParsePropertyBool(node, "autoScale", true);
|
||||||
TileRect = ParsePropertyRect(node, "tileRect", true, new Rect(0, 0, TextureRef.width, TextureRef.height));
|
TileRect = ParsePropertyRect(node, "tileRect", true, new Rect(0, 0, TextureRef.width, TextureRef.height));
|
||||||
|
|
||||||
_textureScale.x = TileRect.width / TextureRef.width;
|
_textureScale.x = TileRect.width / TextureRef.width;
|
||||||
|
86
Source/ConformalDecals/ProjectionTarget.cs
Normal file
86
Source/ConformalDecals/ProjectionTarget.cs
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
using System;
|
||||||
|
using ConformalDecals.MaterialModifiers;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.Rendering;
|
||||||
|
|
||||||
|
namespace ConformalDecals {
|
||||||
|
public class ProjectionTarget {
|
||||||
|
private static readonly int _projectionMatrixID = Shader.PropertyToID("_ProjectionMatrix");
|
||||||
|
private static readonly int _decalNormalID = Shader.PropertyToID("_DecalNormal");
|
||||||
|
private static readonly int _decalTangentID = Shader.PropertyToID("_DecalTangent");
|
||||||
|
|
||||||
|
// Projector object data
|
||||||
|
public Transform Projector;
|
||||||
|
|
||||||
|
// Target object data
|
||||||
|
public readonly Transform Target;
|
||||||
|
|
||||||
|
private readonly Renderer _targetRenderer;
|
||||||
|
private readonly Mesh _targetMesh;
|
||||||
|
private Boolean _projectionEnabled;
|
||||||
|
|
||||||
|
// property block
|
||||||
|
public readonly MaterialPropertyBlock DecalMPB;
|
||||||
|
|
||||||
|
public ProjectionTarget(MeshRenderer targetRenderer, Mesh targetMesh, MaterialPropertyCollection properties) {
|
||||||
|
Target = targetRenderer.transform;
|
||||||
|
_targetRenderer = targetRenderer;
|
||||||
|
_targetMesh = targetMesh;
|
||||||
|
var targetMaterial = targetRenderer.sharedMaterial;
|
||||||
|
|
||||||
|
DecalMPB = new MaterialPropertyBlock();
|
||||||
|
|
||||||
|
if (properties.UseBaseNormal) {
|
||||||
|
var normalSrcID = Shader.PropertyToID(properties.BaseNormalSrc);
|
||||||
|
var normalDestID = Shader.PropertyToID(properties.BaseNormalDest);
|
||||||
|
var normalDestIDST = Shader.PropertyToID(properties.BaseNormalDest + "_ST");
|
||||||
|
|
||||||
|
var normal = targetMaterial.GetTexture(normalSrcID);
|
||||||
|
if (normal != null) {
|
||||||
|
|
||||||
|
DecalMPB.SetTexture(normalDestID, targetMaterial.GetTexture(normalSrcID));
|
||||||
|
|
||||||
|
var normalScale = targetMaterial.GetTextureScale(normalSrcID);
|
||||||
|
var normalOffset = targetMaterial.GetTextureOffset(normalSrcID);
|
||||||
|
|
||||||
|
DecalMPB.SetVector(normalDestIDST, new Vector4(normalScale.x, normalScale.y, normalOffset.x, normalOffset.y));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Project(Matrix4x4 orthoMatrix, OrientedBounds projectorBounds) {
|
||||||
|
var projectorToTargetMatrix = Target.worldToLocalMatrix * Projector.localToWorldMatrix;
|
||||||
|
|
||||||
|
var projectionMatrix = orthoMatrix * projectorToTargetMatrix.inverse;
|
||||||
|
var decalNormal = projectorToTargetMatrix.MultiplyVector(Vector3.back).normalized;
|
||||||
|
var decalTangent = projectorToTargetMatrix.MultiplyVector(Vector3.right).normalized;
|
||||||
|
|
||||||
|
DecalMPB.SetMatrix(_projectionMatrixID, projectionMatrix);
|
||||||
|
DecalMPB.SetVector(_decalNormalID, decalNormal);
|
||||||
|
DecalMPB.SetVector(_decalTangentID, decalTangent);
|
||||||
|
|
||||||
|
var targetBounds = new OrientedBounds(Target.localToWorldMatrix, _targetRenderer.bounds);
|
||||||
|
_projectionEnabled = projectorBounds.Intersects(targetBounds);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Render(Material decalMaterial) {
|
||||||
|
if (_projectionEnabled) {
|
||||||
|
if (HighLogic.LoadedSceneIsEditor) {
|
||||||
|
var camera = EditorLogic.fetch.editorCamera;
|
||||||
|
Graphics.DrawMesh(_targetMesh, Target.worldToLocalMatrix, decalMaterial, 0, camera, 0, DecalMPB, ShadowCastingMode.Off, true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (HighLogic.LoadedSceneIsFlight) {
|
||||||
|
foreach (var camera in FlightCamera.fetch.cameras)
|
||||||
|
{
|
||||||
|
Graphics.DrawMesh(_targetMesh, Target.worldToLocalMatrix, decalMaterial, 0, camera, 0, DecalMPB, ShadowCastingMode.Off, true);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user