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.



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

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



285
286
287
# File 'lib/bayonetta/wmb.rb', line 285

def __size(position, parent, index)
  4
end

#clamp(v, max, min) ⇒ Object



365
366
367
368
369
370
371
372
# File 'lib/bayonetta/wmb.rb', line 365

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



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/bayonetta/wmb.rb', line 295

def decode_big_normal(vs)
  v = vs.unpack("L>").first
  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

#decode_small_normal(v) ⇒ Object



352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/bayonetta/wmb.rb', line 352

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

#dump_normalObject



442
443
444
445
446
447
448
449
# File 'lib/bayonetta/wmb.rb', line 442

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

#encode_big_normal(normal) ⇒ Object



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

def encode_big_normal(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].pack("L>")
end

#encode_small_normal(normal) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/bayonetta/wmb.rb', line 374

def encode_small_normal(normal)
  if @wide
    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].pack("L<")
  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

#load_normalObject



429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/bayonetta/wmb.rb', line 429

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
    @normal = decode_small_normal(s)
  end
end

#normalize(fx, fy, fz) ⇒ Object



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

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

#redecode_wideObject



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/bayonetta/wmb.rb', line 324

def redecode_wide
  v = normal_small_orig.unpack("L<").first
  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
  @normals = normalize(fx, fy, fz)
end

#to_aObject



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

def to_a
  [x, y, z]
end

#xObject



255
256
257
# File 'lib/bayonetta/wmb.rb', line 255

def x
  @normal[0]
end

#x=(v) ⇒ Object



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

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

#yObject



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

def y
  @normal[1]
end

#y=(v) ⇒ Object



273
274
275
276
277
# File 'lib/bayonetta/wmb.rb', line 273

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

#zObject



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

def z
  @normal[2]
end

#z=(v) ⇒ Object



279
280
281
282
283
# File 'lib/bayonetta/wmb.rb', line 279

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