|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.IO; |
3 | 4 | using UnityEditor;
|
4 | 5 | using UnityEngine;
|
5 | 6 | using InternalRealtimeCSG;
|
| 7 | +using UnityEngine.Rendering; |
| 8 | + |
6 | 9 |
|
7 | 10 | namespace RealtimeCSG
|
8 | 11 | {
|
@@ -93,10 +96,10 @@ internal static Material GenerateEditorMaterial(string shaderName, string textur
|
93 | 96 | };
|
94 | 97 | if (textureName != null)
|
95 | 98 | {
|
96 |
| - string filename = "Assets/Plugins/RealtimeCSG/Editor/Resources/Textures/" + textureName + ".png"; |
97 |
| - material.mainTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(filename); |
| 99 | + //string filename = "Assets/Plugins/RealtimeCSG/Editor/Resources/Textures/" + textureName + ".png"; |
| 100 | + material.mainTexture = Resources.Load<Texture2D>( string.Format( "RealtimeCSG/Textures/{0}", textureName ) ); //AssetDatabase.LoadAssetAtPath<Texture2D>(filename); |
98 | 101 | if (!material.mainTexture)
|
99 |
| - Debug.LogWarning("Could not find internal texture: " + filename); |
| 102 | + Debug.LogWarning("Could not find internal texture: " + textureName); |
100 | 103 | }
|
101 | 104 | EditorMaterials.Add(name, material);
|
102 | 105 | return material;
|
@@ -142,134 +145,161 @@ internal static bool EqualInternalMaterial(Material o, Material n)
|
142 | 145 | o.name == n.name;
|
143 | 146 | }
|
144 | 147 |
|
145 |
| - |
146 |
| - const string DefaultMaterialPath = "Assets/Plugins/RealtimeCSG/Runtime/Materials/"; |
147 |
| - const string DefaultTexturePath = "Assets/Plugins/RealtimeCSG/Runtime/Textures/"; |
148 |
| - |
149 |
| - internal static void CreateRenderPipelineVersionOfDefaultMaterial(Material defaultMaterial, string materialName) |
150 |
| - { |
151 |
| - if (!defaultMaterial) |
152 |
| - return; |
153 |
| - |
154 |
| - var materialPath = string.Format("{0}{1}/", DefaultMaterialPath, defaultMaterial.shader.name); |
155 |
| - var materialFilename = string.Format("{0}{1}.mat", materialPath, materialName); |
| 148 | + private static string GetRenderPipelineType() |
| 149 | + { |
| 150 | + if( GraphicsSettings.renderPipelineAsset == null ) |
| 151 | + return string.Empty; // we are using built-in, so do nothing. |
156 | 152 |
|
157 |
| - var material = AssetDatabase.LoadAssetAtPath<Material>(materialFilename); |
158 |
| - if (material) |
159 |
| - return; |
| 153 | + string rpDefaultMatShader = GraphicsSettings.renderPipelineAsset.defaultMaterial.shader.name; |
160 | 154 |
|
161 |
| - material = new Material(defaultMaterial); |
| 155 | + if( rpDefaultMatShader.StartsWith( "HDRP" ) ) |
| 156 | + return "HDRP"; |
162 | 157 |
|
163 |
| - try |
164 |
| - { |
165 |
| - if (!System.IO.Directory.Exists(materialPath)) |
166 |
| - System.IO.Directory.CreateDirectory(materialPath); |
167 |
| - // HDRenderPipeline will generate errors when creating it's own type of materials |
168 |
| - AssetDatabase.CreateAsset(material, materialFilename); |
169 |
| - material = AssetDatabase.LoadAssetAtPath<Material>(materialFilename); |
170 |
| - } |
171 |
| - catch (Exception ex) |
172 |
| - { |
173 |
| - Debug.LogException(ex); |
174 |
| - } |
| 158 | + if( rpDefaultMatShader.StartsWith( "LWRP" ) ) |
| 159 | + return "LWRP"; |
175 | 160 |
|
176 |
| - try |
177 |
| - { |
178 |
| - string destTexture = null; |
179 |
| - if (material.HasProperty("_Diffuse")) destTexture = "_Diffuse"; |
180 |
| - else if (material.HasProperty("_Albedo")) destTexture = "_Albedo"; |
181 |
| - else if (material.HasProperty("_BaseColorMap")) destTexture = "_BaseColorMap"; |
182 |
| - else if (material.HasProperty("_MainTex")) destTexture = "_MainTex"; |
183 |
| - if (destTexture != null) |
184 |
| - { |
185 |
| - var texturePath = string.Format("{0}{1}.png", DefaultTexturePath, materialName); |
186 |
| - var defaultTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(texturePath); |
187 |
| - if (defaultTexture) |
188 |
| - { |
189 |
| - material.SetTexture(destTexture, defaultTexture); |
190 |
| - material.mainTexture = defaultTexture; |
191 |
| - } else |
192 |
| - { |
193 |
| - var regularMaterialPath = string.Format("{0}{1}.mat", DefaultMaterialPath, materialName); |
194 |
| - var regularMaterial = AssetDatabase.LoadAssetAtPath<Material>(regularMaterialPath); |
195 |
| - if (regularMaterial) |
196 |
| - { |
197 |
| - material.SetTexture(destTexture, regularMaterial.mainTexture); |
198 |
| - material.mainTexture = regularMaterial.mainTexture; |
199 |
| - } else |
200 |
| - Debug.LogWarning("couldn't find source texture for " + materialName); |
201 |
| - } |
202 |
| - } |
203 |
| - } |
204 |
| - catch(Exception ex) |
205 |
| - { |
206 |
| - Debug.LogException(ex); |
207 |
| - } |
208 |
| - } |
| 161 | + if( rpDefaultMatShader.StartsWith( "URP" ) || rpDefaultMatShader.StartsWith( "Universal Render Pipeline" )) |
| 162 | + return "URP"; |
209 | 163 |
|
| 164 | + return string.Empty; // default rp, do nothing. |
| 165 | + } |
210 | 166 |
|
211 |
| - internal static void CreateRenderPipelineVersionOfDefaultMaterials(Material defaultMaterial) |
212 |
| - { |
213 |
| - if (!defaultMaterial) |
214 |
| - return; |
| 167 | + private static bool TryGetShaderMainTex( Material material, out string mainTexTag ) |
| 168 | + { |
| 169 | + if( material.HasProperty( "_Diffuse" ) ) |
| 170 | + { |
| 171 | + mainTexTag = "_Diffuse"; |
215 | 172 |
|
216 |
| - CreateRenderPipelineVersionOfDefaultMaterial(defaultMaterial, WallMaterialName); |
217 |
| - CreateRenderPipelineVersionOfDefaultMaterial(defaultMaterial, FloorMaterialName); |
218 |
| - CreateRenderPipelineVersionOfDefaultMaterial(defaultMaterial, WindowMaterialName); |
219 |
| - CreateRenderPipelineVersionOfDefaultMaterial(defaultMaterial, MetalMaterialName); |
220 |
| - |
221 |
| - CSGSettings.Reload(); |
222 |
| - var currentMaterial = CSGSettings.DefaultMaterial; |
223 |
| - if (!currentMaterial) |
224 |
| - return; |
225 |
| - |
226 |
| - var currentMaterialPath = AssetDatabase.GetAssetPath(currentMaterial); |
227 |
| - if (!currentMaterialPath.StartsWith(DefaultMaterialPath)) |
228 |
| - return; |
229 |
| - |
230 |
| - var materialPath = string.Format("{0}{1}/", DefaultMaterialPath, defaultMaterial.shader.name); |
231 |
| - if (currentMaterialPath.StartsWith(materialPath)) |
232 |
| - return; |
233 |
| - |
234 |
| - var newMaterialPath = currentMaterialPath.Replace(DefaultMaterialPath, materialPath); |
235 |
| - currentMaterial = AssetDatabase.LoadAssetAtPath<Material>(newMaterialPath); |
236 |
| - if (currentMaterial) |
237 |
| - CSGSettings.DefaultMaterial = currentMaterial; |
238 |
| - CSGSettings.Save(); |
239 |
| - } |
| 173 | + return true; |
| 174 | + } |
240 | 175 |
|
241 |
| - internal static Material GetRuntimeMaterial(string materialName) |
242 |
| - { |
243 |
| - Material defaultMaterial = null; |
244 |
| - var renderPipelineAsset = UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset; |
245 |
| - if (renderPipelineAsset) |
246 |
| - defaultMaterial = DefaultMaterial; |
| 176 | + if( material.HasProperty( "_Albedo" ) ) |
| 177 | + { |
| 178 | + mainTexTag = "_Albedo"; |
247 | 179 |
|
248 |
| - if (!defaultMaterial) |
249 |
| - { |
250 |
| - var defaultFilename = string.Format("{0}{1}.mat", DefaultMaterialPath, materialName); |
251 |
| - return AssetDatabase.LoadAssetAtPath<Material>(defaultFilename); |
252 |
| - } |
253 |
| - |
254 |
| - var materialPath = string.Format("{0}{1}/", DefaultMaterialPath, defaultMaterial.shader.name); |
255 |
| - var materialFilename = string.Format("{0}{1}.mat", materialPath, materialName); |
| 180 | + return true; |
| 181 | + } |
256 | 182 |
|
257 |
| - var material = AssetDatabase.LoadAssetAtPath<Material>(materialFilename); |
258 |
| - if (material) |
259 |
| - return material; |
260 |
| - |
261 |
| - CreateRenderPipelineVersionOfDefaultMaterials(defaultMaterial); |
262 |
| - |
263 |
| - return AssetDatabase.LoadAssetAtPath<Material>(materialFilename); |
264 |
| - } |
| 183 | + if( material.HasProperty( "_BaseColorMap" ) ) |
| 184 | + { |
| 185 | + mainTexTag = "_BaseColorMap"; |
265 | 186 |
|
| 187 | + return true; |
| 188 | + } |
266 | 189 |
|
267 |
| - internal static PhysicMaterial GetRuntimePhysicMaterial(string materialName) |
| 190 | + if( material.HasProperty( "_MainTex" ) ) |
| 191 | + { |
| 192 | + mainTexTag = "_MainTex"; |
| 193 | + |
| 194 | + return true; |
| 195 | + } |
| 196 | + |
| 197 | + mainTexTag = ""; |
| 198 | + |
| 199 | + return false; |
| 200 | + } |
| 201 | + private static bool TryGetMetallicProperty(Material material, out string property ) |
268 | 202 | {
|
269 |
| - var defaultFilename = string.Format("{0}{1}.physicMaterial", DefaultMaterialPath, materialName); |
270 |
| - return AssetDatabase.LoadAssetAtPath<PhysicMaterial>(defaultFilename); |
| 203 | + if( material.HasProperty( "_Metallic" ) ) |
| 204 | + { |
| 205 | + property = "_Metallic"; |
| 206 | + |
| 207 | + return true; |
| 208 | + } |
| 209 | + |
| 210 | + property = "_Metallic"; |
| 211 | + return false; |
271 | 212 | }
|
272 | 213 |
|
| 214 | + /* |
| 215 | + * 1: Check that we actually have the material were looking for |
| 216 | + * 2: Find RP type, and then create any neccessary directories |
| 217 | + * 3: If we arent on the built-in RP, then update the material to the default material shader for the current RP |
| 218 | + * 3.1: Stop entirely if the default material shader doesnt have a compatible default main texture property |
| 219 | + * 4: Save the changes to disk |
| 220 | + */ |
| 221 | + private static void GenerateBuiltinPipelineMaterial( string name ) |
| 222 | + { |
| 223 | + Material material = Resources.Load<Material>( string.Format( "{0}{1}", "RealtimeCSG/Materials/", name ) ); |
| 224 | + |
| 225 | + if( material == null ) |
| 226 | + { |
| 227 | + Debug.LogWarningFormat( "Could not find material '{0}{1}.mat'. Not generating material '{0}{1}.mat' for the current render pipeline.", "RealtimeCSG/Materials/", name ); |
| 228 | + |
| 229 | + return; |
| 230 | + } |
| 231 | + |
| 232 | + string rpType = GetRenderPipelineType(); |
| 233 | + |
| 234 | + if( !string.IsNullOrEmpty( rpType ) ) // if we are using any RP |
| 235 | + { |
| 236 | + AssetDatabase.StartAssetEditing(); |
| 237 | + |
| 238 | + string pipelineShader = GraphicsSettings.renderPipelineAsset.defaultMaterial.shader.name; |
| 239 | + |
| 240 | + material.shader = Shader.Find( pipelineShader ); |
| 241 | + |
| 242 | + string mainTexTag; |
| 243 | + |
| 244 | + if( TryGetShaderMainTex( material, out mainTexTag ) ) |
| 245 | + { |
| 246 | + material.SetTexture( mainTexTag, Resources.Load<Texture2D>( string.Format( "RealtimeCSG/Textures/{0}", name ) ) ); |
| 247 | + |
| 248 | + if( material.name.Equals( "Metal" ) ) |
| 249 | + { |
| 250 | + // if the material is the built-in metal texture, then we'll set its metalness to 100% |
| 251 | + |
| 252 | + string metalnessTag; |
| 253 | + |
| 254 | + if( TryGetMetallicProperty( material, out metalnessTag ) ) |
| 255 | + { |
| 256 | + material.SetFloat( metalnessTag, 1f ); |
| 257 | + material.SetFloat( "_Smoothness", 1f ); |
| 258 | + material.SetTexture( "_MetallicGlossMap", Resources.Load<Texture2D>( "RealtimeCSG/Textures/checker_m" ) ); |
| 259 | + } |
| 260 | + |
| 261 | + if( material.HasProperty( "_DetailAlbedoMap" ) ) |
| 262 | + { |
| 263 | + material.SetTexture( "_DetailAlbedoMap", Resources.Load<Texture2D>( "RealtimeCSG/Textures/checker" ) ); |
| 264 | + } |
| 265 | + } |
| 266 | + } |
| 267 | + else // we failed to find any default RP shader, so we'll just abort early |
| 268 | + { |
| 269 | + AssetDatabase.StopAssetEditing(); |
| 270 | + |
| 271 | + Debug.LogErrorFormat( "Could not find a compatible default render pipeline shader. Shader '{0}' does not contain any properties named '_MainTex', '_Albedo', '_Diffuse', or '_BaseColorMap'." ); |
| 272 | + |
| 273 | + return; |
| 274 | + } |
| 275 | + |
| 276 | + AssetDatabase.StopAssetEditing(); |
| 277 | + |
| 278 | + EditorUtility.SetDirty( material ); |
| 279 | + AssetDatabase.SaveAssetIfDirty( material ); |
| 280 | + |
| 281 | + AssetDatabase.Refresh( ImportAssetOptions.ForceUpdate ); |
| 282 | + |
| 283 | + Resources.UnloadUnusedAssets(); |
| 284 | + } |
| 285 | + |
| 286 | + // using built-in, just dont do anything. |
| 287 | + } |
| 288 | + |
| 289 | + internal static Material GetRuntimeMaterial( string materialName ) |
| 290 | + { |
| 291 | + GenerateBuiltinPipelineMaterial( WallMaterialName ); |
| 292 | + GenerateBuiltinPipelineMaterial( FloorMaterialName ); |
| 293 | + GenerateBuiltinPipelineMaterial( WindowMaterialName ); |
| 294 | + GenerateBuiltinPipelineMaterial( MetalMaterialName ); |
| 295 | + |
| 296 | + return Resources.Load<Material>( string.Format( "RealtimeCSG/Materials/{0}/{1}", GetRenderPipelineType(), materialName ) ); |
| 297 | + } |
| 298 | + |
| 299 | + internal static PhysicMaterial GetRuntimePhysicMaterial(string materialName) |
| 300 | + { |
| 301 | + return Resources.Load<PhysicMaterial>( string.Format( "RealtimeCSG/Materials/{0}", materialName ) ); |
| 302 | + } |
273 | 303 |
|
274 | 304 | private static Material _defaultMaterial;
|
275 | 305 | public static Material DefaultMaterial
|
|
0 commit comments