Glsl WebGL中的片段着色器-灯光建模

Glsl WebGL中的片段着色器-灯光建模,glsl,webgl,Glsl,Webgl,我写了一个片段着色器,主要是为了模仿其他示例。几乎每一条线都是清晰的,但我想了解这是否可以被视为每片段照明或只是顶点照明。一般来说,我该如何改进这一点 precision mediump float; uniform sampler2D sampler; /*Values passed from application via uniform1i*/ varying vec2 f_uv; //Texture uv coordinates passed from application const

我写了一个片段着色器,主要是为了模仿其他示例。几乎每一条线都是清晰的,但我想了解这是否可以被视为每片段照明或只是顶点照明。一般来说,我该如何改进这一点

precision mediump float;
uniform sampler2D sampler; /*Values passed from application via uniform1i*/
varying vec2 f_uv; //Texture uv coordinates passed from application
const vec3 source_ambient_color = vec3(0.8,0.8,0.8);/*Source light*/
const vec3 source_diffuse_color = vec3(0.5,0.5,0.5);
const vec3 source_specular_color = vec3(1.,1.,1.);
const vec3 source_direction = vec3(0.,0.,1.);/*source position (is this a point light?)*/
const vec3 material_ambient_color = vec3 (0.8,0.8,0.8);/*Material properties*/
const vec3 material_diffuse_color = vec3 (0.5,0.5,0.5);
const vec3 material_specular_color = vec3 (1.,1.,1.);
const float material_glossiness = 10.;/*Glossiness*/
varying vec3 v_normal;/*Values of vertices normals, coming from application*/
varying vec3 vView;/*Camera View*/

void main()  {
             vec3 color = vec3(texture2D(sampler,f_uv)); /*assigns to the variable color the pixel of the texure*/
             vec3 I_ambient = source_ambient_color*material_ambient_color;/*calculate by multiply the source ambient by the material ambient (I think they'd better share the same value, for realistic result)*/
             vec3 I_diffuse = source_diffuse_color*material_diffuse_color*max(0.,dot(v_normal, source_direction));/*calculate the scalar product of the angle between viewing direction and light position to produce a correct diffuse*/
             vec3 V = normalize(vView); /*normalize viewing vector to calculate angle later*/ 
             vec3 R = reflect(source_direction,v_normal);/*I believe this returns the angle between viewing direction and light position and computes the reflection*/
             vec3 I_specular = source_specular_color*material_specular_color*pow(max(dot(R,V),0.), material_glossiness); /*scalar product of viewing and reflect (I believe to compute the intensity of the specular effect*/
             vec3 I = I_ambient+I_diffuse+I_specular;/*add the previous calculation togheter to produce a light model*/
             gl_FragColor = vec4(I*color,1);/*multiply the texture color by the lighting model to simulate the lighting*/
             }

您正在碎片着色器中计算照明,因此它是每碎片或每像素照明。如果要在顶点着色器中计算照明并插值生成的颜色,那么它将是逐顶点的。为了改进它,我修改了*/sourcelight/*注释。你希望它做什么?只是一个注释错误,我已经在编辑中修复了它。我只是想知道是否还有其他的灯光模型可以应用。考虑到我的例子是一个有效的起点,我想说这个问题太宽泛了。您已经编写了每像素phong着色器。是的,有很多照明模型。材料建模的领域是巨大的,非常有趣,因此您可能想阅读更多关于它的内容。Phong很常见,除非你看到你需要的其他东西,否则我会坚持使用它。不久前我处于你的位置,所以这里有一些建议,建议从那里开始:1添加gamma校正;2增加衰减;3在灯光照亮的地方增加限制,让聚光灯4编写javascript包装器/管理器来处理灯光/材料制服;5实现多个灯光6添加阴影;哦,仅仅添加每像素Phong着色对于大多数应用来说已经相当不错了,下一个最大的改进就是法线贴图。这就是我想要的答案。如果你有关于这个的链接,把它们扔进去。谢谢瓦克雅斯珀。