threepipe
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.html 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Screen Pass Extension</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <!-- Import maps polyfill -->
  8. <!-- Remove this when import maps will be widely supported -->
  9. <script async src="https://unpkg.com/es-module-shims@1.6.3/dist/es-module-shims.js"></script>
  10. <script type="importmap">
  11. {
  12. "imports": {
  13. "threepipe": "./../../dist/index.mjs",
  14. "@threepipe/plugin-tweakpane": "./../../plugins/tweakpane/dist/index.mjs"
  15. }
  16. }
  17. </script>
  18. <style id="example-style">
  19. html, body, #canvas-container, #mcanvas {
  20. width: 100%;
  21. height: 100%;
  22. margin: 0;
  23. overflow: hidden;
  24. }
  25. </style>
  26. <script type="module" src="../examples-utils/global-loading.mjs"></script>
  27. <script type="module" src="../examples-utils/simple-code-preview.mjs"></script>
  28. <script id="example-script" type="module">
  29. import {_testFinish, _testStart, ThreeViewer, Color, shaderReplaceString} from 'threepipe'
  30. import {TweakpaneUiPlugin} from "@threepipe/plugin-tweakpane"
  31. // Add a material extension to the screen shader material to modify the final rendered image.
  32. // Here, `#glMarker` patched in the screen shader material will be replaced with a custom function that applies a tint color to the final rendered image.
  33. // Checkout the ScreenPass guide for more details: https://threepipe.org/docs/guides/screen-pass
  34. const viewer = new ThreeViewer({
  35. canvas: document.getElementById('mcanvas'),
  36. tonemap: true, // also tonemap (this is also added as an extension)
  37. })
  38. const extension = {
  39. extraUniforms: {
  40. tintColor: {value: new Color(0, 1, 1)} // cyan tint
  41. },
  42. parsFragmentSnippet: ` // this is added before the main function
  43. uniform vec3 tintColor;
  44. vec4 applyTint(vec4 color) {
  45. return vec4(color.rgb * tintColor, color.a);
  46. }
  47. `,
  48. shaderExtender: (shader, material, renderer)=>{
  49. console.log('Patching shader')
  50. shader.fragmentShader = shaderReplaceString(shader.fragmentShader,
  51. '#glMarker', `
  52. diffuseColor = applyTint(diffuseColor);
  53. `,
  54. {prepend: true} // (prepend, not replace)
  55. )
  56. },
  57. // add other properties like computeCacheKey etc
  58. } /*as MaterialExtension */
  59. viewer.renderManager.screenPass.material.registerMaterialExtensions([extension])
  60. async function init() {
  61. viewer.scene.backgroundColor.set(0)
  62. await Promise.all([
  63. viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr'),
  64. viewer.load('https://threejs.org/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', {
  65. autoCenter: true,
  66. autoScale: true,
  67. })])
  68. // Add the color to the UI
  69. const ui = viewer.addPluginSync(TweakpaneUiPlugin, true)
  70. ui.appendChild({
  71. type: 'color',
  72. property: [extension.extraUniforms.tintColor, 'value'],
  73. label: 'Tint Color',
  74. description: 'Change the tint color applied to the final rendered image.',
  75. onChange: ()=>{
  76. viewer.setDirty()
  77. }
  78. })
  79. }
  80. _testStart()
  81. init().finally(_testFinish)
  82. </script>
  83. </head>
  84. <body>
  85. <div id="canvas-container">
  86. <canvas id="mcanvas"></canvas>
  87. </div>
  88. </body>