threepipe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

script.ts 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {
  2. _testFinish,
  3. BaseGroundPlugin, DirectionalLight2,
  4. GBufferPlugin,
  5. IObject3D, LoadingScreenPlugin, PhysicalMaterial,
  6. PickingPlugin,
  7. RenderTargetPreviewPlugin, SSAAPlugin,
  8. ThreeViewer, Object3DWidgetsPlugin, TransformControlsPlugin, AssetExporterPlugin, OrbitControls3,
  9. } from 'threepipe'
  10. import {TweakpaneUiPlugin} from '@threepipe/plugin-tweakpane'
  11. // @ts-expect-error todo fix
  12. import {BloomPlugin, SSReflectionPlugin, TemporalAAPlugin, VelocityBufferPlugin, SSGIPlugin} from '@threepipe/webgi-plugins'
  13. async function init() {
  14. const viewer = new ThreeViewer({
  15. canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
  16. msaa: true,
  17. rgbm: true,
  18. zPrepass: false,
  19. renderScale: 'auto',
  20. maxRenderScale: 1.5,
  21. dropzone: {
  22. addOptions: {
  23. disposeSceneObjects: true,
  24. },
  25. },
  26. plugins: [LoadingScreenPlugin, GBufferPlugin, BloomPlugin, SSAAPlugin, TemporalAAPlugin, Object3DWidgetsPlugin, TransformControlsPlugin, AssetExporterPlugin, new VelocityBufferPlugin(undefined, false), SSReflectionPlugin],
  27. // rgbm: false,
  28. })
  29. viewer.renderManager.stableNoise = true
  30. viewer.getPlugin(SSReflectionPlugin)!.enabled = false
  31. const ssgi = viewer.addPluginSync(new SSGIPlugin())
  32. viewer.addPluginSync(new BaseGroundPlugin())
  33. const ui = viewer.addPluginSync(new TweakpaneUiPlugin(true))
  34. await viewer.setEnvironmentMap('https://hdrihaven.r2cache.com/hdr/1k/empty_warehouse_01_1k.hdr', {
  35. setBackground: true,
  36. })
  37. await viewer.load<IObject3D>('https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Sponza/glTF/Sponza.gltf', {
  38. autoCenter: false,
  39. autoScale: true,
  40. autoScaleRadius: 30,
  41. })
  42. viewer.scene.modelRoot.traverse((o) => {
  43. if (o.material) {
  44. const mat = o.material as PhysicalMaterial
  45. mat.emissiveIntensity *= 10
  46. }
  47. })
  48. viewer.scene.envMapIntensity = 0.0
  49. ui.setupPluginUi(ssgi)
  50. ui.setupPluginUi(SSReflectionPlugin)
  51. ui.setupPluginUi(BaseGroundPlugin)
  52. ui.setupPluginUi(PickingPlugin)
  53. ui.setupPluginUi(BloomPlugin)
  54. ui.setupPluginUi(TemporalAAPlugin)
  55. ui.setupPluginUi(VelocityBufferPlugin)
  56. ui.setupPluginUi(AssetExporterPlugin)
  57. const targetPreview = await viewer.addPlugin(RenderTargetPreviewPlugin)
  58. targetPreview.addTarget(() => ssgi.target, 'ssgi')
  59. // const gb = viewer.getPlugin(GBufferPlugin)
  60. // targetPreview.addTarget(() => gb?.target, 'depth')
  61. const camera = viewer.scene.mainCamera
  62. camera.position.set(-1, 5, -0.7)
  63. camera.target.set(-4, 4, -0.7)
  64. const controls = camera.controls as OrbitControls3
  65. controls.minDistance = 2
  66. controls.maxDistance = 3
  67. controls.autoPushTarget = true
  68. controls.autoPullTarget = true
  69. const light = new DirectionalLight2()
  70. viewer.scene.addObject(light)
  71. light.position.set(0, 20, 0)
  72. light.lookAt(-25, 0, 0)
  73. light.intensity = 30
  74. light.castShadow = true
  75. light.shadow.camera.left = -25
  76. light.shadow.camera.right = 25
  77. light.shadow.camera.top = 25
  78. light.shadow.camera.bottom = -25
  79. light.shadow.mapSize.set(1024, 1024)
  80. // todo add to DirectionalLight
  81. light.uiConfig.children!.push({
  82. type: 'vec2',
  83. label: 'Shadow Map Size',
  84. property: [light?.shadow, 'mapSize'],
  85. onChange: ()=>{
  86. light.shadow.map?.dispose()
  87. light.shadow.mapPass?.dispose()
  88. light.shadow.map = null as any
  89. light.shadow.mapPass = null as any
  90. },
  91. },
  92. {
  93. type: 'slider',
  94. bounds: [-0.001, 0.001],
  95. stepSize: 0.00002,
  96. label: 'Shadow Bias',
  97. property: [light?.shadow, 'bias'],
  98. onChange: (e)=>light.setDirty(e),
  99. },
  100. {
  101. type: 'slider',
  102. bounds: [-0.1, 0.1],
  103. stepSize: 0.005,
  104. label: 'Shadow Normal Bias',
  105. property: [light?.shadow, 'normalBias'],
  106. onChange: (e)=>light.setDirty(e),
  107. },
  108. {
  109. type: 'slider',
  110. bounds: [0, 5],
  111. label: 'Shadow radius',
  112. property: [light?.shadow, 'radius'],
  113. onChange: (e)=>light.setDirty(e),
  114. },
  115. {
  116. type: 'slider',
  117. bounds: [0.1, 50],
  118. label: 'Shadow frustum',
  119. // property: [light.shadow, 'radius'],
  120. getValue: ()=>{
  121. return light.shadow.camera.right * 2
  122. },
  123. setValue: (v: number)=>{
  124. light.shadow.camera.left = -v / 2
  125. light.shadow.camera.right = v / 2
  126. light.shadow.camera.top = v / 2
  127. light.shadow.camera.bottom = -v / 2
  128. },
  129. onChange: (e)=>light.setDirty(e),
  130. })
  131. ui.appendChild(light.uiConfig)
  132. }
  133. init().then(_testFinish)