rpfm_lib/files/rigidmodel/vertices/
mod.rs1use getset::{Getters, MutGetters, Setters};
72use nalgebra::{Vector2, Vector4};
73use serde::{Deserialize, Serialize};
74
75use crate::binary::{ReadBytes, WriteBytes};
76use crate::error::{Result, RLibError};
77
78use super::materials::MaterialType;
79
80#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize)]
89#[repr(u16)]
90pub enum VertexFormat {
91 #[default]
93 Static = 0,
94 Collision = 1,
96 Weighted = 3,
98 Cinematic = 4,
100 Grass = 5,
102 Tree = 6,
104 Billboard = 7,
106 Rope = 11,
108 Uk8 = 8,
110 Uk12 = 12,
112 ClothSim = 25,
114}
115
116#[derive(Clone, Debug, Default, PartialEq, Getters, MutGetters, Setters, Serialize, Deserialize)]
139#[getset(get = "pub", get_mut = "pub", set = "pub")]
140pub struct Vertex {
141
142 position: Vector4<f32>,
148
149 texture_coordinate: Vector2<f32>,
152
153 texture_coordinate2: Vector2<f32>,
159
160 normal: Vector4<f32>,
165
166 tangent: Vector4<f32>,
169
170 bitangent: Vector4<f32>,
173
174 color: Vector4<f32>,
176
177 bone_indices: Vector4<u8>,
181
182 weights: Vector4<f32>,
185
186 uk_1: Vector4<u8>,
191
192 position2: Vector4<f32>,
196
197 uk_2: Vector2<f32>,
199
200 tree_colour_wind: Vector4<f32>,
202
203 uk_4: f32,
205}
206
207impl TryFrom<u16> for VertexFormat {
212 type Error = RLibError;
213 fn try_from(value: u16) -> Result<Self> {
214 match value {
215 _ if value == Self::Static as u16 => Ok(Self::Static),
216 _ if value == Self::Collision as u16 => Ok(Self::Collision),
217 _ if value == Self::Weighted as u16 => Ok(Self::Weighted),
218 _ if value == Self::Cinematic as u16 => Ok(Self::Cinematic),
219 _ if value == Self::Grass as u16 => Ok(Self::Grass),
220 _ if value == Self::Tree as u16 => Ok(Self::Tree),
221 _ if value == Self::Billboard as u16 => Ok(Self::Billboard),
222 _ if value == Self::Rope as u16 => Ok(Self::Rope),
223 _ if value == Self::Uk8 as u16 => Ok(Self::Uk8),
224 _ if value == Self::Uk12 as u16 => Ok(Self::Uk12),
225 _ if value == Self::ClothSim as u16 => Ok(Self::ClothSim),
226 _ => Err(RLibError::DecodingRigidModelUnknownVertexFormat(value))
227 }
228 }
229}
230
231impl From<VertexFormat> for u16 {
232 fn from(value: VertexFormat) -> u16 {
233 value as u16
234 }
235}
236
237impl Vertex {
238
239 pub fn skinning_weights(&self, vtype: VertexFormat) -> Vector4<f32> {
249 match vtype {
250 VertexFormat::Weighted => Vector4::new(self.weights.x, 1.0 - self.weights.x, 0.0, 0.0),
251 _ => self.weights,
252 }
253 }
254
255 pub fn read<R: ReadBytes>(data: &mut R, version: u32, vtype: VertexFormat, mtype: MaterialType) -> Result<Self> {
275 let mut v = Self::default();
276 match vtype {
277 VertexFormat::Static => match static_layout(version, mtype) {
278 MaterialType::TiledDirtmap |
279 MaterialType::ShipAmbientmap |
280 MaterialType::Decal |
281 MaterialType::DecalDirtmap |
282 MaterialType::Dirtmap |
283 MaterialType::Cloth |
284 MaterialType::Unlit |
285 MaterialType::WaterDisplaceVolume |
286 MaterialType::Uk54 |
287 MaterialType::DefaultMaterial => {
288 v.position = data.read_vector_4_f32_normal_from_vector_4_f16()?;
289 v.texture_coordinate = data.read_vector_2_f32_from_vector_2_f16()?;
290 v.texture_coordinate2 = data.read_vector_2_f32_from_vector_2_f16()?;
291 v.normal = data.read_vector_4_f32_normal_from_vector_4_u8()?;
292 v.tangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
293 v.bitangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
294 v.uk_1 = data.read_vector_4_u8()?;
295 },
296 MaterialType::RsTerrain => {
299 v.position = data.read_vector_4_f32_from_vector_4_f16()?;
300 v.normal = data.read_vector_4_f32_from_vector_4_f16()?;
301 },
302 MaterialType::WeightedTextureBlend if version <= 6 => {
304 v.position = data.read_vector_4_f32_from_vector_4_f16()?;
305 v.normal = data.read_vector_4_f32_from_vector_4_f16()?;
306 },
307 MaterialType::WeightedTextureBlend => {
308 v.position = data.read_vector_4_f32_normal_from_vector_4_f16()?;
309 v.normal = data.read_vector_4_f32_normal_from_vector_4_f16()?;
310 },
311 MaterialType::ProjectedDecalV4 => {
313 v.position = data.read_vector_4_f32_from_vector_4_f16()?;
314 },
315 MaterialType::RsRiverDeprecated |
316 MaterialType::RsRiver => {
317 v.position = data.read_vector_4_f32()?;
318 v.normal = data.read_vector_4_f32()?;
319 },
320 MaterialType::ProjectedDecal |
321 MaterialType::ProjectedDecalV2 |
322 MaterialType::ProjectedDecalV3 => {
323 v.position = data.read_vector_4_f32()?;
324 },
325 MaterialType::StaticPointLight |
327 MaterialType::Uk56 |
328 MaterialType::Uk57 => {},
329 MaterialType::TextureCombo |
330 MaterialType::TextureComboVertexWind => {
331 v.position = data.read_vector_4_f32_normal_from_vector_4_f16()?;
332 v.texture_coordinate = data.read_vector_2_f32_from_vector_2_f16()?;
333 v.normal = data.read_vector_4_f32_normal_from_vector_4_u8()?;
334 v.tangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
335 v.bitangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
336 v.uk_1 = data.read_vector_4_u8()?;
337 },
338 MaterialType::Uk27 => {
339 v.position = data.read_vector_4_f32_from_vec_3_f32()?;
340 v.texture_coordinate = Vector2::new(data.read_f32()?, data.read_f32()?);
341 v.texture_coordinate2 = Vector2::new(data.read_f32()?, data.read_f32()?);
342 v.normal = data.read_vector_4_f32_normal_from_vector_4_u8()?;
343 v.tangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
344 v.bitangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
345 v.uk_1 = data.read_vector_4_u8()?;
346 },
347 MaterialType::DebugGeometry => {
348 v.position = data.read_vector_4_f32_normal_from_vector_4_f16()?;
349 v.normal = data.read_vector_4_f32_normal_from_vector_4_f16()?;
350 v.color = data.read_vector_4_f32_pct_from_vector_4_u8()?;
351 },
352 MaterialType::BowWave => {
353 v.position = data.read_vector_4_f32_normal_from_vector_4_f16()?;
354 v.position2 = data.read_vector_4_f32_normal_from_vector_4_f16()?;
355 v.texture_coordinate = data.read_vector_2_f32_from_vector_2_f16()?;
356 v.uk_4 = data.read_f32()?;
357 },
358 MaterialType::CustomTerrain if version <= 6 => {
360 v.position = data.read_vector_4_f32()?;
361 v.uk_1 = data.read_vector_4_u8()?;
362 v.normal = data.read_vector_4_f32_normal_from_vector_4_u8()?;
363 v.tangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
364 v.texture_coordinate = data.read_vector_2_f32_from_vector_2_f16()?;
365 v.texture_coordinate2 = data.read_vector_2_f32_from_vector_2_f16()?;
366 },
367 MaterialType::CustomTerrain => {
368 v.position = data.read_vector_4_f32()?;
369 v.uk_1 = data.read_vector_4_u8()?;
370 v.normal = data.read_vector_4_f32_normal_from_vector_4_u8()?;
371 v.tangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
372 v.texture_coordinate = Vector2::new(data.read_f32()?, data.read_f32()?);
373 },
374 MaterialType::TerrainBlend => {
375 v.position = data.read_vector_4_f32()?;
376 v.normal = data.read_vector_4_f32()?;
377 },
378 MaterialType::AlphaBlend => {
379 v.texture_coordinate = data.read_vector_2_f32_from_vector_2_f16()?;
380 v.texture_coordinate2 = data.read_vector_2_f32_from_vector_2_f16()?;
381 }
382 _ => return Err(RLibError::DecodingRigidModelUnsupportedVertexFormatForMaterial(vtype.into(), mtype.into()))
383 },
384
385 VertexFormat::Collision => {
386 v.position = data.read_vector_4_f32_from_vec_3_f32()?;
387 v.normal = data.read_vector_4_f32_from_vec_3_f32()?;
388 }
389
390 VertexFormat::Weighted => {
391 v.position = data.read_vector_4_f32_normal_from_vector_4_f16()?;
392
393 let bone_indices = data.read_vector_2_u8()?;
394 v.bone_indices = Vector4::new(bone_indices.x, bone_indices.y, 0, 0);
395
396 let weights = data.read_vector_2_f32_pct_from_vector_2_u8()?;
397 v.weights = Vector4::new(weights.x, weights.y, 0.0, 0.0);
398
399 v.normal = data.read_vector_4_f32_normal_from_vector_4_u8()?;
400 v.texture_coordinate = data.read_vector_2_f32_from_vector_2_f16()?;
401 v.tangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
402 v.bitangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
403
404 if version >= 8 {
405 v.uk_1 = data.read_vector_4_u8()?;
406 }
407 }
408 VertexFormat::Cinematic => {
409 v.position = data.read_vector_4_f32_normal_from_vector_4_f16()?;
410
411 v.bone_indices = data.read_vector_4_u8()?;
413 v.weights = data.read_vector_4_f32_pct_from_vector_4_u8()?;
414 v.normal = data.read_vector_4_f32_normal_from_vector_4_u8()?;
415 v.texture_coordinate = data.read_vector_2_f32_from_vector_2_f16()?;
416 v.tangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
417 v.bitangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
418
419 if version >= 8 {
420 v.uk_1 = data.read_vector_4_u8()?;
421 }
422 }
423 VertexFormat::Grass => {
424 v.position = data.read_vector_4_f32_normal_from_vector_4_f16()?;
425 v.texture_coordinate = Vector2::new(data.read_f32()?, data.read_f32()?);
426 v.normal = data.read_vector_4_f32_normal_from_vector_4_u8()?;
427 v.tangent = data.read_vector_4_f32_normal_from_vector_4_u8()?;
428 v.uk_1 = data.read_vector_4_u8()?;
429 }
430 VertexFormat::Tree => {
431 v.position = data.read_vector_4_f32_normal_from_vector_4_f16()?;
432 v.position2 = data.read_vector_4_f32_normal_from_vector_4_f16()?;
433 v.normal = data.read_vector_4_f32_normal_from_vector_4_f16()?;
434 v.tangent = data.read_vector_4_f32_normal_from_vector_4_f16()?;
435 v.bitangent = data.read_vector_4_f32_normal_from_vector_4_f16()?;
436 v.texture_coordinate = data.read_vector_2_f32_from_vector_2_f16()?;
437 v.texture_coordinate2 = data.read_vector_2_f32_from_vector_2_f16()?;
438 v.uk_2 = data.read_vector_2_f32_from_vector_2_f16()?;
439 v.tree_colour_wind = data.read_vector_4_f32_from_vector_4_f16()?;
440 }
441 VertexFormat::Billboard |
442 VertexFormat::Uk8 => {
443 v.position = data.read_vector_4_f32_normal_from_vector_4_f16()?;
444 v.texture_coordinate = data.read_vector_2_f32_from_vector_2_f16()?;
445 }
446 VertexFormat::Uk12 if version <= 6 => {
447 v.position = data.read_vector_4_f32_from_vector_4_f16()?;
448 v.normal = data.read_vector_4_f32_from_vector_4_f16()?;
449 }
450 VertexFormat::Uk12 => {
451 v.position = data.read_vector_4_f32_normal_from_vector_4_f16()?;
452 v.texture_coordinate = data.read_vector_2_f32_from_vector_2_f16()?;
453 v.texture_coordinate2 = data.read_vector_2_f32_from_vector_2_f16()?;
454 v.uk_1 = data.read_vector_4_u8()?;
455 }
456 _ => return Err(RLibError::DecodingRigidModelUnknownVertexFormat(vtype.into()))
457 }
458
459 Ok(v)
460 }
461
462 pub fn write<W: WriteBytes>(&self, data: &mut W, version: u32, vtype: VertexFormat, mtype: MaterialType) -> Result<()> {
482 match vtype {
483 VertexFormat::Static => match static_layout(version, mtype) {
484 MaterialType::TiledDirtmap |
485 MaterialType::ShipAmbientmap |
486 MaterialType::Decal |
487 MaterialType::DecalDirtmap |
488 MaterialType::Dirtmap |
489 MaterialType::Cloth |
490 MaterialType::Unlit |
491 MaterialType::WaterDisplaceVolume |
492 MaterialType::Uk54 |
493 MaterialType::DefaultMaterial => {
494 data.write_vector_4_f32_normal_as_vector_4_f16(self.position)?;
495 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate)?;
496 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate2)?;
497 data.write_vector_4_f32_normal_as_vector_4_u8(self.normal)?;
498 data.write_vector_4_f32_normal_as_vector_4_u8(self.tangent)?;
499 data.write_vector_4_f32_normal_as_vector_4_u8(self.bitangent)?;
500
501 data.write_vector_4_u8(self.uk_1)?;
503 }
504 MaterialType::RsTerrain => {
505 data.write_vector_4_f32_as_vector_4_f16(*self.position())?;
506 data.write_vector_4_f32_as_vector_4_f16(*self.normal())?;
507 }
508 MaterialType::WeightedTextureBlend if version <= 6 => {
509 data.write_vector_4_f32_as_vector_4_f16(*self.position())?;
510 data.write_vector_4_f32_as_vector_4_f16(*self.normal())?;
511 }
512 MaterialType::WeightedTextureBlend => {
513 data.write_vector_4_f32_normal_as_vector_4_f16(*self.position())?;
514 data.write_vector_4_f32_normal_as_vector_4_f16(*self.normal())?;
515 }
516 MaterialType::ProjectedDecalV4 => {
517 data.write_vector_4_f32_as_vector_4_f16(*self.position())?;
518 }
519 MaterialType::RsRiverDeprecated |
520 MaterialType::RsRiver => {
521 data.write_vector_4_f32(*self.position())?;
522 data.write_vector_4_f32(*self.normal())?;
523 }
524 MaterialType::ProjectedDecal |
525 MaterialType::ProjectedDecalV2 |
526 MaterialType::ProjectedDecalV3 => {
527 data.write_vector_4_f32(*self.position())?;
528 }
529 MaterialType::StaticPointLight |
530 MaterialType::Uk56 |
531 MaterialType::Uk57 => {}
532 MaterialType::TextureCombo |
533 MaterialType::TextureComboVertexWind => {
534 data.write_vector_4_f32_normal_as_vector_4_f16(self.position)?;
535 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate)?;
536 data.write_vector_4_f32_normal_as_vector_4_u8(self.normal)?;
537 data.write_vector_4_f32_normal_as_vector_4_u8(self.tangent)?;
538 data.write_vector_4_f32_normal_as_vector_4_u8(self.bitangent)?;
539 data.write_vector_4_u8(self.uk_1)?;
540 }
541 MaterialType::Uk27 => {
542 data.write_vector_4_f32_to_vector_3_f32(self.position)?;
543 data.write_f32(self.texture_coordinate.x)?;
544 data.write_f32(self.texture_coordinate.y)?;
545 data.write_f32(self.texture_coordinate2.x)?;
546 data.write_f32(self.texture_coordinate2.y)?;
547 data.write_vector_4_f32_normal_as_vector_4_u8(self.normal)?;
548 data.write_vector_4_f32_normal_as_vector_4_u8(self.tangent)?;
549 data.write_vector_4_f32_normal_as_vector_4_u8(self.bitangent)?;
550 data.write_vector_4_u8(self.uk_1)?;
551 }
552 MaterialType::DebugGeometry => {
553 data.write_vector_4_f32_normal_as_vector_4_f16(self.position)?;
554 data.write_vector_4_f32_normal_as_vector_4_f16(self.normal)?;
555 data.write_vector_4_f32_pct_as_vector_4_u8(self.color)?;
556 }
557 MaterialType::BowWave => {
558 data.write_vector_4_f32_normal_as_vector_4_f16(self.position)?;
559 data.write_vector_4_f32_normal_as_vector_4_f16(self.position2)?;
560 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate)?;
561 data.write_f32(self.uk_4)?;
562 }
563 MaterialType::CustomTerrain if version <= 6 => {
564 data.write_vector_4_f32(self.position)?;
565 data.write_vector_4_u8(self.uk_1)?;
566 data.write_vector_4_f32_normal_as_vector_4_u8(self.normal)?;
567 data.write_vector_4_f32_normal_as_vector_4_u8(self.tangent)?;
568 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate)?;
569 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate2)?;
570 }
571 MaterialType::CustomTerrain => {
572 data.write_vector_4_f32(self.position)?;
573 data.write_vector_4_u8(self.uk_1)?;
574 data.write_vector_4_f32_normal_as_vector_4_u8(self.normal)?;
575 data.write_vector_4_f32_normal_as_vector_4_u8(self.tangent)?;
576 data.write_f32(self.texture_coordinate.x)?;
577 data.write_f32(self.texture_coordinate.y)?;
578 }
579 MaterialType::TerrainBlend => {
580 data.write_vector_4_f32(*self.position())?;
581 data.write_vector_4_f32(*self.normal())?;
582 }
583 MaterialType::AlphaBlend => {
584 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate)?;
585 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate2)?;
586 }
587 _ => return Err(RLibError::DecodingRigidModelUnsupportedVertexFormatForMaterial(vtype.into(), mtype.into()))
588 },
589
590 VertexFormat::Collision => {
591 data.write_vector_4_f32_to_vector_3_f32(self.position)?;
592 data.write_vector_4_f32_to_vector_3_f32(self.normal)?;
593 }
594
595 VertexFormat::Weighted => {
596 data.write_vector_4_f32_normal_as_vector_4_f16(self.position)?;
597 data.write_vector_2_u8(Vector2::new(self.bone_indices.x, self.bone_indices.y))?;
598 data.write_vector_2_f32_pct_as_vector_2_u8(Vector2::new(self.weights.x, self.weights.y))?;
599 data.write_vector_4_f32_normal_as_vector_4_u8(self.normal)?;
600 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate)?;
601 data.write_vector_4_f32_normal_as_vector_4_u8(self.tangent)?;
602 data.write_vector_4_f32_normal_as_vector_4_u8(self.bitangent)?;
603
604 if version >= 8 {
605 data.write_vector_4_u8(self.uk_1)?;
606 }
607 }
608 VertexFormat::Cinematic => {
609 data.write_vector_4_f32_normal_as_vector_4_f16(self.position)?;
610 data.write_vector_4_u8(self.bone_indices)?;
611 data.write_vector_4_f32_pct_as_vector_4_u8(self.weights)?;
612 data.write_vector_4_f32_normal_as_vector_4_u8(self.normal)?;
613 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate)?;
614 data.write_vector_4_f32_normal_as_vector_4_u8(self.tangent)?;
615 data.write_vector_4_f32_normal_as_vector_4_u8(self.bitangent)?;
616
617 if version >= 8 {
618 data.write_vector_4_u8(self.uk_1)?;
619 }
620 }
621 VertexFormat::Grass => {
622 data.write_vector_4_f32_normal_as_vector_4_f16(self.position)?;
623 data.write_f32(self.texture_coordinate.x)?;
624 data.write_f32(self.texture_coordinate.y)?;
625 data.write_vector_4_f32_normal_as_vector_4_u8(self.normal)?;
626 data.write_vector_4_f32_normal_as_vector_4_u8(self.tangent)?;
627 data.write_vector_4_u8(self.uk_1)?;
628 }
629 VertexFormat::Tree => {
630 data.write_vector_4_f32_normal_as_vector_4_f16(self.position)?;
631 data.write_vector_4_f32_normal_as_vector_4_f16(self.position2)?;
632 data.write_vector_4_f32_normal_as_vector_4_f16(self.normal)?;
633 data.write_vector_4_f32_normal_as_vector_4_f16(self.tangent)?;
634 data.write_vector_4_f32_normal_as_vector_4_f16(self.bitangent)?;
635 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate)?;
636 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate2)?;
637 data.write_vector_2_f32_as_vector_2_f16(self.uk_2)?;
638 data.write_vector_4_f32_as_vector_4_f16(self.tree_colour_wind)?;
639 }
640 VertexFormat::Billboard |
641 VertexFormat::Uk8 => {
642 data.write_vector_4_f32_normal_as_vector_4_f16(*self.position())?;
643 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate)?;
644 }
645 VertexFormat::Uk12 if version <= 6 => {
646 data.write_vector_4_f32_as_vector_4_f16(self.position)?;
647 data.write_vector_4_f32_as_vector_4_f16(self.normal)?;
648 }
649 VertexFormat::Uk12 => {
650 data.write_vector_4_f32_normal_as_vector_4_f16(*self.position())?;
651 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate)?;
652 data.write_vector_2_f32_as_vector_2_f16(self.texture_coordinate2)?;
653 data.write_vector_4_u8(self.uk_1)?;
654 }
655 _ => return Err(RLibError::DecodingRigidModelUnknownVertexFormat(vtype.into()))
656 }
657
658 Ok(())
659 }
660}
661fn static_layout(version: u32, mtype: MaterialType) -> MaterialType {
667 match mtype {
668 MaterialType::TerrainBlend if version <= 6 => MaterialType::DefaultMaterial,
669 _ => mtype,
670 }
671}
672
673