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

时间:2014-11-18 09:56:04

标签: 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 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*/
             }

0 个答案:

没有答案