Class: Bayonetta::WMBFile

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

Defined Under Namespace

Classes: Batch, BatchHeader, Bayo1Material, Material, Mesh, MeshHeader, ShaderName, TexInfo, TexInfos, UnknownStruct, WMBFileHeader

Constant Summary collapse

VERTEX_TYPES =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(input_name, output_name, output_big = false) ⇒ Object



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

def self.convert(input_name, output_name, output_big = false)
  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
  input_big = validate_endianness(input)

  if output_name.respond_to?(:write) && output_name.respond_to?(:seek)
    output = output_name
  else
    output = File.open(output_name, "wb")
  end
  output.write("\xFB"*input.size)
  output.rewind

  wmb = self::new
  wmb.instance_variable_set(:@__was_big, input_big)
  wmb.__convert(input, output, input_big, output_big)

  input.close unless input_name.respond_to?(:read) && input_name.respond_to?(:seek)
  output.close unless output_name.respond_to?(:write) && output_name.respond_to?(:seek)
  wmb
end

.is_wmb3?(input) ⇒ Boolean

Returns:

  • (Boolean)


1255
1256
1257
1258
1259
1260
# File 'lib/bayonetta/wmb.rb', line 1255

def self.is_wmb3?(input)
  input.rewind
  id = input.read(4).unpack("a4").first
  input.rewind
  return id == "WMB3".b || id == "3BMW".b
end

.load(input_name) ⇒ Object



1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
# File 'lib/bayonetta/wmb.rb', line 1226

def self.load(input_name)
  if input_name.respond_to?(:read) && input_name.respond_to?(:seek)
    input = input_name
  else
    input = File.open(input_name, "rb")
  end
  if is_wmb3?(input)
    input.close unless input_name.respond_to?(:read) && input_name.respond_to?(:seek)
    return WMB3File::load(input_name)
  end
  input = StringIO::new(input.read, "rb")
  input_big = validate_endianness(input)

  wmb = self::new
  wmb.instance_variable_set(:@__was_big, input_big)
  wmb.__load(input, input_big)
  input.close unless input_name.respond_to?(:read) && input_name.respond_to?(:seek)

  wmb
end

.validate_endianness(input) ⇒ Object



1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
# File 'lib/bayonetta/wmb.rb', line 1262

def self.validate_endianness(input)
  input.rewind
  id = input.read(4).unpack("a4").first
  case id
  when "WMB\0".b
    input_big = false
  when "\0BMW".b
    input_big = true
  else
    raise "Invalid file type #{id}!"
  end
  input.rewind
  input_big
end

Instance Method Details

#add_ancestors_bone_refsObject



2013
2014
2015
2016
2017
2018
2019
2020
# File 'lib/bayonetta/wmb.rb', line 2013

def add_ancestors_bone_refs
  @meshes.each { |m|
    m.batches.each { |b|
      b.add_ancestors_bone_refs(@vertexes, get_bone_structure)
    }
  }
  self
end

#add_previous_bone_refsObject



2022
2023
2024
2025
2026
2027
2028
2029
# File 'lib/bayonetta/wmb.rb', line 2022

def add_previous_bone_refs
  @meshes.each { |m|
    m.batches.each { |b|
      b.add_previous_bone_refs(@vertexes, get_bone_structure)
    }
  }
  self
end

#advanced_materialsObject



1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
# File 'lib/bayonetta/wmb.rb', line 1931

def advanced_materials
  if is_bayo2?
    materials
  else
    materials.collect { |m|
      if $material_db[m.type][:layout]
        Bayo1Material::new(m)
      else
        m
      end
    }
  end
end

#check_normalsObject



1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
# File 'lib/bayonetta/wmb.rb', line 1317

def check_normals
  return self if @__was_big
  wide_normals = false
  @vertexes.each { |v|
    if v.normal.normal_small_orig.bytes.first != 0
      wide_normals = true
    end
  }
  if wide_normals
    @vertexes.each { |v|
      v.normal.wide = true
      v.normal.redecode_wide
    }
  end
  self
end

#cleanup_bone_refsObject



2004
2005
2006
2007
2008
2009
2010
2011
# File 'lib/bayonetta/wmb.rb', line 2004

def cleanup_bone_refs
  @meshes.each { |m|
    m.batches.each { |b|
      b.cleanup_bone_refs(@vertexes)
    }
  }
  self
end

#cleanup_bonesObject



2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
# File 'lib/bayonetta/wmb.rb', line 2031

def cleanup_bones
  used_bones = Set[]
  @meshes.each { |m|
    m.batches.each { |b|
      used_bones.merge b.bone_refs
    }
  }
  bones = get_bone_structure
  used_bones.to_a.each { |bi|
    used_bones.merge bones[bi].parents.collect(&:index)
  }
  restrict_bones(used_bones)
  self
end

#cleanup_material_sizesObject



1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
# File 'lib/bayonetta/wmb.rb', line 1976

def cleanup_material_sizes
  raise "Unsupported for Bayonetta 2!" if @shader_names
  @materials.each { |m|
     type = m.type
     if $material_db.key?(type) && $material_db[type][:size]
       size = $material_db[type][:size]
     else
       warn "Unknown material type #{m.type}!"
       next
     end
     data_number = (size - 4)/4
     m.material_data = m.material_data.first(data_number)
  }
  self
end

#cleanup_materialsObject



1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
# File 'lib/bayonetta/wmb.rb', line 1945

def cleanup_materials
  used_materials = Set[]
  @meshes.each { |m|
    m.batches.each { |b|
      if @tex_infos #Bayo 2
        used_materials.add(b.header.ex_mat_id)
      else #Bayo 1
        used_materials.add(b.header.material_id)
      end
    }
  }
  materials = @header.num_materials.times.to_a
  kept_materials = materials & used_materials.to_a
  correspondance_table = kept_materials.each_with_index.to_h
  @materials.select!.with_index { |_, i| used_materials.include?(i) }
  @header.num_materials = used_materials.size
  if @shader_names
    @shader_names.select!.with_index { |_, i| used_materials.include?(i) }
  end
  @meshes.each { |m|
    m.batches.each { |b|
      if @tex_infos
        b.header.ex_mat_id = correspondance_table[b.header.ex_mat_id]
      else
        b.header.material_id = correspondance_table[b.header.material_id]
      end
    }
  }
  self
end

#cleanup_textures(input_name, overwrite) ⇒ Object



1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
# File 'lib/bayonetta/wmb.rb', line 1857

def cleanup_textures(input_name, overwrite)
  if File.exist?(input_name.gsub(".wmb",".wtb"))
    wtb = WTBFile::new(File::new(input_name.gsub(".wmb",".wtb"), "rb"))
    if overwrite
      output_name = input_name.gsub(".wmb",".wtb")
    else
      output_name = "wtb_output/#{File.basename(input_name, ".wmb")}.wtb"
    end
    wtp = false
  elsif File.exist?(input_name.gsub(".wmb",".wta"))
    wtb = WTBFile::new(File::new(input_name.gsub(".wmb",".wta"), "rb"), true, File::new(input_name.gsub(".wmb",".wtp"), "rb"))
    if overwrite
      output_name = input_name.gsub(".wmb",".wta")
    else
      output_name = "wtb_output/#{File.basename(input_name, ".wmb")}.wta"
    end
    wtp = true
  else
    raise "Could not find texture file!"
  end

  available_textures = {}
  digests = []
  wtb.each.with_index { |(info, t), i|
    if @tex_info #Bayo 2
      digest = Digest::SHA1.hexdigest(t.read)
      available_textures[info[2]] = digest
      digests.push( digest )
    else #Bayo 1
      digest = Digest::SHA1.hexdigest(t.read)
      available_textures[i] = digest
      digests.push( digest )
    end
    t.rewind
  }
  used_textures_digest_map = {}
  used_texture_digests = Set[]
  @materials.each { |m|
    m.material_data[0..4].each { |tex_id|
      if available_textures.key?(tex_id)
        digest = available_textures[tex_id]
        used_textures_digest_map[tex_id] = digest
        used_texture_digests.add(digest)
      end
    }
  }
  index_list = digests.each_with_index.collect { |d,i| [i,d] }.select { |i, d|
    used_texture_digests.delete?(d)
  }.collect { |i,d| i }
  new_wtb = WTBFile::new(nil, wtb.big, wtp)
  j = 0
  digest_to_tex_id_map = {}
  wtb.each.with_index { |(info, t), i|
    if index_list.include?(i)
      new_wtb.push( t, info[1], info[2])
      if @tex_info
        digest_to_tex_id_map[digests[i]] = info[2]
      else
        digest_to_tex_id_map[digests[i]] = j
      end
      j += 1
    end
  }
  new_wtb.dump(output_name)
  @materials.each { |m|
    m.material_data[0..4].each_with_index { |tex_id, i|
      if available_textures.key?(tex_id)
        digest = available_textures[tex_id]
        m.material_data[i] = digest_to_tex_id_map[digest]
      end
    }
  }
end

#cleanup_vertexesObject



2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
# File 'lib/bayonetta/wmb.rb', line 2088

def cleanup_vertexes
  used_vertex_indexes = []
  @meshes.each { |m|
    m.batches.each { |b|
      used_vertex_indexes += b.vertex_indices
    }
  }
  used_vertex_indexes = used_vertex_indexes.sort.uniq
  @vertexes = used_vertex_indexes.collect { |i| @vertexes[i] }
  @vertexes_ex_data = used_vertex_indexes.collect { |i| @vertexes_ex_data[i] } if @vertexes_ex_data
  @header.num_vertexes = @vertexes.size
  vertex_map = used_vertex_indexes.each_with_index.to_h
  @meshes.each { |m|
    m.batches.each { |b|
      b.indices.collect! { |i|
        vertex_map[i + b.header.vertex_offset]
      }
      b.recompute_from_absolute_indices
    }
  }
  self
end

#copy_uv12(mesh_list) ⇒ Object



2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
# File 'lib/bayonetta/wmb.rb', line 2284

def copy_uv12(mesh_list)
  raise "No UV2 in model!" unless @vertexes_ex_data[0].respond_to?(:mapping2)
  mesh_list.each { |i|
    @meshes[i].batches.each { |b|
      b.vertex_indices.each { |vi|
        @vertexes_ex_data[vi].mapping2.u = @vertexes[vi].mapping.u
        @vertexes_ex_data[vi].mapping2.v = @vertexes[vi].mapping.v
      }
    }
  }
end

#copy_vertex_properties(vertex_hash, **options) ⇒ Object



2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
# File 'lib/bayonetta/wmb.rb', line 2164

def copy_vertex_properties(vertex_hash, **options)
  vertex_usage = nil
  vertex_usage = get_vertex_usage if options[:bone_infos]
  vertex_hash.each { |ivi, ovis|
    ovis = [ovis].flatten
    ovis.each { |ovi|
      iv = @vertexes[ivi]
      ov = @vertexes[ovi]
      if options[:position]
        if @positions
          @positions[ovi].x = @positions[ivi].x
          @positions[ovi].y = @positions[ivi].y
          @positions[ovi].z = @positions[ivi].z
        end
        if ov.respond_to?(:position)
          ov.position.x = iv.position.x
          ov.position.y = iv.position.y
          ov.position.z = iv.position.z
        end
        if ov.respond_to?(:position2)
          ov.position2.x = iv.position2.x
          ov.position2.y = iv.position2.y
          ov.position2.z = iv.position2.z
        end
        if @vertexes_ex_data
          if @vertexes_ex_data[ovi].respond_to?(:position2)
            @vertexes_ex_data[ovi].position2.x = @vertexes_ex_data[ivi].position2.x
            @vertexes_ex_data[ovi].position2.y = @vertexes_ex_data[ivi].position2.y
            @vertexes_ex_data[ovi].position2.z = @vertexes_ex_data[ivi].position2.z
          end
        end
      end
      if options[:mapping]
        if ov.respond_to?(:mapping) 
          ov.mapping.u = iv.mapping.u
          ov.mapping.v = iv.mapping.v
        end
        if ov.respond_to?(:mapping2)
          ov.mapping2.u = iv.mapping2.u
          ov.mapping2.v = iv.mapping2.v
        end
        if @vertexes_ex_data && @vertexes_ex_data[ovi].respond_to?(:mapping2)
          @vertexes_ex_data[ovi].mapping2.u = @vertexes_ex_data[ivi].mapping2.u
          @vertexes_ex_data[ovi].mapping2.v = @vertexes_ex_data[ivi].mapping2.v
        end
      end
      if options[:normal]
        ov.normal = iv.normal
      end
      if options[:tangents]
        ov.tangents = iv.tangents
      end
      if options[:color]
        if ov.respond_to?(:color)
          ov.color = iv.color
        end
        if @vertexes_ex_data && @vertexes_ex_data[ovi].respond_to?(:color)
          @vertexes_ex_data[ovi].color = @vertexes_ex_data[ivi].color
        end
      end
      if options[:bone_infos] && ov.respond_to?(:bone_infos)
        input_batches = vertex_usage[ivi]
        raise "Unormalized vertex #{ivi} , normalize first, and recompute vertex numbers!" if input_batches.length > 1
        raise "Unused vertex #{ivi}!" if input_batches.length == 0
        output_batches = vertex_usage[ovi]
        raise "Unormalized vertex #{ovi} , normalize first, and recompute vertex numbers!" if output_batches.length > 1
        raise "Unused vertex #{ovi}!" if output_batches.length == 0
        input_batch = input_batches.first
        output_batch = output_batches.first
        input_bone_indexes_and_weights = iv.bone_infos.get_indexes_and_weights
        output_bone_indexes_and_weights = input_bone_indexes_and_weights.collect { |bi, bw|
          [input_batch.bone_refs[bi], bw]
        }.collect { |bi, bw|
          new_bi = output_batch.bone_refs.find_index(bi)
          unless new_bi
            new_bi = output_batch.bone_refs.length
            output_batch.bone_refs.push(bi)
            output_batch.num_bone_ref = output_batch.bone_refs.length
          end
          [new_bi, bw]
        }
        ov.bone_infos.set_indexes_and_weights( output_bone_indexes_and_weights )
      end
    }
  }
  self
end

#delete_batches(hash) ⇒ Object



1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
# File 'lib/bayonetta/wmb.rb', line 1832

def delete_batches(hash)
  hash.each { |k, list|
    raise "Mesh #{k} was not found in the model!" unless @meshes[k]
    list = case list
      when Enumerable
        list.to_a
      else
        [list]
      end
    kept_batches = @meshes[k].batches.length.times.to_a - list
    @meshes[k].batches = kept_batches.collect { |i|
      raise "Batch #{i} was not found in mesh #{k}!" unless @meshes[k].batches[i]
      @meshes[k].batches[i]
    }
    @meshes[k].header.num_batch = @meshes[k].batches.length
  }
  self
end

#delete_bones(list) ⇒ Object



1851
1852
1853
1854
1855
# File 'lib/bayonetta/wmb.rb', line 1851

def delete_bones(list)
  used_bones = (@header.num_bones.times.to_a - list)
  restrict_bones(used_bones)
  self
end

#delete_meshes(list) ⇒ Object



1742
1743
1744
1745
1746
1747
1748
1749
# File 'lib/bayonetta/wmb.rb', line 1742

def delete_meshes(list)
  kept_meshes = @meshes.size.times.to_a - list
  @meshes = kept_meshes.collect { |i|
    @meshes[i]
  }
  @header.num_meshes = @meshes.size
  self
end

#dummy_meshes(list) ⇒ Object



1772
1773
1774
1775
1776
1777
1778
1779
# File 'lib/bayonetta/wmb.rb', line 1772

def dummy_meshes(list)
  list.map { |i| m = @meshes[i]
    m.header.num_batch = 1
    m.batches = [m.batches.first]
    m.batches[0].set_triangles([m.batches[0].triangles.first[0]]*3)
  }
  self
end

#dump(output_name, output_big = false) ⇒ Object



1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
# File 'lib/bayonetta/wmb.rb', line 1277

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")
  end
  output.rewind

  if was_big? != output_big
    if output_big
      u_a = header.u_a
      header.u_a = -1
    else
      u_a = header.u_a
      header.u_a = 0
    end
  end
  __set_dump_state(output, output_big, nil, nil)
  __dump_fields
  __unset_dump_state
  if was_big? != output_big
    header.u_a = u_a
  end

  sz = output.size
  sz = align(sz, 0x20)
  if sz > output.size
    output.seek(sz-1)
    output.write("\x00")
  end

  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



2046
2047
2048
2049
2050
2051
2052
2053
# File 'lib/bayonetta/wmb.rb', line 2046

def dump_bones(list = nil)
  bone_struct = Struct::new(:index, :parent, :relative_position, :position, :global_index, :symmetric, :flag)
  table = @bone_index_translate_table.table.invert
  list = (0...@header.num_bones) unless list
  list.collect { |bi|
    bone_struct::new(bi, @bone_hierarchy[bi], @bone_relative_positions[bi], @bone_positions[bi], table[bi],  @header.offset_bone_symmetries > 0x0 ? @bone_symmetries[bi] : -1, @header.offset_bone_flags > 0x0 ? @bone_flags[bi] : 5)
  }
end

#duplicate_meshes(list) ⇒ Object



1781
1782
1783
1784
1785
1786
1787
1788
# File 'lib/bayonetta/wmb.rb', line 1781

def duplicate_meshes(list)
  @meshes += list.collect { |i|
    @meshes[i].duplicate(@positions, @vertexes, @vertexes_ex_data)
  }
  @header.num_meshes = @meshes.size
  @header.num_vertexes = @vertexes.size
  self
end

#fix_ex_dataObject



2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
# File 'lib/bayonetta/wmb.rb', line 2272

def fix_ex_data
  @vertexes.each_with_index { |v, i|
    if @vertexes_ex_data[i].respond_to?(:color)
      @vertexes_ex_data[i].color.data = 0xffc0c0c0
    end
    if @vertexes_ex_data[i].respond_to?(:mapping2) 
      @vertexes_ex_data[i].mapping2.u = v.mapping.u
      @vertexes_ex_data[i].mapping2.v = v.mapping.v
    end
  }
end

#get_bone_structureObject



1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
# File 'lib/bayonetta/wmb.rb', line 1352

def get_bone_structure
  bones = @bone_positions.collect { |p|
    Bone::new(p)
  }
  bones.each_with_index { |b, i|
    if @bone_hierarchy[i] == -1
      b.parent = nil
    else
      b.parent = bones[@bone_hierarchy[i]]
      bones[@bone_hierarchy[i]].children.push(b)
    end
    b.index = i
    b.relative_position = @bone_relative_positions[i]
    b.symmetric = @bone_symmetries[i] if @header.offset_bone_symmetries > 0x0
    b.flag = @bone_flags[i] if @header.offset_bone_flags > 0x0
  }
end

#get_vertex_field(field, vi) ⇒ Object



1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
# File 'lib/bayonetta/wmb.rb', line 1175

def get_vertex_field(field, vi)
  if @vertexes[vi].respond_to?(field)
    return @vertexes[vi].send(field)
  elsif @vertexes_ex_data && @vertexes_ex_data[vi].respond_to?(field)
    return @vertexes_ex_data[vi].send(field)
  elsif field == :position && @positions
    return @positions[vi]
  else
    return nil
  end
end

#get_vertex_fieldsObject



1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
# File 'lib/bayonetta/wmb.rb', line 1155

def get_vertex_fields
  if @vertex_fields
    return @vertex_fields
  else
    types = VERTEX_TYPES[ [ @header.u_b, @header.vertex_ex_data_size, @header.vertex_ex_data] ]
    @vertex_fields = []
    if types[0]
      types[0].each { |name, type|
        @vertex_fields.push(name)
      }
    end
    if types[1]
      types[1].each { |name, type|
        @vertex_fields.push(name)
      }
    end
    return @vertex_fields
  end
end

#get_vertex_typesObject



1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
# File 'lib/bayonetta/wmb.rb', line 1130

def get_vertex_types
  if @vertex_type
    return [@vertex_type, @vertex_ex_type]
  else
    types = VERTEX_TYPES[ [ @header.u_b, @header.vertex_ex_data_size, @header.vertex_ex_data] ]
    @vertex_type = Class::new(LibBin::Structure)
    @vertex_size = 0
    if types[0]
      types[0].each { |name, type|
        @vertex_type.register_field(name, VERTEX_FIELDS[type][0])
        @vertex_size += VERTEX_FIELDS[type][1]
      }
    end
    @vertex_ex_type = Class::new(LibBin::Structure)
    @vertex_ex_size = 0
    if types[1]
      types[1].each { |name, type|
        @vertex_ex_type.register_field(name, VERTEX_FIELDS[type][0])
        @vertex_ex_size += VERTEX_FIELDS[type][1]
      }
    end
    return [@vertex_type, @vertex_ex_type]
  end
end

#get_vertex_usageObject



2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
# File 'lib/bayonetta/wmb.rb', line 2111

def get_vertex_usage
  vertex_usage = Hash::new { |h, k| h[k] = [] }
  @meshes.each { |m|
    m.batches.each { |b|
      b.vertex_indices.each { |i|
        vertex_usage[i].push(b)
      }
    }
  }
  vertex_usage.each { |k,v| v.uniq! }
  vertex_usage
end

#import_bones(list) ⇒ Object



2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
# File 'lib/bayonetta/wmb.rb', line 2055

def import_bones( list )
  table = @bone_index_translate_table.table
  @header.num_bones += list.length
  list.each { |b|
    table[b[:global_index]] = b[:index]
    @bone_hierarchy.push b[:parent]
    @bone_relative_positions.push b[:relative_position]
    @bone_positions.push b[:position]
    @bone_symmetries.push b[:symmetric] if @header.offset_bone_symmetries > 0x0
    @bone_flags.push b[:flag] if @header.offset_bone_flags > 0x0
  }
  @bone_index_translate_table.table = table
  self
end

#is_bayo2?Boolean

Returns:

  • (Boolean)


1251
1252
1253
# File 'lib/bayonetta/wmb.rb', line 1251

def is_bayo2?
  @header.offset_shader_names != 0 || @header.offset_tex_infos != 0
end

#materials_texturesObject



1122
1123
1124
1125
1126
1127
1128
# File 'lib/bayonetta/wmb.rb', line 1122

def materials_textures
  return {} unless @tex_infos
  tex_ids = texture_ids
  @materials.each_with_index.collect { |m, i|
    [i, m.material_data.select { |t| tex_ids.include?(t) }]
  }.to_h
end

#maximize_material_sizesObject



1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
# File 'lib/bayonetta/wmb.rb', line 1992

def maximize_material_sizes
  raise "Unsupported for Bayonetta 2!" if @shader_names
  max_size_mat = $material_db.select { |k, v| v[:size] }.max_by { |k, v|
    v[:size]
  }
  max_data_number = (max_size_mat[1][:size] - 4)/4
  @materials.each { |m|
    m.material_data = m.material_data + [0]*(max_data_number - m.material_data.size)
  }
  self
end

#merge_meshes(hash) ⇒ Object



1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
# File 'lib/bayonetta/wmb.rb', line 1819

def merge_meshes(hash)
  hash.each { |k, vs|
    raise "Mesh #{k} was not found in the model!" unless @meshes[k]
    vs = [vs].flatten
    vs.each { |v|
      raise "Mesh #{v} was not found in the model!" unless @meshes[v]
      @meshes[k].batches += @meshes[v].batches
    }
    @meshes[k].header.num_batch = @meshes[k].batches.length
  }
  self
end

#move_meshes(positions) ⇒ Object



1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
# File 'lib/bayonetta/wmb.rb', line 1801

def move_meshes(positions)
  raise "Invalid positions!" unless positions.size > 0
  positions.each { |k, v|
    raise "Mesh #{k} was not found in the model!" unless @meshes[k]
    raise "Invalid target position #{v}!" unless v >= 0 && v < @meshes.length
  }
  raise "Duplicate mesh found!" unless positions.keys.uniq.size == positions.size
  raise "Duplicate target position found!" unless positions.values.uniq.size == positions.size
  m_p = positions.to_a.sort { |(m1, _), (m2, _)| m2 <=> m1 }
  m_a = m_p.collect { |m, p|
    [@meshes.delete_at(m), p]
  }.sort { |(_, p1), (_, p2)| p1 <=> p2 }
  m_a.each { |m, p|
    @meshes.insert(p, m)
  }
  self
end

#normalize_vertex_usageObject

Duplicate vertexes used by several batches



2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
# File 'lib/bayonetta/wmb.rb', line 2125

def normalize_vertex_usage
  vertex_usage = get_vertex_usage
  vertex_usage.select! { |k, v| v.length > 1 }
  batches = Set::new
  vertex_usage.each { |vi, blist|
    batches.merge blist[1..-1]
  }
  batches.each { |b|
    new_batch = b.duplicate(@positions, @vertexes, @vertexes_ex_data)
    b.header = new_batch.header
    b.indices = new_batch.indices
  }
  @header.num_vertexes = @vertexes.size
  self
end

#normals_to_narrowObject



1343
1344
1345
1346
1347
1348
1349
1350
# File 'lib/bayonetta/wmb.rb', line 1343

def normals_to_narrow
  @vertexes.each { |v|
    v.normal.wide = false
    v.normal.normal_big_orig = nil
    v.normal.normal_small_orig = nil
  }
  self
end

#normals_to_wideObject



1334
1335
1336
1337
1338
1339
1340
1341
# File 'lib/bayonetta/wmb.rb', line 1334

def normals_to_wide
  @vertexes.each { |v|
    v.normal.wide = true
    v.normal.normal_big_orig = nil
    v.normal.normal_small_orig = nil
  }
  self
end

#order_bonesObject



1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
# File 'lib/bayonetta/wmb.rb', line 1713

def order_bones
  arr = @bone_index_translate_table.table.sort
  old_local_to_new_local = {}
  arr.each_with_index { |(_, old_local), new_local|
    old_local_to_new_local[old_local] = new_local
  }
  new_local_to_old_local = old_local_to_new_local.invert
  bones = get_bone_structure
  new_bones = bones.size.times.collect { |new_bi|
    b = bones[new_local_to_old_local[new_bi]]
    b.index = new_bi
    b
  }
  new_bones.each { |b|
    raise "Invali hierarchy: #{b.parent.index} >= #{b.index} !" if b.parent && b.parent.index >= b.index
  }
  set_bone_structure(new_bones)
  new_table = @bone_index_translate_table.table.collect { |k, v| [k, old_local_to_new_local[v]] }.to_h
  @bone_index_translate_table.table = new_table
  @meshes.each_with_index { |m, i|
    m.batches.each_with_index { |b, j|
      b.bone_refs.collect! { |bi|
        old_local_to_new_local[bi]
      }
    }
  }
  self
end

#recompute_batch_tangents(batch) ⇒ Object



2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
# File 'lib/bayonetta/wmb.rb', line 2310

def recompute_batch_tangents(batch)
  vertex_indices = batch.vertex_indices.uniq
  tangents = vertex_indices.collect { |i| [i, Linalg::Vector::new(0, 0, 0, 0)] }.to_h
  indices = batch.triangles.flatten
  inconsistentuvs = 0
  # https://stackoverflow.com/a/66918075
  indices.each_with_index { |i, l|
    j = indices[(l + 1) % 3 + (l / 3) * 3]
    k = indices[(l + 2) % 3 + (l / 3) * 3]
    n = Linalg::Vector::new(*@vertexes[i].normal.to_a, 0.0)
    pi = Linalg::Vector::new(*@vertexes[i].position.to_a, 0.0)
    mi = Linalg::Vector::new(*@vertexes[i].mapping.to_a, 0.0, 0.0)
    pj = Linalg::Vector::new(*@vertexes[j].position.to_a, 0.0)
    mj = Linalg::Vector::new(*@vertexes[j].mapping.to_a, 0.0, 0.0)
    pk = Linalg::Vector::new(*@vertexes[k].position.to_a, 0.0)
    mk = Linalg::Vector::new(*@vertexes[k].mapping.to_a, 0.0, 0.0)
    v1 = pj - pi
    v2 = pk - pi
    t1 = mj - mi
    t2 = mk - mi
    uv2xArea = t1.x * t2.y - t1.y * t2.x
    next if (uv2xArea.abs < 9.5367431640625e-07)
    flip = uv2xArea > 0.0 ? 1.0 : -1.0
    inconsistentuvs += 1 if (tangents[i].w != 0 && tangents[i].w != flip)
    tangents[i].w = flip
    c = v1.x * n.x + v1.y * n.y + v1.z * n.z
    v1 -= n * (v1.dot(n));
    v2 -= n * (v2.dot(n));
    s = ((v1 * t2.y - v2 * t1.y) * flip).normalize
    ac = v1.dot(v2) / (v1.length * v2.length)
    ac = (ac > 1.0 ? 1.0 : (ac < -1.0 ? -1.0 : ac))
    angle = Math.acos(ac);
    tangents[i] += s*angle
  }
  warn "Found #{inconsistentuvs} inconsistent UVs" if inconsistentuvs > 0
  tangents.each { |i, t|
    t.normalize!
    @vertexes[i].tangents.set(t.x, t.y, t.z, -t.w)
  }
end

#recompute_layoutObject



2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
# File 'lib/bayonetta/wmb.rb', line 2360

def recompute_layout
  get_vertex_types

  if is_bayo2?
    last_offset = 0xc0
  else
    last_offset = 0x80
  end

  @header.num_vertexes = @vertexes.size if @vertexes

  if @header.offset_positions > 0x0
    last_offset = @header.offset_positions = align(last_offset, 0x20)
    last_offset += @header.num_vertexes * 12
  end

  if @header.offset_vertexes > 0x0
    last_offset = @header.offset_vertexes = align(last_offset, 0x20)
    last_offset += @header.num_vertexes * @vertex_size
  end
  if @header.offset_vertexes_ex_data > 0x0
    last_offset += 0x20 if is_bayo2?
    last_offset = @header.offset_vertexes_ex_data = align(last_offset, 0x20)
    last_offset += @header.num_vertexes * @vertex_ex_size
  end
  if @header.offset_bone_relative_position > 0x0
    last_offset = @header.offset_bone_hierarchy = align(last_offset, 0x20)
    last_offset += @header.num_bones * 2
  end
  if @header.offset_bone_relative_position > 0x0
    last_offset = @header.offset_bone_relative_position = align(last_offset, 0x20)
    last_offset += @header.num_bones * 12
  end
  if @header.offset_bone_position > 0x0
    last_offset = @header.offset_bone_position = align(last_offset, 0x20)
    last_offset += @header.num_bones * 12
  end
  if @header.offset_bone_index_translate_table > 0x0
    last_offset = @header.offset_bone_index_translate_table = align(last_offset, 0x20)
    last_offset += @bone_index_translate_table.__size
  end
  if @header.offset_u_j > 0x0
    last_offset = @header.offset_u_j = align(last_offset, 0x20)
    last_offset += @u_j.__size
  end
  if @header.offset_bone_symmetries > 0x0
    last_offset = @header.offset_bone_symmetries = align(last_offset, 0x20)
    last_offset += @header.num_bones * 2
  end
  if @header.offset_bone_flags > 0x0
    last_offset = @header.offset_bone_flags = align(last_offset, 0x20)
    last_offset += @header.num_bones
  end
  if @header.offset_shader_names > 0x0
    last_offset = @header.offset_shader_names = align(last_offset, 0x20)
    last_offset += @header.num_materials * 16
  end
  if @header.offset_tex_infos > 0x0
    last_offset = @header.offset_tex_infos = align(last_offset, 0x20)
    last_offset += 4 + @tex_infos.num_tex_infos * 8
  end

  last_offset = @header.offset_materials_offsets = align(last_offset, 0x20)
  off = 0
  @materials_offsets = []
  @header.num_materials.times { |i|
    @materials_offsets.push off
    off += @materials[i].__size
    off =  align(off, 0x4)
  }

  last_offset += 4*@header.num_materials
  last_offset = @header.offset_materials = align(last_offset, 0x20)

  last_offset +=  @materials.collect(&:__size).reduce(&:+)
  # Pad last material with zeros
  pad_count = (align(last_offset, 0x20) - last_offset)/4
  @materials.last.material_data += [0]*pad_count
  last_offset = @header.offset_meshes_offsets = align(last_offset, 0x20)

  off = 0
  @meshes_offsets = []
  @header.num_meshes.times { |i|
    @meshes[i].recompute_layout
    @meshes_offsets.push off
    off += @meshes[i].__size
    off = align(off, 0x20)
  }

  last_offset += 4*@header.num_meshes
  last_offset = @header.offset_meshes = align(last_offset, 0x20)
end

#recompute_relative_positionsObject



1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
# File 'lib/bayonetta/wmb.rb', line 1370

def recompute_relative_positions
  @bone_hierarchy.each_with_index { |b, i|
    if b != -1
      #puts "bone: #{i} parent: #{b} position: #{@bone_positions[i]} pposition: #{@bone_positions[b]}"
      @bone_relative_positions[i] = @bone_positions[i] - @bone_positions[b]
    else
      @bone_relative_positions[i] = @bone_positions[i]
    end
  }
  self
end

#recompute_tangents(mesh_list) ⇒ Object



2351
2352
2353
2354
2355
2356
2357
2358
# File 'lib/bayonetta/wmb.rb', line 2351

def recompute_tangents(mesh_list)
  raise "Vertex don't have tangents information!" unless @vertexes[0].respond_to?(:tangents)
  mesh_list.each { |i|
    @meshes[i].batches.each { |b|
      recompute_batch_tangents(b)
    }
  }
end

#remap_bones(bone_map) ⇒ Object



1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
# File 'lib/bayonetta/wmb.rb', line 1690

def remap_bones(bone_map)
  raise "Global index specified multiple times!" unless bone_map.values.uniq.size == bone_map.size
  local_to_global = @bone_index_translate_table.table.invert
  unknown_bones = bone_map.keys - local_to_global.keys
  raise "Unknown bones: #{unknown_bones}!" unless unknown_bones.size == 0
  global_tt = {}
  bone_map.each { |k, v|
    global_tt[local_to_global.delete(k)] = v
  }
  puts global_tt
  table = local_to_global.invert
  new_global_indexes = bone_map.values - table.keys
  raise "Global indexes: #{bone_map.values - new_global_indexes} still in use!" unless new_global_indexes.size == bone_map.size
  bone_map.each { |k, v|
    table[v] = k
  }
  @bone_symmetries.collect! { |k|
    global_tt[k] ? global_tt[k] : k
  } if @bone_symmetries
  @bone_index_translate_table.table = table
  self
end

#remove_batch_vertex_offsetsObject



2262
2263
2264
2265
2266
2267
2268
2269
2270
# File 'lib/bayonetta/wmb.rb', line 2262

def remove_batch_vertex_offsets
  @meshes.each { |m|
    m.batches.each { |b|
      b.indices = b.vertex_indices
      b.recompute_from_absolute_indices
    }
  }
  self
end

#remove_duplicate_vertexesObject



2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
# File 'lib/bayonetta/wmb.rb', line 2141

def remove_duplicate_vertexes
  vertex_indices = @header.num_vertexes.times
  equivalent_vertex_indices = vertex_indices.group_by { |indx|
    data = get_vertex_fields.collect { |f|
      get_vertex_field(f, indx).to_a
    }
  }
  indx_map = {}
  equivalent_vertex_indices.each { |k, group|
    group.each { |indx|
      indx_map[indx] = group.first
    }
  }
  @meshes.each { |m|
    m.batches.each { |b|
      b.indices = b.vertex_indices.collect { |i|
        indx_map[i]
      }
      b.recompute_from_absolute_indices
    }
  }
end

#remove_triangle_stripsObject



2070
2071
2072
2073
2074
2075
2076
# File 'lib/bayonetta/wmb.rb', line 2070

def remove_triangle_strips
  @meshes.each { |m|
    m.batches.each { |b|
      b.set_triangles(b.triangles)
    }
  }
end

#renumber_batchesObject



2252
2253
2254
2255
2256
2257
2258
2259
2260
# File 'lib/bayonetta/wmb.rb', line 2252

def renumber_batches
  @meshes.each_with_index { |m, i|
    m.header.id = i
    m.batches.each { |b|
      b.header.mesh_id = i
    }
  }
  self
end

#reverse_tangents_byte_order(mesh_list) ⇒ Object



2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
# File 'lib/bayonetta/wmb.rb', line 2296

def reverse_tangents_byte_order(mesh_list)
  raise "Vertex don't have tangents information!" unless @vertexes[0].respond_to?(:tangents)
  vertex_indices = []
  mesh_list.each { |i|
    @meshes[i].batches.each { |b|
      vertex_indices += b.vertex_indices
    }
  }
  vertex_indices.uniq!
  vertex_indices.each { |i|
    @vertexes[i].tangents.data = [@vertexes[i].tangents.data].pack("L<").unpack("L>").first
  }
end

#revert_triangles(mesh_list) ⇒ Object



2078
2079
2080
2081
2082
2083
2084
2085
2086
# File 'lib/bayonetta/wmb.rb', line 2078

def revert_triangles( mesh_list )
  mesh_list.each { |mesh_index|
    @meshes[mesh_index].batches.each { |b|
      b.set_triangles( b.triangles.collect { |n1, n2, n3|
        [n1, n3, n2]
      })
    }
  }
end

#rotate(*args) ⇒ Object



1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
# File 'lib/bayonetta/wmb.rb', line 1489

def rotate(*args)
  if args.length == 2
    (rx, ry, rz), center = args
  elsif args.length == 3
    rx, ry, rz = args
    center = nil
  else
    raise "Invalid arguments for rotate: #{args.inspect}!"
  end
  m = Linalg::get_rotation_matrix(rx, ry, rz, center: center)
  if @positions
    @positions.each { |p|
      r = m * Linalg::Vector::new(p.x, p.y, p.z)
      p.x = r.x
      p.y = r.y
      p.z = r.z
    }
  end
  if @vertexes && @vertexes.first.respond_to?(:position)
    @vertexes.each { |v|
      r = m * Linalg::Vector::new(v.position.x, v.position.y, v.position.z)
      v.position.x = r.x
      v.position.y = r.y
      v.position.z = r.z
    }
  end
  if @vertexes && @vertexes.first.respond_to?(:position2)
    @vertexes.each { |v|
      r = m * Linalg::Vector::new(v.position2.x, v.position2.y, v.position2.z)
      v.position2.x = r.x
      v.position2.y = r.y
      v.position2.z = r.z
    }
  end
  if @vertexes_ex_data && @vertexes_ex_data.first.respond_to?(:position2)
    @vertexes_ex_data.each { |v|
      r = m * Linalg::Vector::new(v.position2.x, v.position2.y, v.position2.z)
      v.position2.x = r.x
      v.position2.y = r.y
      v.position2.z = r.z
    }
  end
  if @bone_positions
    @bone_positions.each { |p|
      r = m * Linalg::Vector::new(p.x, p.y, p.z)
      p.x = r.x
      p.y = r.y
      p.z = r.z
    }
    recompute_relative_positions
  end
  if @vertexes
    @vertexes.each { |v|
      r = m * Linalg::Vector::new(v.normal.x, v.normal.y, v.normal.z, 0.0)
      v.normal.x = r.x
      v.normal.y = r.y
      v.normal.z = r.z
      if v.tangents.data != 0xc0c0c0ff
        r = m * Linalg::Vector::new(v.tangents.x, v.tangents.y, v.tangents.z, 0.0)
        v.tangents.x = r.x
        v.tangents.y = r.y
        v.tangents.z = r.z
      end
    }
  end
  self
end

#scale(s) ⇒ Object



1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
# File 'lib/bayonetta/wmb.rb', line 1409

def scale(s)
  if @positions
    @positions.each { |p|
      p.x = p.x * s
      p.y = p.y * s
      p.z = p.z * s
    }
  end
  if @vertexes && @vertexes.first.respond_to?(:position)
    @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
  if @vertexes && @vertexes.first.respond_to?(:position2)
    @vertexes.each { |v|
      v.position2.x = v.position2.x * s
      v.position2.y = v.position2.y * s
      v.position2.z = v.position2.z * s
    }
  end
  if @vertexes_ex_data && @vertexes_ex_data.first.respond_to?(:position2)
    @vertexes_ex_data.each { |v|
      v.position2.x = v.position2.x * s
      v.position2.y = v.position2.y * s
      v.position2.z = v.position2.z * s
    }
  end
  if @bone_positions
    @bone_positions.each { |p|
      p.x = p.x * s
      p.y = p.y * s
      p.z = p.z * s
    }
    recompute_relative_positions
  end
  self
end

#set_bone_structure(bones) ⇒ Object



1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
# File 'lib/bayonetta/wmb.rb', line 1382

def set_bone_structure(bones)
  @bone_hierarchy = []
  @bone_relative_positions = []
  @bone_positions = []
  @bone_symmetries = [] if @header.offset_bone_symmetries > 0x0
  @bone_flags = [] if @header.offset_bone_flags > 0x0
  bones.each { |b|
    p_index = -1
    p_index = b.parent.index if b.parent
    @bone_hierarchy.push p_index
    @bone_positions.push b.position
    rel_position = b.relative_position
    unless rel_position
      if b.parent
        rel_position = b.position - b.parent.position
      else
        rel_position = b.position
      end
    end
    @bone_relative_positions.push rel_position
    @bone_symmetries.push b.symmetric if @header.offset_bone_symmetries > 0x0
    @bone_flags.push b.flag if @header.offset_bone_flags > 0x0
  }
  @header.num_bones = bones.size
  self
end

#set_pose(pose, exp) ⇒ Object



1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
# File 'lib/bayonetta/wmb.rb', line 1557

def set_pose(pose, exp)
  table = @bone_index_translate_table.table
  bones = get_bone_structure
  tracks = Hash::new { |h,k| h[k] = {} }
  tracks = bones.collect { |b|
    [ b.relative_position.x,
      b.relative_position.y,
      b.relative_position.z,
      0.0, 0.0, 0.0,
      0.0,
      1.0, 1.0, 1.0,
      1.0, 1.0, 1.0 ]
  }
  pose.each { |b, ts|
    bi = table[b]
    if bi
      ts.each { |ti, v|
        tracks[bi][ti] = v
      }
    end
  }
  if exp
    exp.apply(tracks, table)
  end
  matrices = tracks.each_with_index.collect { |ts, bi|
    if @header.offset_bone_flags > 0x0
      order = @bone_flags[bi]
    else
      order = nil
    end
    m = Linalg::get_translation_matrix(*ts[0..2])
    pi = @bone_hierarchy[bi]
    if pi != -1
      parent_cumulative_scale = tracks[pi][10..12]
      m = m * Linalg::get_inverse_scaling_matrix(*parent_cumulative_scale)
      3.times { |i| ts[10+i] *= parent_cumulative_scale[i] }
    end
    3.times { |i|  ts[10+i] *= ts[7+i] }
    m = m * Linalg::get_rotation_matrix(*ts[3..5], order: order)
    m = m * Linalg::get_scaling_matrix(*ts[10..12])
  }
  multiplied_matrices = []
  inverse_bind_pose = bones.collect { |b|
    Linalg::get_translation_matrix(-1 * b.position.x, -1 * b.position.y, -1 * b.position.z)
  }
  bones.each { |b|
    if b.parent
      multiplied_matrices[b.index] = multiplied_matrices[b.parent.index] * matrices[b.index]
    else
      multiplied_matrices[b.index] = matrices[b.index]
    end
  }
  bones.each { |b|
    v = multiplied_matrices[b.index] * Linalg::Vector::new( 0.0, 0.0, 0.0 )
    b.position.x = v.x
    b.position.y = v.y
    b.position.z = v.z
    b.relative_position = nil
  }
  set_bone_structure(bones)
  multiplied_matrices = bones.collect { |b|
    multiplied_matrices[b.index] * inverse_bind_pose[b.index]
  }
  vertex_usage = get_vertex_usage
  vertex_usage.each { |vi, bs|
    bone_refs = bs.first.bone_refs
    bone_infos = get_vertex_field(:bone_infos, 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)
    }
    normal_matrix = vertex_matrix.inverse.transpose
    vp = get_vertex_field(:position, 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, vi)
    new_n = (normal_matrix * Linalg::Vector::new(n.x, n.y, n.z, 0.0)).normalize
    n.x = new_n.x
    n.y = new_n.y
    n.z = new_n.z
    t = get_vertex_field(:tangents, vi)
    new_t = (normal_matrix * Linalg::Vector::new(t.x, t.y, t.z, 0.0)).normalize
    t.x = new_t.x
    t.y = new_t.y
    t.z = new_t.z
    p2 = get_vertex_field(:position2, vi)
    if p2
      new_p2 = vertex_matrix * Linalg::Vector::new(p2.x, p2.y, p2.z)
      p2.x = new_p2.x
      p2.y = new_p2.y
      p2.z = new_p2.z
    end
  }
  self
end

#set_vertex_field(field, vi, val) ⇒ Object



1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
# File 'lib/bayonetta/wmb.rb', line 1187

def set_vertex_field(field, vi, val)
  if @vertexes[vi].respond_to?(field)
    return @vertexes[vi].send(:"#{field}=", val)
  elsif @vertexes_ex_data && @vertexes_ex_data[vi].respond_to?(field)
    return @vertexes_ex_data[vi].send(:"#{field}=", val)
  elsif field == :position && @positions
    return @positions[vi] = val
  else
    raise "Couldn't find field: #{field}!"
  end
end

#shift(x, y, z) ⇒ Object



1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
# File 'lib/bayonetta/wmb.rb', line 1449

def shift(x, y, z)
  if @positions
    @positions.each { |p|
      p.x = p.x + x
      p.y = p.y + y
      p.z = p.z + z
    }
  end
  if @vertexes && @vertexes.first.respond_to?(:position)
    @vertexes.each { |v|
      v.position.x = v.position.x + x
      v.position.y = v.position.y + y
      v.position.z = v.position.z + z
    }
  end
  if @vertexes && @vertexes.first.respond_to?(:position2)
    @vertexes.each { |v|
      v.position2.x = v.position2.x + x
      v.position2.y = v.position2.y + y
      v.position2.z = v.position2.z + z
    }
  end
  if @vertexes_ex_data && @vertexes_ex_data.first.respond_to?(:position2)
    @vertexes_ex_data.each { |v|
      v.position2.x = v.position2.x + x
      v.position2.y = v.position2.y + y
      v.position2.z = v.position2.z + z
    }
  end
  if @bone_positions
    @bone_positions.each { |p|
      p.x = p.x + x
      p.y = p.y + y
      p.z = p.z + z
    }
    recompute_relative_positions
  end
  self
end

#split_meshes(list) ⇒ Object



1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
# File 'lib/bayonetta/wmb.rb', line 1751

def split_meshes(list)
  kept_meshes = @meshes.size.times.to_a - list
  split_meshes = @meshes.size.times.to_a - kept_meshes
  new_meshes = []
  split_meshes.each { |i|
    @meshes[i].batches.each_with_index { |b, j|
      new_mesh = @meshes[i].dup
      new_mesh.header = @meshes[i].header.dup
      new_mesh.header.name = @meshes[i].header.name.tr("\x00","") + ("_%02d" % j)
      new_mesh.batches = [b]
      new_meshes.push new_mesh
    }
  }
  @meshes = kept_meshes.collect { |i|
    @meshes[i]
  }
  @meshes += new_meshes
  @header.num_meshes = @meshes.size
  self
end

#swap_meshes(hash) ⇒ Object



1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
# File 'lib/bayonetta/wmb.rb', line 1790

def swap_meshes(hash)
  hash.each { |k, v|
    raise "Mesh #{k} was not found in the model!" unless @meshes[k]
    raise "Mesh #{v} was not found in the model!" unless @meshes[v]
    tmp = @meshes[k]
    @meshes[k] =  @meshes[v]
    @meshes[v] = tmp
  }
  self
end

#texture_idsObject



1117
1118
1119
1120
# File 'lib/bayonetta/wmb.rb', line 1117

def texture_ids
  return [] unless @tex_infos
  @tex_infos.tex_infos.collect { |i| i.id }
end

#was_big?Boolean

Returns:

  • (Boolean)


1247
1248
1249
# File 'lib/bayonetta/wmb.rb', line 1247

def was_big?
  @__was_big
end