diff --git a/src/renderers/shaders/ShaderLib.js b/src/renderers/shaders/ShaderLib.js index f22b73fd41306d..484a20df5d75ff 100644 --- a/src/renderers/shaders/ShaderLib.js +++ b/src/renderers/shaders/ShaderLib.js @@ -19,100 +19,8 @@ THREE.ShaderLib = { ] ), - vertexShader: [ - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "void main() {", - - '#include ', - '#include ', - '#include ', - '#include ', - - " #ifdef USE_ENVMAP", - - '#include ', - '#include ', - '#include ', - '#include ', - - " #endif", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - '#include ', - '#include ', - - "}" - - ].join( "\n" ), - - fragmentShader: [ - - "uniform vec3 diffuse;", - "uniform float opacity;", - - "#ifndef FLAT_SHADED", - - " varying vec3 vNormal;", - - "#endif", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "void main() {", - - " vec4 diffuseColor = vec4( diffuse, opacity );", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - " ReflectedLight reflectedLight;", - " reflectedLight.directDiffuse = vec3( 0.0 );", - " reflectedLight.directSpecular = vec3( 0.0 );", - " reflectedLight.indirectDiffuse = diffuseColor.rgb;", - " reflectedLight.indirectSpecular = vec3( 0.0 );", - - '#include ', - - " vec3 outgoingLight = reflectedLight.indirectDiffuse;", - - '#include ', - '#include ', - '#include ', - - " gl_FragColor = vec4( outgoingLight, diffuseColor.a );", - - "}" - - ].join( "\n" ) + vertexShader: THREE.ShaderChunk['meshbasic_vert'], + fragmentShader: THREE.ShaderChunk['meshbasic_frag'] }, @@ -134,140 +42,8 @@ THREE.ShaderLib = { ] ), - vertexShader: [ - - "#define LAMBERT", - - "varying vec3 vLightFront;", - - "#ifdef DOUBLE_SIDED", - - " varying vec3 vLightBack;", - - "#endif", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "void main() {", - - '#include ', - '#include ', - '#include ', - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - '#include ', - '#include ', - '#include ', - '#include ', - - "}" - - ].join( "\n" ), - - fragmentShader: [ - - "uniform vec3 diffuse;", - "uniform vec3 emissive;", - "uniform float opacity;", - - "varying vec3 vLightFront;", - - "#ifdef DOUBLE_SIDED", - - " varying vec3 vLightBack;", - - "#endif", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "void main() {", - - " vec4 diffuseColor = vec4( diffuse, opacity );", - " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );", - " vec3 totalEmissiveLight = emissive;", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - // accumulation - " reflectedLight.indirectDiffuse = getAmbientLightIrradiance( ambientLightColor );", - - '#include ', - - " reflectedLight.indirectDiffuse *= BRDF_Diffuse_Lambert( diffuseColor.rgb );", - - " #ifdef DOUBLE_SIDED", - - " reflectedLight.directDiffuse = ( gl_FrontFacing ) ? vLightFront : vLightBack;", - - " #else", - - " reflectedLight.directDiffuse = vLightFront;", - - " #endif", - - " reflectedLight.directDiffuse *= BRDF_Diffuse_Lambert( diffuseColor.rgb ) * getShadowMask();", - - // modulation - '#include ', - - " vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveLight;", - - '#include ', - - '#include ', - - '#include ', - - " gl_FragColor = vec4( outgoingLight, diffuseColor.a );", - - "}" - - ].join( "\n" ) + vertexShader: THREE.ShaderChunk['meshlambert_vert'], + fragmentShader: THREE.ShaderChunk['meshlambert_frag'] }, @@ -294,132 +70,8 @@ THREE.ShaderLib = { ] ), - vertexShader: [ - - "#define PHONG", - - "varying vec3 vViewPosition;", - - "#ifndef FLAT_SHADED", - - " varying vec3 vNormal;", - - "#endif", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "void main() {", - - '#include ', - '#include ', - '#include ', - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED - - " vNormal = normalize( transformedNormal );", - - "#endif", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - " vViewPosition = - mvPosition.xyz;", - - '#include ', - '#include ', - '#include ', - '#include ', - - "}" - - ].join( "\n" ), - - fragmentShader: [ - - "#define PHONG", - - "uniform vec3 diffuse;", - "uniform vec3 emissive;", - "uniform vec3 specular;", - "uniform float shininess;", - "uniform float opacity;", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "void main() {", - - " vec4 diffuseColor = vec4( diffuse, opacity );", - " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );", - " vec3 totalEmissiveLight = emissive;", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - // accumulation - '#include ', - '#include ', - - // modulation - '#include ', - - "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveLight;", - - '#include ', - '#include ', - - '#include ', - - " gl_FragColor = vec4( outgoingLight, diffuseColor.a );", - - "}" - - ].join( "\n" ) + vertexShader: THREE.ShaderChunk['meshphong_vert'], + fragmentShader: THREE.ShaderChunk['meshphong_frag'] }, @@ -449,143 +101,8 @@ THREE.ShaderLib = { ] ), - vertexShader: [ - - "#define STANDARD", - - "varying vec3 vViewPosition;", - - "#ifndef FLAT_SHADED", - - " varying vec3 vNormal;", - - "#endif", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "void main() {", // STANDARD - - '#include ', - '#include ', - '#include ', - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "#ifndef FLAT_SHADED", // Normal computed with derivatives when FLAT_SHADED - - " vNormal = normalize( transformedNormal );", - - "#endif", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - " vViewPosition = - mvPosition.xyz;", - - '#include ', - '#include ', - '#include ', - - "}" - - ].join( "\n" ), - - fragmentShader: [ - - "#define STANDARD", - - "uniform vec3 diffuse;", - "uniform vec3 emissive;", - "uniform float roughness;", - "uniform float metalness;", - "uniform float opacity;", - - "uniform float envMapIntensity;", // temporary - - "varying vec3 vViewPosition;", - - "#ifndef FLAT_SHADED", - - " varying vec3 vNormal;", - - "#endif", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "void main() {", - - " vec4 diffuseColor = vec4( diffuse, opacity );", - " ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );", - " vec3 totalEmissiveLight = emissive;", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - // accumulation - '#include ', - '#include ', - - // modulation - '#include ', - - "vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveLight;", - - '#include ', - - '#include ', - - " gl_FragColor = vec4( outgoingLight, diffuseColor.a );", - - "}" - - ].join( "\n" ) + vertexShader: THREE.ShaderChunk['meshstandard_vert'], + fragmentShader: THREE.ShaderChunk['meshstandard_frag'] }, @@ -598,68 +115,8 @@ THREE.ShaderLib = { ] ), - vertexShader: [ - - "uniform float size;", - "uniform float scale;", - - '#include ', - '#include ', - '#include ', - '#include ', - - "void main() {", - - '#include ', - '#include ', - '#include ', - - " #ifdef USE_SIZEATTENUATION", - " gl_PointSize = size * ( scale / - mvPosition.z );", - " #else", - " gl_PointSize = size;", - " #endif", - - '#include ', - '#include ', - '#include ', - - "}" - - ].join( "\n" ), - - fragmentShader: [ - - "uniform vec3 diffuse;", - "uniform float opacity;", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "void main() {", - - " vec3 outgoingLight = vec3( 0.0 );", - " vec4 diffuseColor = vec4( diffuse, opacity );", - - '#include ', - '#include ', - '#include ', - '#include ', - - " outgoingLight = diffuseColor.rgb;", - - '#include ', - - " gl_FragColor = vec4( outgoingLight, diffuseColor.a );", - - "}" - - ].join( "\n" ) + vertexShader: THREE.ShaderChunk['points_vert'], + fragmentShader: THREE.ShaderChunk['points_frag'] }, @@ -678,70 +135,8 @@ THREE.ShaderLib = { ] ), - vertexShader: [ - - "uniform float scale;", - "attribute float lineDistance;", - - "varying float vLineDistance;", - - '#include ', - '#include ', - '#include ', - - "void main() {", - - '#include ', - - " vLineDistance = scale * lineDistance;", - - " vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );", - " gl_Position = projectionMatrix * mvPosition;", - - '#include ', - - "}" - - ].join( "\n" ), - - fragmentShader: [ - - "uniform vec3 diffuse;", - "uniform float opacity;", - - "uniform float dashSize;", - "uniform float totalSize;", - - "varying float vLineDistance;", - - '#include ', - '#include ', - '#include ', - '#include ', - - "void main() {", - - " if ( mod( vLineDistance, totalSize ) > dashSize ) {", - - " discard;", - - " }", - - " vec3 outgoingLight = vec3( 0.0 );", - " vec4 diffuseColor = vec4( diffuse, opacity );", - - '#include ', - '#include ', - - " outgoingLight = diffuseColor.rgb;", // simple shader - - '#include ', - - " gl_FragColor = vec4( outgoingLight, diffuseColor.a );", - - "}" - - ].join( "\n" ) + vertexShader: THREE.ShaderChunk['linedashed_vert'], + fragmentShader: THREE.ShaderChunk['linedashed_frag'] }, @@ -755,52 +150,8 @@ THREE.ShaderLib = { }, - vertexShader: [ - - '#include ', - '#include ', - '#include ', - - "void main() {", - - '#include ', - '#include ', - '#include ', - '#include ', - - "}" - - ].join( "\n" ), - - fragmentShader: [ - - "uniform float mNear;", - "uniform float mFar;", - "uniform float opacity;", - - '#include ', - '#include ', - - "void main() {", - - '#include ', - - " #ifdef USE_LOGDEPTHBUF_EXT", - - " float depth = gl_FragDepthEXT / gl_FragCoord.w;", - - " #else", - - " float depth = gl_FragCoord.z / gl_FragCoord.w;", - - " #endif", - - " float color = 1.0 - smoothstep( mNear, mFar, depth );", - " gl_FragColor = vec4( vec3( color ), opacity );", - - "}" - - ].join( "\n" ) + vertexShader: THREE.ShaderChunk['depth_vert'], + fragmentShader: THREE.ShaderChunk['depth_frag'] }, @@ -812,44 +163,8 @@ THREE.ShaderLib = { }, - vertexShader: [ - - "varying vec3 vNormal;", - - '#include ', - '#include ', - '#include ', - - "void main() {", - - " vNormal = normalize( normalMatrix * normal );", - - '#include ', - '#include ', - '#include ', - '#include ', - - "}" - - ].join( "\n" ), - - fragmentShader: [ - - "uniform float opacity;", - "varying vec3 vNormal;", - - '#include ', - '#include ', - - "void main() {", - - " gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );", - - '#include ', - - "}" - - ].join( "\n" ) + vertexShader: THREE.ShaderChunk['normal_vert'], + fragmentShader: THREE.ShaderChunk['normal_frag'] }, @@ -864,44 +179,8 @@ THREE.ShaderLib = { "tFlip": { type: "f", value: - 1 } }, - vertexShader: [ - - "varying vec3 vWorldPosition;", - - '#include ', - '#include ', - - "void main() {", - - " vWorldPosition = transformDirection( position, modelMatrix );", - - " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );", - - '#include ', - - "}" - - ].join( "\n" ), - - fragmentShader: [ - - "uniform samplerCube tCube;", - "uniform float tFlip;", - - "varying vec3 vWorldPosition;", - - '#include ', - '#include ', - - "void main() {", - - " gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) );", - - '#include ', - - "}" - - ].join( "\n" ) + vertexShader: THREE.ShaderChunk['cube_vert'], + fragmentShader: THREE.ShaderChunk['cube_frag'] }, @@ -916,49 +195,8 @@ THREE.ShaderLib = { "tFlip": { type: "f", value: - 1 } }, - vertexShader: [ - - "varying vec3 vWorldPosition;", - - '#include ', - '#include ', - - "void main() {", - - " vWorldPosition = transformDirection( position, modelMatrix );", - - " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );", - - '#include ', - - "}" - - ].join( "\n" ), - - fragmentShader: [ - - "uniform sampler2D tEquirect;", - "uniform float tFlip;", - - "varying vec3 vWorldPosition;", - - '#include ', - '#include ', - - "void main() {", - - // " gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) );", - "vec3 direction = normalize( vWorldPosition );", - "vec2 sampleUV;", - "sampleUV.y = saturate( tFlip * direction.y * -0.5 + 0.5 );", - "sampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;", - "gl_FragColor = texture2D( tEquirect, sampleUV );", - - '#include ', - - "}" - - ].join( "\n" ) + vertexShader: THREE.ShaderChunk['equirect_vert'], + fragmentShader: THREE.ShaderChunk['equirect_frag'] }, @@ -978,64 +216,8 @@ THREE.ShaderLib = { uniforms: {}, - vertexShader: [ - - '#include ', - '#include ', - '#include ', - '#include ', - - "void main() {", - - '#include ', - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "}" - - ].join( "\n" ), - - fragmentShader: [ - - '#include ', - '#include ', - - "vec4 pack_depth( const in float depth ) {", - - " const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );", - " const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );", - " vec4 res = mod( depth * bit_shift * vec4( 255 ), vec4( 256 ) ) / vec4( 255 );", - " res -= res.xxyz * bit_mask;", - " return res;", - - "}", - - "void main() {", - - '#include ', - - " #ifdef USE_LOGDEPTHBUF_EXT", - - " gl_FragData[ 0 ] = pack_depth( gl_FragDepthEXT );", - - " #else", - - " gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );", - - " #endif", - - //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );", - //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );", - //"gl_FragData[ 0 ] = pack_depth( z );", - //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );", - - "}" - - ].join( "\n" ) + vertexShader: THREE.ShaderChunk['depthRGBA_vert'], + fragmentShader: THREE.ShaderChunk['depthRGBA_frag'] }, @@ -1048,61 +230,8 @@ THREE.ShaderLib = { }, - vertexShader: [ - - "varying vec4 vWorldPosition;", - - '#include ', - '#include ', - '#include ', - - "void main() {", - - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - '#include ', - - "vWorldPosition = worldPosition;", - - "}" - - ].join( "\n" ), - - fragmentShader: [ - - "uniform vec3 lightPos;", - "varying vec4 vWorldPosition;", - - '#include ', - - "vec4 pack1K ( float depth ) {", - - " depth /= 1000.0;", - " const vec4 bitSh = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );", - " const vec4 bitMsk = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );", - " vec4 res = mod( depth * bitSh * vec4( 255 ), vec4( 256 ) ) / vec4( 255 );", - " res -= res.xxyz * bitMsk;", - " return res; ", - - "}", - - "float unpack1K ( vec4 color ) {", - - " const vec4 bitSh = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );", - " return dot( color, bitSh ) * 1000.0;", - - "}", - - "void main () {", - - " gl_FragColor = pack1K( length( vWorldPosition.xyz - lightPos.xyz ) );", - - "}" - - ].join( "\n" ) + vertexShader: THREE.ShaderChunk['distanceRGBA_vert'], + fragmentShader: THREE.ShaderChunk['distanceRGBA_frag'] } diff --git a/src/renderers/shaders/ShaderLib/cube_frag.glsl b/src/renderers/shaders/ShaderLib/cube_frag.glsl new file mode 100644 index 00000000000000..c277332838a02d --- /dev/null +++ b/src/renderers/shaders/ShaderLib/cube_frag.glsl @@ -0,0 +1,15 @@ +uniform samplerCube tCube; +uniform float tFlip; + +varying vec3 vWorldPosition; + +#include +#include + +void main() { + + gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) ); + + #include + +} diff --git a/src/renderers/shaders/ShaderLib/cube_vert.glsl b/src/renderers/shaders/ShaderLib/cube_vert.glsl new file mode 100644 index 00000000000000..ef1766621d4ce5 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/cube_vert.glsl @@ -0,0 +1,14 @@ +varying vec3 vWorldPosition; + +#include +#include + +void main() { + + vWorldPosition = transformDirection( position, modelMatrix ); + + gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); + + #include + +} diff --git a/src/renderers/shaders/ShaderLib/depthRGBA_frag.glsl b/src/renderers/shaders/ShaderLib/depthRGBA_frag.glsl new file mode 100644 index 00000000000000..13e53eaaad8292 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/depthRGBA_frag.glsl @@ -0,0 +1,33 @@ +#include +#include + +vec4 pack_depth( const in float depth ) { + + const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 ); + const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 ); + vec4 res = mod( depth * bit_shift * vec4( 255 ), vec4( 256 ) ) / vec4( 255 ); + res -= res.xxyz * bit_mask; + return res; + +} + +void main() { + + #include + + #ifdef USE_LOGDEPTHBUF_EXT + + gl_FragData[ 0 ] = pack_depth( gl_FragDepthEXT ); + + #else + + gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z ); + + #endif + + //gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w ); + //float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 ); + //gl_FragData[ 0 ] = pack_depth( z ); + //gl_FragData[ 0 ] = vec4( z, z, z, 1.0 ); + +} diff --git a/src/renderers/shaders/ShaderLib/depthRGBA_vert.glsl b/src/renderers/shaders/ShaderLib/depthRGBA_vert.glsl new file mode 100644 index 00000000000000..17c26f48c2e0c7 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/depthRGBA_vert.glsl @@ -0,0 +1,16 @@ +#include +#include +#include +#include + +void main() { + + #include + + #include + #include + #include + #include + #include + +} diff --git a/src/renderers/shaders/ShaderLib/depth_frag.glsl b/src/renderers/shaders/ShaderLib/depth_frag.glsl new file mode 100644 index 00000000000000..8244133200a9ab --- /dev/null +++ b/src/renderers/shaders/ShaderLib/depth_frag.glsl @@ -0,0 +1,25 @@ +uniform float mNear; +uniform float mFar; +uniform float opacity; + +#include +#include + +void main() { + + #include + + #ifdef USE_LOGDEPTHBUF_EXT + + float depth = gl_FragDepthEXT / gl_FragCoord.w; + + #else + + float depth = gl_FragCoord.z / gl_FragCoord.w; + + #endif + + float color = 1.0 - smoothstep( mNear, mFar, depth ); + gl_FragColor = vec4( vec3( color ), opacity ); + +} diff --git a/src/renderers/shaders/ShaderLib/depth_vert.glsl b/src/renderers/shaders/ShaderLib/depth_vert.glsl new file mode 100644 index 00000000000000..21ad26694b59ab --- /dev/null +++ b/src/renderers/shaders/ShaderLib/depth_vert.glsl @@ -0,0 +1,12 @@ +#include +#include +#include + +void main() { + + #include + #include + #include + #include + +} diff --git a/src/renderers/shaders/ShaderLib/distanceRGBA_frag.glsl b/src/renderers/shaders/ShaderLib/distanceRGBA_frag.glsl new file mode 100644 index 00000000000000..7a08829dd132e9 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/distanceRGBA_frag.glsl @@ -0,0 +1,26 @@ +uniform vec3 lightPos; +varying vec4 vWorldPosition; + +#include + +vec4 pack1K ( float depth ) { + + depth /= 1000.0; + const vec4 bitSh = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 ); + const vec4 bitMsk = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 ); + vec4 res = mod( depth * bitSh * vec4( 255 ), vec4( 256 ) ) / vec4( 255 ); + res -= res.xxyz * bitMsk; + return res; + +} + +float unpack1K ( vec4 color ) { + + const vec4 bitSh = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 ); + return dot( color, bitSh ) * 1000.0; + +} + +void main () { + + gl_FragColor = pack1K( length( vWorldPosition.xyz - lightPos.xyz ) ); diff --git a/src/renderers/shaders/ShaderLib/distanceRGBA_vert.glsl b/src/renderers/shaders/ShaderLib/distanceRGBA_vert.glsl new file mode 100644 index 00000000000000..f968a3f6d4afd1 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/distanceRGBA_vert.glsl @@ -0,0 +1,18 @@ +varying vec4 vWorldPosition; + +#include +#include +#include + +void main() { + + #include + #include + #include + #include + #include + #include + + vWorldPosition = worldPosition; + +} diff --git a/src/renderers/shaders/ShaderLib/equirect_frag.glsl b/src/renderers/shaders/ShaderLib/equirect_frag.glsl new file mode 100644 index 00000000000000..faafc31d307e9e --- /dev/null +++ b/src/renderers/shaders/ShaderLib/equirect_frag.glsl @@ -0,0 +1,20 @@ +uniform sampler2D tEquirect; +uniform float tFlip; + +varying vec3 vWorldPosition; + +#include +#include + +void main() { + + // gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) ); + vec3 direction = normalize( vWorldPosition ); + vec2 sampleUV; + sampleUV.y = saturate( tFlip * direction.y * -0.5 + 0.5 ); + sampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5; + gl_FragColor = texture2D( tEquirect, sampleUV ); + + #include + +} diff --git a/src/renderers/shaders/ShaderLib/equirect_vert.glsl b/src/renderers/shaders/ShaderLib/equirect_vert.glsl new file mode 100644 index 00000000000000..ef1766621d4ce5 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/equirect_vert.glsl @@ -0,0 +1,14 @@ +varying vec3 vWorldPosition; + +#include +#include + +void main() { + + vWorldPosition = transformDirection( position, modelMatrix ); + + gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); + + #include + +} diff --git a/src/renderers/shaders/ShaderLib/linedashed_frag.glsl b/src/renderers/shaders/ShaderLib/linedashed_frag.glsl new file mode 100644 index 00000000000000..710ba75b13eb70 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/linedashed_frag.glsl @@ -0,0 +1,34 @@ +uniform vec3 diffuse; +uniform float opacity; + +uniform float dashSize; +uniform float totalSize; + +varying float vLineDistance; + +#include +#include +#include +#include + +void main() { + + if ( mod( vLineDistance, totalSize ) > dashSize ) { + + discard; + + } + + vec3 outgoingLight = vec3( 0.0 ); + vec4 diffuseColor = vec4( diffuse, opacity ); + + #include + #include + + outgoingLight = diffuseColor.rgb; // simple shader + + #include + + gl_FragColor = vec4( outgoingLight, diffuseColor.a ); + +} diff --git a/src/renderers/shaders/ShaderLib/linedashed_vert.glsl b/src/renderers/shaders/ShaderLib/linedashed_vert.glsl new file mode 100644 index 00000000000000..14c0254744d225 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/linedashed_vert.glsl @@ -0,0 +1,21 @@ +uniform float scale; +attribute float lineDistance; + +varying float vLineDistance; + +#include +#include +#include + +void main() { + + #include + + vLineDistance = scale * lineDistance; + + vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); + gl_Position = projectionMatrix * mvPosition; + + #include + +} diff --git a/src/renderers/shaders/ShaderLib/meshbasic_frag.glsl b/src/renderers/shaders/ShaderLib/meshbasic_frag.glsl new file mode 100644 index 00000000000000..8d6acc10311925 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/meshbasic_frag.glsl @@ -0,0 +1,49 @@ +uniform vec3 diffuse; +uniform float opacity; + +#ifndef FLAT_SHADED + + varying vec3 vNormal; + +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void main() { + + vec4 diffuseColor = vec4( diffuse, opacity ); + + #include + #include + #include + #include + #include + #include + + ReflectedLight reflectedLight; + reflectedLight.directDiffuse = vec3( 0.0 ); + reflectedLight.directSpecular = vec3( 0.0 ); + reflectedLight.indirectDiffuse = diffuseColor.rgb; + reflectedLight.indirectSpecular = vec3( 0.0 ); + + #include + + vec3 outgoingLight = reflectedLight.indirectDiffuse; + + #include + #include + #include + + gl_FragColor = vec4( outgoingLight, diffuseColor.a ); + +} diff --git a/src/renderers/shaders/ShaderLib/meshbasic_vert.glsl b/src/renderers/shaders/ShaderLib/meshbasic_vert.glsl new file mode 100644 index 00000000000000..42c325351e9145 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/meshbasic_vert.glsl @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +void main() { + + #include + #include + #include + #include + + #ifdef USE_ENVMAP + + #include + #include + #include + #include + + #endif + + #include + #include + #include + #include + #include + + #include + #include + +} diff --git a/src/renderers/shaders/ShaderLib/meshlambert_frag.glsl b/src/renderers/shaders/ShaderLib/meshlambert_frag.glsl new file mode 100644 index 00000000000000..56420c419a7e1f --- /dev/null +++ b/src/renderers/shaders/ShaderLib/meshlambert_frag.glsl @@ -0,0 +1,78 @@ +uniform vec3 diffuse; +uniform vec3 emissive; +uniform float opacity; + +varying vec3 vLightFront; + +#ifdef DOUBLE_SIDED + + varying vec3 vLightBack; + +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void main() { + + vec4 diffuseColor = vec4( diffuse, opacity ); + ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) ); + vec3 totalEmissiveLight = emissive; + + #include + #include + #include + #include + #include + #include + #include + + // accumulation + reflectedLight.indirectDiffuse = getAmbientLightIrradiance( ambientLightColor ); + + #include + + reflectedLight.indirectDiffuse *= BRDF_Diffuse_Lambert( diffuseColor.rgb ); + + #ifdef DOUBLE_SIDED + + reflectedLight.directDiffuse = ( gl_FrontFacing ) ? vLightFront : vLightBack; + + #else + + reflectedLight.directDiffuse = vLightFront; + + #endif + + reflectedLight.directDiffuse *= BRDF_Diffuse_Lambert( diffuseColor.rgb ) * getShadowMask(); + + // modulation + #include + + vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveLight; + + #include + + #include + + #include + + gl_FragColor = vec4( outgoingLight, diffuseColor.a ); + +} diff --git a/src/renderers/shaders/ShaderLib/meshlambert_vert.glsl b/src/renderers/shaders/ShaderLib/meshlambert_vert.glsl new file mode 100644 index 00000000000000..de1dc9789ccb08 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/meshlambert_vert.glsl @@ -0,0 +1,46 @@ +#define LAMBERT + +varying vec3 vLightFront; + +#ifdef DOUBLE_SIDED + + varying vec3 vLightBack; + +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void main() { + + #include + #include + #include + + #include + #include + #include + #include + #include + + #include + #include + #include + #include + #include + + #include + #include + #include + #include + +} diff --git a/src/renderers/shaders/ShaderLib/meshphong_frag.glsl b/src/renderers/shaders/ShaderLib/meshphong_frag.glsl new file mode 100644 index 00000000000000..4716aba47c496f --- /dev/null +++ b/src/renderers/shaders/ShaderLib/meshphong_frag.glsl @@ -0,0 +1,61 @@ +#define PHONG + +uniform vec3 diffuse; +uniform vec3 emissive; +uniform vec3 specular; +uniform float shininess; +uniform float opacity; + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void main() { + + vec4 diffuseColor = vec4( diffuse, opacity ); + ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) ); + vec3 totalEmissiveLight = emissive; + + #include + #include + #include + #include + #include + #include + #include + #include + + // accumulation + #include + #include + + // modulation + #include + + vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveLight; + + #include + #include + + #include + + gl_FragColor = vec4( outgoingLight, diffuseColor.a ); + +} diff --git a/src/renderers/shaders/ShaderLib/meshphong_vert.glsl b/src/renderers/shaders/ShaderLib/meshphong_vert.glsl new file mode 100644 index 00000000000000..bb3f8e06da7bcf --- /dev/null +++ b/src/renderers/shaders/ShaderLib/meshphong_vert.glsl @@ -0,0 +1,55 @@ +#define PHONG + +varying vec3 vViewPosition; + +#ifndef FLAT_SHADED + + varying vec3 vNormal; + +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void main() { + + #include + #include + #include + + #include + #include + #include + #include + #include + +#ifndef FLAT_SHADED // Normal computed with derivatives when FLAT_SHADED + + vNormal = normalize( transformedNormal ); + +#endif + + #include + #include + #include + #include + #include + #include + + vViewPosition = - mvPosition.xyz; + + #include + #include + #include + #include + +} diff --git a/src/renderers/shaders/ShaderLib/meshstandard_frag.glsl b/src/renderers/shaders/ShaderLib/meshstandard_frag.glsl new file mode 100644 index 00000000000000..f9d9e7fd3e5192 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/meshstandard_frag.glsl @@ -0,0 +1,73 @@ +#define STANDARD + +uniform vec3 diffuse; +uniform vec3 emissive; +uniform float roughness; +uniform float metalness; +uniform float opacity; + +uniform float envMapIntensity; // temporary + +varying vec3 vViewPosition; + +#ifndef FLAT_SHADED + + varying vec3 vNormal; + +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void main() { + + vec4 diffuseColor = vec4( diffuse, opacity ); + ReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) ); + vec3 totalEmissiveLight = emissive; + + #include + #include + #include + #include + #include + #include + #include + #include + #include + #include + + // accumulation + #include + #include + + // modulation + #include + + vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveLight; + + #include + + #include + + gl_FragColor = vec4( outgoingLight, diffuseColor.a ); + +} diff --git a/src/renderers/shaders/ShaderLib/meshstandard_vert.glsl b/src/renderers/shaders/ShaderLib/meshstandard_vert.glsl new file mode 100644 index 00000000000000..7ff4464fb7e3e3 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/meshstandard_vert.glsl @@ -0,0 +1,54 @@ +#define STANDARD + +varying vec3 vViewPosition; + +#ifndef FLAT_SHADED + + varying vec3 vNormal; + +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void main() { // STANDARD + + #include + #include + #include + + #include + #include + #include + #include + #include + +#ifndef FLAT_SHADED // Normal computed with derivatives when FLAT_SHADED + + vNormal = normalize( transformedNormal ); + +#endif + + #include + #include + #include + #include + #include + #include + + vViewPosition = - mvPosition.xyz; + + #include + #include + #include + +} diff --git a/src/renderers/shaders/ShaderLib/normal_frag.glsl b/src/renderers/shaders/ShaderLib/normal_frag.glsl new file mode 100644 index 00000000000000..b36ee79fc67513 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/normal_frag.glsl @@ -0,0 +1,13 @@ +uniform float opacity; +varying vec3 vNormal; + +#include +#include + +void main() { + + gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity ); + + #include + +} diff --git a/src/renderers/shaders/ShaderLib/normal_vert.glsl b/src/renderers/shaders/ShaderLib/normal_vert.glsl new file mode 100644 index 00000000000000..7c427a7559bc13 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/normal_vert.glsl @@ -0,0 +1,16 @@ +varying vec3 vNormal; + +#include +#include +#include + +void main() { + + vNormal = normalize( normalMatrix * normal ); + + #include + #include + #include + #include + +} diff --git a/src/renderers/shaders/ShaderLib/points_frag.glsl b/src/renderers/shaders/ShaderLib/points_frag.glsl new file mode 100644 index 00000000000000..0ae13938c69822 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/points_frag.glsl @@ -0,0 +1,27 @@ +uniform vec3 diffuse; +uniform float opacity; + +#include +#include +#include +#include +#include +#include + +void main() { + + vec3 outgoingLight = vec3( 0.0 ); + vec4 diffuseColor = vec4( diffuse, opacity ); + + #include + #include + #include + #include + + outgoingLight = diffuseColor.rgb; + + #include + + gl_FragColor = vec4( outgoingLight, diffuseColor.a ); + +} diff --git a/src/renderers/shaders/ShaderLib/points_vert.glsl b/src/renderers/shaders/ShaderLib/points_vert.glsl new file mode 100644 index 00000000000000..675de54a798ef1 --- /dev/null +++ b/src/renderers/shaders/ShaderLib/points_vert.glsl @@ -0,0 +1,25 @@ +uniform float size; +uniform float scale; + +#include +#include +#include +#include + +void main() { + + #include + #include + #include + + #ifdef USE_SIZEATTENUATION + gl_PointSize = size * ( scale / - mvPosition.z ); + #else + gl_PointSize = size; + #endif + + #include + #include + #include + +} diff --git a/src/renderers/webgl/WebGLProgram.js b/src/renderers/webgl/WebGLProgram.js index 25db38731ad5a8..1ca93248155ffa 100644 --- a/src/renderers/webgl/WebGLProgram.js +++ b/src/renderers/webgl/WebGLProgram.js @@ -194,11 +194,12 @@ THREE.WebGLProgram = ( function () { function parseIncludes( string ) { - var pattern = /#include +<([\w\d.]+)>/g; + var pattern = /#include[ \t]+<([\w\d.]+)>/g; function replace( match, include ) { - - return THREE.ShaderChunk[ include ]; + var replace = THREE.ShaderChunk[ include ]; + if( ! replace ) throw new Error( "can not resolve #include <"+include+">"); + return parseIncludes( replace ); } diff --git a/utils/build/includes/common.json b/utils/build/includes/common.json index cb01a361b75bb2..6f75beb7603c0b 100644 --- a/utils/build/includes/common.json +++ b/utils/build/includes/common.json @@ -193,6 +193,30 @@ "src/renderers/shaders/ShaderChunk/worldpos_vertex.glsl", "src/renderers/shaders/UniformsUtils.js", "src/renderers/shaders/UniformsLib.js", + "src/renderers/shaders/ShaderLib/cube_frag.glsl", + "src/renderers/shaders/ShaderLib/cube_vert.glsl", + "src/renderers/shaders/ShaderLib/depth_frag.glsl", + "src/renderers/shaders/ShaderLib/depth_vert.glsl", + "src/renderers/shaders/ShaderLib/depthRGBA_frag.glsl", + "src/renderers/shaders/ShaderLib/depthRGBA_vert.glsl", + "src/renderers/shaders/ShaderLib/distanceRGBA_frag.glsl", + "src/renderers/shaders/ShaderLib/distanceRGBA_vert.glsl", + "src/renderers/shaders/ShaderLib/equirect_frag.glsl", + "src/renderers/shaders/ShaderLib/equirect_vert.glsl", + "src/renderers/shaders/ShaderLib/linedashed_frag.glsl", + "src/renderers/shaders/ShaderLib/linedashed_vert.glsl", + "src/renderers/shaders/ShaderLib/meshbasic_frag.glsl", + "src/renderers/shaders/ShaderLib/meshbasic_vert.glsl", + "src/renderers/shaders/ShaderLib/meshlambert_frag.glsl", + "src/renderers/shaders/ShaderLib/meshlambert_vert.glsl", + "src/renderers/shaders/ShaderLib/meshphong_frag.glsl", + "src/renderers/shaders/ShaderLib/meshphong_vert.glsl", + "src/renderers/shaders/ShaderLib/meshstandard_frag.glsl", + "src/renderers/shaders/ShaderLib/meshstandard_vert.glsl", + "src/renderers/shaders/ShaderLib/normal_frag.glsl", + "src/renderers/shaders/ShaderLib/normal_vert.glsl", + "src/renderers/shaders/ShaderLib/points_frag.glsl", + "src/renderers/shaders/ShaderLib/points_vert.glsl", "src/renderers/shaders/ShaderLib.js", "src/renderers/WebGLRenderer.js", "src/renderers/WebGLRenderTarget.js",