lingo3d
Version:
Lingo3D is a React/Vue 3d game development framework that ships with a complete visual editor
125 lines (120 loc) • 5.49 kB
JavaScript
import { GLSL3, Matrix3, ShaderMaterial, TangentSpaceNormalMap, Uniform, Vector2 } from "three";
// WebGL1: will render normals to RGB channel and roughness to A channel
// WebGL2: will render normals to RGB channel of "gNormal" buffer, roughness to A channel of "gNormal" buffer, depth to RGBA channel of "gDepth" buffer
// and velocity to "gVelocity" buffer
export class MRTMaterial extends ShaderMaterial {
constructor() {
super({
type: "MRTMaterial",
defines: {
USE_UV: "",
TEMPORAL_RESOLVE: ""
},
uniforms: {
opacity: new Uniform(1),
normalMap: new Uniform(null),
normalScale: new Uniform(new Vector2(1, 1)),
uvTransform: new Uniform(new Matrix3()),
roughness: new Uniform(1),
roughnessMap: new Uniform(null)
},
vertexShader: /* glsl */ `
varying vec2 vHighPrecisionZW;
varying vec3 vViewPosition;
void main() {
vViewPosition = - mvPosition.xyz;
vHighPrecisionZW = gl_Position.zw;
vUv = ( uvTransform * vec3( uv, 1 ) ).xy;
}
`,
fragmentShader: /* glsl */ `
varying vec3 vViewPosition;
layout(location = 0) out vec4 gNormal;
layout(location = 1) out vec4 gDepth;
varying vec2 vHighPrecisionZW;
uniform float roughness;
void main() {
float roughnessFactor = roughness;
if(roughness > 10.0e9){
roughnessFactor = 1.;
}else{
vec4 texelRoughness = texture2D( roughnessMap, vUv );
// reads channel G, compatible with a combined OcclusionRoughnessMetallic (RGB) texture
roughnessFactor *= texelRoughness.g;
}
vec3 normalColor = packNormalToRGB( normal );
float fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;
vec4 depthColor = packDepthToRGBA( fragCoordZ );
gNormal = vec4( normalColor, roughnessFactor );
gDepth = depthColor;
gl_FragColor = vec4(normalColor, roughnessFactor);
}
`,
toneMapped: false
});
this.normalMapType = TangentSpaceNormalMap;
this.normalScale = new Vector2(1, 1);
Object.defineProperty(this, "glslVersion", {
get() {
return "USE_MRT" in this.defines ? GLSL3 : null;
},
set(_) { }
});
}
}
//# sourceMappingURL=MRTMaterial.js.map