【发布时间】:2018-08-26 17:03:18
【问题描述】:
我开始着手编写着色器,使用 THREE.js 构建 OpenGL 场景以使用用 GLSL 编写的着色器非常方便。到目前为止,我一直在主 HTML 文件的脚本字段中包含 GLSL 代码:
<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>
我正在处理一个更复杂的着色器项目,并希望将主 html 文件拆分,以便着色器代码可以单独保存,但我发现这很难做到。
我尝试使用 jquery 读取存储在单独文本文件中的着色器代码(我正在运行本地服务器):
<script src ="node_modules/jquery/dist/jquery.js"></script>
<script>
$(function(){
$("#fragShader").load("mainShader.html"); // contains GLSL code
});
</script>
<script id="fragShader"></script>
但着色器无法编译 - fragment ERROR: Missing main()。
非常感谢对这个 js 新手的任何帮助!
【问题讨论】:
标签: javascript html three.js glsl