• UE4采样纹理指定位置颜色


    极坐标

    1. float polar_coordinates(float2 uvs) {
    2. struct SF {
    3. static float remap_value_range(float input, float input_low, float input_high, float target_low, float target_high) {
    4. return (input - input_low) / (input_high - input_low) * (target_high - target_low) + target_low;
    5. }
    6. };
    7. float pi = acos(-1);
    8. float2 uv = float2(SF::remap_value_range(uvs.x, 0, 1, -1, 1), SF::remap_value_range(uvs.y, 0, 1, -1, 1));
    9. return SF::remap_value_range(atan2(uv.x, uv.y) / pi, -1, 1, 0, 1);
    10. }

    旋转

    1. /**
    2. * 旋转
    3. * rotation_center - 旋转点
    4. * rotation_angle - 旋转角度
    5. */
    6. float2 rotate(float2 uvs, float2 rotation_center, float rotation_angle) {
    7. float pi = acos(-1);
    8. rotation_angle *= 2 * pi;
    9. // 逆时针旋转
    10. float2x2 mt = float2x2(
    11. cos(rotation_angle), sin(rotation_angle),
    12. -sin(rotation_angle), cos(rotation_angle)
    13. );
    14. /** 顺时针旋转
    15. float2x2 mt = float2x2(
    16. cos(rotation_angle), -sin(rotation_angle),
    17. sin(rotation_angle), cos(rotation_angle)
    18. );
    19. */
    20. return mul(uvs + rotation_center * -1, mt) + rotation_center;
    21. }

    也可以使用 UE 自带的 CustomRotator 材质函数。

    溶解

    1. /**
    2. * 溶解
    3. * texture2d - 纹理
    4. * texture2d_noise - 噪点纹理
    5. * value - 溶解程度
    6. * edge - 溶解边缘
    7. * xyz - 溶解边缘颜色
    8. * w - 溶解边缘宽度
    9. */
    10. float4 dissolve(float2 uvs, Texture2D texture2d, float2 uvs_noise Texture2D texture2d_noise, float value, float4 edge) {
    11. float3 texture_color = Texture2DSampleLevel(texture2d, texture2dSampler, uvs, 0).xyz;
    12. // float3 texture_color = Texture2DSample(texture2d, Material.Texture2D_0Sampler, uvs).xyz;
    13. float noise_value = Texture2DSampleLevel(texture2d_noise, texture2d_noiseSampler, uvs_noise, 1).x;
    14. // float noise_value = Texture2DSample(texture2d_noise, Material.Texture2D_1Sampler, uvs_noise).x;
    15. value = saturate(value) * 2 - 1 + noise_value;
    16. float dissolve_value = value * step(value, 1) * step(saturate(1 - edge.w), value);
    17. float3 dissolve_color = edge.xyz * dissolve_value;
    18. value = saturate(value);
    19. float alpha = 1 - floor(value);
    20. texture_color = lerp(texture_color, 0, value);
    21. float3 color = saturate(texture_color + dissolve_color);
    22. return float4(color, alpha);
    23. }

    燃烧

    1. /**
    2. * 燃烧
    3. * texture2d - 纹理
    4. * texture2d_noise - 噪点纹理
    5. * noise_scale - 噪点尺寸
    6. * value - 燃烧程度
    7. * edge - 燃烧边缘
    8. * xyz - 燃烧边缘颜色
    9. * w - 燃烧边缘宽度
    10. */
    11. float4 burn(float2 uvs, Texture2D texture2d, Texture2D texture2d_noise, float noise_scale, float value, float4 edge) {
    12. float3 texture_color = Texture2DSampleLevel(texture2d, texture2dSampler, uvs, 0).xyz;
    13. // float3 texture_color = Texture2DSample(texture2d, Material.Texture2D_0Sampler, uvs).xyz;
    14. float noise_value = Texture2DSampleLevel(texture2d_noise, texture2d_noiseSampler, uvs, 1).x * noise_scale;
    15. // float noise_value = Texture2DSample(texture2d_noise, Material.Texture2D_1Sampler, uvs).x * noise_scale;
    16. float progress = uvs.x + noise_value;
    17. value *= 1 + noise_scale + edge.w;
    18. float react_1 = step(value, progress);
    19. float react_2 = step(value - edge.w, progress);
    20. float react_3 = react_2 - react_1;
    21. float3 color = edge.xyz * react_3 + texture_color * react_1;
    22. return float4(color, react_2);
    23. }

    查找边缘

    1. float3 find_edges(float2 uvs, Texture2D texture2d, float2 texture_size) {
    2. float GX[3][3] = {
    3. {-1, -2, -1},
    4. {0, 0, 0},
    5. {1, 2, 1}
    6. };
    7. float GY[3][3] = {
    8. {-1, 0, 1},
    9. {-2, 0, 2},
    10. {-1, 0, 1}
    11. };
    12. float ex = 0.f;
    13. float ey = 0.f;
    14. for(int x = -1; x <= 1; x++) {
    15. for (int y = -1; y <= 1; y++) {
    16. float2 uvs_offset = float2(uvs.x + x * texture_size.x, uvs.y + y * texture_size.y);
    17. float3 pixel_color = Texture2DSampleLevel(texture2d, texture2dSampler, uvs_offset, 0);
    18. ex += pixel_color * GX[x + 1][y + 1];
    19. ey += pixel_color * GY[x + 1][y + 1];
    20. }
    21. }
    22. return saturate(1 - (abs(ex) + abs(ey)));
    23. }

    浮雕

    1. float3 emboss(float2 uvs, Texture2D texture2d, float2 texture_size, float value) {
    2. float GX[3][3] = {
    3. {-1, -2, -1},
    4. {0, 0, 0},
    5. {1, 2, 1}
    6. };
    7. float GY[3][3] = {
    8. {-1, 0, 1},
    9. {-2, 0, 2},
    10. {-1, 0, 1}
    11. };
    12. float ex = 0.f;
    13. float ey = 0.f;
    14. for(int x = -1; x <= 1; x++) {
    15. for (int y = -1; y <= 1; y++) {
    16. float2 uvs_offset = float2(uvs.x + x * texture_size.x, uvs.y + y * texture_size.y);
    17. float3 pixel_color = Texture2DSampleLevel(texture2d, texture2dSampler, uvs_offset, 0);
    18. ex += pixel_color * GX[x + 1][y + 1];
    19. ey += pixel_color * GY[x + 1][y + 1];
    20. }
    21. }
    22. return value + ex + ey;
    23. }

    去色

    1. float3 desaturation(float3 color) {
    2. float3 factor = float3(0.299, 0.587, 0.114);
    3. return dot(color, factor);
    4. }

    也可以使用 UE 自带的 Desaturation 表达式。

    抖动

    1. float3 shake(float2 uvs, Texture2D texture2d, Texture2D texture2d_noise, float range, float value) {
    2. float noise_value = Texture2DSampleLevel(texture2d_noise, texture2d_noiseSampler, float2(sin(value), cos(value)), 0).x;
    3. noise_value *= range;
    4. // 采样红色,右移
    5. float texture_color_r = Texture2DSampleLevel(texture2d, texture2dSampler, float2(uvs.x + noise_value, uvs.y), 1).x;
    6. // 采样绿色,不变
    7. float texture_color_g = Texture2DSampleLevel(texture2d, texture2dSampler, uvs, 1).y;
    8. // 采样蓝色,左移
    9. float texture_color_b = Texture2DSampleLevel(texture2d, texture2dSampler, float2(uvs.x - noise_value, uvs.y), 1).z;
    10. return float3(texture_color_r, texture_color_g, texture_color_b);
    11. }

  • 相关阅读:
    智慧港口解决方案-最新全套文件
    Python机器学习、深度学习在气象、海洋、水文领域实践应用
    C++ 数列游戏
    什么是集成测试?集成测试方法有哪些?
    java基础巩固12
    淘宝/天猫优惠券查询接口 API 返回值说明
    入门力扣自学笔记134 C++ (题目编号658)
    linux tasks errors
    什么是vue?
    数学建模学习笔记(3):灰色关联分析
  • 原文地址:https://blog.csdn.net/qq_42486920/article/details/126438437