Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omnibus PR with fixes for #333, #334, #335, and README updates #340

Merged
merged 26 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a13fac2
begin work on #300 #332
kerfuffles Jan 28, 2022
4286ffe
convert icons to embedded equivalents, fix icon path issues when RCSG…
kerfuffles Jan 28, 2022
5a3c938
Update README.md
kerfuffles Jan 28, 2022
7503c49
Update Icon2EmbeddedAssetsUtility.cs
kerfuffles Jan 28, 2022
75539a7
Merge branch 'remove-unity-project' of https://github.com/Kerfuffles/…
kerfuffles Jan 28, 2022
71e9eb2
Update Icon2EmbeddedAssetsUtility.cs
kerfuffles Jan 28, 2022
3bb23b6
(not working yet) work on fixing material/physic material loading, ig…
kerfuffles Feb 1, 2022
e6c78fd
revert material utility changes
kerfuffles Feb 1, 2022
0f1b593
fix missing materials, and account for RCSG folder being moved when g…
kerfuffles Feb 1, 2022
577e650
add package.json
kerfuffles Feb 1, 2022
4fcd236
update to fix versions of unity back to 5.6.4f1
kerfuffles Feb 3, 2022
4da15ba
Convert project to Plugins/ folder only (#333)
kerfuffles Feb 11, 2022
f96ce46
fix inability to build player due to a missed setting in .asmdef
kerfuffles Feb 12, 2022
396ea85
Merge branch 'master' into remove-unity-project
kerfuffles Feb 12, 2022
342bb1c
Fix inability to build player due to missed setting in .asmdef (#334)
kerfuffles Feb 12, 2022
3a7cfab
Merge branch 'LogicalError:master' into remove-unity-project
kerfuffles Feb 13, 2022
d36aad8
Update readme, use folder search for RCSG root instead of file search…
kerfuffles Mar 6, 2022
a659a08
bring changes over from #341 to resolve merge conflicts
kerfuffles Mar 8, 2022
f19816b
Merge remote-tracking branch 'upstream/master'
kerfuffles Mar 8, 2022
e0b0c2a
Merge branch 'master' into fix-player-build
kerfuffles Mar 8, 2022
9f21056
include missed step in readme for updating manually
kerfuffles Mar 8, 2022
9f277b6
fix folder search not working when installed as a package
kerfuffles Mar 8, 2022
fe462f0
Update README.md
kerfuffles Mar 8, 2022
f1eee0c
update package version
kerfuffles Mar 11, 2022
f62109d
(hopefully) fix broken import with render pipelines
kerfuffles Mar 12, 2022
13ed86a
include changes missed due to project not being saved
kerfuffles Mar 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
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.
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
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