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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {
  2. _testFinish,
  3. _testStart,
  4. IObject3D,
  5. LoadingScreenPlugin,
  6. OrbitControls3,
  7. PickingPlugin,
  8. ThreeViewer,
  9. TransformControlsPlugin,
  10. } from 'threepipe'
  11. import {TweakpaneUiPlugin} from '@threepipe/plugin-tweakpane'
  12. import * as THREE from 'threepipe'
  13. async function init() {
  14. const viewer = new ThreeViewer({
  15. canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
  16. msaa: false, // todo this is not working here, but its working in the other stencil example.
  17. rgbm: true,
  18. plugins: [LoadingScreenPlugin, PickingPlugin, TransformControlsPlugin],
  19. stencil: true,
  20. })
  21. const ui = viewer.addPluginSync(new TweakpaneUiPlugin(true))
  22. ui.setupPlugins(PickingPlugin, TransformControlsPlugin)
  23. await viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr', {
  24. setBackground: true,
  25. })
  26. const result = await viewer.load<IObject3D>('https://threejs.org/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', {
  27. autoCenter: true,
  28. autoScale: true,
  29. })
  30. const model = result?.getObjectByName('node_damagedHelmet_-6514')
  31. const config = model?.uiConfig
  32. if (config) ui.appendChild(config)
  33. const maskMaterial = new THREE.MeshBasicMaterial({
  34. color: 0x222222,
  35. stencilRef: 1,
  36. depthWrite: false,
  37. stencilWrite: true,
  38. depthTest: true,
  39. stencilFunc: THREE.AlwaysStencilFunc,
  40. stencilZPass: THREE.ReplaceStencilOp,
  41. })
  42. const maskPlane = new THREE.Mesh(new THREE.PlaneGeometry(4, 4), maskMaterial)
  43. viewer.scene.addObject(maskPlane)
  44. const maskCube = new THREE.Mesh(new THREE.BoxGeometry(4, 4, 20), new THREE.MeshBasicMaterial({
  45. color: 0xffffff,
  46. stencilRef: 1,
  47. depthWrite: false,
  48. stencilWrite: true,
  49. depthTest: true,
  50. stencilFunc: THREE.AlwaysStencilFunc,
  51. stencilZPass: THREE.ReplaceStencilOp,
  52. colorWrite:false,
  53. }))
  54. maskCube.userData.bboxVisible = false
  55. maskCube.userData.userSelectable = false
  56. maskCube.position.set(0, 0, 10)
  57. viewer.scene.addObject(maskCube)
  58. maskPlane.addEventListener('objectUpdate', ()=>{
  59. maskCube.position.set(maskPlane.position.x, maskPlane.position.y, maskPlane.position.z + 10)
  60. })
  61. const mat = model?.materials?.[0]
  62. if (mat && model) {
  63. mat.stencilWrite = true
  64. mat.stencilRef = 1
  65. mat.stencilFunc = THREE.LessEqualStencilFunc
  66. model.renderOrder = 2
  67. model.position.z = -0.4
  68. }
  69. viewer.getPlugin(PickingPlugin)?.setSelectedObject(model)
  70. const controls = viewer.scene.mainCamera.controls as OrbitControls3
  71. controls.minAzimuthAngle = -1.5
  72. controls.maxAzimuthAngle = 1.5
  73. }
  74. _testStart()
  75. init().finally(_testFinish)