BlueGit

timfa raytracer

Files

raytracer / src / ppda / viewFrame.js

viewFrame.js

viewFrame.js 3.7 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 → rel 0.
25 // Yaw 90 = model LEFT. Walking around counterclockwise (front→NPC left
26 // →back) must yield 0→90→180. That is facing - toPlayer (not the reverse,
27 // which mirrored left/right).
28 var rel = (npcFacingDeg || 0) - toPlayer;
29 rel = ((rel % 360) + 360) % 360;
30 return rel;
31 },
32
33 nearestInList: function (value, list, circular360)
34 {
35 if (!list || !list.length)
36 { return 0; }
37 var best = list[0];
38 var bestD = Infinity;
39 for (var i = 0; i < list.length; i++)
40 {
41 var v = list[i];
42 var d = Math.abs(value - v);
43 if (circular360)
44 { d = Math.min(d, 360 - d); }
45 if (d < bestD)
46 {
47 bestD = d;
48 best = v;
49 }
50 }
51 return best;
52 },
53
54 pickYaw: function (relativeYawDeg, yaws)
55 {
56 return this.nearestInList(((relativeYawDeg % 360) + 360) % 360, yaws || [0], true);
57 },
58
59 pickPitch: function (playerPitchRad, pitches)
60 {
61 // Legacy: map look-pitch. Prefer pickPitchFromHeight for billboards so
62 // looking up/down does not tilt the NPC atlas.
63 var deg = -(playerPitchRad || 0) * 180 / Math.PI;
64 return this.nearestInList(deg, pitches || [0]);
65 },
66
67 /**
68 * Atlas pitch from height difference only (not player look pitch).
69 * playerEyeZ / npcCenterZ in world units; dist = horizontal distance.
70 * Paperdoll: positive pitch = viewed from above.
71 */
72 pickPitchFromHeight: function (playerEyeZ, npcCenterZ, horizDist, pitches)
73 {
74 var d = Math.max(0.01, horizDist || 0.01);
75 // Positive paperdoll pitch = from above → when NPC is lower than the eye.
76 var deg = Math.atan2((playerEyeZ || 0) - (npcCenterZ || 0), d) * 180 / Math.PI;
77 return this.nearestInList(deg, pitches || [0]);
78 },
79
80 frameIndex: function (timeSec, fps, frameCount)
81 {
82 var n = Math.max(1, frameCount | 0);
83 var f = Math.max(1, fps | 0);
84 var idx = Math.floor((timeSec || 0) * f);
85 idx %= n;
86 if (idx < 0)
87 { idx += n; }
88 return idx;
89 },
90
91 findClip: function (ppda, clipTokenOrName)
92 {
93 var clips = ppda && ppda.clips || [];
94 for (var i = 0; i < clips.length; i++)
95 {
96 if (clips[i].token === clipTokenOrName || clips[i].name === clipTokenOrName)
97 { return clips[i]; }
98 }
99 return clips[0] || null;
100 },
101
102 findView: function (frame, pitch, yaw)
103 {
104 var views = frame && frame.views || [];
105 for (var i = 0; i < views.length; i++)
106 {
107 if (views[i].pitch === pitch && views[i].yaw === yaw)
108 { return views[i]; }
109 }
110 return null;
111 },
112
113 uvRect: function (view, atlasW, atlasH)
114 {
115 if (!view)
116 { return { u0: 0, v0: 0, u1: 1, v1: 1, x: 0, y: 0, w: 0, h: 0 }; }
117 var w = atlasW || 1;
118 var h = atlasH || 1;
119 return {
120 x: view.x,
121 y: view.y,
122 w: view.w,
123 h: view.h,
124 u0: view.x / w,
125 v0: view.y / h,
126 u1: (view.x + view.w) / w,
127 v1: (view.y + view.h) / h
128 };
129 }
130};
131
132if (typeof module !== "undefined" && module.exports)
133{ module.exports = PpdaViewFrame; }