WebGL and HTML shader-type

2019-06-15 07:26发布

I declare my GLSL ES shader program within a HTML file, using this code:

<script id="shader-fs" type="x-shader/x-fragment">..shader-code..</script>

as seen in the learning WebGL examples. Everything works fine, but I dont understand why I should use the type attribute of the script tag. I want to know where the "x-shader/x-fragment" value is specified. Who does that.. the W3C, the Khronos Group or the browser developers? Can anybody help me? Tahnk you.

2条回答
老娘就宠你
2楼-- · 2019-06-15 07:37

The idea is that the browser doensn't know the type 'x-shader/x-fragment'. Your code will work fine if you change the type to something else (like 'foo').

In other words there is no standard for how to store shader source code. But you will need it as a string when the shaders are compiled.

时光不老,我们不散
3楼-- · 2019-06-15 07:57

There is no official organization who specified that GLSL code should be put within a <script> tag of type "x-shader/x-fragment".

The only reason the GLSL code is placed within that <script> tag is because the tutorial writer decided his code would be cleaner if he placed the GLSL code within a <script> tag rather than a normal string.

However since WebGL takes in GLSL code as a string value, the author had to write a helper function called getShader(gl, id) to grab the script tag from the page and convert it into a javascript string, before passing it to WebGL.

The reason the author chose a type value of "x-shader/x-fragment" is because "x-shader/x-fragment" is not a known script type by the browser and thus would be safely ignored by the browser.

查看更多
登录 后发表回答