gpt4 book ai didi

javascript - 三个js : make custom shader match the standard rendering

转载 作者:行者123 更新时间:2023-11-29 15:23:56 25 4
gpt4 key购买 nike

出于性能原因,我需要在一个场景中显示数百个移动的四面体。我在那里使用需要自定义着色器的 instancedbuffergeometry。

场景还包含具有规则几何形状(非缓冲区)的对象和一些灯光(我准备了精简代码片段:https://jsfiddle.net/negan/xs3k1yz4/)。

我的问题是四面体没有被遮蔽,因此它们的光照似乎适合场景的其余部分。原因可能是我构建的原始着色器:

<script id="vertexShader" type="x-shader/x-vertex">
attribute vec3 offset;
attribute vec4 color;
varying vec4 vColor;
varying vec3 vNormal;
void main() {
vColor = color;
vNormal = normalMatrix * vec3(normal);
gl_Position = projectionMatrix *
modelViewMatrix *
vec4(position*1.0+offset,1.0);
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
varying vec4 vColor;
varying vec3 vNormal;

void main() {
float di = 0.4*dot(vNormal,normalize(vec3(1.,1.,0.)))+0.4*dot(vNormal,normalize(vec3(1.,0.,1.)));
di = di+0.2;
vec4 vColor2= vColor*vec4(1.0,1.,1.,0.2)*di;
gl_FragColor = vColor2;// adjust the alpha
}
</script>

有没有办法让自定义着色器适合我在场景中定义的灯光?着色器还以不产生定向光印象的方式渲染面孔。我宁愿让单个面均匀地照亮,而不是从顶点插值颜色,但我无法实现。

感谢任何指点或帮助。

最佳答案

Here's a gist of the full fragment and vertex shader source对于 Three.js MeshPhongMaterial 着色器,从 r83 开始。

Three.js 使用字符串连接系统构建着色器,因此几乎不可能通过查看 Three.js 源代码来找出着色器的源代码。

上面的 Gist 是通过安装 shader editor Chrome extension 生成的,转到具有 MeshPhongMaterial like this one 的 Three.js 示例页面并使用着色器编辑器检查正在运行的着色器程序的完整源代码:

shader editor source

Three.js 将所有默认制服(如光照数据)传递给所有着色器程序,因此如果您使用上述 Gist 代码创建自定义着色器,灯光、骨骼等都将自动工作。

我会获取完整的源代码并手动添加您的逻辑,将您的计算结果直接添加到现有的 gl_FragColor = ...

以后我的工具ShaderFrog将使您自动将任何 Three.js 着色器功能(灯光、骨骼、变形目标等)添加到任何自定义着色器。 ShaderFrog 已经可以自动将任何着色器组合在一起,但我还没有完成完全支持三个功能所需的手动工作。

关于javascript - 三个js : make custom shader match the standard rendering,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41212490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com