diff --git a/examples/js/pmrem/PMREMGenerator.js b/examples/js/pmrem/PMREMGenerator.js index f13e2d6600fbd63a2433a7adb098547e54908036..3590dff0e019414f0df35b080032a29be9bcd30a 100644 --- a/examples/js/pmrem/PMREMGenerator.js +++ b/examples/js/pmrem/PMREMGenerator.js @@ -28,7 +28,7 @@ this.sourceTexture = cubeTexture; // encoded formats do not interpolate well, thus turn off interpolation for them - var textureFilter = ( sourceTexture.encoding === THREE.Linear ) ? THREE.LinearFilter : THREE.NearestFilter; + var textureFilter = ( this.sourceTexture.encoding === THREE.Linear ) ? THREE.LinearFilter : THREE.NearestFilter; this.sourceTexture.minFilter = this.sourceTexture.magFilter = textureFilter; this.cubeLods = []; @@ -60,7 +60,7 @@ THREE.PMREMGenerator.prototype = { - constructor : PMREMGenerator, + constructor : THREE.PMREMGenerator, update: function( renderer ) { @@ -172,8 +172,8 @@ THREE.PMREMGenerator.prototype = { else\n\ color = vec4(1.0,0.0,1.0,1.0);\n\ return color;\n\ - }", - THREE.ShaderChunk[ "encodings" ], + }" + + THREE.ShaderChunk[ "encodings" ] + "void main() {\n\ vec3 sampleDirection;\n\ vec2 uv = vUv*2.0 - 1.0;\n\ @@ -215,7 +215,7 @@ THREE.PMREMGenerator.prototype = { rgbColor.rgb += color;\n\ }\n\ rgbColor /= float(NumSamples);\n\ - gl_FragColor = texelEncode( rgbColor, encoding );\n\ + gl_FragColor = texelEncode( vec4( rgbColor, 1.0 ), encoding );\n\ }" } ); diff --git a/examples/js/pmrem/PMREM_CubeUVPacker.js b/examples/js/pmrem/PMREM_CubeUVPacker.js index 3d3ccfc6082e4ad2f283c7d3f272828727979141..c115521377ed13766dc543551fe8bd4f9ee7fe07 100644 --- a/examples/js/pmrem/PMREM_CubeUVPacker.js +++ b/examples/js/pmrem/PMREM_CubeUVPacker.js @@ -2,73 +2,8 @@ * @author Prashant Sharma / spidersharma03 */ -var vertexShaderPMREMCubeUV = "precision highp float;\ - varying vec2 vUv;\ - void main() {\ - vUv = uv;\ - gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\ - }"; - -var fragmentShaderPMREMCubeUV = "precision highp float;\ - varying vec2 vUv;\ - uniform samplerCube cubeTexture;\ - uniform float mapSize;\ - uniform vec3 testColor;\ - uniform int faceIndex;\ - \ - const float PI = 3.14159265358979;\ - vec4 sampleCubeMap(float phi, float theta) {\ - float sinTheta = sin(theta);\ - float cosTheta = cos(theta);\ - vec3 sampleDir = vec3(cos(phi) * sinTheta, cosTheta, sin(phi) * sinTheta);\ - vec4 color = textureCube(cubeTexture, sampleDir);\ - return color * vec4(testColor, 1.0);\ - }\ - void main() {\ - vec3 sampleDirection;\ - vec2 uv = vUv;\ - uv = uv * 2.0 - 1.0;\ - uv.y *= -1.0;\ - if(faceIndex == 0) {\ - sampleDirection = normalize(vec3(1.0, uv.y, -uv.x));\ - }\ - else if(faceIndex == 1) {\ - sampleDirection = normalize(vec3(uv.x, 1.0, uv.y));\ - }\ - else if(faceIndex == 2) {\ - sampleDirection = normalize(vec3(uv.x, uv.y, 1.0));\ - }\ - else if(faceIndex == 3) {\ - sampleDirection = normalize(vec3(-1.0, uv.y, uv.x));\ - }\ - else if(faceIndex == 4) {\ - sampleDirection = normalize(vec3(uv.x, -1.0, -uv.y));\ - }\ - else {\ - sampleDirection = normalize(vec3(-uv.x, uv.y, -1.0));\ - }\ - vec4 color = textureCube(cubeTexture, (sampleDirection));\ - gl_FragColor = color * vec4(testColor, 1.0);\ - }\ -"; -var uniformsPMREMCubeUV = { - "faceIndex": { type: 'i', value: 0 }, - "mapSize": { type: 'f', value: 0 }, - "cubeTexture": { type: 't', value: null }, - "testColor": { type: 'v3', value: new THREE.Vector3( 1, 1, 1 ) } -}; -var testColor = []; -testColor.push( new THREE.Vector3( 1, 0, 0 ) ); -testColor.push( new THREE.Vector3( 0, 1, 0 ) ); -testColor.push( new THREE.Vector3( 0, 0, 1 ) ); -testColor.push( new THREE.Vector3( 1, 1, 0 ) ); -testColor.push( new THREE.Vector3( 0, 1, 1 ) ); -testColor.push( new THREE.Vector3( 1, 0, 1 ) ); -testColor.push( new THREE.Vector3( 1, 1, 1 ) ); -testColor.push( new THREE.Vector3( 0.5, 1, 0.5 ) ); - -var PMREM_CubeUVPacker = function( cubeTextureLods, numLods ) { +THREE.PMREM_CubeUVPacker = function( cubeTextureLods, numLods ) { this.cubeLods = cubeTextureLods; this.numLods = numLods; @@ -109,16 +44,25 @@ var PMREM_CubeUVPacker = function( cubeTextureLods, numLods ) { var mipOffsetX = 0; var mipOffsetY = 0; var mipSize = size; + + /* + var testColor = []; + testColor.push( new THREE.Vector3( 1, 0, 0 ) ); + testColor.push( new THREE.Vector3( 0, 1, 0 ) ); + testColor.push( new THREE.Vector3( 0, 0, 1 ) ); + testColor.push( new THREE.Vector3( 1, 1, 0 ) ); + testColor.push( new THREE.Vector3( 0, 1, 1 ) ); + testColor.push( new THREE.Vector3( 1, 0, 1 ) ); + testColor.push( new THREE.Vector3( 1, 1, 1 ) ); + testColor.push( new THREE.Vector3( 0.5, 1, 0.5 ) );*/ + for ( var j = 0; j < nMips; j ++ ) { // Mip Maps for ( var k = 0; k < 6; k ++ ) { // 6 Cube Faces - var material = new THREE.ShaderMaterial(); - material.vertexShader = vertexShaderPMREMCubeUV; - material.fragmentShader = fragmentShaderPMREMCubeUV; - material.uniforms = THREE.UniformsUtils.clone( uniformsPMREMCubeUV ); + var material = this.getShader(); material.uniforms[ "cubeTexture" ].value = this.cubeLods[ i ]; material.uniforms[ "faceIndex" ].value = k; material.uniforms[ "mapSize" ].value = mipSize; @@ -147,12 +91,79 @@ var PMREM_CubeUVPacker = function( cubeTextureLods, numLods ) { }; -PMREM_CubeUVPacker.prototype = { - constructor : PMREM_CubeUVPacker, +THREE.PMREM_CubeUVPacker.prototype = { + + constructor : THREE.PMREM_CubeUVPacker, update: function( renderer ) { renderer.render( this.scene, this.camera, this.CubeUVRenderTarget, true ); - } + }, + + getShader: function() { + + return new THREE.ShaderMaterial( { + + uniforms: { + "faceIndex": { type: 'i', value: 0 }, + "mapSize": { type: 'f', value: 0 }, + "cubeTexture": { type: 't', value: null }, + "testColor": { type: 'v3', value: new THREE.Vector3( 1, 1, 1 ) } + }, + + vertexShader: + "precision highp float;\ + varying vec2 vUv;\ + void main() {\ + vUv = uv;\ + gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\ + }", + + fragmentShader: + "precision highp float;\ + varying vec2 vUv;\ + uniform samplerCube cubeTexture;\ + uniform float mapSize;\ + uniform vec3 testColor;\ + uniform int faceIndex;\ + \ + const float PI = 3.14159265358979;\ + vec4 sampleCubeMap(float phi, float theta) {\ + float sinTheta = sin(theta);\ + float cosTheta = cos(theta);\ + vec3 sampleDir = vec3(cos(phi) * sinTheta, cosTheta, sin(phi) * sinTheta);\ + vec4 color = textureCube(cubeTexture, sampleDir);\ + return color * vec4(testColor, 1.0);\ + }\ + void main() {\ + vec3 sampleDirection;\ + vec2 uv = vUv;\ + uv = uv * 2.0 - 1.0;\ + uv.y *= -1.0;\ + if(faceIndex == 0) {\ + sampleDirection = normalize(vec3(1.0, uv.y, -uv.x));\ + }\ + else if(faceIndex == 1) {\ + sampleDirection = normalize(vec3(uv.x, 1.0, uv.y));\ + }\ + else if(faceIndex == 2) {\ + sampleDirection = normalize(vec3(uv.x, uv.y, 1.0));\ + }\ + else if(faceIndex == 3) {\ + sampleDirection = normalize(vec3(-1.0, uv.y, uv.x));\ + }\ + else if(faceIndex == 4) {\ + sampleDirection = normalize(vec3(uv.x, -1.0, -uv.y));\ + }\ + else {\ + sampleDirection = normalize(vec3(-uv.x, uv.y, -1.0));\ + }\ + vec4 color = textureCube(cubeTexture, (sampleDirection));\ + gl_FragColor = color * vec4(testColor, 1.0);\ + }" + }); + + } + }; diff --git a/examples/textures/roughness_map.jpg b/examples/textures/roughness_map.jpg new file mode 100644 index 0000000000000000000000000000000000000000..46d254dc870af4e75bbfd0b8a4fa2af983295d6a Binary files /dev/null and b/examples/textures/roughness_map.jpg differ diff --git a/examples/webgl_materials_ibl_hdr.html b/examples/webgl_materials_ibl_hdr.html new file mode 100644 index 0000000000000000000000000000000000000000..3dbef751ead6d994a5317b21af655798c6a9aa06 --- /dev/null +++ b/examples/webgl_materials_ibl_hdr.html @@ -0,0 +1,396 @@ + + + + threejs webgl - materials + + + + + + +
+
threejs - PMREM Generator by Prashant Sharma and Ben Houston.
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +