@bigmistqke/view.gl 0.1.4 → 0.1.6
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/.prettierignore +1 -0
- package/README.md +72 -109
- package/dev/src/examples/glsl-composition.ts +1 -1
- package/dist/index.d.mts +3 -2
- package/dist/index.mjs +2 -1
- package/dist/index.mjs.map +1 -1
- package/dist/tag.d.mts +1 -1
- package/dist/tag.mjs +13 -4
- package/dist/tag.mjs.map +1 -1
- package/dist/{types-CGTxl3Eq.d.mts → types-COnZSrXA.d.mts} +165 -2
- package/package.json +1 -1
- package/src/index.ts +6 -1
- package/src/tag.ts +50 -4
- package/src/types.ts +3 -8
- package/test/tag.test.ts +269 -0
package/test/tag.test.ts
CHANGED
|
@@ -992,6 +992,275 @@ describe('compile with symbols', () => {
|
|
|
992
992
|
})
|
|
993
993
|
})
|
|
994
994
|
|
|
995
|
+
describe('glsl array interpolation', () => {
|
|
996
|
+
let gl: GL
|
|
997
|
+
|
|
998
|
+
beforeEach(() => {
|
|
999
|
+
gl = createMockGL()
|
|
1000
|
+
})
|
|
1001
|
+
|
|
1002
|
+
it('should interpolate arrays of resource tags', () => {
|
|
1003
|
+
const shader = glsl`
|
|
1004
|
+
${[uniform.float('u_time'), uniform.vec2('u_resolution')]}
|
|
1005
|
+
${[attribute.vec3('a_position'), attribute.vec2('a_uv')]}
|
|
1006
|
+
|
|
1007
|
+
void main() {
|
|
1008
|
+
gl_Position = vec4(a_position, 1.0);
|
|
1009
|
+
}
|
|
1010
|
+
`
|
|
1011
|
+
|
|
1012
|
+
const { schema } = compile(gl, shader, glsl``)
|
|
1013
|
+
|
|
1014
|
+
expect(schema.uniforms).toEqual({
|
|
1015
|
+
u_time: { kind: 'float' },
|
|
1016
|
+
u_resolution: { kind: 'vec2' },
|
|
1017
|
+
})
|
|
1018
|
+
|
|
1019
|
+
expect(schema.attributes).toEqual({
|
|
1020
|
+
a_position: { kind: 'vec3' },
|
|
1021
|
+
a_uv: { kind: 'vec2' },
|
|
1022
|
+
})
|
|
1023
|
+
})
|
|
1024
|
+
|
|
1025
|
+
it('should interpolate arrays with mixed types', () => {
|
|
1026
|
+
const sharedUniforms = glsl`
|
|
1027
|
+
${uniform.float('u_time')}
|
|
1028
|
+
${uniform.vec2('u_resolution')}
|
|
1029
|
+
`
|
|
1030
|
+
|
|
1031
|
+
const shader = glsl`
|
|
1032
|
+
${[sharedUniforms, uniform.vec3('u_color')]}
|
|
1033
|
+
${[attribute.vec3('a_position'), 'precision mediump float;']}
|
|
1034
|
+
|
|
1035
|
+
void main() {
|
|
1036
|
+
gl_Position = vec4(a_position, 1.0);
|
|
1037
|
+
}
|
|
1038
|
+
`
|
|
1039
|
+
|
|
1040
|
+
const { schema } = compile(gl, shader, glsl``)
|
|
1041
|
+
|
|
1042
|
+
expect(schema.uniforms).toEqual({
|
|
1043
|
+
u_time: { kind: 'float' },
|
|
1044
|
+
u_resolution: { kind: 'vec2' },
|
|
1045
|
+
u_color: { kind: 'vec3' },
|
|
1046
|
+
})
|
|
1047
|
+
|
|
1048
|
+
expect(schema.attributes).toEqual({
|
|
1049
|
+
a_position: { kind: 'vec3' },
|
|
1050
|
+
})
|
|
1051
|
+
|
|
1052
|
+
// Check that the string was interpolated in the shader source
|
|
1053
|
+
const shaderSource = (gl.shaderSource as any).mock.calls.find(
|
|
1054
|
+
(call: any[]) => call[1].includes('precision mediump float;')
|
|
1055
|
+
)
|
|
1056
|
+
expect(shaderSource).toBeTruthy()
|
|
1057
|
+
})
|
|
1058
|
+
|
|
1059
|
+
it('should interpolate arrays with symbols', () => {
|
|
1060
|
+
const u_time_symbol = Symbol('u_time')
|
|
1061
|
+
const a_pos_symbol = Symbol('a_position')
|
|
1062
|
+
|
|
1063
|
+
const shader = glsl`
|
|
1064
|
+
${[uniform.float(u_time_symbol), uniform.vec3('u_color')]}
|
|
1065
|
+
${[attribute.vec3(a_pos_symbol), attribute.vec2('a_uv')]}
|
|
1066
|
+
|
|
1067
|
+
void main() {
|
|
1068
|
+
gl_Position = vec4(${a_pos_symbol}, 1.0);
|
|
1069
|
+
}
|
|
1070
|
+
`
|
|
1071
|
+
|
|
1072
|
+
const { schema } = compile(gl, shader, glsl``)
|
|
1073
|
+
|
|
1074
|
+
expect(schema.uniforms).toEqual({
|
|
1075
|
+
[u_time_symbol]: { kind: 'float' },
|
|
1076
|
+
u_color: { kind: 'vec3' },
|
|
1077
|
+
})
|
|
1078
|
+
|
|
1079
|
+
expect(schema.attributes).toEqual({
|
|
1080
|
+
[a_pos_symbol]: { kind: 'vec3' },
|
|
1081
|
+
a_uv: { kind: 'vec2' },
|
|
1082
|
+
})
|
|
1083
|
+
|
|
1084
|
+
// Check that symbol was interpolated in the shader source
|
|
1085
|
+
const shaderSource = (gl.shaderSource as any).mock.calls.find(
|
|
1086
|
+
(call: any[]) => call[1].includes('attribute vec3 VIEW_GL_ALIAS_') &&
|
|
1087
|
+
call[1].includes('uniform float VIEW_GL_ALIAS_')
|
|
1088
|
+
)
|
|
1089
|
+
expect(shaderSource).toBeTruthy()
|
|
1090
|
+
})
|
|
1091
|
+
|
|
1092
|
+
it('should interpolate nested arrays', () => {
|
|
1093
|
+
const shader = glsl`
|
|
1094
|
+
${[[uniform.float('u_time'), uniform.vec2('u_resolution')], uniform.vec3('u_color')]}
|
|
1095
|
+
${[[attribute.vec3('a_position')], [attribute.vec2('a_uv'), attribute.vec3('a_normal')]]}
|
|
1096
|
+
|
|
1097
|
+
void main() {
|
|
1098
|
+
gl_Position = vec4(a_position, 1.0);
|
|
1099
|
+
}
|
|
1100
|
+
`
|
|
1101
|
+
|
|
1102
|
+
const { schema } = compile(gl, shader, glsl``)
|
|
1103
|
+
|
|
1104
|
+
expect(schema.uniforms).toEqual({
|
|
1105
|
+
u_time: { kind: 'float' },
|
|
1106
|
+
u_resolution: { kind: 'vec2' },
|
|
1107
|
+
u_color: { kind: 'vec3' },
|
|
1108
|
+
})
|
|
1109
|
+
|
|
1110
|
+
expect(schema.attributes).toEqual({
|
|
1111
|
+
a_position: { kind: 'vec3' },
|
|
1112
|
+
a_uv: { kind: 'vec2' },
|
|
1113
|
+
a_normal: { kind: 'vec3' },
|
|
1114
|
+
})
|
|
1115
|
+
})
|
|
1116
|
+
|
|
1117
|
+
it('should interpolate arrays with interleaved attributes', () => {
|
|
1118
|
+
const vertexData = interleave('vertexData', [
|
|
1119
|
+
attribute.vec2('a_position'),
|
|
1120
|
+
attribute.vec2('a_uv'),
|
|
1121
|
+
])
|
|
1122
|
+
|
|
1123
|
+
const shader = glsl`
|
|
1124
|
+
${[vertexData, uniform.mat4('u_mvpMatrix')]}
|
|
1125
|
+
|
|
1126
|
+
void main() {
|
|
1127
|
+
gl_Position = u_mvpMatrix * vec4(a_position, 0.0, 1.0);
|
|
1128
|
+
}
|
|
1129
|
+
`
|
|
1130
|
+
|
|
1131
|
+
const { schema } = compile(gl, shader, glsl``)
|
|
1132
|
+
|
|
1133
|
+
expect(schema.uniforms).toEqual({
|
|
1134
|
+
u_mvpMatrix: { kind: 'mat4' },
|
|
1135
|
+
})
|
|
1136
|
+
|
|
1137
|
+
expect(schema.interleavedAttributes).toEqual({
|
|
1138
|
+
vertexData: {
|
|
1139
|
+
layout: [
|
|
1140
|
+
{ key: 'a_position', kind: 'vec2' },
|
|
1141
|
+
{ key: 'a_uv', kind: 'vec2' },
|
|
1142
|
+
],
|
|
1143
|
+
instanced: false,
|
|
1144
|
+
},
|
|
1145
|
+
})
|
|
1146
|
+
})
|
|
1147
|
+
|
|
1148
|
+
it('should interpolate arrays with composed glsl templates', () => {
|
|
1149
|
+
const sharedUniforms = glsl`
|
|
1150
|
+
${uniform.float('u_time')}
|
|
1151
|
+
${uniform.vec2('u_resolution')}
|
|
1152
|
+
`
|
|
1153
|
+
|
|
1154
|
+
const lightingCode = glsl`
|
|
1155
|
+
${uniform.vec3('u_lightPos')}
|
|
1156
|
+
|
|
1157
|
+
vec3 calculateLighting(vec3 pos) {
|
|
1158
|
+
return normalize(u_lightPos - pos);
|
|
1159
|
+
}
|
|
1160
|
+
`
|
|
1161
|
+
|
|
1162
|
+
const shader = glsl`
|
|
1163
|
+
${[sharedUniforms, lightingCode]}
|
|
1164
|
+
${[attribute.vec3('a_position'), attribute.vec3('a_normal')]}
|
|
1165
|
+
|
|
1166
|
+
void main() {
|
|
1167
|
+
vec3 lighting = calculateLighting(a_position);
|
|
1168
|
+
gl_Position = vec4(a_position, 1.0);
|
|
1169
|
+
}
|
|
1170
|
+
`
|
|
1171
|
+
|
|
1172
|
+
const { schema } = compile(gl, shader, glsl``)
|
|
1173
|
+
|
|
1174
|
+
expect(schema.uniforms).toEqual({
|
|
1175
|
+
u_time: { kind: 'float' },
|
|
1176
|
+
u_resolution: { kind: 'vec2' },
|
|
1177
|
+
u_lightPos: { kind: 'vec3' },
|
|
1178
|
+
})
|
|
1179
|
+
|
|
1180
|
+
expect(schema.attributes).toEqual({
|
|
1181
|
+
a_position: { kind: 'vec3' },
|
|
1182
|
+
a_normal: { kind: 'vec3' },
|
|
1183
|
+
})
|
|
1184
|
+
|
|
1185
|
+
// Check that both templates and the function were interpolated
|
|
1186
|
+
const shaderSource = (gl.shaderSource as any).mock.calls.find(
|
|
1187
|
+
(call: any[]) => call[1].includes('calculateLighting') &&
|
|
1188
|
+
call[1].includes('uniform float u_time') &&
|
|
1189
|
+
call[1].includes('uniform vec3 u_lightPos')
|
|
1190
|
+
)
|
|
1191
|
+
expect(shaderSource).toBeTruthy()
|
|
1192
|
+
})
|
|
1193
|
+
|
|
1194
|
+
it('should handle empty arrays', () => {
|
|
1195
|
+
const shader = glsl`
|
|
1196
|
+
${[]}
|
|
1197
|
+
${uniform.float('u_time')}
|
|
1198
|
+
${[]}
|
|
1199
|
+
|
|
1200
|
+
void main() {
|
|
1201
|
+
gl_Position = vec4(1.0);
|
|
1202
|
+
}
|
|
1203
|
+
`
|
|
1204
|
+
|
|
1205
|
+
const { schema } = compile(gl, shader, glsl``)
|
|
1206
|
+
|
|
1207
|
+
expect(schema.uniforms).toEqual({
|
|
1208
|
+
u_time: { kind: 'float' },
|
|
1209
|
+
})
|
|
1210
|
+
|
|
1211
|
+
expect(schema.attributes).toEqual({})
|
|
1212
|
+
expect(schema.interleavedAttributes).toEqual({})
|
|
1213
|
+
})
|
|
1214
|
+
|
|
1215
|
+
it('should interpolate arrays with WebGL2 syntax', () => {
|
|
1216
|
+
const shader = glsl`#version 300 es
|
|
1217
|
+
${[uniform.float('u_time'), attribute.vec3('a_position')]}
|
|
1218
|
+
|
|
1219
|
+
void main() {
|
|
1220
|
+
gl_Position = vec4(a_position, 1.0);
|
|
1221
|
+
}
|
|
1222
|
+
`
|
|
1223
|
+
|
|
1224
|
+
compile(gl, shader, glsl``)
|
|
1225
|
+
|
|
1226
|
+
// Check that shaderSource was called with WebGL2 syntax
|
|
1227
|
+
const shaderSource = (gl.shaderSource as any).mock.calls.find(
|
|
1228
|
+
(call: any[]) => call[1].includes('uniform float u_time') &&
|
|
1229
|
+
call[1].includes('in vec3 a_position')
|
|
1230
|
+
)
|
|
1231
|
+
expect(shaderSource).toBeTruthy()
|
|
1232
|
+
})
|
|
1233
|
+
|
|
1234
|
+
it('should preserve order when interpolating arrays', () => {
|
|
1235
|
+
const shader = glsl`
|
|
1236
|
+
${['// First comment', uniform.float('u_time'), '// Second comment']}
|
|
1237
|
+
|
|
1238
|
+
void main() {
|
|
1239
|
+
gl_Position = vec4(1.0);
|
|
1240
|
+
}
|
|
1241
|
+
`
|
|
1242
|
+
|
|
1243
|
+
compile(gl, shader, glsl``)
|
|
1244
|
+
|
|
1245
|
+
// Check that order is preserved in the shader source
|
|
1246
|
+
const shaderSource = (gl.shaderSource as any).mock.calls.find(
|
|
1247
|
+
(call: any[]) => call[1].includes('// First comment') &&
|
|
1248
|
+
call[1].includes('uniform float u_time') &&
|
|
1249
|
+
call[1].includes('// Second comment')
|
|
1250
|
+
)?.[1]
|
|
1251
|
+
|
|
1252
|
+
expect(shaderSource).toBeTruthy()
|
|
1253
|
+
|
|
1254
|
+
// Verify order
|
|
1255
|
+
const firstIndex = shaderSource.indexOf('// First comment')
|
|
1256
|
+
const uniformIndex = shaderSource.indexOf('uniform float u_time')
|
|
1257
|
+
const secondIndex = shaderSource.indexOf('// Second comment')
|
|
1258
|
+
|
|
1259
|
+
expect(firstIndex).toBeLessThan(uniformIndex)
|
|
1260
|
+
expect(uniformIndex).toBeLessThan(secondIndex)
|
|
1261
|
+
})
|
|
1262
|
+
})
|
|
1263
|
+
|
|
995
1264
|
describe('compile', () => {
|
|
996
1265
|
let gl: GL
|
|
997
1266
|
let mockProgram: WebGLProgram
|