MCPcopy Create free account
hub / github.com/melonjs/melonJS / projectVertices

Function projectVertices

packages/melonjs/src/math/vertex.ts:71–138  ·  view source on GitHub ↗
(
	src: Float32Array,
	dst: Float32Array,
	count: number,
	matrix: Float32Array,
	width: number,
	height: number,
	offsetX = 0,
	offsetY = 0,
	zScale = 0,
)

Source from the content-addressed store, hash-verified

69 * @param zScale - scale factor for Z output (0 = don't compute Z)
70 */
71export function projectVertices(
72 src: Float32Array,
73 dst: Float32Array,
74 count: number,
75 matrix: Float32Array,
76 width: number,
77 height: number,
78 offsetX = 0,
79 offsetY = 0,
80 zScale = 0,
81) {
82 // hoist all constants out of the loop
83 const centerX = width * 0.5 + offsetX;
84 const centerY = height * 0.5 + offsetY;
85 const negHeight = -height;
86
87 // extract matrix elements once (avoids repeated indexed access)
88 const m0 = matrix[0];
89 const m1 = matrix[1];
90 const m2 = matrix[2];
91 const m3 = matrix[3];
92 const m4 = matrix[4];
93 const m5 = matrix[5];
94 const m6 = matrix[6];
95 const m7 = matrix[7];
96 const m8 = matrix[8];
97 const m9 = matrix[9];
98 const m10 = matrix[10];
99 const m11 = matrix[11];
100 const m12 = matrix[12];
101 const m13 = matrix[13];
102 const m14 = matrix[14];
103 const m15 = matrix[15];
104
105 if (zScale !== 0) {
106 const negZScale = -zScale;
107 for (let i = 0; i < count; i++) {
108 const i3 = i * 3;
109 const vx = src[i3];
110 const vy = src[i3 + 1];
111 const vz = src[i3 + 2];
112
113 const tw = m3 * vx + m7 * vy + m11 * vz + m15;
114 const invW = tw !== 0 ? 1.0 / tw : 1.0;
115
116 dst[i3] = (m0 * vx + m4 * vy + m8 * vz + m12) * invW * width + centerX;
117 dst[i3 + 1] =
118 (m1 * vx + m5 * vy + m9 * vz + m13) * invW * negHeight + centerY;
119 dst[i3 + 2] = (m2 * vx + m6 * vy + m10 * vz + m14) * invW * negZScale;
120 }
121 } else {
122 // fast path: skip Z computation entirely
123 for (let i = 0; i < count; i++) {
124 const i3 = i * 3;
125 const vx = src[i3];
126 const vy = src[i3 + 1];
127 const vz = src[i3 + 2];
128

Callers 3

_projectVerticesMethod · 0.90
mesh.spec.jsFile · 0.90
vertex.spec.jsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected