Class: Bayonetta::Normal

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VectorAccessor

#[], #[]=

Constructor Details

#initializeNormal

Returns a new instance of Normal.



257
258
259
260
261
262
# File 'lib/bayonetta/wmb.rb', line 257

def initialize
  @normal = [0.0, 0.0, 0.0]
  @normal_big_orig = nil
  @normal_small_orig = nil
  @wide = false
end

Instance Attribute Details

#normalObject

Returns the value of attribute normal.



243
244
245
# File 'lib/bayonetta/wmb.rb', line 243

def normal
  @normal
end

#normal_big_origObject

Returns the value of attribute normal_big_orig.



244
245
246
# File 'lib/bayonetta/wmb.rb', line 244

def normal_big_orig
  @normal_big_orig
end

#normal_small_origObject

Returns the value of attribute normal_small_orig.



245
246
247
# File 'lib/bayonetta/wmb.rb', line 245

def normal_small_orig
  @normal_small_orig
end

#wideObject

Returns the value of attribute wide.



246
247
248
# File 'lib/bayonetta/wmb.rb', line 246

def wide
  @wide
end

Instance Method Details

#__convert_fieldsObject



456
457
458
# File 'lib/bayonetta/wmb.rb', line 456

def __convert_fields
  convert_normal
end

#__dump_fieldsObject



464
465
466
# File 'lib/bayonetta/wmb.rb', line 464

def __dump_fields
  dump_normal
end

#__load_fieldsObject



460
461
462
# File 'lib/bayonetta/wmb.rb', line 460

def __load_fields
  load_normal
end

#__size(position, parent, index) ⇒ Object



294
295
296
# File 'lib/bayonetta/wmb.rb', line 294

def __size(position, parent, index)
  4
end

#clamp(v, max, min) ⇒ Object



357
358
359
360
361
362
363
364
# File 'lib/bayonetta/wmb.rb', line 357

def clamp(v, max, min)
  if v > max
    v = max
  elsif v < min
    v = min
  end
  v
end

#convert_normalObject



451
452
453
454
# File 'lib/bayonetta/wmb.rb', line 451

def convert_normal
  load_normal
  dump_normal
end

#decode_big_normal(vs) ⇒ Object



332
333
334
# File 'lib/bayonetta/wmb.rb', line 332

def decode_big_normal(vs)
  unpack_wide(vs.unpack("L>").first)
end

#decode_small_normal(v) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/bayonetta/wmb.rb', line 344

def decode_small_normal(v)
  n = v.unpack("c4")
  nx = n[3]
  ny = n[2]
  nz = n[1]
  mag = 127.0
  fx = nx.to_f/mag
  fy = ny.to_f/mag
  fz = nz.to_f/mag

  normalize(fx, fy, fz)
end

#decode_small_normal_wide(vs) ⇒ Object



336
337
338
# File 'lib/bayonetta/wmb.rb', line 336

def decode_small_normal_wide(vs)
  unpack_wide(vs.unpack("L<").first)
end

#dump_normalObject



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/bayonetta/wmb.rb', line 429

def dump_normal
  s2 =
    if __output_big
      if @normal_big_orig
        @normal_big_orig
      elsif is_bayo2? && @normal_small_orig
        @normal_small_orig.reverse
      else
        encode_big_normal(@normal)
      end
    else
      if @normal_small_orig
        @normal_small_orig
      elsif is_bayo2?
        @normal_big_orig ? @normal_big_orig.reverse : encode_small_normal_wide(@normal)
      else
        encode_small_normal(@normal)
      end
    end
  __output.write(s2)
end

#encode_big_normal(normal) ⇒ Object



408
409
410
# File 'lib/bayonetta/wmb.rb', line 408

def encode_big_normal(normal)
  [pack_wide(normal)].pack("L>")
end

#encode_small_normal(normal) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/bayonetta/wmb.rb', line 391

def encode_small_normal(normal)
  if @wide
    encode_small_normal_wide(normal)
  else
    fx = normal[0]
    fy = normal[1]
    fz = normal[2]
    nx = (fx*127.0).to_i
    ny = (fy*127.0).to_i
    nz = (fz*127.0).to_i
    nx = clamp(nx, 127, -128)
    ny = clamp(ny, 127, -128)
    nz = clamp(nz, 127, -128)
    [0, nz, ny, nx].pack("c4")
  end
end

#encode_small_normal_wide(normal) ⇒ Object



387
388
389
# File 'lib/bayonetta/wmb.rb', line 387

def encode_small_normal_wide(normal)
    [pack_wide(normal)].pack("L<")
end

#is_bayo2?Boolean

Returns:

  • (Boolean)


248
249
250
251
252
253
254
255
# File 'lib/bayonetta/wmb.rb', line 248

def is_bayo2?
  if __parent.__parent.respond_to?(:is_bayo2?)
    return __parent.__parent.is_bayo2?
  elsif __parent.__parent.__parent.respond_to?(:is_bayo2?)
    return __parent.__parent.__parent.is_bayo2?
  end
  raise "Cannot determine if Bayo2 or not!"
end

#load_normalObject



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/bayonetta/wmb.rb', line 412

def load_normal
  s = __input.read(4)
  if __input_big
    @normal_big_orig = s
    @normal_small_orig = nil
    @normal = decode_big_normal(s)
  else
    @normal_small_orig = s
    @normal_big_orig = nil
    if is_bayo2?
      @normal = decode_small_normal_wide(s)
    else
      @normal = decode_small_normal(s)
    end
  end
end

#normalize(fx, fy, fz) ⇒ Object



298
299
300
301
302
# File 'lib/bayonetta/wmb.rb', line 298

def normalize(fx, fy, fz)
  nrm = Math::sqrt(fx*fx+fy*fy+fz*fz)
  return [0.0, 0.0, 0.0] if nrm == 0.0
  [fx/nrm, fy/nrm, fz/nrm]
end

#pack_wide(normal) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/bayonetta/wmb.rb', line 366

def pack_wide(normal)
    fx = normal[0]
    fy = normal[1]
    fz = normal[2]
    mag = (1<<9)-1
    nx = (fx*(mag).to_f).to_i
    ny = (fy*(mag).to_f).to_i
    nz = (fz*(mag).to_f).to_i
    nx = clamp(nx, mag, -1-mag)
    ny = clamp(ny, mag, -1-mag)
    nz = clamp(nz, mag, -1-mag)
    mask = (1<<10)-1
    v = 0
    v |= nz & mask
    v <<= 10
    v |= ny & mask
    v <<= 10
    v |= nx & mask
    v
end

#redecode_wideObject



340
341
342
# File 'lib/bayonetta/wmb.rb', line 340

def redecode_wide
  @normals = decode_small_normal_wide(normal_small_orig)
end

#to_aObject



469
470
471
# File 'lib/bayonetta/wmb.rb', line 469

def to_a
  [x, y, z]
end

#unpack_wide(v) ⇒ Object



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

def unpack_wide(v)
  nx = v & ((1<<10)-1)
  ny = (v >> 10) & ((1<<10)-1)
  nz = (v >> 20) & ((1<<10)-1)
  sx = nx & (1<<9)
  sy = ny & (1<<9)
  sz = nz & (1<<9)
  if sx
    nx ^= sx
    nx = -(sx-nx)
  end
  if sy
    ny ^= sy
    ny = -(sy-ny)
  end
  if sz
    nz ^= sz
    nz = -(sz-nz)
  end

  mag = ((1<<9)-1).to_f
  fx = nx.to_f/mag
  fy = ny.to_f/mag
  fz = nz.to_f/mag

  normalize(fx, fy, fz)
end

#xObject



264
265
266
# File 'lib/bayonetta/wmb.rb', line 264

def x
  @normal[0]
end

#x=(v) ⇒ Object



276
277
278
279
280
# File 'lib/bayonetta/wmb.rb', line 276

def x=(v)
  @normal_big_orig = nil
  @normal_small_orig = nil
  @normal[0] = v
end

#yObject



268
269
270
# File 'lib/bayonetta/wmb.rb', line 268

def y
  @normal[1]
end

#y=(v) ⇒ Object



282
283
284
285
286
# File 'lib/bayonetta/wmb.rb', line 282

def y=(v)
  @normal_big_orig = nil
  @normal_small_orig = nil
  @normal[1] = v
end

#zObject



272
273
274
# File 'lib/bayonetta/wmb.rb', line 272

def z
  @normal[2]
end

#z=(v) ⇒ Object



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

def z=(v)
  @normal_big_orig = nil
  @normal_small_orig = nil
  @normal[2] = v
end