- Home /
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
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.
I created a new shader. Added this shader to a material. Then added material to the cube. It's not working. Cube is pink.
I use Unity 4.7.1. $$anonymous$$aybe this is the problem, why the shader not works.
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
Follow this Question
Related Questions
Shader works only on Cubes properly (Alpha problem) 1 Answer
Cancel out mesh alpha 1 Answer
Direction of a mesh 2 Answers
Will Semi Transparent mesh with several folds of geometry sort correctly? 1 Answer
How to return from shader the indexes of triangles/vertices that was actually rendered? 1 Answer