public string GenerateShaderCollectionFileContent(List <CustomShaderMeta> shadersImportMetaList) { StringBuilder sb = new StringBuilder(); // Order is reverse sb.AppendLine("// ============================================="); sb.AppendLine("// = Closet Hit Shader Collection ="); sb.AppendLine("// = Auto-generated File. Do not edit manually ="); sb.AppendLine($"// = Time: {System.DateTime.Now.ToLongDateString()} {System.DateTime.Now.ToLongTimeString()} ="); sb.AppendLine("// ============================================="); sb.AppendLine(); // sb.AppendLine("#pragma editor_sync_compilation"); shadersImportMetaList.ForEach((shader) => { var relPath = GPUMainProgramPathProvider.RelativeToGPUMain(shader.absPath); sb.AppendLine($"#include \"{relPath}\""); }); sb.AppendLine("float3 Shade(inout Ray ray, RayHit hit, float3 ambientLightUpper)"); sb.AppendLine("{"); // TODO: Determine which kind of switch attribute works // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-switch sb.AppendLine("switch(hit.matIndex)"); sb.AppendLine("{"); for (int s = 0; s < shadersImportMetaList.Count; s++) { sb.AppendLine($"case {s}:"); sb.AppendLine($" return {shadersImportMetaList[s].name}(ray, hit, ambientLightUpper);"); } sb.AppendLine($"default:"); sb.AppendLine($" return float3(0, 0, 0);"); sb.AppendLine("}"); sb.AppendLine("}"); return(sb.ToString()); }
public string GenerateShaderCollectionFileContent(SortedList <string, GUID> sortedByName, SortedList <GUID, CustomShaderMeta> shadersImportMetaList) { Debug.Log("[ClosestHit] GenerateShaderCollectionFileContent"); StringBuilder sb = new StringBuilder(); // Order is reverse sb.AppendLine("// ============================================="); sb.AppendLine("// = Closet Hit Shader Collection ="); sb.AppendLine("// = Auto-generated File. Do not edit manually ="); sb.AppendLine("// ============================================="); sb.AppendLine(); // sb.AppendLine("#pragma editor_sync_compilation"); foreach (var kvp in shadersImportMetaList) { var relPath = GPUMainProgramPathProvider.RelativeToGPUMain(kvp.Value.absPath); sb.AppendLine($"#include \"{relPath}\""); } sb.AppendLine("void SecRays(Ray ray, RayHit hit, inout SecRaysAtHit secRays)"); sb.AppendLine("{"); // TODO: Determine which kind of switch attribute works // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-switch sb.AppendLine(" switch(_Primitives[hit.primitiveId].materialIndex)"); sb.AppendLine(" {"); int secRaysIndex = 0; foreach (var shaderNameGUIDPair in sortedByName) { var guid = shaderNameGUIDPair.Value; sb.AppendLine($" case {secRaysIndex}:"); sb.AppendLine($" {shadersImportMetaList[guid].name}_SecRays(ray, hit, secRays);"); sb.AppendLine($" break;"); secRaysIndex++; } sb.AppendLine(" }"); sb.AppendLine("}"); sb.AppendLine("float3 ClosestHit(inout Ray ray, RayHit hit, float3 ambientLightUpper, float3 secondaryRayColor)"); sb.AppendLine("{"); // TODO: Determine which kind of switch attribute works // https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-switch sb.AppendLine("switch(_Primitives[hit.primitiveId].materialIndex)"); sb.AppendLine("{"); int index = 0; foreach (var shaderNameGUIDPair in sortedByName) { var guid = shaderNameGUIDPair.Value; sb.AppendLine($"case {index}:"); sb.AppendLine($" return {shadersImportMetaList[guid].name}(ray, hit, ambientLightUpper, secondaryRayColor);"); index++; } sb.AppendLine($"default:"); sb.AppendLine($" return float3(0, 1, 1);"); sb.AppendLine("}"); sb.AppendLine("}"); return(sb.ToString()); }