alelievr / Mixture

Mixture is a powerful node-based tool crafted in unity to generate all kinds of textures in realtime
https://trello.com/b/2JiH2Vsp/mixture
MIT License
1.18k stars 124 forks source link

Mixture Variant from C# #25

Closed felixcantet closed 3 years ago

felixcantet commented 3 years ago

Hi,

I try to create a node that is able to import a graph asset and generate a custom port for each texture output of the imported graph. It's really experimental, but it's working ImportNode (left is the import graph node, right is imported graph)

I'm now trying to have access to graph parameters and be able to modify it from the Node Inspector. It works, but it modify the source graph asset, that is not a desired behaviour. I think Mixture variant is the thing i'm looking for, but I can't find a way to create mixture variants on the fly from the input graph (retrieve from the Texture with MixtureDatabase.GetGraphFromTexture() ). Does the Variant must necessarily be write on disk ? Maybe it could be a child asset of the current graph ? Do you have any advice to adress this issue ?

I don't know if i'm very clear, and sorry for my english.

Thank you

alelievr commented 3 years ago

Hello,

I think what you're looking for is a SubGraph, like the one in ShaderGraph but they don't exist yet (it's on the roadmap but pretty far).

You should be able to create a Mixture variant on the fly, change its parameters, and process it with the API that already exists. For example you can do something like this:

        var graph = MixtureDatabase.GetGraphFromTexture(graphTexture);

        var variant = ScriptableObject.CreateInstance<MixtureVariant>();
        variant.SetParent(graph);

        var sourceParam = graph.GetExposedParameter("Source");
        var targetParam = graph.GetExposedParameter("Target");

        // Clone parameters to avoid modifying the original asset
        sourceParam.Clone();
        targetParam.Clone();

        sourceParam.value = source;
        targetParam.value = target;

        variant.overrideParameters.Add(sourceParam);
        variant.overrideParameters.Add(targetParam);

        // Create the destination texture
        var settings = graph.outputNode.rtSettings;
        Texture2D destination = new Texture2D(
            settings.GetWidth(graph),
            settings.GetHeight(graph),
            settings.GetGraphicsFormat(graph),
            TextureCreationFlags.None
        );

        variant.ProcessGraphWithOverrides();

        // Readback the result
        graph.ReadbackMainTexture(destination);

Alternatively, you can just save the values of all parameters of the graph before executing it (by cloning all parameters for example) and set them back after you finish your operation (this is what the Mixture Variants are doing)

felixcantet commented 3 years ago

Hello ! That's exactly what I was looking for !

Thank you very much !