Skip to content

Commit c485a10

Browse files
committed
Merge branch 'shadergen' of https://github.com/lukeplowden/p5.js into shadergen
2 parents 951ee60 + 9cac072 commit c485a10

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/webgl/ShaderGen.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ function shadergen(p5, fn) {
141141
// These classes are for expressing GLSL functions in Javascript without
142142
// needing to transpile the user's code.
143143

144+
144145
class BaseNode {
145146
constructor(isInternal, type) {
146147
if (new.target === BaseNode) {
@@ -248,6 +249,9 @@ function shadergen(p5, fn) {
248249
// Check that the types of the operands are compatible.
249250
enforceType(other){
250251
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+
}
251255
if (!isGLSLNativeType(other.type)) {
252256
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 '.'`)
253257
}
@@ -370,6 +374,7 @@ function shadergen(p5, fn) {
370374
this.addVectorComponents();
371375
}
372376

377+
373378
toGLSL(context) {
374379
return `${this.name}`;
375380
}
@@ -529,14 +534,24 @@ function shadergen(p5, fn) {
529534
const glslNativeTypes = ['int', 'float', 'vec2', 'vec3', 'vec4', 'sampler2D'];
530535
return glslNativeTypes.includes(typeName);
531536
}
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+
}
532545

533546
// Shader Generator
534547
// This class is responsible for converting the nodes into an object containing GLSL code, to be used by p5.Shader.modify
535548

536549
class ShaderGenerator {
550+
constructor(userCallback, originalShader, srcLocations) {
537551
constructor(userCallback, originalShader, srcLocations) {
538552
GLOBAL_SHADER = this;
539553
this.userCallback = userCallback;
554+
this.userCallback = userCallback;
540555
this.srcLocations = srcLocations;
541556
this.generateHookOverrides(originalShader);
542557
this.output = {
@@ -659,6 +674,7 @@ function shadergen(p5, fn) {
659674

660675
// Generating uniformFloat, uniformVec, createFloat, etc functions
661676
// Maps a GLSL type to the name suffix for method names
677+
const GLSLTypesToIdentifiers = {
662678
const GLSLTypesToIdentifiers = {
663679
int: 'Int',
664680
float: 'Float',
@@ -676,9 +692,11 @@ function shadergen(p5, fn) {
676692
vec4: (value) => new VectorNode(value, 'vec4'),
677693
};
678694

695+
for (const glslType in GLSLTypesToIdentifiers) {
679696
for (const glslType in GLSLTypesToIdentifiers) {
680697
// Generate uniform*() Methods for creating uniforms
681698
const typeIdentifier = GLSLTypesToIdentifiers[glslType];
699+
const typeIdentifier = GLSLTypesToIdentifiers[glslType];
682700
const uniformMethodName = `uniform${typeIdentifier}`;
683701

684702
ShaderGenerator.prototype[uniformMethodName] = function(...args) {

0 commit comments

Comments
 (0)