-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeSDF.js
76 lines (70 loc) · 2.22 KB
/
makeSDF.js
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
function makeSDF(data){
//Offset and Scale should be passed as uniform
const functionHead =
'float SDF(vec3 pos){\n'+
'vec3 p = pos;\n' +
'const int Iterations = '+ data.Iterations.toString() + ';\n'+
'int rescale = 0;\n'+
'for(int n=0;n<Iterations;n++){\n';
const functionTail =
'}\n'+
'return (length(p)- AtomSize) * pow(Scale, -float(rescale));\n'+
'}\n';
var ans = '';
ans += functionHead;
for(i=0;i<5;i++){
ans = ans + genTransform(data.transforms[i]);
}
ans += functionTail;
console.log(ans);
return ans;
}
/*
this is faster ?
const genTransform = {
"translate":(t) => {
return ccccc;
}
};
genTransform[t.type](t)
*/
const genTransform = function(t){
if(t.type == 'translate'){
//t.axis should be defined
return 'p += Offset * ((Scale-1.0)/Scale) * '+vec3ToString(t.axis)+';\n';
}
else if(t.type == 'rotate'){
//t.axis and t.theta should be defined
return 'p = rotate(p, ' + vec3ToString(t.axis) +', '+str(t.theta) + ');\n';
}else if(t.type == 'reflect_tetra_1'){
const ans =
'if(p.x+p.y<0.0) p.xy = -p.yx; // fold 1\n'+
'if(p.x+p.z<0.0) p.zx = -p.xz; // fold 2\n'+
'if(p.y+p.z<0.0) p.yz = -p.zy; // fold 3\n';
return ans;
}else if(t.type == 'reflect_tetra_2'){
const ans =
'if(p.x-p.y<0.0) p.xy = p.yx; // fold 1\n'+
'if(p.x-p.z<0.0) p.xz = p.zx; // fold 2\n'+
'if(p.y-p.z<0.0) p.yz = p.zy; // fold 3\n';
return ans;
}else if(t.type == 'reflect_square'){
return 'p = abs(p);\n';
}else if(t.type == 'reflect_octa'){
const ans =
'if(p.x-p.y<0.0)p.xy = p.yx;\n'+
'if(p.x+p.y<0.0)p.xy =-p.yx;\n'+
'if(p.x-p.z<0.0)p.xz = p.zx;\n'+
'if(p.x+p.z<0.0)p.xz =-p.zx;\n';
return ans;
}else if(t.type == 'scale'){
return 'p = Scale*p - Offset*(Scale-1.0);rescale+=1;\n';
}else{ // custom
//source should be defined
return '\n'+t.source+'\n';
}
}
const vec3ToString = function(v){
return 'vec3('+str(v[0])+', '+str(v[1])+', '+str(v[2])+')';
}
const str = (f)=>{return f.toFixed(2).toString();}