Material Node Editor for Simple Analysis

The first problem is that there is no small C/C++ open source material node editor project on the Internet. Unity does, but because it is C# and CG, it may not be too representative. In fact, after analysis, it is found that it is similar to C/C++ GLSL or C# CG should make no difference.

The reference code is blender2.79, C code, GLSL language

And Unity plugin ShaderForge 1.38 and Amplify Shader Editor 1.4.5, C# code, CG language

There is an article in Zhihu that briefly mentioned that there is nothing dry, and it is okay to read it as an entertaining foreword.

https://www.zhihu.com/question/35106026

In fact, by tracking the above three source codes, the idea is basically the same. First, define the node Node, which has various properties, and then when compiling the new shader, only compile those active nodes connected to the main output out. Inactive may just save to comments or something, not involved in compilation.

After that, it is the process of generating the corresponding shader code based on all the current valid nodes. It is all hard-coded, and there is no difficulty in physical work. Unity's simple call API, GLSL recompiles glShaderSource, glCompileShader

blender\source\blender\gpu\intern\gpu_codegen.c

GPU_generate_pass function

fragmentcode = code_generate_fragment(nodes, outlink->output);
vertexcode = code_generate_vertex(nodes, type);
geometrycode = code_generate_geometry(nodes, use_opensubdiv);

Then go in with the GPU_shader_create_ex function to understand

You can add two lines of code yourself, just save glsl to a file when compiling the new shader directly, and add a serial number, which can be used by other programs.

But I don't think it's necessary, because the shader code is almost the same, and it's the same in any coloring language to adjust the effect.

 

I wanted to do this thing last year, but after spending a day from yesterday to today to understand the general principle, I lost interest, and I was too lazy to write it, alas... I feel like I have become accustomed to eating ready-made waste...

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325290615&siteId=291194637