Skip to content

Commit

Permalink
[Update] Folder refactor and prompts from txt file
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketrajnish committed Dec 31, 2023
1 parent e25a865 commit e3fc0fe
Show file tree
Hide file tree
Showing 28 changed files with 1,202 additions and 909 deletions.
523 changes: 0 additions & 523 deletions src/Text To Material/Assets/Scenes/SampleScene.unity

This file was deleted.

1,118 changes: 1,118 additions & 0 deletions src/Text To Material/Assets/Scenes/Test.unity

Large diffs are not rendered by default.

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

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine.Networking;

Expand Down Expand Up @@ -97,4 +98,5 @@ public static string InvokeChat(string prompt, string model = "gpt-3.5-turbo")

}

}
}
#endif
8 changes: 8 additions & 0 deletions src/Text To Material/Assets/Scripts/T2M.meta

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

8 changes: 8 additions & 0 deletions src/Text To Material/Assets/Scripts/T2M/Settings.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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.IO;
#if UNITY_EDITOR
using Unity.EditorCoroutines.Editor;
using UnityEditor;
using UnityEngine;
Expand All @@ -11,6 +12,7 @@ namespace T2M
{
/// <summary>
/// This static class is meant to generate the materials.
/// We parse a txt file containing the additional prompts and send it to the API.
/// We set the material properties, generate texture, convert it to normal map.
/// We then apply the generated texture map and normal map to the material.
/// </summary>
Expand Down Expand Up @@ -63,18 +65,11 @@ public static void GenerateMaterial(CurrentSettings settings, string matPrompt,
if (!Directory.Exists(generatedMaterialPath))
AssetDatabase.CreateFolder(Path.GetDirectoryName(generatedMaterialPath), Path.GetFileName(generatedMaterialPath));

string specificMatPrompt = $"Generate a Unity Standard Shader material with the following characteristics: {matPrompt}. " +
"Please provide the material properties in the following format: Albedo: [color], Metallic: [value], Smoothness: [value], " +
"Emission: [color]. For Tiling and Offset, provide two values separated by a space, in the format 'x y'. " +
"Example format for Tiling and Offset: 'Tiling: 1 1', 'Offset: 0 0'." +
"Please provide the material properties in the specified format without brackets." +
"For example: Albedo: red, Metallic: 1, Smoothness: 1, Emission: red, Tiling: 10 10, Offset: 0 0"; // return response in a specified format to be parsed
string specificMatPrompt = GetMaterialPrompt(matPrompt); // return response in a specified format to be parsed
string specificTexPrompt = GetTexturePrompt(texPrompt); // return seamless textures

string specificTexPrompt = $"Create a high-quality, strictly seamless texture that can be tiled flawlessly for a 3D material, matching these characteristics: {texPrompt}. " +
"The texture must have absolutely no visible seams or discontinuities, ensuring it can be tiled repeatedly without any noticeable edges. " +
"It should also be evenly lit and high-resolution to support close-up views and detailed rendering without pixelation."; // return seamless textures

string materialProperties = OpenAIUtil.InvokeChat(specificMatPrompt, GetSelectedGPTModel(settings.GPT_MODEL));
string materialProperties = OpenAIUtil.InvokeChat(specificMatPrompt, GetSelectedGPTModel(settings.GPT_MODEL));

MaterialProperties materialValues = ParseMaterialProperties(materialProperties);

string textureUrl = OpenAIUtil.InvokeImage(specificTexPrompt, GetSelectedDalleModel(settings.DALLE_MODEL),
Expand Down Expand Up @@ -116,6 +111,32 @@ public static void GenerateMaterial(CurrentSettings settings, string matPrompt,
}, normStrength));
}
// converting a text file to array of strings
private static string[] ReadPromptsFromFile(string filePath)
{
if (File.Exists(filePath))
{
string[] lines = File.ReadAllLines(filePath); // each element of array is 1 line
return lines;
}
else
{
Debug.LogError("File not found: " + filePath);
return new string[0];
}
}

public static string GetMaterialPrompt(string matPrompt)
{
string[] prompts = ReadPromptsFromFile(Path.Combine(Application.dataPath, "Scripts/T2M/prompts.txt"));
return string.Format(prompts.Length > 0 ? prompts[0] : "", matPrompt); // 1st line - mat prompt
}

public static string GetTexturePrompt(string texPrompt)
{
string[] prompts = ReadPromptsFromFile(Path.Combine(Application.dataPath, "Scripts/T2M/prompts.txt")); // 2nd line - tex prompt
return string.Format(prompts.Length > 1 ? prompts[1] : "", texPrompt);
}
// download the image from the response url
public static IEnumerator DownloadTextureFromUrl(string url, System.Action<Texture2D, Texture2D> onCompleted, float normalStrength = 100.0f)
{
Expand Down Expand Up @@ -205,10 +226,14 @@ public static MaterialProperties ParseMaterialProperties(string response)
switch (key)
{
case "Albedo":
Color albedoColor;
if (ColorUtility.TryParseHtmlString(value, out albedoColor))
string[] rgba1 = value.Split(' ');
if (rgba1.Length == 4 &&
float.TryParse(rgba1[0], out float r1) &&
float.TryParse(rgba1[1], out float g1) &&
float.TryParse(rgba1[2], out float b1) &&
float.TryParse(rgba1[3], out float a1))
{
values.Albedo = albedoColor;
values.Albedo = new Color(r1, g1, b1, a1);
}
break;
case "Metallic":
Expand All @@ -222,12 +247,16 @@ public static MaterialProperties ParseMaterialProperties(string response)
{
values.Smoothness = smoothness;
}
break;
break;
case "Emission":
Color emissionColor;
if (ColorUtility.TryParseHtmlString(value, out emissionColor))
string[] rgba2 = value.Split(' ');
if (rgba2.Length == 4 &&
float.TryParse(rgba2[0], out float r2) &&
float.TryParse(rgba2[1], out float g2) &&
float.TryParse(rgba2[2], out float b2) &&
float.TryParse(rgba2[3], out float a2))
{
values.Emission = emissionColor;
values.Emission = new Color(r2, g2, b2, a2);
}
break;
case "Tiling":
Expand Down Expand Up @@ -260,9 +289,9 @@ private static void ApplyMaterialProperties(Material material, MaterialPropertie
material.SetFloat("_Metallic", values.Metallic);
material.SetFloat("_Glossiness", values.Smoothness);


material.SetTextureScale("_MainTex", values.Tiling);
material.SetTextureOffset("_MainTex", values.Offset);

material.SetTexture("_MainTex", texture);
if (normalMap != null)
{
Expand All @@ -278,3 +307,4 @@ private static void ApplyMaterialProperties(Material material, MaterialPropertie
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if UNITY_EDITOR
using UnityEditor;

namespace T2M
Expand Down Expand Up @@ -39,3 +40,4 @@ public static SettingsProvider CreateCustomSettingsProvider()
}

} // namespace T2M
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;

namespace T2M
Expand Down Expand Up @@ -85,3 +86,4 @@ void OnGUI()
}
}
}
#endif
2 changes: 2 additions & 0 deletions src/Text To Material/Assets/Scripts/T2M/prompts.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Generate a Unity Standard Shader material with the following characteristics: {0}. Please provide the material properties in the following format: Albedo: [r g b a], Metallic: [value], Smoothness: [value], Emission: [r g b a]. For Tiling and Offset, provide two values separated by a space, in the format 'x y'. Example format for Tiling and Offset: 'Tiling: 1 1', 'Offset: 0 0'. Please provide the material properties in the specified format without brackets. For example: Albedo: 1 0 0 1, Metallic: 1, Smoothness: 1, Emission: 1 1 1 0, Tiling: 10 10, Offset: 0 0
Create a high-quality, strictly seamless texture that can be tiled flawlessly for a 3D material, matching these characteristics: {0}. The texture must have absolutely no visible seams or discontinuities, ensuring it can be tiled repeatedly without any noticeable edges. It should also be evenly lit and high-resolution to support close-up views and detailed rendering without pixelation.
7 changes: 7 additions & 0 deletions src/Text To Material/Assets/Scripts/T2M/prompts.txt.meta

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

This file was deleted.

Binary file not shown.

0 comments on commit e3fc0fe

Please sign in to comment.