Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Smoothly blending between two matcap textures if inside a cube

Discussion in 'Shaders' started by Solvit8, Feb 28, 2023.

  1. Solvit8

    Solvit8

    Joined:
    Jan 16, 2023
    Posts:
    7
    I'm trying to smoothly blend between two matcap textures if the object intersects a predefined cube in the shader. I've got everything working, except the smoothly blending part.

    This is what I use to check if a pixel is inside of the cube. Simply use the value it returns in a lerp function between the two textures, which can only be a 1 or a 0 so it wont give any smoothness.

    float insideBox3D(float3 v, float3 bottomLeft, float3 topRight) {
    float3 s = step(bottomLeft, v) - step(topRight, v);
    return (s.x * s.y * s.z);
    }

    I'm not sure if there is a better function to use that would give me a smoother result, or if theres a way to smooth out the values.
     

    Attached Files:

  2. Solvit8

    Solvit8

    Joined:
    Jan 16, 2023
    Posts:
    7
    I've got something that kinda works. Not sure if its optimal or really that viable, but it gives me results I'm alright with


    float insideBox3D(float3 v, float3 bottomLeft, float3 topRight) {
    float3 center = float3(
    ( bottomLeft.x + topRight.x ) / 2,
    ( bottomLeft.y + topRight.y ) / 2,
    ( bottomLeft.z + topRight.z ) / 2);
    float3 dimensions = float3(
    abs( distance( bottomLeft.x, topRight.x) ) + 2,
    abs( distance( bottomLeft.y, topRight.y) ) + 2,
    abs( distance( bottomLeft.z, topRight.z) ) + 2);
    float3 s = abs(step(bottomLeft, v) - step(topRight, v));
    return 1 - (
    clamp( ( s.x + (abs( distance(v, center) ) / dimensions.x) - s.x ), 0, 1) *
    clamp( ( s.y + (abs( distance(v, center) ) / dimensions.y) - s.y ), 0, 1) *
    clamp( ( s.z + (abs( distance(v, center) ) / dimensions.z) - s.z ), 0, 1) );
    }

    some parts could possibly be optimized, but it works.

    Also apologies for any formatting issues, I'm a little new to posting to the unity forums.