Skip to content

Commit 247da05

Browse files
committed
Merge pull request #615 from Golmote/prism-glsl
Add support for GLSL (OpenGL Shading Language)
2 parents 5d976a6 + 7494940 commit 247da05

10 files changed

+442
-2
lines changed

components.js

+5
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ var components = {
161161
"title": "Git",
162162
"owner": "lgiraudel"
163163
},
164+
"glsl": {
165+
"title": "GLSL",
166+
"require": "clike",
167+
"owner": "Golmote"
168+
},
164169
"go": {
165170
"title": "Go",
166171
"require": "clike",

components/prism-glsl.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Prism.languages.glsl = Prism.languages.extend('clike', {
2+
'comment': [
3+
/\/\*[\w\W]*?\*\//,
4+
/\/\/(?:\\(?:\r\n|[\s\S])|.)*/
5+
],
6+
'number': /\b(?:0x[\da-f]+|(?:\.\d+|\d+\.?\d*)(?:e[+-]?\d+)?)[ulf]*\b/i,
7+
'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/
8+
});
9+
10+
Prism.languages.insertBefore('glsl', 'comment', {
11+
'preprocessor': {
12+
pattern: /(^[ \t]*)#(?:(?:define|undef|if|ifdef|ifndef|else|elif|endif|error|pragma|extension|version|line)\b)?/m,
13+
lookbehind: true,
14+
alias: 'builtin'
15+
}
16+
});

components/prism-glsl.min.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-glsl.html

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<h1>GLSL (OpenGL Shading Language)</h1>
2+
<p>To use this language, use the class "language-glsl".</p>
3+
4+
<h2>Vertex shader example</h2>
5+
<pre><code>attribute vec3 vertex;
6+
attribute vec3 normal;
7+
8+
uniform mat4 _mvProj;
9+
uniform mat3 _norm;
10+
11+
varying vec3 vColor;
12+
varying vec3 localPos;
13+
14+
#pragma include "light.glsl"
15+
16+
// constants
17+
vec3 materialColor = vec3(1.0,0.7,0.8);
18+
vec3 specularColor = vec3(1.0,1.0,1.0);
19+
20+
void main(void) {
21+
// compute position
22+
gl_Position = _mvProj * vec4(vertex, 1.0);
23+
24+
localPos = vertex;
25+
26+
// compute light info
27+
vec3 n = normalize(_norm * normal);
28+
vec3 diffuse;
29+
float specular;
30+
float glowingSpecular = 50.0;
31+
getDirectionalLight(n, _dLight, glowingSpecular, diffuse, specular);
32+
vColor = max(diffuse,_ambient.xyz)*materialColor+specular*specularColor+_ambient;
33+
}</code></pre>
34+
35+
<h2>Fragment shader example</h2>
36+
<pre><code>#ifdef GL_ES
37+
precision highp float;
38+
#endif
39+
40+
uniform vec3 BrickColor, MortarColor;
41+
uniform vec3 BrickSize;
42+
uniform vec3 BrickPct;
43+
44+
varying vec3 vColor;
45+
varying vec3 localPos;
46+
void main()
47+
{
48+
vec3 color;
49+
vec3 position, useBrick;
50+
51+
52+
position = localPos / BrickSize.xyz;
53+
54+
if (fract(position.y * 0.5) > 0.5){
55+
position.x += 0.5;
56+
position.z += 0.5;
57+
}
58+
59+
position = fract(position);
60+
61+
useBrick = step(position, BrickPct.xyz);
62+
63+
color = mix(MortarColor, BrickColor, useBrick.x * useBrick.y * useBrick.z);
64+
color *= vColor;
65+
66+
gl_FragColor = vec4(color, 1.0);
67+
}
68+
</code></pre>

plugins/show-language/prism-show-language.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if (typeof self === 'undefined' || !self.Prism || !self.document) {
55
}
66

77
// The languages map is built automatically with gulp
8-
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"}/*]*/;
8+
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"}/*]*/;
99
Prism.hooks.add('before-highlight', function(env) {
1010
var pre = env.element.parentNode;
1111
if (!pre || !/pre/i.test(pre.nodeName)) {

plugins/show-language/prism-show-language.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**/
2+
/* foo
3+
bar */
4+
//
5+
// foo
6+
// foo\
7+
bar
8+
9+
----------------------------------------------------
10+
11+
[
12+
["comment", "/**/"],
13+
["comment", "/* foo\r\nbar */"],
14+
["comment", "//"],
15+
["comment", "// foo"],
16+
["comment", "// foo\\\r\nbar"]
17+
]
18+
19+
----------------------------------------------------
20+
21+
Checks for comments.

0 commit comments

Comments
 (0)