• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 27 at 12:40 PM by Cobo3.
avatar image
0
Question by Cobo3 · Sep 04, 2018 at 03:12 PM · shaderdeferreddecals

How can I distort deferred decals?

Hello everybody!

We are working on deferred decals right now. Given the shape of a cube, which can be freely scaled and rotated, the system projects the decal on the world with that shape. This allows for cool effects such as skewed decals.alt text

What we would like to do, however, is be able to freely transform the shape of the cube, that is, be able to specify the position of each vertex as desired by code; so that the resulting decal is properly projected. We want to achieve a distortion similar as the one shown here: alt text

To do that, we need to specify the position of each vertex and pass that into the shader, somehow, to distort the image accordingly. How can we do that?

The C# code we use is the following:

 Mesh mesh = CreateCubeMesh(); //The exact implementation is irrelevant
 CommandBuffer commandBuffer = new CommandBuffer();
 //Some otehr code...
 commandBuffer.DrawMesh(mesh, transform.localToWorldMatrix, material);

The vertex function we use is the following:

 v2f vert(appdata v)
 {
     v2f o;
     UNITY_SETUP_INSTANCE_ID(v);
     UNITY_TRANSFER_INSTANCE_ID(v, o);
     o.pos = UnityObjectToClipPos(v.vertex);
     o.uv = ComputeScreenPos(o.pos);
     o.ray = mul(UNITY_MATRIX_MV, v.vertex).xyz * float3(-1, -1, 1);
     o.decalNormal  = normalize(mul((float3x3)unity_ObjectToWorld, float3(0, 1, 0)));
     o.decalTangent = normalize(mul((float3x3)unity_ObjectToWorld, float3(1, 0, 0)));
     return o;
 }

And the fragment function is this:

 void frag(v2f i, out float4 outSpecSmoothness : SV_Target0, out float4 outNormal : SV_Target1)
 {
     i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
     float2 uv = i.uv.xy / i.uv.w;
     float limitTo = GetLimitTo();
     if (!isnan(limitTo))
         clip(abs(limitTo - tex2D(_DecaliciousLimitToGameObject, uv).r) > 0.1f ? -1 : 1);
     float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv);
     depth = Linear01Depth(depth);
     float4 vpos = float4(i.ray * depth,1);
     float3 wpos = mul(unity_CameraToWorld, vpos).xyz;
     float3 clipPos = mul(unity_WorldToObject, float4(wpos, 1)).xyz;
     clip(0.5f - abs(clipPos.xyz));
     float2 texUV = TRANSFORM_TEX((clipPos.xz + 0.5), _MainTex);
     float mask = tex2D(_MaskTex, texUV).r * GetMaskMultiplier();
     clip(mask - 0.0005f);
 }

untitled-1.png (317.2 kB)
image.png (161.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

146 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone explain what I'm doing wrong in these shaders? 0 Answers

Tesselletion "bypassed" by Depth of Field 0 Answers

deferred rendering path shader not working 0 Answers

Shader is using forward lighting against my will? 1 Answer

How to define custom deferred ambient lighting? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges