@pluto-engine/vite-plugin-wgsl 1.0.7 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +23 -14
- package/dist/index.mjs +23 -14
- package/package.json +1 -1
- package/src/index.ts +29 -17
package/dist/index.js
CHANGED
|
@@ -57,17 +57,20 @@ function transpileWGSLtoGLSL(wgsl) {
|
|
|
57
57
|
if (t === "u32") return "uint";
|
|
58
58
|
return t;
|
|
59
59
|
}
|
|
60
|
-
const
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
60
|
+
const vertGlobals = [];
|
|
61
|
+
const fragGlobals = [];
|
|
62
|
+
const globalRegex2 = /@group\(\d+\)\s*@binding\(\d+\)\s*var(?:<uniform>)?\s+([a-zA-Z0-9_]+)\s*:\s*([a-zA-Z0-9_<>]+);/g;
|
|
63
|
+
let gm2;
|
|
64
|
+
while ((gm2 = globalRegex2.exec(wgsl)) !== null) {
|
|
65
|
+
const name = gm2[1];
|
|
66
|
+
const type = gm2[2];
|
|
66
67
|
if (type.includes("texture_2d_array")) {
|
|
67
|
-
|
|
68
|
+
fragGlobals.push(`uniform sampler2DArray ${name};`);
|
|
68
69
|
} else if (type === "sampler") {
|
|
69
70
|
} else {
|
|
70
|
-
|
|
71
|
+
const glslDecl = `uniform ${convertType(type)} ${name};`;
|
|
72
|
+
vertGlobals.push(glslDecl);
|
|
73
|
+
fragGlobals.push(glslDecl);
|
|
71
74
|
}
|
|
72
75
|
}
|
|
73
76
|
const vertInputArgs = vertexMatch[2].split(",").map((s) => s.trim()).filter(Boolean);
|
|
@@ -79,10 +82,15 @@ function transpileWGSLtoGLSL(wgsl) {
|
|
|
79
82
|
}
|
|
80
83
|
}
|
|
81
84
|
let vertBody = vertexMatch[4];
|
|
82
|
-
vertBody = vertBody.replace(
|
|
83
|
-
vertBody = vertBody.replace(
|
|
84
|
-
vertBody = vertBody.replace(
|
|
85
|
-
vertBody = vertBody.replace(
|
|
85
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*vec4<f32>/g, "vec4 $1 = vec4");
|
|
86
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*vec3<f32>/g, "vec3 $1 = vec3");
|
|
87
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*vec2<f32>/g, "vec2 $1 = vec2");
|
|
88
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*mat4x4<f32>/g, "mat4 $1 = mat4");
|
|
89
|
+
vertBody = vertBody.replace(
|
|
90
|
+
/\blet\s+([a-zA-Z0-9_]+)\s*=\s*([a-zA-Z0-9_]+)\s*\+\s*vec2/g,
|
|
91
|
+
"vec2 $1 = $2 + vec2"
|
|
92
|
+
);
|
|
93
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*/g, "float $1 = ");
|
|
86
94
|
vertBody = vertBody.replace(/vec2<f32>/g, "vec2");
|
|
87
95
|
vertBody = vertBody.replace(/vec3<f32>/g, "vec3");
|
|
88
96
|
vertBody = vertBody.replace(/vec4<f32>/g, "vec4");
|
|
@@ -95,7 +103,7 @@ function transpileWGSLtoGLSL(wgsl) {
|
|
|
95
103
|
precision highp float;
|
|
96
104
|
|
|
97
105
|
${vertAttrs.join("\n")}
|
|
98
|
-
${
|
|
106
|
+
${vertGlobals.join("\n")}
|
|
99
107
|
|
|
100
108
|
${vertVaryings}
|
|
101
109
|
|
|
@@ -116,8 +124,9 @@ ${vertBody}
|
|
|
116
124
|
const glslFrag = `#version 300 es
|
|
117
125
|
precision highp float;
|
|
118
126
|
precision highp sampler2DArray;
|
|
127
|
+
precision highp int;
|
|
119
128
|
|
|
120
|
-
${
|
|
129
|
+
${fragGlobals.join("\n")}
|
|
121
130
|
${fragVaryings}
|
|
122
131
|
|
|
123
132
|
out vec4 fragColor;
|
package/dist/index.mjs
CHANGED
|
@@ -32,17 +32,20 @@ function transpileWGSLtoGLSL(wgsl) {
|
|
|
32
32
|
if (t === "u32") return "uint";
|
|
33
33
|
return t;
|
|
34
34
|
}
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
35
|
+
const vertGlobals = [];
|
|
36
|
+
const fragGlobals = [];
|
|
37
|
+
const globalRegex2 = /@group\(\d+\)\s*@binding\(\d+\)\s*var(?:<uniform>)?\s+([a-zA-Z0-9_]+)\s*:\s*([a-zA-Z0-9_<>]+);/g;
|
|
38
|
+
let gm2;
|
|
39
|
+
while ((gm2 = globalRegex2.exec(wgsl)) !== null) {
|
|
40
|
+
const name = gm2[1];
|
|
41
|
+
const type = gm2[2];
|
|
41
42
|
if (type.includes("texture_2d_array")) {
|
|
42
|
-
|
|
43
|
+
fragGlobals.push(`uniform sampler2DArray ${name};`);
|
|
43
44
|
} else if (type === "sampler") {
|
|
44
45
|
} else {
|
|
45
|
-
|
|
46
|
+
const glslDecl = `uniform ${convertType(type)} ${name};`;
|
|
47
|
+
vertGlobals.push(glslDecl);
|
|
48
|
+
fragGlobals.push(glslDecl);
|
|
46
49
|
}
|
|
47
50
|
}
|
|
48
51
|
const vertInputArgs = vertexMatch[2].split(",").map((s) => s.trim()).filter(Boolean);
|
|
@@ -54,10 +57,15 @@ function transpileWGSLtoGLSL(wgsl) {
|
|
|
54
57
|
}
|
|
55
58
|
}
|
|
56
59
|
let vertBody = vertexMatch[4];
|
|
57
|
-
vertBody = vertBody.replace(
|
|
58
|
-
vertBody = vertBody.replace(
|
|
59
|
-
vertBody = vertBody.replace(
|
|
60
|
-
vertBody = vertBody.replace(
|
|
60
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*vec4<f32>/g, "vec4 $1 = vec4");
|
|
61
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*vec3<f32>/g, "vec3 $1 = vec3");
|
|
62
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*vec2<f32>/g, "vec2 $1 = vec2");
|
|
63
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*mat4x4<f32>/g, "mat4 $1 = mat4");
|
|
64
|
+
vertBody = vertBody.replace(
|
|
65
|
+
/\blet\s+([a-zA-Z0-9_]+)\s*=\s*([a-zA-Z0-9_]+)\s*\+\s*vec2/g,
|
|
66
|
+
"vec2 $1 = $2 + vec2"
|
|
67
|
+
);
|
|
68
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*/g, "float $1 = ");
|
|
61
69
|
vertBody = vertBody.replace(/vec2<f32>/g, "vec2");
|
|
62
70
|
vertBody = vertBody.replace(/vec3<f32>/g, "vec3");
|
|
63
71
|
vertBody = vertBody.replace(/vec4<f32>/g, "vec4");
|
|
@@ -70,7 +78,7 @@ function transpileWGSLtoGLSL(wgsl) {
|
|
|
70
78
|
precision highp float;
|
|
71
79
|
|
|
72
80
|
${vertAttrs.join("\n")}
|
|
73
|
-
${
|
|
81
|
+
${vertGlobals.join("\n")}
|
|
74
82
|
|
|
75
83
|
${vertVaryings}
|
|
76
84
|
|
|
@@ -91,8 +99,9 @@ ${vertBody}
|
|
|
91
99
|
const glslFrag = `#version 300 es
|
|
92
100
|
precision highp float;
|
|
93
101
|
precision highp sampler2DArray;
|
|
102
|
+
precision highp int;
|
|
94
103
|
|
|
95
|
-
${
|
|
104
|
+
${fragGlobals.join("\n")}
|
|
96
105
|
${fragVaryings}
|
|
97
106
|
|
|
98
107
|
out vec4 fragColor;
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -39,20 +39,24 @@ export function transpileWGSLtoGLSL(wgsl: string): { vert: string; frag: string
|
|
|
39
39
|
return t;
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
// Parse globals
|
|
43
|
-
const
|
|
44
|
-
const
|
|
42
|
+
// Parse globals: vertex shader用(samplerを除く)とfragment shader用に分離
|
|
43
|
+
const vertGlobals: string[] = [];
|
|
44
|
+
const fragGlobals: string[] = [];
|
|
45
|
+
const globalRegex2 =
|
|
45
46
|
/@group\(\d+\)\s*@binding\(\d+\)\s*var(?:<uniform>)?\s+([a-zA-Z0-9_]+)\s*:\s*([a-zA-Z0-9_<>]+);/g;
|
|
46
|
-
let
|
|
47
|
-
while ((
|
|
48
|
-
const name =
|
|
49
|
-
const type =
|
|
47
|
+
let gm2;
|
|
48
|
+
while ((gm2 = globalRegex2.exec(wgsl)) !== null) {
|
|
49
|
+
const name = gm2[1];
|
|
50
|
+
const type = gm2[2];
|
|
50
51
|
if (type.includes('texture_2d_array')) {
|
|
51
|
-
|
|
52
|
+
// sampler2DArray はフラグメントシェーダーのみ
|
|
53
|
+
fragGlobals.push(`uniform sampler2DArray ${name};`);
|
|
52
54
|
} else if (type === 'sampler') {
|
|
53
|
-
// GLSL
|
|
55
|
+
// GLSL はテクスチャとサンプラーを統合するためスキップ
|
|
54
56
|
} else {
|
|
55
|
-
|
|
57
|
+
const glslDecl = `uniform ${convertType(type)} ${name};`;
|
|
58
|
+
vertGlobals.push(glslDecl);
|
|
59
|
+
fragGlobals.push(glslDecl);
|
|
56
60
|
}
|
|
57
61
|
}
|
|
58
62
|
|
|
@@ -70,11 +74,18 @@ export function transpileWGSLtoGLSL(wgsl: string): { vert: string; frag: string
|
|
|
70
74
|
}
|
|
71
75
|
|
|
72
76
|
let vertBody = vertexMatch[4];
|
|
73
|
-
//
|
|
74
|
-
vertBody = vertBody.replace(
|
|
75
|
-
vertBody = vertBody.replace(
|
|
76
|
-
vertBody = vertBody.replace(
|
|
77
|
-
vertBody = vertBody.replace(
|
|
77
|
+
// まず型付き let を GLSL の変数宣言に変換(順序が重要)
|
|
78
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*vec4<f32>/g, 'vec4 $1 = vec4');
|
|
79
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*vec3<f32>/g, 'vec3 $1 = vec3');
|
|
80
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*vec2<f32>/g, 'vec2 $1 = vec2');
|
|
81
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*mat4x4<f32>/g, 'mat4 $1 = mat4');
|
|
82
|
+
// vec2型変数への代入から右辺の型を推論(例: scaledPos + vec2(...) は vec2)
|
|
83
|
+
vertBody = vertBody.replace(
|
|
84
|
+
/\blet\s+([a-zA-Z0-9_]+)\s*=\s*([a-zA-Z0-9_]+)\s*\+\s*vec2/g,
|
|
85
|
+
'vec2 $1 = $2 + vec2',
|
|
86
|
+
);
|
|
87
|
+
// 残った型なし let はスカラー(float)として変換
|
|
88
|
+
vertBody = vertBody.replace(/\blet\s+([a-zA-Z0-9_]+)\s*=\s*/g, 'float $1 = ');
|
|
78
89
|
vertBody = vertBody.replace(/vec2<f32>/g, 'vec2');
|
|
79
90
|
vertBody = vertBody.replace(/vec3<f32>/g, 'vec3');
|
|
80
91
|
vertBody = vertBody.replace(/vec4<f32>/g, 'vec4');
|
|
@@ -91,7 +102,7 @@ export function transpileWGSLtoGLSL(wgsl: string): { vert: string; frag: string
|
|
|
91
102
|
precision highp float;
|
|
92
103
|
|
|
93
104
|
${vertAttrs.join('\n')}
|
|
94
|
-
${
|
|
105
|
+
${vertGlobals.join('\n')}
|
|
95
106
|
|
|
96
107
|
${vertVaryings}
|
|
97
108
|
|
|
@@ -116,8 +127,9 @@ ${vertBody}
|
|
|
116
127
|
const glslFrag = `#version 300 es
|
|
117
128
|
precision highp float;
|
|
118
129
|
precision highp sampler2DArray;
|
|
130
|
+
precision highp int;
|
|
119
131
|
|
|
120
|
-
${
|
|
132
|
+
${fragGlobals.join('\n')}
|
|
121
133
|
${fragVaryings}
|
|
122
134
|
|
|
123
135
|
out vec4 fragColor;
|