Text Shader override

We are replacing the generated shader in the xcode project, in a build postprocessing step using something like the snippet below.

You need to copy the usd shader that’s generated from your modifications. You can either modify it by hand in Reality Composer Pro, or find it in the xcode assets. If you do a text search in xcode for the shader name you should be able to find the xyz.usda.polyspatial file. This is simply a usd file that’s been renamed.

        List<string> shaderFiles = GetShaderFiles(path);
        ReplaceShader("VisionOS", "USD_TextSDFSmoothstep", shaderFiles);


    private static List<string> GetShaderFiles(string projectPath)
    {
        List<string> shaderFiles = new List<string>();

        string shaderDir = $"{projectPath}/Data/Raw/PolySpatialAssets/VirtualArtifacts/Extra/";
        var dirs = Directory.GetDirectories(shaderDir);
        foreach (var dir in dirs)
        {
            var files = Directory.GetFiles(dir);
            foreach (var f in files)
            {
                if (f.Contains(".usda.polyspatial"))
                {
                    shaderFiles.Add(f);
                }
            }
        }

        foreach (var f in shaderFiles)
        {
            Debug.Log(f);
        }
        return shaderFiles;
    }

    private static string FindShader(string shaderName, List<string> shaderFiles)
    {
        foreach (var f in shaderFiles)
        {
            string data = File.ReadAllText(f);
            if (data.IndexOf($"\"{shaderName}\"") >= 0)
            {
                return f;
            }
        }

        Debug.LogError($"Did not find shader {shaderName}");

        return null;
    }

    private static void ReplaceShader(string shaderPath, string shaderName, List<string> shaderFiles)
    {
        string newData = File.ReadAllText($"{shaderPath}/{shaderName}.usd");
        if (newData.Length < 1)
        {
            Debug.LogError($"ReplaceShader({shaderPath}, {shaderName}, shaderFiles) did not find shader data");
            return;
        }

        string f = FindShader(shaderName, shaderFiles);
        if (f != null)
        {
            string origData = File.ReadAllText(f);
            if (origData != newData)
            {
                File.WriteAllText(f, newData);
                Debug.Log($"{shaderName} in file {f} has been replaced!");
            }
            else
            {
                Debug.Log($"{shaderName} no changes!");
            }
        }
    }

(We are also patching other shaders to get better performance, but that’s a bit more esoteric)

1 Like