@@ -141,6 +141,7 @@ function shadergen(p5, fn) {
141
141
// These classes are for expressing GLSL functions in Javascript without
142
142
// needing to transpile the user's code.
143
143
144
+
144
145
class BaseNode {
145
146
constructor ( isInternal , type ) {
146
147
if ( new . target === BaseNode ) {
@@ -248,6 +249,9 @@ function shadergen(p5, fn) {
248
249
// Check that the types of the operands are compatible.
249
250
enforceType ( other ) {
250
251
if ( isShaderNode ( other ) ) {
252
+ if ( ! isGLSLNativeType ( other . type ) ) {
253
+ throw new TypeError ( `You've tried to perform an operation on a struct of type: ${ other . type } . Try accessing a member on that struct with '.'` )
254
+ }
251
255
if ( ! isGLSLNativeType ( other . type ) ) {
252
256
throw new TypeError ( `You've tried to perform an operation on a struct of type: ${ other . type } . Try accessing a member on that struct with '.'` )
253
257
}
@@ -370,6 +374,7 @@ function shadergen(p5, fn) {
370
374
this . addVectorComponents ( ) ;
371
375
}
372
376
377
+
373
378
toGLSL ( context ) {
374
379
return `${ this . name } ` ;
375
380
}
@@ -529,14 +534,24 @@ function shadergen(p5, fn) {
529
534
const glslNativeTypes = [ 'int' , 'float' , 'vec2' , 'vec3' , 'vec4' , 'sampler2D' ] ;
530
535
return glslNativeTypes . includes ( typeName ) ;
531
536
}
537
+ }
538
+
539
+ // Helper function to check if a type is a user defined struct or native type
540
+ function isGLSLNativeType ( typeName ) {
541
+ // Supported types for now
542
+ const glslNativeTypes = [ 'int' , 'float' , 'vec2' , 'vec3' , 'vec4' , 'sampler2D' ] ;
543
+ return glslNativeTypes . includes ( typeName ) ;
544
+ }
532
545
533
546
// Shader Generator
534
547
// This class is responsible for converting the nodes into an object containing GLSL code, to be used by p5.Shader.modify
535
548
536
549
class ShaderGenerator {
550
+ constructor ( userCallback , originalShader , srcLocations ) {
537
551
constructor ( userCallback , originalShader , srcLocations ) {
538
552
GLOBAL_SHADER = this ;
539
553
this . userCallback = userCallback ;
554
+ this . userCallback = userCallback ;
540
555
this . srcLocations = srcLocations ;
541
556
this . generateHookOverrides ( originalShader ) ;
542
557
this . output = {
@@ -659,6 +674,7 @@ function shadergen(p5, fn) {
659
674
660
675
// Generating uniformFloat, uniformVec, createFloat, etc functions
661
676
// Maps a GLSL type to the name suffix for method names
677
+ const GLSLTypesToIdentifiers = {
662
678
const GLSLTypesToIdentifiers = {
663
679
int : 'Int' ,
664
680
float : 'Float' ,
@@ -676,9 +692,11 @@ function shadergen(p5, fn) {
676
692
vec4 : ( value ) => new VectorNode ( value , 'vec4' ) ,
677
693
} ;
678
694
695
+ for ( const glslType in GLSLTypesToIdentifiers ) {
679
696
for ( const glslType in GLSLTypesToIdentifiers ) {
680
697
// Generate uniform*() Methods for creating uniforms
681
698
const typeIdentifier = GLSLTypesToIdentifiers [ glslType ] ;
699
+ const typeIdentifier = GLSLTypesToIdentifiers [ glslType ] ;
682
700
const uniformMethodName = `uniform${ typeIdentifier } ` ;
683
701
684
702
ShaderGenerator . prototype [ uniformMethodName ] = function ( ...args ) {
0 commit comments