ssao-pass.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { QuadSchema, QuadValues } from 'mol-gl/compute/util';
  7. import { TextureSpec, Values, UniformSpec } from 'mol-gl/renderable/schema';
  8. import { ShaderCode } from 'mol-gl/shader-code';
  9. import { WebGLContext } from 'mol-gl/webgl/context';
  10. import { Texture } from 'mol-gl/webgl/texture';
  11. import { ValueCell } from 'mol-util';
  12. import { createComputeRenderItem } from 'mol-gl/webgl/render-item';
  13. import { createComputeRenderable } from 'mol-gl/renderable';
  14. import { Vec2 } from 'mol-math/linear-algebra';
  15. import { ParamDefinition as PD } from 'mol-util/param-definition';
  16. const SSAOPassSchema = {
  17. ...QuadSchema,
  18. tColor: TextureSpec('texture', 'rgba', 'ubyte', 'nearest'),
  19. tDepth: TextureSpec('texture', 'rgba', 'ubyte', 'nearest'),
  20. uTexSize: UniformSpec('v2'),
  21. uEnable: UniformSpec('i'),
  22. uKernelSize: UniformSpec('i'),
  23. uBias: UniformSpec('f'),
  24. uRadius: UniformSpec('f'),
  25. }
  26. export const SSAOPassParams = {
  27. enable: PD.Boolean(true),
  28. kernelSize: PD.Numeric(4, { min: 1, max: 100, step: 1 }),
  29. bias: PD.Numeric(0.5, { min: 0, max: 1, step: 0.01 }),
  30. radius: PD.Numeric(128, { min: 0, max: 256, step: 1 }),
  31. }
  32. export type SSAOPassProps = PD.Values<typeof SSAOPassParams>
  33. export function getSSAOPassRenderable(ctx: WebGLContext, colorTexture: Texture, depthTexture: Texture, props: Partial<SSAOPassProps>) {
  34. const p = { ...PD.getDefaultValues(SSAOPassParams), props }
  35. const values: Values<typeof SSAOPassSchema> = {
  36. ...QuadValues,
  37. tColor: ValueCell.create(colorTexture),
  38. tDepth: ValueCell.create(depthTexture),
  39. uTexSize: ValueCell.create(Vec2.create(colorTexture.width, colorTexture.height)),
  40. uEnable: ValueCell.create(p.enable ? 1 : 0),
  41. uKernelSize: ValueCell.create(p.kernelSize),
  42. uBias: ValueCell.create(p.bias),
  43. uRadius: ValueCell.create(p.radius),
  44. }
  45. const schema = { ...SSAOPassSchema }
  46. const shaderCode = ShaderCode(
  47. require('mol-gl/shader/quad.vert').default,
  48. require('mol-gl/shader/passes/ssao.frag').default
  49. )
  50. const renderItem = createComputeRenderItem(ctx, 'triangles', shaderCode, schema, values)
  51. return createComputeRenderable(renderItem, values)
  52. }