Skip to content

Commit

Permalink
Omnibus PR with fixes for #333, #334, #335, and README updates (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
kerfuffles authored Mar 23, 2022
1 parent dd58bc8 commit 904fc06
Show file tree
Hide file tree
Showing 727 changed files with 1,765 additions and 3,838 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ ProjectSettings/ProjectVersion\.txt
*.lib

Packages/com\.chisel\.core/Chisel/Core/API\.private/Native/Plugin/x64/Chisel\[TEST\]\.exp\.meta
LICENSE.md.meta
Icons.zip.meta
README.md.meta
Readme.meta
Readme/Images.meta
Readme/Images/house_view.png.meta
Binary file added Icons.zip
Binary file not shown.
5 changes: 2 additions & 3 deletions RealtimeCSG/Assets/Plugins.meta → Plugins.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using InternalRealtimeCSG;
using UnityEngine.Rendering;


namespace RealtimeCSG
{
Expand Down Expand Up @@ -93,10 +96,10 @@ internal static Material GenerateEditorMaterial(string shaderName, string textur
};
if (textureName != null)
{
string filename = "Assets/Plugins/RealtimeCSG/Editor/Resources/Textures/" + textureName + ".png";
material.mainTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(filename);
//string filename = "Assets/Plugins/RealtimeCSG/Editor/Resources/Textures/" + textureName + ".png";
material.mainTexture = Resources.Load<Texture2D>( string.Format( "RealtimeCSG/Textures/{0}", textureName ) ); //AssetDatabase.LoadAssetAtPath<Texture2D>(filename);
if (!material.mainTexture)
Debug.LogWarning("Could not find internal texture: " + filename);
Debug.LogWarning("Could not find internal texture: " + textureName);
}
EditorMaterials.Add(name, material);
return material;
Expand Down Expand Up @@ -142,134 +145,161 @@ internal static bool EqualInternalMaterial(Material o, Material n)
o.name == n.name;
}


const string DefaultMaterialPath = "Assets/Plugins/RealtimeCSG/Runtime/Materials/";
const string DefaultTexturePath = "Assets/Plugins/RealtimeCSG/Runtime/Textures/";

internal static void CreateRenderPipelineVersionOfDefaultMaterial(Material defaultMaterial, string materialName)
{
if (!defaultMaterial)
return;

var materialPath = string.Format("{0}{1}/", DefaultMaterialPath, defaultMaterial.shader.name);
var materialFilename = string.Format("{0}{1}.mat", materialPath, materialName);
private static string GetRenderPipelineType()
{
if( GraphicsSettings.renderPipelineAsset == null )
return string.Empty; // we are using built-in, so do nothing.

var material = AssetDatabase.LoadAssetAtPath<Material>(materialFilename);
if (material)
return;
string rpDefaultMatShader = GraphicsSettings.renderPipelineAsset.defaultMaterial.shader.name;

material = new Material(defaultMaterial);
if( rpDefaultMatShader.StartsWith( "HDRP" ) )
return "HDRP";

try
{
if (!System.IO.Directory.Exists(materialPath))
System.IO.Directory.CreateDirectory(materialPath);
// HDRenderPipeline will generate errors when creating it's own type of materials
AssetDatabase.CreateAsset(material, materialFilename);
material = AssetDatabase.LoadAssetAtPath<Material>(materialFilename);
}
catch (Exception ex)
{
Debug.LogException(ex);
}
if( rpDefaultMatShader.StartsWith( "LWRP" ) )
return "LWRP";

try
{
string destTexture = null;
if (material.HasProperty("_Diffuse")) destTexture = "_Diffuse";
else if (material.HasProperty("_Albedo")) destTexture = "_Albedo";
else if (material.HasProperty("_BaseColorMap")) destTexture = "_BaseColorMap";
else if (material.HasProperty("_MainTex")) destTexture = "_MainTex";
if (destTexture != null)
{
var texturePath = string.Format("{0}{1}.png", DefaultTexturePath, materialName);
var defaultTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(texturePath);
if (defaultTexture)
{
material.SetTexture(destTexture, defaultTexture);
material.mainTexture = defaultTexture;
} else
{
var regularMaterialPath = string.Format("{0}{1}.mat", DefaultMaterialPath, materialName);
var regularMaterial = AssetDatabase.LoadAssetAtPath<Material>(regularMaterialPath);
if (regularMaterial)
{
material.SetTexture(destTexture, regularMaterial.mainTexture);
material.mainTexture = regularMaterial.mainTexture;
} else
Debug.LogWarning("couldn't find source texture for " + materialName);
}
}
}
catch(Exception ex)
{
Debug.LogException(ex);
}
}
if( rpDefaultMatShader.StartsWith( "URP" ) || rpDefaultMatShader.StartsWith( "Universal Render Pipeline" ))
return "URP";

return string.Empty; // default rp, do nothing.
}

internal static void CreateRenderPipelineVersionOfDefaultMaterials(Material defaultMaterial)
{
if (!defaultMaterial)
return;
private static bool TryGetShaderMainTex( Material material, out string mainTexTag )
{
if( material.HasProperty( "_Diffuse" ) )
{
mainTexTag = "_Diffuse";

CreateRenderPipelineVersionOfDefaultMaterial(defaultMaterial, WallMaterialName);
CreateRenderPipelineVersionOfDefaultMaterial(defaultMaterial, FloorMaterialName);
CreateRenderPipelineVersionOfDefaultMaterial(defaultMaterial, WindowMaterialName);
CreateRenderPipelineVersionOfDefaultMaterial(defaultMaterial, MetalMaterialName);

CSGSettings.Reload();
var currentMaterial = CSGSettings.DefaultMaterial;
if (!currentMaterial)
return;

var currentMaterialPath = AssetDatabase.GetAssetPath(currentMaterial);
if (!currentMaterialPath.StartsWith(DefaultMaterialPath))
return;

var materialPath = string.Format("{0}{1}/", DefaultMaterialPath, defaultMaterial.shader.name);
if (currentMaterialPath.StartsWith(materialPath))
return;

var newMaterialPath = currentMaterialPath.Replace(DefaultMaterialPath, materialPath);
currentMaterial = AssetDatabase.LoadAssetAtPath<Material>(newMaterialPath);
if (currentMaterial)
CSGSettings.DefaultMaterial = currentMaterial;
CSGSettings.Save();
}
return true;
}

internal static Material GetRuntimeMaterial(string materialName)
{
Material defaultMaterial = null;
var renderPipelineAsset = UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset;
if (renderPipelineAsset)
defaultMaterial = DefaultMaterial;
if( material.HasProperty( "_Albedo" ) )
{
mainTexTag = "_Albedo";

if (!defaultMaterial)
{
var defaultFilename = string.Format("{0}{1}.mat", DefaultMaterialPath, materialName);
return AssetDatabase.LoadAssetAtPath<Material>(defaultFilename);
}

var materialPath = string.Format("{0}{1}/", DefaultMaterialPath, defaultMaterial.shader.name);
var materialFilename = string.Format("{0}{1}.mat", materialPath, materialName);
return true;
}

var material = AssetDatabase.LoadAssetAtPath<Material>(materialFilename);
if (material)
return material;

CreateRenderPipelineVersionOfDefaultMaterials(defaultMaterial);

return AssetDatabase.LoadAssetAtPath<Material>(materialFilename);
}
if( material.HasProperty( "_BaseColorMap" ) )
{
mainTexTag = "_BaseColorMap";

return true;
}

internal static PhysicMaterial GetRuntimePhysicMaterial(string materialName)
if( material.HasProperty( "_MainTex" ) )
{
mainTexTag = "_MainTex";

return true;
}

mainTexTag = "";

return false;
}
private static bool TryGetMetallicProperty(Material material, out string property )
{
var defaultFilename = string.Format("{0}{1}.physicMaterial", DefaultMaterialPath, materialName);
return AssetDatabase.LoadAssetAtPath<PhysicMaterial>(defaultFilename);
if( material.HasProperty( "_Metallic" ) )
{
property = "_Metallic";

return true;
}

property = "_Metallic";
return false;
}

/*
* 1: Check that we actually have the material were looking for
* 2: Find RP type, and then create any neccessary directories
* 3: If we arent on the built-in RP, then update the material to the default material shader for the current RP
* 3.1: Stop entirely if the default material shader doesnt have a compatible default main texture property
* 4: Save the changes to disk
*/
private static void GenerateBuiltinPipelineMaterial( string name )
{
Material material = Resources.Load<Material>( string.Format( "{0}{1}", "RealtimeCSG/Materials/", name ) );

if( material == null )
{
Debug.LogWarningFormat( "Could not find material '{0}{1}.mat'. Not generating material '{0}{1}.mat' for the current render pipeline.", "RealtimeCSG/Materials/", name );

return;
}

string rpType = GetRenderPipelineType();

if( !string.IsNullOrEmpty( rpType ) ) // if we are using any RP
{
AssetDatabase.StartAssetEditing();

string pipelineShader = GraphicsSettings.renderPipelineAsset.defaultMaterial.shader.name;

material.shader = Shader.Find( pipelineShader );

string mainTexTag;

if( TryGetShaderMainTex( material, out mainTexTag ) )
{
material.SetTexture( mainTexTag, Resources.Load<Texture2D>( string.Format( "RealtimeCSG/Textures/{0}", name ) ) );

if( material.name.Equals( "Metal" ) )
{
// if the material is the built-in metal texture, then we'll set its metalness to 100%

string metalnessTag;

if( TryGetMetallicProperty( material, out metalnessTag ) )
{
material.SetFloat( metalnessTag, 1f );
material.SetFloat( "_Smoothness", 1f );
material.SetTexture( "_MetallicGlossMap", Resources.Load<Texture2D>( "RealtimeCSG/Textures/checker_m" ) );
}

if( material.HasProperty( "_DetailAlbedoMap" ) )
{
material.SetTexture( "_DetailAlbedoMap", Resources.Load<Texture2D>( "RealtimeCSG/Textures/checker" ) );
}
}
}
else // we failed to find any default RP shader, so we'll just abort early
{
AssetDatabase.StopAssetEditing();

Debug.LogErrorFormat( "Could not find a compatible default render pipeline shader. Shader '{0}' does not contain any properties named '_MainTex', '_Albedo', '_Diffuse', or '_BaseColorMap'." );

return;
}

AssetDatabase.StopAssetEditing();

EditorUtility.SetDirty( material );
AssetDatabase.SaveAssetIfDirty( material );

AssetDatabase.Refresh( ImportAssetOptions.ForceUpdate );

Resources.UnloadUnusedAssets();
}

// using built-in, just dont do anything.
}

internal static Material GetRuntimeMaterial( string materialName )
{
GenerateBuiltinPipelineMaterial( WallMaterialName );
GenerateBuiltinPipelineMaterial( FloorMaterialName );
GenerateBuiltinPipelineMaterial( WindowMaterialName );
GenerateBuiltinPipelineMaterial( MetalMaterialName );

return Resources.Load<Material>( string.Format( "RealtimeCSG/Materials/{0}/{1}", GetRenderPipelineType(), materialName ) );
}

internal static PhysicMaterial GetRuntimePhysicMaterial(string materialName)
{
return Resources.Load<PhysicMaterial>( string.Format( "RealtimeCSG/Materials/{0}", materialName ) );
}

private static Material _defaultMaterial;
public static Material DefaultMaterial
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions Plugins/Editor/Scripts/Data/Generated.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 904fc06

Please sign in to comment.