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 /
  • Help Room /
avatar image
0
Question by himynameisandi · Sep 07, 2017 at 10:57 AM · shadertransparencyopacitycrosssection

how to add transparency to a cross-section-shader?

Hi Guys,

I hope you can help me out. I need a cross-section-shader with the possibility to change the Transparency via Slider. (Like the "Glossiness" or "Metallic" - option; already implemented)

I found a Shader, which works very fine for me. But i can not change the transparency with it. I've tried it for a few weeks now, but i always failed in manipulating the current shader-script. I'm not made for writing shaders :'(

But i guess it's not that hard for somebody who is good at writing them.. so maybe somebody can help me out of my misery. :(

Here's the code:

     Shader "CrossSection/OnePlaneBSP" {
        Properties{
            _Color("Color", Color) = (1,1,1,1)
            _CrossColor("Cross Section Color", Color) = (1,1,1,1)
            _MainTex("Albedo (RGB)", 2D) = "white" {}
            _Glossiness("Smoothness", Range(0,1)) = 0.5
            _Metallic("Metallic", Range(0,1)) = 0.0
            _PlaneNormal("PlaneNormal",Vector) = (0,1,0,0)
            _PlanePosition("PlanePosition",Vector) = (0,0,0,1)
            _StencilMask("Stencil Mask", Range(0, 255)) = 255
        }
        SubShader {
            Tags { "RenderType"="Opaque" }
            //LOD 200
            Stencil
            {
                Ref [_StencilMask]
                CompBack Always
                PassBack Replace
      
                CompFront Always
                PassFront Zero
            }
            Cull Back
                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 worldPos;
            };
      
            half _Glossiness;
            half _Metallic;
            fixed4 _Color;
            fixed4 _CrossColor;
            fixed3 _PlaneNormal;
            fixed3 _PlanePosition;
            bool checkVisability(fixed3 worldPos)
            {
                float dotProd1 = dot(worldPos - _PlanePosition, _PlaneNormal);
                return dotProd1 > 0  ;
            }
            void surf(Input IN, inout SurfaceOutputStandard o) {
                if (checkVisability(IN.worldPos))discard;
                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
          
                Cull Front
                CGPROGRAM
     #pragma surface surf NoLighting  noambient
      
            struct Input {
                half2 uv_MainTex;
                float3 worldPos;
      
            };
            sampler2D _MainTex;
            fixed4 _Color;
            fixed4 _CrossColor;
            fixed3 _PlaneNormal;
            fixed3 _PlanePosition;
            bool checkVisability(fixed3 worldPos)
            {
                float dotProd1 = dot(worldPos - _PlanePosition, _PlaneNormal);
                return dotProd1 >0 ;
            }
            fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten)
            {
                fixed4 c;
                c.rgb = s.Albedo;
                c.a = s.Alpha;
                return c;
            }
      
            void surf(Input IN, inout SurfaceOutput o)
            {
                if (checkVisability(IN.worldPos))discard;
                o.Albedo = _CrossColor;
      
            }
                ENDCG
          
        }
        //FallBack "Diffuse"
     }

you can also find this asset for free in the asset store if you want to have a closer look at it: https://www.assetstore.unity3d.com/en/#!/content/66300

I just want to change the transparency (opacity) of the object, too. :/

Thank you guys!

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

2 Replies

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

Answer by func_Mathias · Dec 24, 2017 at 11:15 AM

I actually found this post trying to do the same thing with the same shader and eventually found out, so in case you still need this then here's how I did it.

These are the lines that I changed/added so you can tell what's different

 _Transparency("Transparency", Range(0.0,1)) = 0.5
 Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
 ZWrite Off
 Blend SrcAlpha OneMinusSrcAlpha
 #pragma surface surf Standard fullforwardshadows alpha
 float _Transparency;
 o.Alpha = _Transparency;


And heres the whole shader as I'd recomend using it (not using the plain colored inside as it acts badly with the transparency)

 Shader "CrossSection/OnePlaneBSPTransparent" {
         Properties{
             _Color("Color", Color) = (1,1,1,1)
             _CrossColor("Cross Section Color", Color) = (1,1,1,1)
             _MainTex("Albedo (RGB)", 2D) = "white" {}
             _Glossiness("Smoothness", Range(0,1)) = 0.5
             _Metallic("Metallic", Range(0,1)) = 0.0
             _Transparency("Transparency", Range(0.0,1)) = 0.5
             _PlaneNormal("PlaneNormal",Vector) = (0,1,0,0)
             _PlanePosition("PlanePosition",Vector) = (0,0,0,1)
             _StencilMask("Stencil Mask", Range(0, 255)) = 255
         }
         SubShader {
             Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
             //LOD 200
             ZWrite Off
             Blend SrcAlpha OneMinusSrcAlpha
 
             Stencil
             {
                 Ref [_StencilMask]
                 CompBack Always
                 PassBack Replace
       
                 CompFront Always
                 PassFront Zero
             }
             Cull Back
                 CGPROGRAM
                 // Physically based Standard lighting model, and enable shadows on all light types
      #pragma surface surf Standard fullforwardshadows alpha
       
                 // Use shader model 3.0 target, to get nicer looking lighting
      #pragma target 3.0
       
                 sampler2D _MainTex;
       
             struct Input {
                 float2 uv_MainTex;
       
                 float3 worldPos;
             };
       
             half _Glossiness;
             half _Metallic;
             fixed4 _Color;
             fixed4 _CrossColor;
             fixed3 _PlaneNormal;
             fixed3 _PlanePosition;
             float _Transparency;
             bool checkVisability(fixed3 worldPos)
             {
                 float dotProd1 = dot(worldPos - _PlanePosition, _PlaneNormal);
                 return dotProd1 > 0  ;
             }
             void surf(Input IN, inout SurfaceOutputStandard o) {
                 if (checkVisability(IN.worldPos))discard;
                 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 = _Transparency;
             }
             ENDCG
 
             Cull Front
                 CGPROGRAM
                 // Physically based Standard lighting model, and enable shadows on all light types
      #pragma surface surf Standard fullforwardshadows alpha
       
                 // Use shader model 3.0 target, to get nicer looking lighting
      #pragma target 3.0
       
                 sampler2D _MainTex;
       
             struct Input {
                 float2 uv_MainTex;
       
                 float3 worldPos;
             };
       
             half _Glossiness;
             half _Metallic;
             fixed4 _Color;
             fixed4 _CrossColor;
             fixed3 _PlaneNormal;
             fixed3 _PlanePosition;
             float _Transparency;
             bool checkVisability(fixed3 worldPos)
             {
                 float dotProd1 = dot(worldPos - _PlanePosition, _PlaneNormal);
                 return dotProd1 > 0  ;
             }
             void surf(Input IN, inout SurfaceOutputStandard o) {
                 if (checkVisability(IN.worldPos))discard;
                 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 = _Transparency;
             }
             ENDCG
 
         }
         //FallBack "Diffuse"
      }


I made the inside faces use the same code as the outside faces, but if you don't want this you can just remove the lines from "Cull Front" (line 67) to "ENDCG" (line 104).

Hope this ends your misery if it wasn't already over :)

Comment
Add comment · Show 1 · 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 himynameisandi · Jan 09, 2018 at 11:07 AM 0
Share

Works perfect! Thank you very much! That's exactly what I needed!

avatar image
0

Answer by willyci · Feb 27, 2018 at 09:21 PM

Works perfectly! Thank you very much! That's exactly what I needed!

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

157 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with transparency of an object 0 Answers

Add distance fade to his shader 1 Answer

Circular Bar Transparent gradient 0 Answers

Transparency using Scene Color node not layering multiple transparent objects over each other 0 Answers

Problem with transparency with x86_64 build 1 Answer


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