2020-05-27 22:19:46 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace ConformalDecals.MaterialModifiers {
|
2020-05-31 00:03:14 +00:00
|
|
|
public class MaterialPropertyCollection : ScriptableObject {
|
2020-06-02 07:53:17 +00:00
|
|
|
private static readonly int OpacityId = Shader.PropertyToID("_Opacity");
|
|
|
|
private static readonly int CutoffId = Shader.PropertyToID("_Cutoff");
|
2020-06-02 00:48:53 +00:00
|
|
|
|
2020-06-02 07:53:17 +00:00
|
|
|
public MaterialTextureProperty MainMaterialTextureProperty { get; set; }
|
2020-05-27 22:43:23 +00:00
|
|
|
|
2020-05-31 00:03:14 +00:00
|
|
|
private List<MaterialProperty> _materialProperties;
|
2020-06-02 07:53:17 +00:00
|
|
|
private List<MaterialTextureProperty> _textureMaterialProperties;
|
2020-05-27 22:43:23 +00:00
|
|
|
|
2020-06-02 04:09:14 +00:00
|
|
|
public Material DecalMaterial {
|
|
|
|
get {
|
|
|
|
if (_protoDecalMaterial == null) {
|
|
|
|
_protoDecalMaterial = MakeMaterial(_decalShader);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _protoDecalMaterial;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 07:53:17 +00:00
|
|
|
private Shader _decalShader;
|
2020-06-02 04:09:14 +00:00
|
|
|
private Material _protoDecalMaterial;
|
|
|
|
|
2020-06-02 07:53:17 +00:00
|
|
|
public void Initialize() {
|
2020-05-30 04:02:58 +00:00
|
|
|
_materialProperties = new List<MaterialProperty>();
|
2020-06-02 07:53:17 +00:00
|
|
|
_textureMaterialProperties = new List<MaterialTextureProperty>();
|
|
|
|
}
|
2020-05-30 04:02:58 +00:00
|
|
|
|
2020-06-02 07:53:17 +00:00
|
|
|
public void AddProperty(MaterialProperty property) {
|
2020-06-03 01:07:05 +00:00
|
|
|
if (property == null) throw new ArgumentNullException("Tried to add a null property");
|
|
|
|
if (_materialProperties == null || _textureMaterialProperties == null) {
|
|
|
|
Initialize();
|
|
|
|
Debug.LogWarning("Tried to add a property to uninitialized property collection! correcting now.");
|
|
|
|
}
|
|
|
|
|
2020-06-02 07:53:17 +00:00
|
|
|
foreach (var p in _materialProperties) {
|
|
|
|
if (p.PropertyName == property.PropertyName) {
|
|
|
|
_materialProperties.Remove(property);
|
|
|
|
}
|
2020-05-30 04:02:58 +00:00
|
|
|
}
|
2020-05-27 22:19:46 +00:00
|
|
|
|
2020-06-02 07:53:17 +00:00
|
|
|
_materialProperties.Add(property);
|
2020-05-27 22:43:23 +00:00
|
|
|
|
2020-06-02 07:53:17 +00:00
|
|
|
if (property is MaterialTextureProperty textureProperty) {
|
|
|
|
foreach (var p in _textureMaterialProperties) {
|
|
|
|
if (p.PropertyName == textureProperty.PropertyName) {
|
|
|
|
_textureMaterialProperties.Remove(textureProperty);
|
|
|
|
}
|
2020-05-27 22:43:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 07:53:17 +00:00
|
|
|
_textureMaterialProperties.Add(textureProperty);
|
2020-05-27 22:19:46 +00:00
|
|
|
|
2020-06-02 07:53:17 +00:00
|
|
|
if (textureProperty.IsMain) MainMaterialTextureProperty ??= textureProperty;
|
|
|
|
}
|
|
|
|
}
|
2020-05-27 22:19:46 +00:00
|
|
|
|
2020-06-02 07:53:17 +00:00
|
|
|
public void SetShader(string shaderName) {
|
2020-06-03 01:07:05 +00:00
|
|
|
if (string.IsNullOrEmpty(shaderName)) {
|
|
|
|
if (_decalShader == null) {
|
|
|
|
Debug.Log("Using default decal shader");
|
|
|
|
shaderName = "ConformalDecals/Paint/Diffuse";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-27 22:19:46 +00:00
|
|
|
}
|
2020-05-31 00:03:14 +00:00
|
|
|
|
2020-06-02 07:53:17 +00:00
|
|
|
var shader = Shabby.Shabby.FindShader(shaderName);
|
|
|
|
|
|
|
|
if (shader == null) throw new FormatException($"Unable to find specified shader '{shaderName}'");
|
|
|
|
|
|
|
|
_decalShader = shader;
|
2020-05-27 22:19:46 +00:00
|
|
|
}
|
2020-06-02 07:53:17 +00:00
|
|
|
|
2020-06-02 04:09:14 +00:00
|
|
|
public void SetScale(Material material, Vector2 scale) {
|
2020-05-30 04:02:58 +00:00
|
|
|
foreach (var textureProperty in _textureMaterialProperties) {
|
2020-06-02 04:09:14 +00:00
|
|
|
textureProperty.UpdateScale(material, scale);
|
2020-05-30 04:02:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-02 07:53:17 +00:00
|
|
|
|
2020-06-02 04:09:14 +00:00
|
|
|
public void SetOpacity(Material material, float opacity) {
|
2020-06-02 07:53:17 +00:00
|
|
|
material.SetFloat(OpacityId, opacity);
|
2020-06-02 04:09:14 +00:00
|
|
|
}
|
2020-06-02 00:48:53 +00:00
|
|
|
|
2020-06-02 04:09:14 +00:00
|
|
|
public void SetCutoff(Material material, float cutoff) {
|
2020-06-02 07:53:17 +00:00
|
|
|
material.SetFloat(CutoffId, cutoff);
|
2020-06-02 00:48:53 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 04:09:14 +00:00
|
|
|
private Material MakeMaterial(Shader shader) {
|
|
|
|
var material = new Material(shader);
|
|
|
|
foreach (MaterialProperty property in _materialProperties) {
|
|
|
|
property.Modify(material);
|
|
|
|
}
|
|
|
|
|
|
|
|
return material;
|
2020-06-02 00:48:53 +00:00
|
|
|
}
|
2020-05-27 22:19:46 +00:00
|
|
|
}
|
|
|
|
}
|