Class: Bayonetta::WMB3File

Inherits:
LibBin::Structure
  • Object
show all
Includes:
Alignment
Defined in:
lib/bayonetta/wmb3.rb

Defined Under Namespace

Classes: Batch, Bone, BoneSet, ColTreeNode, Header, InfoPair, Lod, Material, Mesh, MeshMaterialPair, Unknown1, VertexGroup

Constant Summary collapse

VERTEX_TYPES =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(input_name) ⇒ Object



722
723
724
725
726
727
728
729
730
731
732
733
734
735
# File 'lib/bayonetta/wmb3.rb', line 722

def self.load(input_name)
  if input_name.respond_to?(:read) && input_name.respond_to?(:seek)
    input = input_name
  else
    File.open(input_name, "rb") { |f|
      input = StringIO::new(f.read, "rb")
    }
  end
  wmb = self::new
  wmb.instance_variable_set(:@__was_big, false)
  wmb.__load(input, false)
  input.close unless input_name.respond_to?(:read) && input_name.respond_to?(:seek)
  wmb
end

Instance Method Details

#cleanup_vertexesObject



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
# File 'lib/bayonetta/wmb3.rb', line 301

def cleanup_vertexes
  vertex_usage, index_usage = get_vertex_index_usage
  @vertex_groups.each_with_index { |vertex_group, vertex_group_index|
    new_vertex_map = vertex_usage[vertex_group_index].uniq.sort.each_with_index.to_h
    vertex_group.vertexes = new_vertex_map.keys.collect { |i|
      vertex_group.vertexes[i]
    }
    vertex_group.vertexes_ex_data = new_vertex_map.keys.collect { |i|
      vertex_group.vertexes_ex_data[i]
    }
    vertex_group.header.num_vertexes = vertex_group.vertexes.size

    new_index_map = index_usage[vertex_group_index].uniq.sort.each_with_index.to_h
    vertex_group.indices = new_index_map.keys.collect { |i|
      new_vertex_map[vertex_group.indices[i]]
    }
    vertex_group.header.num_indices = vertex_group.indices.size
    @batches.select { |batch| batch.vertex_group_index == vertex_group_index }.each { |batch|
      batch.vertex_start = new_vertex_map[batch.vertex_start]
      batch.index_start = new_index_map[batch.index_start]
      if batch.vertex_start != 0
        ((batch.index_start)...(batch.index_start + batch.num_indices)).each { |i|
          vertex_group.indices[i] = vertex_group.indices[i] - batch.vertex_start
        }
      end
    }
  }
end

#delete_batches(batch_list) ⇒ Object



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
# File 'lib/bayonetta/wmb3.rb', line 395

def delete_batches(batch_list)
  if @lods && @batches
    batch_indexes = @header.info_batches.number.times.to_a
    batch_indexes -= batch_list
    batch_index_map = batch_indexes.each_with_index.to_h
    @batches = batch_indexes.collect { |index|
      @batches[index]
    }
    @header.info_batches.number = @batches.size
    @lods.each { |lod|
      if lod.batch_infos
        new_batch_infos = []
        lod.batch_infos.each_with_index { |batch_info, index|
          unless batch_list.include?(lod.header.batch_start + index)
            new_batch_infos.push batch_info
          end
          lod.batch_infos = new_batch_infos
          lod.header.num_batch_infos = lod.batch_infos.size
        }
      end
    }
    @lods.each { |lod|
      if lod.batch_infos
        lod.header.batch_start = batch_index_map[lod.header.batch_start]
      end
    }
  end
end

#delete_meshes(list) ⇒ Object



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
# File 'lib/bayonetta/wmb3.rb', line 343

def delete_meshes(list)
  kept_meshes = @meshes.size.times.to_a - list
  new_mesh_map = kept_meshes.each_with_index.to_h
  if @meshes
    @meshes = kept_meshes.collect { |i|
      @meshes[i]
    }
    @header.info_meshes.number = @meshes.size
  end
  if @mesh_material_pairs
    @mesh_material_pairs = @mesh_material_pairs.select { |pair|
      ! list.include?(pair.mesh_index)
    }
    @header.info_mesh_material_pairs.number = @mesh_material_pairs.size
    @mesh_material_pairs.each { |pair|
      pair.mesh_index = new_mesh_map[pair.mesh_index]
    }
  end
  if @lods && @batches
    batch_indexes = @header.info_batches.number.times.to_a
    filtered_batches = Set::new
    @lods.each { |lod|
      if lod.batch_infos
        lod.batch_infos.each_with_index { |batch_info, index|
          if list.include?(batch_info.mesh_index)
            filtered_batches.add( index + lod.header.batch_start )
          end
        }
        lod.batch_infos = lod.batch_infos.select { |batch_info|
          ! list.include?(batch_info.mesh_index)
        }
        lod.header.num_batch_infos = lod.batch_infos.size
        lod.batch_infos.each { |batch_info|
          batch_info.mesh_index = new_mesh_map[batch_info.mesh_index]
        }
      end
    }
    batch_indexes -= filtered_batches.to_a
    batch_index_map = batch_indexes.each_with_index.to_h
    @batches = batch_indexes.collect { |index|
      @batches[index]
    }
    @header.info_batches.number = @batches.size
    @lods.each { |lod|
      if lod.batch_infos
        lod.header.batch_start = batch_index_map[lod.header.batch_start]
      end
    }
  end
  self
end

#dump(output_name, output_big = false) ⇒ Object



737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
# File 'lib/bayonetta/wmb3.rb', line 737

def dump(output_name, output_big = false)
  if output_name.respond_to?(:write) && output_name.respond_to?(:seek)
    output = output_name
  else
    output = StringIO::new("", "wb")#File.open(output_name, "wb")
  end
  output.rewind

  __set_dump_state(output, output_big, nil, nil)
  __dump_fields
  __unset_dump_state


  unless output_name.respond_to?(:write) && output_name.respond_to?(:seek)
    File.open(output_name, "wb") { |f|
      f.write output.string
    }
    output.close
  end
  self
end

#dump_bones(list = nil) ⇒ Object



629
630
631
632
633
634
635
636
# File 'lib/bayonetta/wmb3.rb', line 629

def dump_bones(list = nil)
  bone_struct = Struct::new(:index, :parent, :relative_position, :position, :global_index, :symmetric, :flag)
  list = (0...@header.num_bones) unless list
  list.collect { |bi|
    bone_struct::new(bi, @bones[bi].parent_index, @bones[bi].local_position, @bones[bi].position,
                     @bones[bi].id, -1, 5)
  }
end

#get_vertex_field(field, vg, vi) ⇒ Object



597
598
599
# File 'lib/bayonetta/wmb3.rb', line 597

def get_vertex_field(field, vg, vi)
  @vertex_groups[vg].get_vertex_field(field, vi)
end

#get_vertex_index_usageObject



330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/bayonetta/wmb3.rb', line 330

def get_vertex_index_usage
  vertex_usage = Hash::new { |h, k| h[k] = [] }
  index_usage = Hash::new { |h, k| h[k] = [] }
  @batches.each { |batch|
    index_range = (batch.index_start)...(batch.index_start + batch.num_indices)
    index_usage[batch.vertex_group_index] += index_range.to_a
    vertex_usage[batch.vertex_group_index] += @vertex_groups[batch.vertex_group_index].indices[index_range].collect { |vertex|
      vertex + batch.vertex_start
    }
  }
  return [vertex_usage, index_usage]
end

#get_vertex_usageObject



638
639
640
641
642
643
644
645
646
647
# File 'lib/bayonetta/wmb3.rb', line 638

def get_vertex_usage
  vertex_usage = Hash::new { |h, k| h[k] = [] }
  @batches.each { |b|
    @vertex_groups[b.vertex_group_index].indices[b.index_start...(b.index_start+b.num_indices)].each { |i|
      vertex_usage[[b.vertex_group_index, i]].push( b )
    }
  }
  vertex_usage.each { |k,v| v.uniq! }
  vertex_usage
end

#materials_texturesObject



295
296
297
298
299
# File 'lib/bayonetta/wmb3.rb', line 295

def materials_textures
  mat_tex = @materials.collect { |m|
    [m.name, m.textures.collect { |t| [t.texture_id, t.name] }]
  }.to_h
end

#recompute_layoutObject



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
# File 'lib/bayonetta/wmb3.rb', line 424

def recompute_layout
  last_offset = 0x88

  if @header.info_bones.number > 0
    last_offset = @header.info_bones.offset = align(last_offset, 0x10)
    last_offset += @bones.first.__size * @header.info_bones.number
  else
    @header.info_bones.offset = 0x0
  end

  if @header.info_bones.number > 0
    last_offset = @header.info_bone_index_translate_table.offset = align(last_offset, 0x10)
    last_offset += @bone_index_translate_table.__size
  else
    @header.info_bone_index_translate_table.offset = 0x0
  end

  if @header.info_vertex_groups.number > 0
    last_offset = @header.info_vertex_groups.offset = align(last_offset, 0x4)
    last_offset += @vertex_groups.first.header.__size * @header.info_vertex_groups.number
    @vertex_groups.each { |vg|
      if vg.header.num_vertexes > 0
        last_offset = vg.header.offset_vertexes = align(last_offset, 0x10)
        last_offset += vg.header.vertex_size * vg.header.num_vertexes
        if vg.header.vertex_ex_data_size > 0
          last_offset = vg.header.offset_vertexes_ex_data = align(last_offset, 0x10)
          last_offset += vg.header.vertex_ex_data_size * vg.header.num_vertexes
        end
      end
      if vg.header.num_indices > 0
        last_offset = vg.header.offset_indices = align(last_offset, 0x10)
        last_offset += (@header.flags & 0x8 > 0 ? 4 : 2) * vg.header.num_indices
      end
    }
  else
    @header.info_vertex_groups.offset = 0x0
  end

  if @header.info_batches.number > 0
    last_offset = @header.info_batches.offset = align(last_offset, 0x4)
    last_offset += @batches.first.__size * @header.info_batches.number
  else
    @header.info_batches.offset = 0x0
  end

  if @header.info_lods.number > 0
    last_offset = @header.info_lods.offset = align(last_offset, 0x4)
    last_offset += @lods.first.header.__size * @header.info_lods.number
    @lods.each { |lod|
      if lod.header.num_batch_infos > 0
        lod.header.offset_batch_infos = last_offset
        last_offset += lod.batch_infos.first.__size * lod.header.num_batch_infos
      end
      lod.header.offset_name = last_offset
      last_offset += lod.name.size
    }
  else
    @header.info_lods.offset = 0x0
  end

  if @header.info_mesh_material_pairs.number > 0
    last_offset = @header.info_mesh_material_pairs.offset = align(last_offset, 0x10)
    last_offset += @mesh_material_pairs.first.__size * @header.info_mesh_material_pairs.number
  else
    @header.info_mesh_material_pairs.offset = 0x0
  end

  if @header.info_col_tree_nodes.number > 0
    last_offset = @header.info_col_tree_nodes.offset = align(last_offset, 0x10)
    last_offset += @col_tree_nodes.first.__size * @header.info_col_tree_nodes.number
  else
    @header.info_col_tree_nodes.offset = 0x0
  end

  if @header.info_bone_sets.number > 0
    last_offset = @header.info_bone_sets.offset = align(last_offset, 0x10)
    last_offset += 0x8 * @header.info_bone_sets.number
    @bone_sets.each { |bone_set|
      last_offset = bone_set.offset_bone_indices = align(last_offset, 0x10)
      last_offset += 0x2 * bone_set.num_bone_indices
    }
  else
    @header.info_bone_sets.offset = 0x0
  end

  if @header.info_bone_map.number > 0
    last_offset = @header.info_bone_map.offset = align(last_offset, 0x10)
    last_offset += 0x4 * @header.info_bone_map.number
  else
    @header.info_bone_map.offset = 0x0
  end

  if @header.info_meshes.number > 0
    last_offset = @header.info_meshes.offset = align(last_offset, 0x4)
    last_offset += @meshes.first.header.__size * @header.info_meshes.number
    @meshes.each { |mesh|
      mesh.header.offset_name = last_offset
      last_offset += mesh.name.size
      if mesh.header.num_materials > 0
        mesh.header.offset_materials = last_offset
        last_offset += 0x2 * mesh.header.num_materials
      else
        mesh.header.offset_materials = 0x0
      end
      if mesh.header.num_bones_indices > 0
        mesh.header.offset_bones_indices = last_offset
        last_offset += 0x2 * mesh.header.num_bones_indices
      else
        mesh.header.offset_bones_indices = 0x0
      end
    }
  else
    @header.info_meshes.offset = 0x0
  end

  if @header.info_materials.number > 0
    last_offset = @header.info_materials.offset = align(last_offset, 0x10)
    last_offset += @materials.first.header.__size * @header.info_materials.number
    @materials.each { |material|
      material.header.offset_name = last_offset
      last_offset += material.name.size
      material.header.offset_shader_name = last_offset
      last_offset += material.shader_name.size
      material.header.offset_technique_name = last_offset
      last_offset += material.technique_name.size
      if material.header.num_textures > 0
        material.header.offset_textures = last_offset
        last_offset += 0x8 * material.header.num_textures
        material.textures.each { |texture|
          texture.offset_name = last_offset
          last_offset += texture.name.size
        }
      else
        material.header.offset_textures = 0x0
      end
      if material.header.num_parameters_groups > 0
        last_offset = material.header.offset_parameters_groups = align(last_offset, 0x10)
        last_offset += 0xC * material.header.num_parameters_groups
        material.parameters_groups.each { |parameter_group|
          last_offset = parameter_group.offset_parameters = align(last_offset, 0x10)
          last_offset += 0x4 * parameter_group.num_parameters
        }
      else
        material.header.offset_parameters_group = 0x0
      end
      if material.header.num_variables > 0
        last_offset = material.header.offset_variables = align(last_offset, 0x10)
        last_offset += 0x8 * material.header.num_variables
        material.variables.each { |variable|
          variable.offset_name = last_offset
          last_offset += variable.name.size
        }
      else
        material.header.offset_variables = 0x0
      end
    }
  else
    @header.info_materials.offset = 0x0
  end

  if @header.info_unknown1.number > 0
    last_offset = @header.info_unknown1.offset = align(last_offset, 0x4)
    last_offset += @unknown1.first.__size * @header.info_unknown1.number
  else
    @header.info_unknown1.offset = 0x0
  end

end

#recompute_relative_positionsObject



649
650
651
652
653
654
655
656
657
658
# File 'lib/bayonetta/wmb3.rb', line 649

def recompute_relative_positions
  @bones.each { |b|
    if b.parent_index != -1
      b.local_position = b.position - @bones[b.parent_index].position
    else
      b.local_position = b.position
    end
  }
  self
end

#scale(s) ⇒ Object



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/bayonetta/wmb3.rb', line 605

def scale(s)
  @vertex_groups.each { |vg|
    if vg.vertexes && vg.vertexes.first.respond_to?(:position)
      vg.vertexes.each { |v|
        v.position.x = v.position.x * s
        v.position.y = v.position.y * s
        v.position.z = v.position.z * s
      }
    end
  }
  @bones.each { |b|
    b.position.x = b.position.x * s
    b.position.y = b.position.y * s
    b.position.z = b.position.z * s
    b.local_position.x = b.local_position.x * s
    b.local_position.y = b.local_position.y * s
    b.local_position.z = b.local_position.z * s
    b.t_position.x = b.t_position.x * s
    b.t_position.y = b.t_position.y * s
    b.t_position.z = b.t_position.z * s
  }
  self
end

#set_tposeObject



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
# File 'lib/bayonetta/wmb3.rb', line 660

def set_tpose
  neutral_scale = Linalg::Vector::new(1.0, 1.0, 1.0)
  inverse_bind_pose = @bones.collect { |b|
    parent_scale = neutral_scale
    parent_scale = @bones[b.parent_index].scale if b.parent_index != -1
    Linalg::get_inverse_transformation_matrix(b.position, b.rotation, b.scale, parent_scale)
  }
  target_pose = @bones.collect { |b|
    Linalg::get_translation_matrix(b.t_position)
  }
  bones.each { |b|
    b.position = b.t_position
    b.rotation.x = 0.0
    b.rotation.y = 0.0
    b.rotation.z = 0.0
    b.scale.x = 1.0
    b.scale.y = 1.0
    b.scale.z = 1.0
    b.local_rotation.x = 0.0
    b.local_rotation.y = 0.0
    b.local_rotation.z = 0.0
    b.local_scale.x = 1.0
    b.local_scale.y = 1.0
    b.local_scale.z = 1.0
  }
  multiplied_matrices = target_pose.each_with_index.collect { |m, i|
    m * inverse_bind_pose[i]
  }
  vertex_usage = get_vertex_usage
  vertex_usage.each { |(vgi, vi), bs|
    if bs.first.bone_set_index >= 0
      bone_set = bone_sets[bs.first.bone_set_index].bone_indices
      bone_refs = bone_set.collect { |bi| @bone_map[bi] }
    else
      bone_refs = @bone_map
    end
    bone_infos = get_vertex_field(:bone_infos, vgi, vi)
    indexes_and_weights = bone_infos.get_indexes_and_weights
    vertex_matrix = Linalg::get_zero_matrix
    indexes_and_weights.each { |bi, bw|
      i = bone_refs[bi]
      vertex_matrix = vertex_matrix + multiplied_matrices[i] * (bw.to_f/255.to_f)
    }
    vp = get_vertex_field(:position, vgi, vi)
    new_vp = vertex_matrix * Linalg::Vector::new(vp.x, vp.y, vp.z)
    vp.x = new_vp.x
    vp.y = new_vp.y
    vp.z = new_vp.z
    n = get_vertex_field(:normal, vgi, vi)
    new_n = vertex_matrix * Linalg::Vector::new(n.x, n.y, n.z, 0.0)
    n.x = new_n.x
    n.y = new_n.y
    n.z = new_n.z
    t = get_vertex_field(:tangents, vgi, vi)
    new_t = vertex_matrix * Linalg::Vector::new(t.x, t.y, t.z, 0.0)
    t.x = new_t.x
    t.y = new_t.y
    t.z = new_t.z
  }
  self
end

#set_vertex_field(field, vg, vi, val) ⇒ Object



601
602
603
# File 'lib/bayonetta/wmb3.rb', line 601

def set_vertex_field(field, vg, vi, val)
  @vertex_groups[vg].set_vertex_field(field, vi, val)
end

#texture_idsObject



288
289
290
291
292
293
# File 'lib/bayonetta/wmb3.rb', line 288

def texture_ids
  ids = @materials.collect { |m|
    m.textures.collect { |t| t.texture_id }
  }
  ids.flatten.uniq
end

#was_big?Boolean

Returns:

  • (Boolean)


593
594
595
# File 'lib/bayonetta/wmb3.rb', line 593

def was_big?
  @__was_big
end