2
\$\begingroup\$

So I have this rather simple surface shader that essentially cuts a hole in the object based on another texture.

Shader "Custom/NewSurfaceShader" {
    Properties {
        _MainTex("Texture (RGB)", 2D) = "white" {}
    _SliceGuide("Slice Guide (RGB)", 2D) = "white" {}
    _SliceAmount("Slice Amount", Range(0.0, 1.0)) = 0.5
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        Cull OFF
        CGPROGRAM

#pragma surface surf Lambert addshadow
        struct Input {
        float2 uv_MainTex;
        float2 uv_SliceGuide;
        float _SliceAmount;
    };
    sampler2D _MainTex;
    sampler2D _SliceGuide;
    float _SliceAmount;



    void surf(Input IN, inout SurfaceOutput o) {

        clip(tex2D(_SliceGuide, IN.uv_SliceGuide).rgb - _SliceAmount);
        o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
    }
    ENDCG
    }
        Fallback "Diffuse"
}

This produces the following effect. Example 1[1]

Now as cool as this looks I want to only apply it to, let's say, one side of the cube and its opposite side. Any suggestions on how do I go about doing this will be appreciated.

\$\endgroup\$
2
  • \$\begingroup\$ I see a couple of ways, but not a single one looks like proper way to do it. (On the other hand, nearly nothing is elegant in the world of shaders.) Anyway, you should narrow the requirements. Will you use it for cubes only or any geometry? What's max number of faces that may need holes per object? Or do you need it to be as generic as possible? \$\endgroup\$ Sep 15, 2016 at 18:01
  • \$\begingroup\$ The simplest explanation is that I want to use this to "blast holes trough object" (not just cubes). Think of it as a disintegration ray what ever it hits it stops existing. \$\endgroup\$
    – Uri Popov
    Sep 16, 2016 at 8:50

1 Answer 1

1
\$\begingroup\$

I have an idea:

  • Shoot Raycast from camera (if first person).
  • Use textureCoord to find the UV coordinates.
  • Pass the coordinates into the shader, and use them to draw the decal.

This should take care of one side of the object, the part facing the raycast. I can suggest a way to detect the other side of the object. But i do not know if this is efficient:

Suggestion: Shoot a 'backtrace' ray starting from quite a distance from the original ray:

You use the ray info from ray i described above and you see in the picture below (1st Ray), find the distance between the origin of the 1st Ray, and its hitPoint's (Hitpoint A) position. Then add some distance to that, and use that point in space to shoot a ray (2nd Ray) back to the 1st Ray's origin. Find the Hitpoint on the opposide side of the object and apply textureCoord again -> pass to shader -> use for second decal.

Keep in mind that there can be other objects hitting the rayCast that are in between the backtrace's origin and hitpoint on your desired object. A RaycastAll and a comparison should fix that possible problem.

enter image description here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .