Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support template string JS output #14

Closed
nylki opened this issue Apr 11, 2018 · 3 comments · Fixed by #53
Closed

support template string JS output #14

nylki opened this issue Apr 11, 2018 · 3 comments · Fixed by #53

Comments

@nylki
Copy link

nylki commented Apr 11, 2018

For a while template literals have been supported in browsers, which allows us to define multiline strings without the need for escape characters like \n.

So a minified shader output could simply be outputted multiline, just like --format none would do, but with backticks enclosing the text. So instead of, what gets now outputted with --format js:

var var_U_RESOLUTION = "m"
var var_U_TIME = "v"

var shader_frag =
 "#version 300 es\n" +
 "uniform float v;" +
 "uniform vec2 m;" +
 "const float f=3.14159,s=50.,n=.01;struct Intersection{bool intersected;vec3 position;vec3 normal;};" +
 "float e(in vec2 v)" +
 "{" +
   "return fract(sin(dot(v.xy,vec2(12.9898,78.233)))*43758.5);" +
 "}" +
 "float x(in vec2 v)" +
 "{" +
   "vec2 f=floor(v),n=fract(v);" +
   "float m=e(f),s=e(f+vec2(1.,0.)),y=e(f+vec2(0.,1.)),I=e(f+vec2(1.,1.));" +
   "vec2 p=n*n*(3.-2.*n);" +
   "float i=mix(m,s,smoothstep(0.,1.,n.x)),x=mix(y,I,smoothstep(0.,1.,n.x)),l=mix(i,x,smoothstep(0.,1.,n.y));" +
   "return l;" +
 "}" +

// snip

the same thing could be written via template strings:

var var_U_RESOLUTION = "m"
var var_U_TIME = "v"

var shader_frag = `
#version 300 es
uniform float v;uniform vec2 m;const float f=3.14159,s=50.,n=.01;struct Intersection{bool intersected;vec3 position;vec3 normal;};float e(in vec2 v){return fract(sin(dot(v.xy,vec2(12.9898,78.233)))*43758.5);}float x(in vec2 v){vec2 f=floor(v),n=fract(v);float m=e(f),s=e(f+vec2(1.,0.)),y=e(f+vec2(0.,1.)),I=e(f+vec2(1.,1.));vec2 p=n*n*(3.-2.*n);float i=mix(m,s,smoothstep(0.,1.,n.x)),x=mix(y,I,smoothstep(0.,1.,n.x)),l=mix(i,x,smoothstep(0.,1.,n.y));return l;} // snip
`

Template literals are supported in all mayor browsers for quite some time now. see: https://caniuse.com/#feat=template-literals

@laurentlb
Copy link
Owner

Thanks, I didn't know this syntax!

@nylki
Copy link
Author

nylki commented Apr 11, 2018

@laurentlb You are welcome! :) I've been using template literals in a recent project to store shader code and thought I'll share this tip here.

Since I have no experience in F# to create a competent Pull Request, I created the issue in the hope you or somebody else picks it up and add the feature to the project.
Not sure though, if it should replace the current js format option or be a new formatting option.

@laurentlb
Copy link
Owner

It looks like it should replace the current format.
This looks like a trivial fix. I can do it next time I work on Shader Minifier (which doesn't happen often).

laurentlb added a commit that referenced this issue May 8, 2021
laurentlb added a commit that referenced this issue May 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants