最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Viewing a material's compiled shader code in THREE.js? - Stack Overflow

programmeradmin5浏览0评论

Whenever I'm writing a custom shader in THREE.js and I make a mistake, the console outputs a nice error message with the piled GLSL code. It includes my code along with all the extra lines that Three.js adds on automatically:

Question:

Is there any way to output the piled vertexShader and fragmentShader of a built-in material? I'm using THREE.MeshLambertMaterial in my project, and I'd like to read the piled GLSL code to understand how it runs under the hood. I know that I can go to the library's \src\renderers\shaders\ShaderLib\meshlambert_frag.glsl but it has dozens of #include lines and does not have all the extra attributes/uniforms that THREE.js automatically appends.

I've tried to console.log the material's properties after first render, but I cannot find the piled GLSL code:

var firstRender = true;

function update(){
    renderer.render(scene, camera);

    if(firstRender === true){
        // Where in myMaterial is the GLSL code stored?
        console.log(myMaterial.program.vertexShader);
        firstRender = false;
    }
}

Whenever I'm writing a custom shader in THREE.js and I make a mistake, the console outputs a nice error message with the piled GLSL code. It includes my code along with all the extra lines that Three.js adds on automatically:

Question:

Is there any way to output the piled vertexShader and fragmentShader of a built-in material? I'm using THREE.MeshLambertMaterial in my project, and I'd like to read the piled GLSL code to understand how it runs under the hood. I know that I can go to the library's \src\renderers\shaders\ShaderLib\meshlambert_frag.glsl but it has dozens of #include lines and does not have all the extra attributes/uniforms that THREE.js automatically appends.

I've tried to console.log the material's properties after first render, but I cannot find the piled GLSL code:

var firstRender = true;

function update(){
    renderer.render(scene, camera);

    if(firstRender === true){
        // Where in myMaterial is the GLSL code stored?
        console.log(myMaterial.program.vertexShader);
        firstRender = false;
    }
}
Share Improve this question edited Aug 30, 2017 at 21:21 genpfault 52.2k12 gold badges91 silver badges151 bronze badges asked Aug 30, 2017 at 20:48 M -M - 28.6k12 gold badges54 silver badges92 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can not only view the three.js shader source code, you can edit it live in the browser using @spite's shader editor extension for Chrome.

It's (as far as I can tell) not possible to programmatically access the final code that is uploaded to the GPU. The only place where it exists is here in the WebGLProgram class. Depending on your use-case you could do one of these things:

  • use a non-minified build of three.js and set a debugger-breakpoint in the WebGLProgram-class (stepping through the code in a debugger is also an excellent way to understand what three.js does).
  • install http://spector.babylonjs./ or http://benvanik.github.io/WebGL-Inspector/ to inspect the shader-code that is executed.
发布评论

评论列表(0)

  1. 暂无评论