I'm starting to get to grips with writing shaders and it has been very convenient to use THREE.js to build an OpenGL scene in order to play around with shaders written in GLSL. Up to now I have been including the GLSL code in a script field within the main HTML file:
<html>
<head>
<meta charset=uft-8>
<title>Babby's first three.js app</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<script src="js/three.js"></script>
<script id="fragShader" type="shader-code">
// GLSL shader goes here!
void main(){
/* shader body */
}
</script>
<script>
// Three.js scene setup goes here
// Get shader code from fragShader script element
var shaderCode = document.getElementById("fragShader").innerHTML;
// Apply it to a material
bufferMaterial = new THREE.ShaderMaterial({
uniforms : {
bufferTexture : {type : "t", value : textureA},
},
fragmentShader : shaderCode
});
// Render everything
</script>
</body>
I'm working on a more complex shader project and would like to split the main html file up so that shader code can be kept seperate but I've found this surprisingly hard to do.
I've tried using jquery to read the shader code stored in a seperate text file (I'm running a local server):
<script src ="node_modules/jquery/dist/jquery.js"></script>
<script>
$(function(){
$("#fragShader").load("mainShader.html"); // contains GLSL code
});
</script>
<script id="fragShader"></script>
But the shader cannot compile - fragment ERROR: Missing main()
.
Any help for this js novice is much appreciated!