using System.Collections.Generic;
using System.Text.RegularExpressions;
public class TextureInterpolator : MonoBehaviour
[Tooltip("Folder containing textures")]
public DefaultAsset textureFolder;
public float normalizedValue = 0f;
[Tooltip("Material with the interpolation shader")]
public Material interpolationMaterial;
[Tooltip("Output RenderTexture where the result will be stored")]
public RenderTexture outputTexture;
// Data structure to hold texture and extracted data
public Texture2D texture;
public string dataVariable;
private List<TextureData> texturesDataList = new List<TextureData>();
texturesDataList.Clear();
if (textureFolder == null)
Debug.LogError("No folder assigned.");
string folderPath = AssetDatabase.GetAssetPath(textureFolder);
string[] guids = AssetDatabase.FindAssets("t:Texture2D", new[] { folderPath });
foreach (string guid in guids)
string assetPath = AssetDatabase.GUIDToAssetPath(guid);
Texture2D tex = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath);
// Extract data from filename
string fileName = System.IO.Path.GetFileNameWithoutExtension(assetPath);
TextureData textureData = new TextureData();
textureData.texture = tex;
// Use Regex to extract dataVariable, year, and month
ParseFileName(fileName, out textureData.dataVariable, out textureData.year, out textureData.month);
texturesDataList.Add(textureData);
Debug.Log($"Loaded {texturesDataList.Count} textures from {folderPath}");
Debug.LogError("Texture loading via AssetDatabase is only available in the Unity Editor.");
void ParseFileName(string fileName, out string dataVariable, out int year, out int month)
// "reanalysis-era5-single-levels-monthly-means_monthly_averaged_reanalysis_Total precipitation_[1979]_[12]"
// Regular expression pattern to match the filename structure
string pattern = @".*_(.+?)_$(\d{4})$_$(\d{1,2})$";
Match match = Regex.Match(fileName, pattern);
dataVariable = match.Groups[1].Value;
int.TryParse(match.Groups[2].Value, out year);
int.TryParse(match.Groups[3].Value, out month);
Debug.LogWarning($"Filename '{fileName}' does not match expected pattern.");
if (texturesDataList.Count < 3)
Debug.LogWarning("At least 3 textures are required for interpolation.");
// For this example, we'll continue using normalizedValue to select textures
// Alternatively, you can implement selection based on extracted data
float scaledValue = normalizedValue * (texturesDataList.Count - 1);
int index = Mathf.FloorToInt(scaledValue);
float t = scaledValue - index;
int prevIndex = Mathf.Clamp(index - 1, 0, texturesDataList.Count - 1);
int currIndex = Mathf.Clamp(index, 0, texturesDataList.Count - 1);
int nextIndex = Mathf.Clamp(index + 1, 0, texturesDataList.Count - 1);
Texture prevTex = texturesDataList[prevIndex].texture;
Texture currTex = texturesDataList[currIndex].texture;
Texture nextTex = texturesDataList[nextIndex].texture;
if (interpolationMaterial != null && outputTexture != null)
interpolationMaterial.SetTexture("_PrevTex", prevTex);
interpolationMaterial.SetTexture("_CurrTex", currTex);
interpolationMaterial.SetTexture("_NextTex", nextTex);
interpolationMaterial.SetFloat("_Interp", t);
// Perform the blit operation
Graphics.Blit(null, outputTexture, interpolationMaterial);
Debug.LogWarning("Interpolation material or output texture not assigned.");