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

Add support for GLSL (OpenGL Shading Language) #615

Merged
merged 4 commits into from
Sep 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ var components = {
"title": "Git",
"owner": "lgiraudel"
},
"glsl": {
"title": "GLSL",
"require": "clike",
"owner": "Golmote"
},
"go": {
"title": "Go",
"require": "clike",
Expand Down
16 changes: 16 additions & 0 deletions components/prism-glsl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Prism.languages.glsl = Prism.languages.extend('clike', {
'comment': [
/\/\*[\w\W]*?\*\//,
/\/\/(?:\\(?:\r\n|[\s\S])|.)*/
],
'number': /\b(?:0x[\da-f]+|(?:\.\d+|\d+\.?\d*)(?:e[+-]?\d+)?)[ulf]*\b/i,
'keyword': /\b(?:attribute|const|uniform|varying|buffer|shared|coherent|volatile|restrict|readonly|writeonly|atomic_uint|layout|centroid|flat|smooth|noperspective|patch|sample|break|continue|do|for|while|switch|case|default|if|else|subroutine|in|out|inout|float|double|int|void|bool|true|false|invariant|precise|discard|return|d?mat[234](?:x[234])?|[ibdu]?vec[234]|uint|lowp|mediump|highp|precision|[iu]?sampler[123]D|[iu]?samplerCube|sampler[12]DShadow|samplerCubeShadow|[iu]?sampler[12]DArray|sampler[12]DArrayShadow|[iu]?sampler2DRect|sampler2DRectShadow|[iu]?samplerBuffer|[iu]?sampler2DMS(?:Array)?|[iu]?samplerCubeArray|samplerCubeArrayShadow|[iu]?image[123]D|[iu]?image2DRect|[iu]?imageCube|[iu]?imageBuffer|[iu]?image[12]DArray|[iu]?imageCubeArray|[iu]?image2DMS(?:Array)?|struct|common|partition|active|asm|class|union|enum|typedef|template|this|resource|goto|inline|noinline|public|static|extern|external|interface|long|short|half|fixed|unsigned|superp|input|output|hvec[234]|fvec[234]|sampler3DRect|filter|sizeof|cast|namespace|using)\b/
});

Prism.languages.insertBefore('glsl', 'comment', {
'preprocessor': {
pattern: /(^[ \t]*)#(?:(?:define|undef|if|ifdef|ifndef|else|elif|endif|error|pragma|extension|version|line)\b)?/m,
lookbehind: true,
alias: 'builtin'
}
});
1 change: 1 addition & 0 deletions components/prism-glsl.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions examples/prism-glsl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<h1>GLSL (OpenGL Shading Language)</h1>
<p>To use this language, use the class "language-glsl".</p>

<h2>Vertex shader example</h2>
<pre><code>attribute vec3 vertex;
attribute vec3 normal;

uniform mat4 _mvProj;
uniform mat3 _norm;

varying vec3 vColor;
varying vec3 localPos;

#pragma include "light.glsl"

// constants
vec3 materialColor = vec3(1.0,0.7,0.8);
vec3 specularColor = vec3(1.0,1.0,1.0);

void main(void) {
// compute position
gl_Position = _mvProj * vec4(vertex, 1.0);

localPos = vertex;

// compute light info
vec3 n = normalize(_norm * normal);
vec3 diffuse;
float specular;
float glowingSpecular = 50.0;
getDirectionalLight(n, _dLight, glowingSpecular, diffuse, specular);
vColor = max(diffuse,_ambient.xyz)*materialColor+specular*specularColor+_ambient;
}</code></pre>

<h2>Fragment shader example</h2>
<pre><code>#ifdef GL_ES
precision highp float;
#endif

uniform vec3 BrickColor, MortarColor;
uniform vec3 BrickSize;
uniform vec3 BrickPct;

varying vec3 vColor;
varying vec3 localPos;
void main()
{
vec3 color;
vec3 position, useBrick;


position = localPos / BrickSize.xyz;

if (fract(position.y * 0.5) > 0.5){
position.x += 0.5;
position.z += 0.5;
}

position = fract(position);

useBrick = step(position, BrickPct.xyz);

color = mix(MortarColor, BrickColor, useBrick.x * useBrick.y * useBrick.z);
color *= vColor;

gl_FragColor = vec4(color, 1.0);
}
</code></pre>
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if (typeof self === 'undefined' || !self.Prism || !self.document) {
}

// The languages map is built automatically with gulp
var Languages = /*languages_placeholder[*/{"css":"CSS","clike":"C-like","javascript":"JavaScript","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","csharp":"C#","cpp":"C++","coffeescript":"CoffeeScript","css-extras":"CSS Extras","fsharp":"F#","http":"HTTP","latex":"LaTeX","lolcode":"LOLCODE","matlab":"MATLAB","nasm":"NASM","nsis":"NSIS","objectivec":"Objective-C","php":"PHP","php-extras":"PHP Extras","powershell":"PowerShell","jsx":"React JSX","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","typescript":"TypeScript","vhdl":"VHDL","wiki":"Wiki markup","yaml":"YAML"}/*]*/;
var Languages = /*languages_placeholder[*/{"css":"CSS","clike":"C-like","javascript":"JavaScript","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","csharp":"C#","cpp":"C++","coffeescript":"CoffeeScript","css-extras":"CSS Extras","fsharp":"F#","glsl":"GLSL","http":"HTTP","latex":"LaTeX","lolcode":"LOLCODE","matlab":"MATLAB","nasm":"NASM","nsis":"NSIS","objectivec":"Objective-C","php":"PHP","php-extras":"PHP Extras","powershell":"PowerShell","jsx":"React JSX","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","typescript":"TypeScript","vhdl":"VHDL","wiki":"Wiki markup","yaml":"YAML"}/*]*/;
Prism.hooks.add('before-highlight', function(env) {
var pre = env.element.parentNode;
if (!pre || !/pre/i.test(pre.nodeName)) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/languages/glsl/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**/
/* foo
bar */
//
// foo
// foo\
bar

----------------------------------------------------

[
["comment", "/**/"],
["comment", "/* foo\r\nbar */"],
["comment", "//"],
["comment", "// foo"],
["comment", "// foo\\\r\nbar"]
]

----------------------------------------------------

Checks for comments.
Loading