-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantize.cpp
85 lines (65 loc) · 2.14 KB
/
quantize.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
#include <ai.h>
AI_SHADER_NODE_EXPORT_METHODS(quantizeMethods);
struct ShaderData {
AtString trace_set;
AtVector cam_pos;
};
enum EdgeParams {
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);
// NOTE 法线的相反方向
AtVector dir = - AiShaderEvalParamVec(p_dir);
if (data->trace_set.length()) {
AiShaderGlobalsSetTraceSet(sg, data->trace_set, false);
}
AtRay ray_intersect_along_normal = AiMakeRay(AI_RAY_SUBSURFACE, pos, &dir, AI_BIG, sg);
AtScrSample hit = AtScrSample();
if(AiTrace(ray_intersect_along_normal, AI_RGB_WHITE, hit)){
// NOTE 获取点云碰撞到模型点的颜色
AtVector hitpoint = hit.point;
// NOTE 获取摄像机到碰撞点的方向
AtVector dir_cam_hit = AiV3Normalize(hitpoint - data->cam_pos);
AtRay ray_intersect_camera = AiMakeRay(AI_RAY_SUBSURFACE, data->cam_pos, &dir_cam_hit, AI_BIG, sg);
AtScrSample hit2 = AtScrSample();
// NOTE 如果摄像机也能碰到才赋值颜色 | 个人认为是避免背面的颜色处理加快速度
if(AiTrace(ray_intersect_camera, AI_RGB_WHITE, hit2)){
sg->out.RGB() = hit2.color;
}
}
AiShaderGlobalsUnsetTraceSet(sg);
}
node_loader {
if (i > 0) return false;
node->methods = quantizeMethods;
node->output_type = AI_TYPE_RGB;
node->name = "quantize";
node->node_type = AI_NODE_SHADER;
strcpy(node->version, AI_VERSION);
return true;
}