threepipe
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

script.ts 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import {
  2. _testFinish,
  3. ITexture,
  4. KTX2LoadPlugin,
  5. Mesh,
  6. PlaneGeometry,
  7. SRGBColorSpace,
  8. ThreeViewer,
  9. UnlitMaterial,
  10. } from 'threepipe'
  11. import {BufferGeometry} from 'three'
  12. async function init() {
  13. const viewer = new ThreeViewer({
  14. canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
  15. msaa: true,
  16. dropzone: {
  17. allowedExtensions: ['ktx2'],
  18. },
  19. })
  20. viewer.addPluginSync(KTX2LoadPlugin)
  21. viewer.scene.setBackgroundColor('#555555')
  22. const urls = [
  23. 'https://threejs.org/examples/textures/compressed/sample_etc1s.ktx2',
  24. 'https://threejs.org/examples/textures/compressed/sample_uastc.ktx2',
  25. 'https://threejs.org/examples/textures/compressed/sample_uastc_zstd.ktx2',
  26. ]
  27. // PlaneGeometry UVs assume flipY=true, which compressed textures don't support.
  28. const geometry = flipY(new PlaneGeometry(1, 1))
  29. let i = 0
  30. for (const url of urls) {
  31. // Load the url as a Texture
  32. const texture = await viewer.load<ITexture>(url)
  33. if (!texture) continue
  34. texture.colorSpace = SRGBColorSpace
  35. const material = new UnlitMaterial({
  36. map: texture,
  37. transparent: true,
  38. })
  39. const plane = new Mesh(geometry, material)
  40. plane.position.set(i % 3 - 1, -Math.floor(i / 3) + 1, 0)
  41. viewer.scene.addObject(plane)
  42. i++
  43. }
  44. // Listen to when a file is dropped
  45. viewer.assetManager.addEventListener('loadAsset', (e)=>{
  46. if (!e.data.isTexture) return
  47. const texture = e.data as ITexture
  48. texture.colorSpace = SRGBColorSpace
  49. const material = new UnlitMaterial({
  50. map: texture,
  51. transparent: true,
  52. })
  53. const plane = new Mesh(geometry, material)
  54. plane.position.set(i % 3 - 1, -Math.floor(i / 3) + 1, 0)
  55. viewer.scene.addObject(plane)
  56. i++
  57. })
  58. }
  59. init().then(_testFinish)
  60. /** Correct UVs to be compatible with `flipY=false` textures. */
  61. function flipY(geometry: BufferGeometry) {
  62. const uv = geometry.attributes.uv
  63. for (let i = 0; i < uv.count; i++) {
  64. uv.setY(i, 1 - uv.getY(i))
  65. }
  66. return geometry
  67. }