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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
use rayon::prelude::*;
use rfw_math::*;
use rtbvh::*;
use std::{fmt::Debug, write};
#[derive(Debug, Copy, Clone)]
pub struct SkinData<'a> {
pub name: &'a str,
pub inverse_bind_matrices: &'a [Mat4],
pub joint_matrices: &'a [Mat4],
}
#[derive(Debug, Copy, Clone)]
pub struct InstancesData2D<'a> {
pub matrices: &'a [Mat4],
}
impl InstancesData2D<'_> {
pub fn len(&self) -> usize {
self.matrices.len()
}
pub fn is_empty(&self) -> bool {
self.matrices.is_empty()
}
}
bitflags::bitflags! {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(transparent)]
pub struct InstanceFlags3D: u32 {
const TRANSFORMED = 1;
}
}
impl Default for InstanceFlags3D {
fn default() -> Self {
Self::all()
}
}
#[derive(Debug, Copy, Clone)]
pub struct InstancesData3D<'a> {
pub matrices: &'a [Mat4],
pub skin_ids: &'a [SkinID],
pub flags: &'a [InstanceFlags3D],
pub local_aabb: Aabb,
}
impl InstancesData3D<'_> {
pub fn len(&self) -> usize {
debug_assert_eq!(self.matrices.len(), self.skin_ids.len());
self.matrices.len()
}
pub fn is_empty(&self) -> bool {
self.matrices.is_empty()
}
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct MeshId2D(pub i32);
impl Default for MeshId2D {
fn default() -> Self {
Self::INVALID
}
}
impl std::fmt::Display for MeshId2D {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "MeshId3D({})", self.0)
}
}
impl MeshId2D {
pub const INVALID: Self = MeshId2D(-1);
pub fn is_valid(&self) -> bool {
self.0 >= 0
}
pub fn as_index(&self) -> Option<usize> {
if self.0 >= 0 {
Some(self.0 as usize)
} else {
None
}
}
}
impl From<MeshId2D> for usize {
fn from(val: MeshId2D) -> Self {
val.0 as usize
}
}
impl From<usize> for MeshId2D {
fn from(i: usize) -> Self {
MeshId2D(i as i32)
}
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct MeshId3D(pub i32);
impl Default for MeshId3D {
fn default() -> Self {
Self::INVALID
}
}
impl std::fmt::Display for MeshId3D {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "MeshId3D({})", self.0)
}
}
impl MeshId3D {
pub const INVALID: Self = MeshId3D(-1);
pub fn is_valid(&self) -> bool {
self.0 >= 0
}
pub fn as_index(&self) -> Option<usize> {
if self.0 >= 0 {
Some(self.0 as usize)
} else {
None
}
}
}
impl From<MeshId3D> for usize {
fn from(val: MeshId3D) -> Self {
val.0 as usize
}
}
impl From<usize> for MeshId3D {
fn from(i: usize) -> Self {
MeshId3D(i as i32)
}
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct SkinID(pub i32);
impl Default for SkinID {
fn default() -> Self {
Self::INVALID
}
}
impl std::fmt::Display for SkinID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl SkinID {
pub const INVALID: Self = SkinID(-1);
pub fn is_valid(&self) -> bool {
self.0 >= 0
}
pub fn as_index(&self) -> Option<usize> {
if self.0 >= 0 {
Some(self.0 as usize)
} else {
None
}
}
}
impl From<usize> for SkinID {
fn from(i: usize) -> Self {
SkinID(i as i32)
}
}
impl From<SkinID> for usize {
fn from(val: SkinID) -> Self {
val.0 as usize
}
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
#[repr(C)]
pub enum DataFormat {
BGRA8 = 0,
RGBA8 = 1,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct TextureData<'a> {
pub width: u32,
pub height: u32,
pub mip_levels: u32,
pub bytes: &'a [u8],
pub format: DataFormat,
}
impl TextureData<'_> {
pub fn offset_for_level(&self, mip_level: usize) -> usize {
assert!(mip_level <= self.mip_levels as usize);
let mut offset = 0;
for i in 0..mip_level {
let (w, h) = self.mip_level_width_height(i);
offset += w * h;
}
offset
}
pub fn mip_level_width(&self, mip_level: usize) -> usize {
let mut w = self.width as usize;
for _ in 0..mip_level {
w >>= 1;
}
w
}
pub fn mip_level_height(&self, mip_level: usize) -> usize {
let mut h = self.height as usize;
for _ in 0..mip_level {
h >>= 1;
}
h
}
pub fn mip_level_width_height(&self, mip_level: usize) -> (usize, usize) {
let mut w = self.width as usize;
let mut h = self.height as usize;
if mip_level == 0 {
return (w, h);
}
for _ in 0..mip_level {
w >>= 1;
h >>= 1
}
(w, h)
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default, PartialEq)]
#[repr(C)]
pub struct Vertex3D {
pub vertex: Vec4,
pub normal: Vec3,
pub mat_id: u32,
pub uv: Vec2,
pub pad0: f32,
pub pad1: f32,
pub tangent: Vec4,
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default, PartialEq)]
#[repr(C)]
pub struct JointData {
pub joint: [u32; 4],
pub weight: Vec4,
}
impl From<JointData> for ([u32; 4], Vec4) {
fn from(val: JointData) -> Self {
(val.joint, val.weight)
}
}
impl<T: Into<[f32; 4]>> From<([u32; 4], T)> for JointData {
fn from(data: ([u32; 4], T)) -> Self {
Self {
joint: data.0,
weight: Vec4::from(data.1.into()),
}
}
}
impl<T: Into<[f32; 4]>> From<([u16; 4], T)> for JointData {
fn from(data: ([u16; 4], T)) -> Self {
Self {
joint: [
data.0[0] as u32,
data.0[1] as u32,
data.0[2] as u32,
data.0[3] as u32,
],
weight: Vec4::from(data.1.into()),
}
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct VertexMesh {
pub bounds: Aabb,
pub first: u32,
pub last: u32,
pub mat_id: u32,
pub padding: u32,
}
bitflags::bitflags! {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[repr(transparent)]
pub struct Mesh3dFlags: u32 {
const SHADOW_CASTER = 1;
const ALLOW_SKINNING = 2;
}
}
impl Default for Mesh3dFlags {
fn default() -> Self {
Self::SHADOW_CASTER | Self::ALLOW_SKINNING
}
}
#[derive(Debug, Clone)]
pub struct MeshData3D<'a> {
pub name: &'a str,
pub bounds: Aabb,
pub vertices: &'a [Vertex3D],
pub triangles: &'a [RTTriangle],
pub ranges: &'a [VertexMesh],
pub skin_data: &'a [JointData],
pub flags: Mesh3dFlags,
}
impl<'a> MeshData3D<'a> {
pub fn apply_skin_vertices(&self, joint_matrices: &[Mat4]) -> SkinnedMesh3D {
SkinnedMesh3D::apply(self.vertices, self.skin_data, self.ranges, joint_matrices)
}
pub fn apply_skin_triangles(&self, joint_matrices: &[Mat4]) -> SkinnedTriangles3D {
SkinnedTriangles3D::apply(self.triangles, self.skin_data, joint_matrices)
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default, PartialEq)]
#[repr(C)]
pub struct Vertex2D {
pub vertex: [f32; 3],
pub tex: u32,
pub uv: [f32; 2],
pub color: [f32; 4],
}
#[derive(Debug, Clone)]
pub struct MeshData2D<'a> {
pub vertices: &'a [Vertex2D],
pub tex_id: Option<usize>,
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
#[repr(C)]
pub struct DeviceMaterial {
pub color: [f32; 4],
pub absorption: [f32; 4],
pub specular: [f32; 4],
pub parameters: [u32; 4],
pub flags: u32,
pub diffuse_map: i32,
pub normal_map: i32,
pub metallic_roughness_map: i32,
pub emissive_map: i32,
pub sheen_map: i32,
pub _dummy: [i32; 2],
}
impl Default for DeviceMaterial {
fn default() -> Self {
Self {
color: [0.0; 4],
absorption: [0.0; 4],
specular: [0.0; 4],
parameters: [0; 4],
flags: 0,
diffuse_map: -1,
normal_map: -1,
metallic_roughness_map: -1,
emissive_map: -1,
sheen_map: -1,
_dummy: [0; 2],
}
}
}
impl DeviceMaterial {
pub fn get_metallic(&self) -> f32 {
(self.parameters[0] & 255) as f32 * 1.0 / 255.0
}
pub fn get_subsurface(&self) -> f32 {
((self.parameters[0].overflowing_shr(8)).0 & 255) as f32 * 1.0 / 255.0
}
pub fn get_specular_f(&self) -> f32 {
((self.parameters[0].overflowing_shr(16)).0 & 255) as f32 * 1.0 / 255.0
}
pub fn get_roughness(&self) -> f32 {
((self.parameters[0].overflowing_shr(24)).0 & 255) as f32 * 1.0 / 255.0
}
pub fn get_specular_tint(&self) -> f32 {
(self.parameters[1] & 255) as f32 * 1.0 / 255.0
}
pub fn get_anisotropic(&self) -> f32 {
((self.parameters[1].overflowing_shr(8)).0 & 255) as f32 * 1.0 / 255.0
}
pub fn get_sheen(&self) -> f32 {
((self.parameters[1].overflowing_shr(16)).0 & 255) as f32 * 1.0 / 255.0
}
pub fn get_sheen_tint(&self) -> f32 {
((self.parameters[1].overflowing_shr(24)).0 & 255) as f32 * 1.0 / 255.0
}
pub fn get_clearcoat(&self) -> f32 {
(self.parameters[2] & 255) as f32 * 1.0 / 255.0
}
pub fn get_clearcoat_gloss(&self) -> f32 {
((self.parameters[2].overflowing_shr(8)).0 & 255) as f32 * 1.0 / 255.0
}
pub fn get_transmission(&self) -> f32 {
((self.parameters[2].overflowing_shr(8)).0 & 255) as f32 * 1.0 / 255.0
}
pub fn get_eta(&self) -> f32 {
((self.parameters[2].overflowing_shr(24)).0 & 255) as f32 * 1.0 / 255.0
}
pub fn get_custom0(&self) -> f32 {
(self.parameters[3] & 255) as f32 * 1.0 / 255.0
}
pub fn get_custom1(&self) -> f32 {
((self.parameters[3].overflowing_shr(8)).0 & 255) as f32 * 1.0 / 255.0
}
pub fn get_custom2(&self) -> f32 {
((self.parameters[3].overflowing_shr(8)).0 & 255) as f32 * 1.0 / 255.0
}
pub fn get_custom3(&self) -> f32 {
((self.parameters[3].overflowing_shr(24)).0 & 255) as f32 * 1.0 / 255.0
}
}
#[derive(Default, Debug, Copy, Clone)]
pub struct CameraView2D {
pub matrix: Mat4,
}
#[derive(Default, Debug, Copy, Clone)]
#[repr(C)]
pub struct CameraView3D {
pub pos: Vec3,
pub right: Vec3,
pub up: Vec3,
pub p1: Vec3,
pub direction: Vec3,
pub lens_size: f32,
pub spread_angle: f32,
pub epsilon: f32,
pub inv_width: f32,
pub inv_height: f32,
pub near_plane: f32,
pub far_plane: f32,
pub aspect_ratio: f32,
pub fov: f32,
pub custom0: Vec4,
pub custom1: Vec4,
}
#[allow(dead_code)]
impl CameraView3D {
pub fn generate_lens_ray(&self, x: u32, y: u32, r0: f32, r1: f32, r2: f32, r3: f32) -> Ray {
let blade = (r0 * 9.0).round();
let r2 = (r2 - blade * (1.0 / 9.0)) * 9.0;
let pi_over_4dot5 = std::f32::consts::PI / 4.5;
let blade_param = blade * pi_over_4dot5;
let (x1, y1) = blade_param.sin_cos();
let blade_param = (blade + 1.0) * pi_over_4dot5;
let (x2, y2) = blade_param.sin_cos();
let (r2, r3) = {
if (r2 + r3) > 1.0 {
(1.0 - r2, 1.0 - r3)
} else {
(r2, r3)
}
};
let xr = x1 * r2 + x2 * r3;
let yr = y1 * r2 + y2 * r3;
let origin = self.pos + self.lens_size * (self.right * xr + self.up * yr);
let u = (x as f32 + r0) * self.inv_width;
let v = (y as f32 + r1) * self.inv_height;
let point_on_pixel = self.p1 + u * self.right + v * self.up;
let direction = (point_on_pixel - origin).normalize();
Ray::new(origin, direction)
}
pub fn generate_ray(&self, x: u32, y: u32) -> Ray {
let u = x as f32 * self.inv_width;
let v = y as f32 * self.inv_height;
let point_on_pixel = self.p1 + u * self.right + v * self.up;
let direction = (point_on_pixel - self.pos).normalize();
Ray::new(self.pos, direction)
}
pub fn generate_lens_ray4(
&self,
x: [u32; 4],
y: [u32; 4],
r0: [f32; 4],
r1: [f32; 4],
r2: [f32; 4],
r3: [f32; 4],
) -> RayPacket4 {
let r0 = Vec4::from(r0);
let r1 = Vec4::from(r1);
let r2 = Vec4::from(r2);
let r3 = Vec4::from(r3);
let blade: Vec4 = r0 * Vec4::splat(9.0);
let r2: Vec4 = (r2 - blade * (1.0 / 9.0)) * 9.0;
let pi_over_4dot5: Vec4 = Vec4::splat(std::f32::consts::PI / 4.5);
let blade_param: Vec4 = blade * pi_over_4dot5;
let (x1, y1) = {
let mut x = [0.0_f32; 4];
let mut y = [0.0_f32; 4];
for i in 0..4 {
let (cos, sin) = blade_param[i].sin_cos();
x[i] = cos;
y[i] = sin;
}
(Vec4::from(x), Vec4::from(y))
};
let blade_param = (blade + Vec4::ONE) * pi_over_4dot5;
let (x2, y2) = {
let mut x = [0.0_f32; 4];
let mut y = [0.0_f32; 4];
for i in 0..4 {
let (cos, sin) = blade_param[i].sin_cos();
x[i] = cos;
y[i] = sin;
}
(Vec4::from(x), Vec4::from(y))
};
let (r2, r3) = {
let mask: BVec4A = (r2 + r3).cmpgt(Vec4::ONE);
(
Vec4::select(mask, Vec4::ONE - r2, r2),
Vec4::select(mask, Vec4::ONE - r3, r3),
)
};
let x = UVec4::from(x).as_f32();
let y = UVec4::from(y).as_f32();
let xr = x1 * r2 + x2 * r2;
let yr = y1 * r2 + y2 * r3;
let u = (x + r0) * self.inv_width;
let v = (y + r1) * self.inv_height;
let p_x = Vec4::from([self.p1[0]; 4]) + u * self.p1[0] + v * self.up[0];
let p_y = Vec4::from([self.p1[1]; 4]) + u * self.p1[1] + v * self.up[1];
let p_z = Vec4::from([self.p1[2]; 4]) + u * self.p1[2] + v * self.up[2];
let direction_x = p_x - Vec4::from([self.pos[0]; 4]);
let direction_y = p_y - Vec4::from([self.pos[1]; 4]);
let direction_z = p_z - Vec4::from([self.pos[2]; 4]);
let length_squared = direction_x * direction_x;
let length_squared = length_squared + direction_y * direction_y;
let length_squared = length_squared + direction_z * direction_z;
let length = vec4_sqrt(length_squared);
let inv_length = Vec4::ONE / length;
let direction_x = direction_x * inv_length;
let direction_y = direction_y * inv_length;
let direction_z = direction_z * inv_length;
let origin_x = Vec4::splat(self.pos[0]);
let origin_y = Vec4::splat(self.pos[1]);
let origin_z = Vec4::splat(self.pos[2]);
let lens_size = Vec4::splat(self.lens_size);
let right_x = Vec4::splat(self.p1[0]);
let right_y = Vec4::splat(self.p1[1]);
let right_z = Vec4::splat(self.p1[2]);
let up_x = Vec4::splat(self.up[0]);
let up_y = Vec4::splat(self.up[1]);
let up_z = Vec4::splat(self.up[2]);
let origin_x = origin_x + lens_size * (right_x * xr + up_x * yr);
let origin_y = origin_y + lens_size * (right_y * xr + up_y * yr);
let origin_z = origin_z + lens_size * (right_z * xr + up_z * yr);
RayPacket4 {
origin_x,
origin_y,
origin_z,
direction_x,
direction_y,
direction_z,
t: [1e34_f32; 4].into(),
inv_direction_x: 1.0 / direction_x,
inv_direction_y: 1.0 / direction_y,
inv_direction_z: 1.0 / direction_z,
}
}
pub fn generate_ray4(&self, x: [u32; 4], y: [u32; 4]) -> RayPacket4 {
let x = UVec4::from(x).as_f32();
let y = UVec4::from(y).as_f32();
let u = x * self.inv_width;
let v = y * self.inv_height;
let p_x = Vec4::from([self.p1[0]; 4]) + u * self.p1[0] + v * self.up[0];
let p_y = Vec4::from([self.p1[1]; 4]) + u * self.p1[1] + v * self.up[1];
let p_z = Vec4::from([self.p1[2]; 4]) + u * self.p1[2] + v * self.up[2];
let direction_x = p_x - Vec4::from([self.pos[0]; 4]);
let direction_y = p_y - Vec4::from([self.pos[1]; 4]);
let direction_z = p_z - Vec4::from([self.pos[2]; 4]);
let length_squared = direction_x * direction_x;
let length_squared = length_squared + direction_y * direction_y;
let length_squared = length_squared + direction_z * direction_z;
let length = vec4_sqrt(length_squared);
let inv_length = Vec4::ONE / length;
let direction_x = direction_x * inv_length;
let direction_y = direction_y * inv_length;
let direction_z = direction_z * inv_length;
let origin_x = [self.pos[0]; 4].into();
let origin_y = [self.pos[1]; 4].into();
let origin_z = [self.pos[2]; 4].into();
RayPacket4 {
origin_x,
origin_y,
origin_z,
direction_x,
direction_y,
direction_z,
t: [1e34_f32; 4].into(),
inv_direction_x: 1.0 / direction_x,
inv_direction_y: 1.0 / direction_y,
inv_direction_z: 1.0 / direction_z,
}
}
fn calculate_matrix(&self) -> (Vec3, Vec3, Vec3) {
let y: Vec3 = Vec3::new(0.0, 1.0, 0.0);
let z: Vec3 = self.direction.normalize();
let x: Vec3 = z.cross(y).normalize();
let y: Vec3 = x.cross(z).normalize();
(x, y, z)
}
pub fn get_rh_matrix(&self) -> Mat4 {
let up = Vec3::new(0.0, 1.0, 0.0);
let projection =
Mat4::perspective_rh_gl(self.fov, self.aspect_ratio, self.near_plane, self.far_plane);
let pos = self.pos;
let dir = self.direction;
let view = Mat4::look_at_rh(pos, pos + dir, up);
projection * view
}
pub fn get_lh_matrix(&self) -> Mat4 {
let up = Vec3::new(0.0, 1.0, 0.0);
let projection =
Mat4::perspective_lh(self.fov, self.aspect_ratio, self.near_plane, self.far_plane);
let pos = self.pos;
let dir = self.direction;
let view = Mat4::look_at_lh(pos, pos + dir, up);
projection * view
}
pub fn get_rh_projection(&self) -> Mat4 {
Mat4::perspective_rh_gl(self.fov, self.aspect_ratio, self.near_plane, self.far_plane)
}
pub fn get_lh_projection(&self) -> Mat4 {
Mat4::perspective_lh(self.fov, self.aspect_ratio, self.near_plane, self.far_plane)
}
pub fn get_rh_view_matrix(&self) -> Mat4 {
let up = Vec3::new(0.0, 1.0, 0.0);
let pos = self.pos;
let dir = self.direction;
Mat4::look_at_rh(pos, pos + dir, up)
}
pub fn get_lh_view_matrix(&self) -> Mat4 {
let up = Vec3::new(0.0, 1.0, 0.0);
let pos = self.pos;
let dir = self.direction;
Mat4::look_at_lh(pos, pos + dir, up)
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Default)]
pub struct SkinnedMesh3D {
pub vertices: Vec<Vertex3D>,
pub ranges: Vec<VertexMesh>,
}
impl SkinnedMesh3D {
pub fn apply(
vertices: &[Vertex3D],
skin_data: &[JointData],
ranges: &[VertexMesh],
joint_matrices: &[Mat4],
) -> Self {
let mut vertices = vertices.to_vec();
let ranges = ranges.to_vec();
vertices.par_iter_mut().enumerate().for_each(|(i, v)| {
let (joint, weight) = skin_data[i].into();
let matrix = weight[0] * joint_matrices[joint[0] as usize];
let matrix = matrix + (weight[1] * joint_matrices[joint[1] as usize]);
let matrix = matrix + (weight[2] * joint_matrices[joint[2] as usize]);
let matrix = matrix + (weight[3] * joint_matrices[joint[3] as usize]);
v.vertex = matrix * v.vertex;
let matrix = matrix.inverse().transpose();
v.normal = (matrix * Vec3A::from(v.normal).extend(0.0)).truncate();
let tangent =
(matrix * Vec3A::new(v.tangent[0], v.tangent[1], v.tangent[2]).extend(0.0)).xyz();
v.tangent = Vec4::new(tangent[0], tangent[1], tangent[2], v.tangent[3]);
});
SkinnedMesh3D { vertices, ranges }
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Default)]
pub struct SkinnedTriangles3D {
pub triangles: Vec<RTTriangle>,
}
impl SkinnedTriangles3D {
pub fn apply(
triangles: &[RTTriangle],
skin_data: &[JointData],
joint_matrices: &[Mat4],
) -> Self {
let mut triangles = triangles.to_vec();
triangles.iter_mut().enumerate().for_each(|(i, t)| {
let i0 = i / 3;
let i1 = i + 1;
let i2 = i + 2;
let (joint, weight) = skin_data[i0].into();
let matrix: Mat4 = weight[0] * joint_matrices[joint[0] as usize];
let matrix: Mat4 = matrix + (weight[1] * joint_matrices[joint[1] as usize]);
let matrix: Mat4 = matrix + (weight[2] * joint_matrices[joint[2] as usize]);
let matrix: Mat4 = matrix + (weight[3] * joint_matrices[joint[3] as usize]);
let n_matrix: Mat4 = matrix.inverse().transpose();
t.vertex0 = (matrix * t.vertex0.extend(1.0)).truncate();
t.n0 = (n_matrix * t.n0.extend(0.0)).truncate();
t.tangent0 = (n_matrix * t.tangent0.xyz().extend(0.0))
.truncate()
.extend(t.tangent2[3]);
let (joint, weight) = skin_data[i1].into();
let matrix: Mat4 = weight[0] * joint_matrices[joint[0] as usize];
let matrix: Mat4 = matrix + (weight[1] * joint_matrices[joint[1] as usize]);
let matrix: Mat4 = matrix + (weight[2] * joint_matrices[joint[2] as usize]);
let matrix: Mat4 = matrix + (weight[3] * joint_matrices[joint[3] as usize]);
let n_matrix: Mat4 = matrix.inverse().transpose();
t.vertex1 = (matrix * t.vertex1.extend(1.0)).truncate();
t.n1 = (n_matrix * t.n1.extend(0.0)).truncate();
t.tangent1 = (n_matrix * t.tangent1.xyz().extend(0.0))
.truncate()
.extend(t.tangent2[3]);
let (joint, weight) = skin_data[i2].into();
let matrix: Mat4 = weight[0] * joint_matrices[joint[0] as usize];
let matrix: Mat4 = matrix + (weight[1] * joint_matrices[joint[1] as usize]);
let matrix: Mat4 = matrix + (weight[2] * joint_matrices[joint[2] as usize]);
let matrix: Mat4 = matrix + (weight[3] * joint_matrices[joint[3] as usize]);
let n_matrix: Mat4 = matrix.inverse().transpose();
t.vertex2 = (matrix * t.vertex2.extend(1.0)).truncate();
t.n2 = (n_matrix * t.n2.extend(0.0)).truncate();
t.tangent2 = (n_matrix * t.tangent2.xyz().extend(0.0))
.truncate()
.extend(t.tangent2[3]);
t.normal = RTTriangle::normal(t.vertex0, t.vertex1, t.vertex2);
});
SkinnedTriangles3D { triangles }
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct RTTriangle {
pub vertex0: Vec3,
pub u0: f32,
pub vertex1: Vec3,
pub u1: f32,
pub vertex2: Vec3,
pub u2: f32,
pub normal: Vec3,
pub v0: f32,
pub n0: Vec3,
pub v1: f32,
pub n1: Vec3,
pub v2: f32,
pub n2: Vec3,
pub id: i32,
pub tangent0: Vec4,
pub tangent1: Vec4,
pub tangent2: Vec4,
pub light_id: i32,
pub mat_id: i32,
pub lod: f32,
pub area: f32,
}
impl Default for RTTriangle {
fn default() -> Self {
Self {
vertex0: Vec3::ZERO,
u0: 0.0,
vertex1: Vec3::ZERO,
u1: 0.0,
vertex2: Vec3::ZERO,
u2: 0.0,
normal: Vec3::ZERO,
v0: 0.0,
n0: Vec3::ZERO,
v1: 0.0,
n1: Vec3::ZERO,
v2: 0.0,
n2: Vec3::ZERO,
id: 0,
tangent0: Vec4::ZERO,
tangent1: Vec4::ZERO,
tangent2: Vec4::ZERO,
light_id: 0,
mat_id: 0,
lod: 0.0,
area: 0.0,
}
}
}
impl SpatialTriangle for RTTriangle {
fn vertex0(&self) -> Vec3 {
self.vertex0
}
fn vertex1(&self) -> Vec3 {
self.vertex1
}
fn vertex2(&self) -> Vec3 {
self.vertex2
}
}
#[allow(dead_code)]
impl RTTriangle {
pub fn vertices(&self) -> (Vec3, Vec3, Vec3) {
(self.vertex0, self.vertex1, self.vertex2)
}
#[inline]
pub fn normal(v0: Vec3, v1: Vec3, v2: Vec3) -> Vec3 {
let a = v1 - v0;
let b = v2 - v0;
a.cross(b).normalize()
}
#[inline]
pub fn area(v0: Vec3, v1: Vec3, v2: Vec3) -> f32 {
let a = (v1 - v0).length();
let b = (v2 - v1).length();
let c = (v0 - v2).length();
let s = (a + b + c) * 0.5;
(s * (s - a) * (s - b) * (s - c)).sqrt()
}
#[inline]
pub fn center(&self) -> Vec3 {
let (v0, v1, v2) = self.vertices();
(v0 + v1 + v2) * (1.0 / 3.0)
}
#[inline(always)]
pub fn bary_centrics(
v0: Vec3,
v1: Vec3,
v2: Vec3,
edge1: Vec3,
edge2: Vec3,
p: Vec3,
n: Vec3,
) -> (f32, f32) {
let abc = n.dot((edge1).cross(edge2));
let pbc = n.dot((v1 - p).cross(v2 - p));
let pca = n.dot((v2 - p).cross(v0 - p));
(pbc / abc, pca / abc)
}
pub fn transform(&self, matrix: Mat4, normal_matrix: Mat3) -> RTTriangle {
let vertex0 = self.vertex0.extend(1.0);
let vertex1 = self.vertex1.extend(1.0);
let vertex2 = self.vertex2.extend(1.0);
let vertex0 = matrix * vertex0;
let vertex1 = matrix * vertex1;
let vertex2 = matrix * vertex2;
let n0 = normal_matrix * self.n0;
let n1 = normal_matrix * self.n1;
let n2 = normal_matrix * self.n2;
RTTriangle {
vertex0: vertex0.truncate(),
vertex1: vertex1.truncate(),
vertex2: vertex2.truncate(),
n0,
n1,
n2,
..(*self)
}
}
}