跳至主要內容

Blender Source Code Analysis

LincZero大约 3 分钟

Blender Source Code Analysis

目录

Node

Socket

枚举定义 DNA_node_types.h

blender-master/source/blender/makesdna/DNA_node_types.h

573行

/* socket value structs for input buttons
 * DEPRECATED now using ID properties
 */

typedef struct bNodeSocketValueInt {
  /** RNA subtype. */
  int subtype;
  int value;
  int min, max;
} bNodeSocketValueInt;

typedef struct bNodeSocketValueFloat {
  /** RNA subtype. */
  int subtype;
  float value;
  float min, max;
} bNodeSocketValueFloat;

typedef struct bNodeSocketValueBoolean {
  char value;
} bNodeSocketValueBoolean;

typedef struct bNodeSocketValueVector {
  /** RNA subtype. */
  int subtype;
  float value[3];
  float min, max;
} bNodeSocketValueVector;

typedef struct bNodeSocketValueRGBA {
  float value[4];
} bNodeSocketValueRGBA;

typedef struct bNodeSocketValueString {
  int subtype;
  char _pad[4];
  /** 1024 = FILEMAX. */
  char value[1024];
} bNodeSocketValueString;

typedef struct bNodeSocketValueObject {
  struct Object *value;
} bNodeSocketValueObject;

typedef struct bNodeSocketValueImage {
  struct Image *value;
} bNodeSocketValueImage;

typedef struct bNodeSocketValueCollection {
  struct Collection *value;
} bNodeSocketValueCollection;

typedef struct bNodeSocketValueTexture {
  struct Tex *value;
} bNodeSocketValueTexture;

typedef struct bNodeSocketValueMaterial {
  struct Material *value;
} bNodeSocketValueMaterial;

/* 输入按钮的套接字值结构 
 * 不赞成现在使用ID属性 
 */
typedef struct bNodeSocketValueInt			// 整数
typedef struct bNodeSocketValueFloat		// 浮点
typedef struct bNodeSocketValueBoolean		// 布尔
typedef struct bNodeSocketValueVector		// 矢量
typedef struct bNodeSocketValueRGBA			// RBGA
typedef struct bNodeSocketValueString		// 字符串
typedef struct bNodeSocketValueObject		// 对象
typedef struct bNodeSocketValueImage		// 图像
typedef struct bNodeSocketValueCollection	//
typedef struct bNodeSocketValueTexture		// 贴图
typedef struct bNodeSocketValueMaterial		// 材质

运算节点与矢量运算节点

截图

image-20220621135448340

枚举判断 node_texture_math.c、NOD_math_functions.hh

blender-master/source/blender/nodes/texture/nodes/node_texture_math.c

static void valuefn(float *out, TexParams *p, bNode *node, bNodeStack **in, short thread)
{
  float in0 = tex_input_value(in[0], p, thread);
  float in1 = tex_input_value(in[1], p, thread);

  switch (node->custom1) {

    case NODE_MATH_ADD:
      *out = in0 + in1;
      break;
    case NODE_MATH_SUBTRACT:
      *out = in0 - in1;
      break;
    case NODE_MATH_MULTIPLY:
      *out = in0 * in1;
      break;
    case NODE_MATH_DIVIDE: {
      if (in1 == 0) {
        /* We don't want to divide by zero. */
        *out = 0.0;
      }
      else {
        *out = in0 / in1;
      }
      break;
    }
          
  ......
  if (node->custom2 & SHD_MATH_CLAMP) {
    CLAMP(*out, 0.0f, 1.0f);
  }
}

blender-master/source/blender/nodes/NOD_math_functions.hh

switch (operation) {
    case NODE_VECTOR_MATH_ADD:
      return dispatch(exec_preset_fast, [](float3 a, float3 b) { return a + b; });
    case NODE_VECTOR_MATH_SUBTRACT:
      return dispatch(exec_preset_fast, [](float3 a, float3 b) { return a - b; });
    case NODE_VECTOR_MATH_MULTIPLY:
      return dispatch(exec_preset_fast, [](float3 a, float3 b) { return a * b; });
    case NODE_VECTOR_MATH_DIVIDE:
      return dispatch(exec_preset_fast, [](float3 a, float3 b) { return safe_divide(a, b); });
    case NODE_VECTOR_MATH_CROSS_PRODUCT:
      return dispatch(exec_preset_fast,
                      [](float3 a, float3 b) { return cross_high_precision(a, b); });
    case NODE_VECTOR_MATH_PROJECT:
      return dispatch(exec_preset_fast, [](float3 a, float3 b) { return project(a, b); });
    case NODE_VECTOR_MATH_REFLECT:
      return dispatch(exec_preset_fast,
                      [](float3 a, float3 b) { return reflect(a, normalize(b)); });
    case NODE_VECTOR_MATH_SNAP:
      return dispatch(exec_preset_fast,
                      [](float3 a, float3 b) { return floor(safe_divide(a, b)) * b; });
    case NODE_VECTOR_MATH_MODULO:
      return dispatch(exec_preset_slow, [](float3 a, float3 b) { return mod(a, b); });
    case NODE_VECTOR_MATH_MINIMUM:
      return dispatch(exec_preset_fast, [](float3 a, float3 b) { return min(a, b); });
    case NODE_VECTOR_MATH_MAXIMUM:
      return dispatch(exec_preset_fast, [](float3 a, float3 b) { return max(a, b); });
    default:
      return false;
  }

枚举定义 DNA_node_types.h

blender-master/source/blender/makesdna/DNA_node_types.h

252行

/** TODO: Limit data in #bNode to what we want to see saved. */
typedef struct bNode {
  struct bNode *next, *prev;

  /** User-defined properties. */
  IDProperty *prop;

  /** Runtime type information. */
  struct bNodeType *typeinfo;
  /** Runtime type identifier. */
  char idname[64];

  /** MAX_NAME. */
  char name[64];
  int flag;
  short type;
  /** Both for dependency and sorting. */
  short done, level;

  /** Used as a boolean for execution. */
  uint8_t need_exec;
  char _pad2[1];

  /** Custom user-defined color. */
  float color[3];

  ListBase inputs, outputs;
  /** Parent node. */
  struct bNode *parent;
  /** Optional link to libdata. */
  struct ID *id;
  /** Custom data, must be struct, for storage in file. */
  void *storage;
  /** The original node in the tree (for localized tree). */
  struct bNode *original;
  /** List of cached internal links (input to output), for muted nodes and operators. */
  ListBase internal_links;

  ......
    
  /** To be abused for buttons. */
  short custom1, custom2;
  float custom3, custom4;
    
  ......
      
} bNode;

1665行

typedef enum NodeMathOperation {
  NODE_MATH_ADD = 0,
  NODE_MATH_SUBTRACT = 1,
  NODE_MATH_MULTIPLY = 2,
  NODE_MATH_DIVIDE = 3,
  NODE_MATH_SINE = 4,
  NODE_MATH_COSINE = 5,
  ......
  NODE_MATH_SMOOTH_MAX = 39,
} NodeMathOperation;

typedef enum NodeVectorMathOperation {
  NODE_VECTOR_MATH_ADD = 0,
  NODE_VECTOR_MATH_SUBTRACT = 1,
  NODE_VECTOR_MATH_MULTIPLY = 2,
  NODE_VECTOR_MATH_DIVIDE = 3,

  NODE_VECTOR_MATH_CROSS_PRODUCT = 4,
  NODE_VECTOR_MATH_PROJECT = 5,
  NODE_VECTOR_MATH_REFLECT = 6,
  NODE_VECTOR_MATH_DOT_PRODUCT = 7,
  ......
  NODE_VECTOR_MATH_MULTIPLY_ADD = 26,
} NodeVectorMathOperation;

Node.h

blender-master/intern/cycles/graph/node.h

struct Node {
  explicit Node(const NodeType *type, ustring name = ustring());
  virtual ~Node() = 0;

  /* set values */
  void set(const SocketType &input, bool value);
  void set(const SocketType &input, int value);
  void set(const SocketType &input, uint value);
  void set(const SocketType &input, float value);
  void set(const SocketType &input, float2 value);
  void set(const SocketType &input, float3 value);
  void set(const SocketType &input, const char *value);
  void set(const SocketType &input, ustring value);
  void set(const SocketType &input, const Transform &value);
  void set(const SocketType &input, Node *value);
  
  ......
      
}