[英]Passing PointLight Info to a custom Shader with three.js
我想創建一種類似於Aerotwist教程中所述的起伏球體的效果。 但是,在教程中,Paul在片段着色器中創建了一個偽造的GLSL硬編碼光源-相反,我想將信息從Three.js PointLight實例傳遞給我的着色器,操縱頂點/法線,然后執行Phong着色。
我對在three.js中的場景進行着色時對GPU考慮的各個級別的理解如下(例如,堅持使用Phong):
問題1:以上理解是否正確?
問題2:是否有辦法在2級和3級之間做某事? 我希望能夠自定義着色器以使頂點位置/法線混亂,但是當three.js包含一個非常好的着色器時,我不想編寫自己的Phong着色器。
問題3:如果在第2級和第3級之間沒有中間立場,而我只需要進入第3級,那么實現這一目標的最佳方法是什么? 我是否要以均勻的形式傳遞燈光的位置,強度等,是否進行頂點/法線修改,然后最后明確編寫Phong陰影計算?
使用three.js來完成您的要求非常簡單
我不確定它在您的Q []中的位置
Q1
現在,您將如何執行此操作。 假設您正在遵循本教程,並且擁有此着色器:
// same name and type as VS
varying vec3 vNormal;
void main() {
//this is hardcoded you want to pass it from your environment
vec3 light = vec3(0.5, 0.2, 1.0);//it needs to be a uniform
// ensure it's normalized
light = normalize(light);//you can normalize it outside of the shader, since it's a directional light
// calculate the dot product of
// the light to the vertex normal
float dProd = max(0.0,
dot(vNormal, light));
// feed into our frag colour
gl_FragColor = vec4(dProd, // R
dProd, // G
dProd, // B
1.0); // A
}
這是您需要做的:
GLSL
uniform vec3 myLightPos;//comes in
void main(){
vec3 light = normalize(myLightPos);//but you better do this in javascript and just pass the normalized vec3
}
Java腳本
new THREE.ShaderMaterial({
uniforms:{
myLightPos:{
type:"v3",
value: new THREE.Vector3()
}
},
vertexShader: yourVertShader,
fragmentShader: yourFragmentShader
});
問題1:正確。 雖然,此板上的某些用戶已發布了駭MeshPhongMaterial
的變通辦法,但這並不是其初衷。
Q2和Q3:查看ShaderLib.js
,您將看到“法線貼圖着色器”。 這是您的理想模板。 是的,您可以復制/重命名並根據自己的喜好對其進行修改。
它使用基於Phong的照明模型,甚至可以為您訪問場景燈。 您這樣稱呼它:
var shader = THREE.ShaderLib[ "normalmap" ];
var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
. . .
var parameters = {
fragmentShader: shader.fragmentShader,
vertexShader: shader.vertexShader,
uniforms: uniforms,
lights: true // set this flag and you have access to scene lights
};
var material = new THREE.ShaderMaterial( parameters );
請參閱以下示例: http : //threejs.org/examples/webgl_materials_normalmap.html和http://threejs.org/examples/webgl_materials_normalmap2.html 。
有關要遵循的編碼模式,請參見ShaderLib.js
和ShaderChunk.js
。
three.js r.67
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.