-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move to TextureUtils class and use that in GLTFExporter, re…
…spect sRGB vs. Linear, cache some generated objects
- Loading branch information
1 parent
22a8e81
commit dc59c08
Showing
2 changed files
with
94 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { | ||
PlaneGeometry, | ||
ShaderMaterial, | ||
Uniform, | ||
Mesh, | ||
PerspectiveCamera, | ||
Scene, | ||
WebGLRenderer, | ||
Texture, | ||
sRGBEncoding | ||
} from 'three'; | ||
|
||
let temporaryRenderer; | ||
let fullscreenQuadGeometry; | ||
let fullscreenQuadMaterial; | ||
let fullscreenQuad; | ||
|
||
function decompress( texture, maxTextureSize, renderer ) { | ||
|
||
if ( !fullscreenQuadGeometry ) fullscreenQuadGeometry = new PlaneGeometry( 2, 2, 1, 1 ); | ||
if ( !fullscreenQuadMaterial ) fullscreenQuadMaterial = new ShaderMaterial( { | ||
uniforms: { blitTexture: new Uniform( texture ) }, | ||
vertexShader: ` | ||
varying vec2 vUv; | ||
void main(){ | ||
vUv = uv; | ||
gl_Position = vec4(position.xy * 1.0,0.,.999999); | ||
}`, | ||
fragmentShader: ` | ||
uniform sampler2D blitTexture; | ||
varying vec2 vUv; | ||
// took from threejs 05fc79cd52b79e8c3e8dec1e7dca72c5c39983a4 | ||
vec4 conv_LinearTosRGB( in vec4 value ) { | ||
return vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.a ); | ||
} | ||
void main(){ | ||
gl_FragColor = vec4(vUv.xy, 0, 1); | ||
#ifdef IS_SRGB | ||
gl_FragColor = conv_LinearTosRGB( texture2D( blitTexture, vUv) ); | ||
#else | ||
gl_FragColor = texture2D( blitTexture, vUv); | ||
#endif | ||
}` | ||
} ); | ||
|
||
fullscreenQuadMaterial.uniforms.blitTexture.value = texture; | ||
fullscreenQuadMaterial.defines.IS_SRGB = texture.encoding == sRGBEncoding; | ||
|
||
if ( !fullscreenQuad ) { | ||
|
||
fullscreenQuad = new Mesh( fullscreenQuadGeometry, fullscreenQuadMaterial ); | ||
fullscreenQuad.frustrumCulled = false; | ||
|
||
} | ||
|
||
const temporaryCam = new PerspectiveCamera(); | ||
const temporaryScene = new Scene(); | ||
temporaryScene.add( fullscreenQuad ); | ||
|
||
if ( !renderer ) { | ||
|
||
if ( !temporaryRenderer ) | ||
temporaryRenderer = new WebGLRenderer( { antialias: false } ); | ||
|
||
renderer = temporaryRenderer; | ||
|
||
} | ||
|
||
renderer.setSize( Math.min(texture.image.width, maxTextureSize), Math.min(texture.image.height, maxTextureSize) ); | ||
renderer.clear(); | ||
renderer.render( temporaryScene, temporaryCam ); | ||
|
||
const readableTexture = new Texture( renderer.domElement ); | ||
readableTexture.userData.mimeType = 'image/png'; | ||
console.log(readableTexture, readableTexture.image) | ||
|
||
return readableTexture; | ||
|
||
} | ||
|
||
export { | ||
decompress | ||
} |