Add projection logic

feature-multiSDF
Andrew Cassidy 4 years ago
parent bd1a588ff7
commit 1287e729eb

@ -1,6 +1,6 @@
<?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')"/>
<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>
@ -10,7 +10,7 @@
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<LangVersion>7.3</LangVersion>
<LangVersion>8</LangVersion>
<RootNamespace>ConformalDecals</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
@ -37,24 +37,25 @@
<Reference Include="Shabby, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>dlls/Shabby.dll</HintPath>
</Reference>
<Reference Include="System"/>
<Reference Include="System.Core"/>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>dlls/UnityEngine.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="MaterialModifiers\ColorMaterialProperty.cs"/>
<Compile Include="MaterialModifiers\FloatMaterialProperty.cs"/>
<Compile Include="MaterialModifiers\MaterialProperty.cs"/>
<Compile Include="MaterialModifiers\MaterialPropertyCollection.cs"/>
<Compile Include="MaterialModifiers\TextureMaterialProperty.cs"/>
<Compile Include="ModuleConformalDecal.cs"/>
<Compile Include="Logging.cs"/>
<Compile Include="Properties\AssemblyInfo.cs"/>
<Compile Include="MaterialModifiers\ColorMaterialProperty.cs" />
<Compile Include="MaterialModifiers\FloatMaterialProperty.cs" />
<Compile Include="MaterialModifiers\MaterialProperty.cs" />
<Compile Include="MaterialModifiers\MaterialPropertyCollection.cs" />
<Compile Include="MaterialModifiers\TextureMaterialProperty.cs" />
<Compile Include="ProjectionTarget.cs" />
<Compile Include="ModuleConformalDecal.cs" />
<Compile Include="Logging.cs" />
<Compile Include="OrientedBounds.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets"/>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>sh -e -c "cp -v '$(TargetPath)' '$(SolutionDir)/Releases/ConformalDecals/Gamedata'"</PostBuildEvent>
</PropertyGroup>

@ -4,32 +4,39 @@ using UnityEngine;
namespace ConformalDecals.MaterialModifiers {
public class MaterialPropertyCollection {
public Shader ShaderRef { get; }
public TextureMaterialProperty MainTextureMaterial { get; }
public TextureMaterialProperty MainTextureProperty { get; }
public Material ParsedMaterial { get; }
public bool UseBaseNormal { get; }
private List<MaterialProperty> _materialModifiers;
private List<TextureMaterialProperty> _texturePropertyMaterialModifiers;
private readonly List<MaterialProperty> _materialProperties;
private readonly List<TextureMaterialProperty> _textureMaterialProperties;
public MaterialPropertyCollection(ConfigNode node, PartModule module) {
_materialModifiers = new List<MaterialProperty>();
_texturePropertyMaterialModifiers = new List<TextureMaterialProperty>();
public String BaseNormalSrc { get; }
public String BaseNormalDest { get; }
var shaderString = node.GetValue("shader");
private const string _normalTextureName = "_BumpMap";
if (shaderString == null)
throw new FormatException("Missing shader name in material");
public MaterialPropertyCollection(ConfigNode node, PartModule module) {
if (shaderString == string.Empty)
throw new FormatException("Empty shader name in material");
// Initialize fields
_materialProperties = new List<MaterialProperty>();
_textureMaterialProperties = new List<TextureMaterialProperty>();
// Get shader
var shaderString = node.GetValue("shader") ?? throw new FormatException("Missing shader name in material");
//TODO: USE SHABBY PROVIDED METHOD HERE INSTEAD
ShaderRef = Shader.Find(shaderString);
var shaderRef = Shabby.Shabby.FindShader(shaderString);
// note to self: null coalescing does not work on UnityEngine classes
if (shaderRef == null) {
throw new FormatException($"Shader not found: '{shaderString}'");
}
if (ShaderRef == null) throw new FormatException($"Shader not found: {shaderString}");
ParsedMaterial = new Material(shaderRef);
// Get useBaseNormal value
var useBaseNormalString = node.GetValue("useBaseNormal");
if (useBaseNormalString != null) {
@ -44,6 +51,11 @@ namespace ConformalDecals.MaterialModifiers {
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) {
try {
MaterialProperty property;
@ -60,26 +72,27 @@ namespace ConformalDecals.MaterialModifiers {
property = new TextureMaterialProperty(propertyNode);
var textureModifier = (TextureMaterialProperty) property;
if (textureModifier.IsMain) {
if (MainTextureMaterial == null) {
MainTextureMaterial = textureModifier;
if (MainTextureProperty == null) {
MainTextureProperty = textureModifier;
}
else {
// multiple textures have been marked as main!
// non-fatal issue, ignore this one and keep using current main texture
module.LogWarning(
$"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;
default:
throw new FormatException($"Invalid property type '{propertyNode.name}' in material");
}
_materialModifiers.Add(property);
_materialProperties.Add(property);
property.Modify(ParsedMaterial);
}
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 string TextureUrl { get; }
public Texture2D TextureRef { get; }
private Vector2 _textureOffset;
private Vector2 _textureScale;
public bool IsNormal { get; }
public bool IsMain { get; }
public bool AutoScale { 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) {
TextureUrl = node.GetValue("textureURL");
@ -28,9 +30,9 @@ namespace ConformalDecals.MaterialModifiers {
if (TextureRef == null)
throw new Exception($"Cannot get texture from texture info '{TextureUrl}' isNormalMap = {IsNormal}");
IsNormal = ParsePropertyBool(node, "isNormalMap", true, false);
IsMain = ParsePropertyBool(node, "isMain", true, false);
AutoScale = ParsePropertyBool(node, "autoScale", true, false);
IsNormal = ParsePropertyBool(node, "isNormalMap", true);
IsMain = ParsePropertyBool(node, "isMain", true);
AutoScale = ParsePropertyBool(node, "autoScale", true);
TileRect = ParsePropertyRect(node, "tileRect", true, new Rect(0, 0, TextureRef.width, TextureRef.height));
_textureScale.x = TileRect.width / TextureRef.width;

@ -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…
Cancel
Save