Shaders.metal 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // Shaders.metal
  3. // LenzCameraNativeModuleForRN
  4. //
  5. // Created by 王昭威 on 2023/1/26.
  6. //
  7. #include "LenzCommon.h"
  8. #include <metal_stdlib>
  9. using namespace metal;
  10. typedef struct{
  11. // float3 position [[attribute]];
  12. }VertextIn;
  13. typedef struct{
  14. float4 position [[position]];
  15. float2 uv;
  16. }VertextOut;
  17. vertex VertextOut vert_main(uint vertId [[vertex_id]]){
  18. float4x4 vertices = float4x4(
  19. float4( -1.0, -1.0, 0.0, 1.0 ),
  20. float4( 1.0, -1.0, 0.0, 1.0 ),
  21. float4( -1.0, 1.0, 0.0, 1.0 ),
  22. float4( 1.0, 1.0, 0.0, 1.0 )
  23. );
  24. float4x2 uvs = float4x2(
  25. float2( 0.0, 1.0 ),
  26. float2( 1.0, 1.0 ),
  27. float2( 0.0, 0.0 ),
  28. float2( 1.0, 0.0 )
  29. );
  30. return VertextOut{
  31. .position = vertices[vertId],
  32. .uv = uvs[vertId],
  33. };
  34. }
  35. fragment half4 frag_main(VertextOut in [[stage_in]], texture2d<float> texture [[texture(CameraCapturedTexture)]]){
  36. sampler s = sampler(address::clamp_to_edge,
  37. filter::linear);
  38. if(is_null_texture(texture)){
  39. return half4(0, 0, 0, 1);
  40. }
  41. return half4(texture.sample(s, in.uv));
  42. }