• 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 /
avatar image
1
Question by Bongmo · Apr 21, 2016 at 07:46 PM · shadermeshalphacubevertices

Cube botttom transparent with shader?

I added a simple cube to the scene. Now, I want, that the bottom of the cube is transparent. How can I make this with a shader?

I tried that with mesh colors. It's working, but it's not like a shader. Mesh.colors

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Scribe · Apr 21, 2016 at 08:39 PM

You could add a simple clip to the standard shader to get rid of one side, if you want to see the 'inside' sides of the cube that remains, you will also need to turn off culling:

 Shader "Custom/TransparentSide" {
     Properties {
         _Color ("Color", Color) = (1,1,1,1)
         _MainTex ("Albedo (RGB) Trans (A)", 2D) = "white" {}
         _Side ("Side Normal", Vector) = (0,-1,0,0)
         _Tolerance ("Tolerance", Range(0.0001, 0.1)) = 0.025
         _Glossiness ("Smoothness", Range(0,1)) = 0.5
         _Metallic ("Metallic", Range(0,1)) = 0.0
     }
     SubShader {
         Tags { "RenderType"="Opaque" }
         LOD 200
         Cull Off
         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard fullforwardshadows
 
         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0
 
         sampler2D _MainTex;
 
         struct Input {
             float2 uv_MainTex;
             float3 worldNormal;
         };
 
         half _Glossiness;
         float _Tolerance;
         fixed3 _Side;
         half _Metallic;
         fixed4 _Color;
 
         void surf (Input IN, inout SurfaceOutputStandard o) {
             float mask = dot(IN.worldNormal, normalize(_Side.xyz));
             clip(1 - _Tolerance - mask);
 
             // Albedo comes from a texture tinted by color
             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
             o.Albedo = c.rgb;
             // Metallic and smoothness come from slider variables
             o.Metallic = _Metallic;
             o.Smoothness = _Glossiness;
             o.Alpha = c.a;
         }
         ENDCG
     } 
     FallBack "Diffuse"
 }

Here, 'Side Normal' is the normal direction of the side you want to cut, i.e. the bottom side of a cube would be x=0, y=-1, z=0. note that the w component is completely disregarded, but no 3 component vector exists as a property type (as far as I know). 'Tolreance' adjusts how strict the cutting is, "should sides almost facing this direction also be cut", the range of values it allows is somewhat arbitrary, though setting it to 0 may not work due to floating point errors.

Comment
Add comment · Show 3 · Share
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
avatar image Bongmo · Apr 21, 2016 at 09:01 PM 0
Share

I created a new shader. Added this shader to a material. Then added material to the cube. It's not working. Cube is pink.

avatar image Bongmo · Apr 21, 2016 at 09:25 PM 0
Share

I use Unity 4.7.1. $$anonymous$$aybe this is the problem, why the shader not works.

avatar image Scribe · Apr 22, 2016 at 07:42 AM 1
Share

Yes, this currently adopts Unity5's standard shader commands, so it will not work in unity 4. The logic remains the same however; turn culling off; add worldNormal to your input struct, add the two new propeties _Side and _Tolerance; find the mask value and clip based on it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

60 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

Related Questions

Will Semi Transparent mesh with several folds of geometry sort correctly? 1 Answer

Highmap on a cube. 0 Answers

How to return from shader the indexes of triangles/vertices that was actually rendered? 1 Answer

How to set mesh normals per face? 2 Answers

Lower-level way to find NEARBY TRIANGLES on the mesh? (Answered.) 4 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