-
Notifications
You must be signed in to change notification settings - Fork 0
/
Object3D.cs
244 lines (205 loc) · 6.65 KB
/
Object3D.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace SharpRayTracer
{
public abstract class Object3D
{
public Material material;
public virtual void Intersect(Ray ray, double tmin, ref Hit hit)
{
}
}
public class Sphere : Object3D
{
public double radius;
public Vector4 center = new Vector4();
public Sphere(Vector4 center, double radius, Material material)
{
this.center = center;
this.radius = radius;
this.material = material;
}
public Sphere(dynamic d)
{
center = new Vector4((JArray)d.center);
radius = d.radius;
material = MaterialList.Materials[(int)d.material];
}
public override void Intersect(Ray ray, double tmin, ref Hit hit)
{
Vector4 l = center - ray.origin;
double tca = l.Dot(ray.direction);
double d2 = l.Dot(l) - tca * tca;
if (d2 > radius * radius) return;
double thc = Math.Sqrt(radius * radius - d2);
double t0 = tca - thc;
double t1 = tca + thc;
double thold = t0;
if (t0 > t1)
{
t0 = t1;
t1 = thold;
}
if (t0 < 0)
{
t0 = t1;
if (t0 < 0) return;
}
if (t0 > tmin && hit.t > t0)
{
hit.normal = (ray.origin + ray.direction * t0 - center).Normalized;
hit.material = material;
hit.t = t0;
hit.isHitObject = true;
}
}
}
public class ObjectGroup : Object3D
{
public List<Object3D> childs = new List<Object3D>();
public ObjectGroup(dynamic g)
{
childs = new List<Object3D>();
foreach (JObject item in g)
{
var parentProp = (JProperty)(item.First!);
string name = parentProp!.Name;
switch (name)
{
case "sphere":
childs.Add(new Sphere(item["sphere"]!));
break;
case "plane":
childs.Add(new Plane(item["plane"]!));
break;
case "triangle":
childs.Add(new Triangle(item["triangle"]!));
break;
case "transform":
childs.Add(new Transformation(item["transform"]!));
break;
default:
break;
}
}
}
public override void Intersect(Ray ray, double tmin, ref Hit hit)
{
foreach (var obj in childs)
{
obj.Intersect(ray, tmin, ref hit);
}
}
}
public class Triangle : Object3D
{
public Vector4 v1;
public Vector4 v2;
public Vector4 v3;
public override void Intersect(Ray ray, double tmin, ref Hit hit)
{
//Find triangle normal
var a = v2 - v1;
var b = v3 - v1;
var N = a.Cross(b);
//Check paralelism
double nDotDir = N.Dot(ray.direction);
if (Math.Abs(nDotDir) < 0.0000001)
{
return;
}
var d = -N.Dot(v1);
var t = -(N.Dot(ray.origin) + d) / nDotDir;
if (t < 0) return;//Behind ray
Vector4 P = ray.origin + t * ray.direction;//Intersection Point
Vector4 C;
//Test for v1
var edge1 = v2 - v1;
var vp1 = P - v1;
C = edge1.Cross(vp1);
if (N.Dot(C) < 0) return; // P is on the right side
//Test for v2
var edge2 = v3 - v2;
var vp2 = P - v2;
C = edge2.Cross(vp2);
if (N.Dot(C) < 0) return; // P is on the right side
//Test for v3
var edge3 = v1 - v3;
var vp3 = P - v3;
C = edge3.Cross(vp3);
if (N.Dot(C) < 0) return; // P is on the right side
if (t > tmin && t < hit.t)
{
hit.t = t;
hit.material = material;
hit.normal = N;
hit.isHitObject = true;
}
}
public Triangle(dynamic d)
{
v1 = new Vector4((JArray)d.v1);
v2 = new Vector4((JArray)d.v2);
v3 = new Vector4((JArray)d.v3);
material = MaterialList.Materials[(int)d.material];
}
}
public class Plane : Object3D
{
Vector4 normal;
double d;//Offset from origin
public override void Intersect(Ray ray, double tmin, ref Hit hit)
{
var t = -(-d + ray.origin.Dot(normal)) / ray.direction.Dot(normal);
if (t > tmin && t < hit.t)
{
hit.t = t;
hit.material = material;
hit.normal = normal;
hit.isHitObject = true;
}
}
public Plane(dynamic d)
{
normal = new Vector4(d.normal);
this.d = d.offset;
material = MaterialList.Materials[(int)d.material];
}
}
public class Transformation : Object3D
{
public Matrix4x4 m;
public Matrix4x4 mInverse;
Object3D obj;
public override void Intersect(Ray ray, double tmin, ref Hit hit)
{
ray.origin = mInverse * ray.origin;
ray.direction = mInverse * ray.direction;
obj.Intersect(ray, tmin, ref hit);
}
public Transformation(dynamic d)
{
m = Matrix4x4.Scale(1, 1, 1);//Identity
if (d["object"].sphere != null) obj = new Sphere(d["object"].sphere);
else if (d["object"].plane != null) obj = new Plane(d["object"].plane);
else if (d["object"].triangle != null) obj = new Triangle(d["object"].triangle);
foreach (var item in d.transformations)
{
if (item.scale != null)
{
double[] v = ((JArray)item.scale).ToObject<double[]>()!;
m = m * Matrix4x4.Scale(v[0], v[1], v[2]);
}else if(item.zrotate != null)
{
m = m * Matrix4x4.RotateZ((double)item.zrotate);
}
}
mInverse = m.Inverse();
}
}
}