首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何向实例化着色器添加照明

如何向实例化着色器添加照明
EN

Stack Overflow用户
提问于 2017-09-23 05:11:43
回答 1查看 967关注 0票数 1

如何将照明(环境光+平行光)添加到与InstancedBufferGeometry一起使用的着色器?

例如,我想将光照添加到下面的代码:https://threejs.org/examples/?q=inst#webgl_buffergeometry_instancing_dynamic

这是我的顶点着色器:

precision highp float;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat3 normalMatrix;
attribute vec3 position;
attribute vec3 offset;
attribute vec3 normal;
attribute vec2 uv;
attribute vec4 orientation;
varying vec2 vUv;

// lighting
struct DirectionalLight {
    vec3 direction;
    vec3 color;
    int shadow;
    float shadowBias;
    float shadowRadius;
    vec2 shadowMapSize;
    };
uniform DirectionalLight directionalLights[ NUM_DIR_LIGHTS ];
uniform vec3 ambientLightColor;
varying vec3 vLightFactor;
//

void main() {
    vec3 vPosition = position;
    vec3 vcV = cross(orientation.xyz, vPosition);
    vPosition = vcV * (2.0 * orientation.w) + (cross(orientation.xyz, vcV) * 2.0 + vPosition);
    vUv = uv;
    gl_Position = projectionMatrix * modelViewMatrix * vec4( offset + vPosition, 1.0 );

    // lighting
    vec4 ecPosition = modelViewMatrix*vec4(offset + vPosition,1.0);
    vec3 ecNormal= -(normalize(normalMatrix*normal));
    vec3 fromLight = normalize(directionalLights[0].direction);
    vec3 toLight = -fromLight;
    vec3 reflectLight = reflect(toLight,ecNormal);
    vec3 viewDir = normalize(-ecPosition.xyz);
    float ndots = dot(ecNormal, toLight);
    float vdotr = max(0.0,dot(viewDir,reflectLight));
    vec3 ambi = ambientLightColor;
    vec3 diff = directionalLights[0].color * ndots;
    vLightFactor = ambi + diff;     
    //

}

这是我的片段着色器:

precision highp float;
uniform sampler2D map;
varying vec2 vUv;

// lighting
varying vec3 vLightFactor;
//

void main() {
    gl_FragColor = texture2D(map, vUv) * vec4(vLightFactor,1.0);
}

这是我的材料:

var uniforms = Object.assign( 
    THREE.UniformsLib['lights'], 
    {
    map: { value: texture }
    }
);  

var material = new THREE.RawShaderMaterial({
    lights: true,
    uniforms: uniforms, 
    vertexShader: document.getElementById( 'vertexShader' ).textContent,
    fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
});

谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-02-15 21:21:39

我的意思是片段着色器应该如下所示:

precision highp float;

varying vec2 vUv;
varying vec3 ecPosition;
varying vec3 ecNormal;

uniform sampler2D map;
uniform mat4 modelViewMatrix;

struct DirectionalLight {
    vec3 direction;
    vec3 color;
    int shadow;
    float shadowBias;
    float shadowRadius;
    vec2 shadowMapSize;
};
uniform DirectionalLight directionalLights[ NUM_DIR_LIGHTS ];
uniform vec3 ambientLightColor;

void main()
{
    // ambient light
    vec3 lightFactor = ambientLightColor;

    // diffuse light
    vec3  ecFromLight    = normalize(directionalLights[0].direction);
    //vec3  ecToLight      = -ecFromLight;
    float NdotL          = max(0.0, dot(ecNormal, ecFromLight));
    lightFactor         += NdotL * directionalLights[0].color; 

    // specular light
    /*
    float shininess = 10.01;
    vec3  ecReflectLight = reflect( ecFromLight, ecNormal );
    vec3  ecViewDir      = normalize(-ecPosition.xyz);
    float VdotR          = max(0.0, dot(ecViewDir, ecReflectLight));
    float kSpecular      = 4.0 * pow( VdotR, 0.3 * shininess ); // <--- set up shininess parameter
    lightFactor         += kSpecular * directionalLights[0].color;
    */

    gl_FragColor = texture2D(map, vUv) * vec4(lightFactor, 1.0);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46373271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档