BlueGit

timfa raytracer

Files

raytracer / src / ppda / viewFrame.js

viewFrame.js

viewFrame.js 3.0 KiB Download Raw
1/**
2 * Choose paperdoll view (yaw/pitch) and animation frame.
3 * Yaw 0 = character front (Paperdoll/glTF convention).
4 */
5var PpdaViewFrame =
6{
7 /**
8 * Relative viewing yaw in degrees [0,360): direction from NPC to player,
9 * minus the NPC's facing yaw (degrees, 0 = faces +X/east in world… mapped
10 * so that when the player looks at the NPC's front, relative yaw ≈ 0).
11 *
12 * World: +X east, +Y south (legacy raycaster). NPC facingYawDeg: 0 looks
13 * toward +X. Paperdoll yaw 0 is front. We map world facing so that the
14 * camera looking opposite to facing sees front.
15 */
16 relativeYawDeg: function (playerX, playerY, npcX, npcY, npcFacingDeg)
17 {
18 var dx = playerX - npcX;
19 var dy = playerY - npcY;
20 // Angle of vector NPC→player in world degrees (0=+X, 90=+Y).
21 var toPlayer = Math.atan2(dy, dx) * 180 / Math.PI;
22 if (toPlayer < 0)
23 { toPlayer += 360; }
24 // When player stands in front of NPC, toPlayer ≈ npcFacing.
25 // Paperdoll wants yaw 0 for front → relative = toPlayer - facing.
26 var rel = toPlayer - (npcFacingDeg || 0);
27 rel = ((rel % 360) + 360) % 360;
28 return rel;
29 },
30
31 nearestInList: function (value, list, circular360)
32 {
33 if (!list || !list.length)
34 { return 0; }
35 var best = list[0];
36 var bestD = Infinity;
37 for (var i = 0; i < list.length; i++)
38 {
39 var v = list[i];
40 var d = Math.abs(value - v);
41 if (circular360)
42 { d = Math.min(d, 360 - d); }
43 if (d < bestD)
44 {
45 bestD = d;
46 best = v;
47 }
48 }
49 return best;
50 },
51
52 pickYaw: function (relativeYawDeg, yaws)
53 {
54 return this.nearestInList(((relativeYawDeg % 360) + 360) % 360, yaws || [0], true);
55 },
56
57 pickPitch: function (playerPitchRad, pitches)
58 {
59 // player pitch: 0 = horizon; positive looks down in this game.
60 // Paperdoll: positive pitch = from above. Invert sign for mapping.
61 var deg = -(playerPitchRad || 0) * 180 / Math.PI;
62 return this.nearestInList(deg, pitches || [0]);
63 },
64
65 frameIndex: function (timeSec, fps, frameCount)
66 {
67 var n = Math.max(1, frameCount | 0);
68 var f = Math.max(1, fps | 0);
69 var idx = Math.floor((timeSec || 0) * f);
70 idx %= n;
71 if (idx < 0)
72 { idx += n; }
73 return idx;
74 },
75
76 findClip: function (ppda, clipTokenOrName)
77 {
78 var clips = ppda && ppda.clips || [];
79 for (var i = 0; i < clips.length; i++)
80 {
81 if (clips[i].token === clipTokenOrName || clips[i].name === clipTokenOrName)
82 { return clips[i]; }
83 }
84 return clips[0] || null;
85 },
86
87 findView: function (frame, pitch, yaw)
88 {
89 var views = frame && frame.views || [];
90 for (var i = 0; i < views.length; i++)
91 {
92 if (views[i].pitch === pitch && views[i].yaw === yaw)
93 { return views[i]; }
94 }
95 return null;
96 },
97
98 uvRect: function (view, atlasW, atlasH)
99 {
100 if (!view)
101 { return { u0: 0, v0: 0, u1: 1, v1: 1, x: 0, y: 0, w: 0, h: 0 }; }
102 var w = atlasW || 1;
103 var h = atlasH || 1;
104 return {
105 x: view.x,
106 y: view.y,
107 w: view.w,
108 h: view.h,
109 u0: view.x / w,
110 v0: view.y / h,
111 u1: (view.x + view.w) / w,
112 v1: (view.y + view.h) / h
113 };
114 }
115};
116
117if (typeof module !== "undefined" && module.exports)
118{ module.exports = PpdaViewFrame; }