THREE.JS法线Shader
以普通情况而论
vNormal = normal;
//...
gl_FragColor = vec4( vNormal, 1. );
vNormal = normal;
//...
gl_FragColor = vec4( normalize( vNormal ) * 0.5 + 0.5, 1. );
vNormal = normalMatrix * normal;
//...
gl_FragColor = vec4( normalize( vNormal ) * 0.5 + 0.5, 1. );
normalMatrix
threejs会自动传递至uniform
import * as THREE from "three";
export class NormalShaderMaterial extends THREE.ShaderMaterial {
constructor(params?: ConstructorParameters<typeof THREE.ShaderMaterial>[0]) {
super({
uniforms: {},
vertexShader: /* glsl */ `
varying vec3 vNormal;
void main() {
vNormal = normalMatrix * normal;
vec4 modelViewPosition = modelViewMatrix * vec4(position, 1.0);
gl_Position = projectionMatrix * modelViewPosition;
}`,
fragmentShader: /* glsl */ `
varying vec3 vNormal;
void main() {
gl_FragColor = vec4( normalize( vNormal ) * 0.5 + 0.5, 1. );
}`,
});
}
}
原文地址:https://blog.csdn.net/printf_hello/article/details/142597807
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!