Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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, 2019 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: 

Follow this Question

Answers Answers and Comments

143 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

Related Questions

Shader is using forward lighting against my will? 1 Answer

toonLighted shader to work with deferred rendered shadows 2 Answers

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

exclude_path:deferred causes see through? 0 Answers

Shader not working properly in Deferred render mode 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges