-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantize_cut_edge.cpp
88 lines (67 loc) · 2.22 KB
/
quantize_cut_edge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// cl /LD cutedge.cpp /I C:/Users/cactus/Arnold-5.2.2.0-windows/include C:/Users/cactus/Arnold-5.2.2.0-windows/lib/ai.lib /link /out:cutedge.dll
#include <ai.h>
AI_SHADER_NODE_EXPORT_METHODS(quantizeCutEdgeMethods);
struct ShaderData {
AtString trace_set;
AtVector cam_pos;
};
enum CutedgeParams {
p_pos,
p_dir,
p_traceset
};
node_parameters {
AiParameterVec("pos", 0.0, 0.0, 0.0);
AiParameterVec("dir", 0.0, 0.0, 0.0);
AiParameterStr("traceset", "");
}
node_initialize {
ShaderData* data = new ShaderData;
AiNodeSetLocalData(node, data);
}
node_update {
ShaderData *data = (ShaderData*)AiNodeGetLocalData(node);
data->trace_set = AiNodeGetStr(node, "traceset");
AtNode *camera = AiUniverseGetCamera();
const AtMatrix& cMat = AiNodeGetMatrix(camera, AtString("matrix"));
data->cam_pos = {cMat[3][0], cMat[3][1], cMat[3][2]};
}
node_finish {
ShaderData* data = (ShaderData*)AiNodeGetLocalData(node);
AiNodeSetLocalData(node, NULL);
delete data;
}
shader_evaluate {
ShaderData* data = (ShaderData*)AiNodeGetLocalData(node);
AtVector pos = AiShaderEvalParamVec(p_pos);
AtVector dir = - AiShaderEvalParamVec(p_dir);
if (data->trace_set.length()) {
AiShaderGlobalsSetTraceSet(sg, data->trace_set, false);
}
AtVector p_orig = sg->P;
AtRay ray_intersect_along_normal = AiMakeRay(AI_RAY_CAMERA, pos, &dir, AI_BIG, sg);
AtScrSample hit = AtScrSample();
if(AiTrace(ray_intersect_along_normal, AI_RGB_WHITE, hit)){
AtVector dir_cam_hit = AiV3Normalize(p_orig - data->cam_pos);
AtRay ray_intersect_camera = AiMakeRay(AI_RAY_CAMERA, data->cam_pos, &dir_cam_hit, AI_BIG, sg);
AtScrSample hit2 = AtScrSample();
if (AiTrace(ray_intersect_camera, AI_RGB_WHITE, hit2)){
// NOTE 判断是否碰撞点所获取的物体是否一致
if (hit2.obj != hit.obj){
sg->out.RGB() = AI_RGB_BLACK;
} else {
sg->out.RGB() = AI_RGB_WHITE;
}
}
}
AiShaderGlobalsUnsetTraceSet(sg);
}
node_loader {
if (i > 0) return false;
node->methods = quantizeCutEdgeMethods;
node->output_type = AI_TYPE_RGB;
node->name = "quantize_cut_edge";
node->node_type = AI_NODE_SHADER;
strcpy(node->version, AI_VERSION);
return true;
}