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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import {
  2. _testFinish,
  3. BoxGeometry,
  4. Color,
  5. LoadingScreenPlugin,
  6. Mesh,
  7. PhysicalMaterial,
  8. PopmotionPlugin,
  9. ThreeViewer,
  10. Vector3,
  11. } from 'threepipe'
  12. import {createSimpleButtons} from '../examples-utils/simple-bottom-buttons.js'
  13. async function init() {
  14. const viewer = new ThreeViewer({
  15. canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
  16. renderScale: 'auto',
  17. plugins: [LoadingScreenPlugin],
  18. })
  19. const popmotion = viewer.addPluginSync(PopmotionPlugin)
  20. await viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr')
  21. const cube = viewer.scene.addObject(new Mesh(
  22. new BoxGeometry(1, 1, 1),
  23. new PhysicalMaterial({color: 0xff0000})
  24. ))
  25. let isMovedUp = false
  26. createSimpleButtons({
  27. ['Move Up/Down']: async(btn) => {
  28. btn.disabled = true
  29. await popmotion.animateTargetAsync(cube, 'position', {
  30. to: cube.position.clone().add(new Vector3(0, isMovedUp ? -1 : 1, 0)),
  31. duration: 500, // ms
  32. onComplete: () => isMovedUp = !isMovedUp,
  33. }) // setDirty is automatically called on the cube since it's the target
  34. btn.disabled = false
  35. },
  36. ['Rotate +90deg']: async(btn) => {
  37. btn.disabled = true
  38. await popmotion.animateAsync({
  39. from: cube.rotation.y,
  40. to: cube.rotation.y + Math.PI / 2,
  41. duration: 500,
  42. onUpdate: (v) => {
  43. cube.rotation.y = v
  44. cube.setDirty()
  45. },
  46. })
  47. btn.disabled = false
  48. },
  49. ['Change Color']: async(btn)=>{
  50. btn.disabled = true
  51. await popmotion.animateAsync({
  52. from: '#' + cube.material.color.getHexString(),
  53. to: '#' + new Color().setHSL(Math.random(), 1, 0.5).getHexString(),
  54. duration: 500,
  55. onUpdate: (v) => {
  56. cube.material.color.set(v)
  57. cube.material.setDirty()
  58. },
  59. })
  60. btn.disabled = false
  61. },
  62. })
  63. }
  64. init().finally(_testFinish)