forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99fd5d6
commit b4b6522
Showing
10 changed files
with
139 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { | ||
Mesh, | ||
ShaderMaterial, | ||
BoxGeometry | ||
} from 'three'; | ||
|
||
class TextureHelper extends Mesh { | ||
|
||
constructor( texture, width = 1, height = 1, depth = 1 ) { | ||
|
||
const material = new ShaderMaterial( { | ||
|
||
type: 'TextureHelperMaterial', | ||
|
||
uniforms: { | ||
|
||
map: { value: texture }, | ||
|
||
}, | ||
|
||
vertexShader: [ | ||
|
||
'varying vec3 vUvw;', | ||
|
||
'void main() {', | ||
|
||
' vUvw = vec3( uv, 0.0 );', // TODO: Populate 'w' for cube, 3D, and array textures. | ||
|
||
' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );', | ||
|
||
'}', | ||
|
||
].join( '\n' ), | ||
|
||
fragmentShader: [ | ||
|
||
'precision highp float;', | ||
|
||
'precision highp sampler2DArray;', | ||
|
||
'precision highp sampler3D;', | ||
|
||
'uniform {samplerType} map;', | ||
|
||
'varying vec3 vUvw;', | ||
|
||
'vec4 textureHelper( in sampler2D map ) { return texture( map, vUvw.xy ); }', | ||
|
||
'vec4 textureHelper( in sampler2DArray map ) { return texture( map, vUvw ); }', | ||
|
||
'vec4 textureHelper( in sampler3D map ) { return texture( map, vUvw ); }', | ||
|
||
'vec4 textureHelper( in samplerCube map ) { return texture( map, vUvw ); }', | ||
|
||
'void main() {', | ||
|
||
' gl_FragColor = linearToOutputTexel( textureHelper( map ) );', | ||
|
||
'}' | ||
|
||
].join( '\n' ).replace( '{samplerType}', getSamplerType( texture ) ) | ||
|
||
} ); | ||
|
||
// TODO: Display as stack of images. | ||
const geometry = new BoxGeometry( width, height, depth ); | ||
if ( texture.flipY === false ) flipY( geometry ); | ||
|
||
super( geometry, material ); | ||
|
||
this.texture = texture; | ||
this.type = 'TextureHelper'; | ||
|
||
} | ||
|
||
dispose() { | ||
|
||
this.geometry.dispose(); | ||
this.material.dispose(); | ||
|
||
} | ||
|
||
} | ||
|
||
function getSamplerType( texture ) { | ||
|
||
if ( texture.isCubeTexture ) { | ||
|
||
return 'samplerCube'; | ||
|
||
} else if ( texture.isDataArrayTexture || texture.isCompressedArrayTexture ) { | ||
|
||
return 'sampler2DArray'; | ||
|
||
} else if ( texture.isData3DTexture || texture.isCompressed3DTexture ) { | ||
|
||
return 'sampler3D'; | ||
|
||
} else { | ||
|
||
return 'sampler2D'; | ||
|
||
} | ||
|
||
} | ||
|
||
/** Correct UVs to be compatible with `flipY=false` textures. */ | ||
function flipY( geometry ) { | ||
|
||
const uv = geometry.attributes.uv; | ||
|
||
for ( let i = 0; i < uv.count; i ++ ) { | ||
|
||
uv.setY( i, 1 - uv.getY( i ) ); | ||
|
||
} | ||
|
||
return geometry; | ||
|
||
} | ||
|
||
export { TextureHelper }; |
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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