【Unity3d】窗体透明
来源:互联网 发布:淘宝销售属性虚拟类目 编辑:程序博客网 时间:2024/06/07 13:50
偶然在国外网站上看到的一个脚本,通过纯色背景抠像的方法,把windows的窗体透明化,可以用来做无背景的小头像、桌面精灵等。
首先新建一个叫TransparentWindow的CS脚本,输入如下代码:
using System;using System.Runtime.InteropServices;using UnityEngine;public class TransparentWindow : MonoBehaviour{ [SerializeField] private Material m_Material; private struct MARGINS { public int cxLeftWidth; public int cxRightWidth; public int cyTopHeight; public int cyBottomHeight; } // Define function signatures to import from Windows APIs [DllImport("user32.dll")] private static extern IntPtr GetActiveWindow(); [DllImport("user32.dll")] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong); [DllImport("Dwmapi.dll")] private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins); // Definitions of window styles const int GWL_STYLE = -16; const uint WS_POPUP = 0x80000000; const uint WS_VISIBLE = 0x10000000; void Start() {#if !UNITY_EDITOR var margins = new MARGINS() { cxLeftWidth = -1 }; // Get a handle to the window var hwnd = GetActiveWindow(); // Set properties of the window // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633591%28v=vs.85%29.aspx SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE); // Extend the window into the client area //See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969512%28v=vs.85%29.aspx DwmExtendFrameIntoClientArea(hwnd, ref margins);#endif } // Pass the output of the camera to the custom material // for chroma replacement void OnRenderImage(RenderTexture from, RenderTexture to) { Graphics.Blit(from, to, m_Material); }}
然后新建一个叫ChromakeyTransparent的Shader:
Shader "Custom/ChromakeyTransparent" { Properties{ _MainTex("Base (RGB)", 2D) = "white" {} _TransparentColourKey("Transparent Colour Key", Color) = (0,0,0,1) _TransparencyTolerance("Transparency Tolerance", Float) = 0.01 } SubShader{ Pass{ Tags{ "RenderType" = "Opaque" } LOD 200 CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct a2v { float4 pos : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; }; v2f vert(a2v input) { v2f output; output.pos = UnityObjectToClipPos(input.pos); output.uv = input.uv; return output; } sampler2D _MainTex; float3 _TransparentColourKey; float _TransparencyTolerance; float4 frag(v2f input) : SV_Target { // What is the colour that *would* be rendered here? float4 colour = tex2D(_MainTex, input.uv); // Calculate the different in each component from the chosen transparency colour float deltaR = abs(colour.r - _TransparentColourKey.r); float deltaG = abs(colour.g - _TransparentColourKey.g); float deltaB = abs(colour.b - _TransparentColourKey.b); // If colour is within tolerance, write a transparent pixel if (deltaR < _TransparencyTolerance && deltaG < _TransparencyTolerance && deltaB < _TransparencyTolerance) { return float4(0.0f, 0.0f, 0.0f, 0.0f); } // Otherwise, return the regular colour return colour; } ENDCG } }}
新建一个材质,选择我们刚刚写好的Shader,将TransparentWindow挂载到摄像机上,摄像机的Clear Flags选择Solid Color,Background选择和材质的Transparent Color Key相同的颜色(建议选择与模型边缘颜色相近的颜色,不然会出现较明显的毛边),将材质拖拽给TransparentWindow的Material变量。
运行之后可以看到背景颜色已经被扣掉(黑色其实是透明):
Build一下之后我们就可以看到类似桌面精灵的一个程序了:
By:蒋志杰
2 0
- 【Unity3d】窗体透明
- 【Unity3d】窗体透明
- Unity3d窗体透明
- 窗体透明
- 透明窗体
- 透明窗体
- 透明窗体
- 透明窗体
- 透明窗体子窗体显示
- 分层窗体,透明窗体(转)
- vb实现透明窗体
- 如何制作透明窗体
- 透明窗体的实现
- 创建透明窗体
- 创建透明窗体
- VC 制作透明窗体
- VB_窗体透明
- c#透明窗体
- Android studio 自动导入(全部)包 import
- [基础]Java内存和GC汇总
- C++第4次实验(基础班)
- Leetcode-Methodology-Preprocessing
- 由alert出来的对象是[object object],就感觉不知所措,而进一步思考,对象究竟是什么?即变成语言(如js、java、c等任意语言)中的变量,究竟是什么东西?
- 【Unity3d】窗体透明
- HTML5 Canvas+JS控制电脑或手机上的摄像头实例.
- c++实验三—百计百钱
- 构造函数那些事儿
- rs232公头、母头的引脚定义
- C++作业4
- Exynos4412 系统移植
- Android 动画学习(一)之View Animation
- 【JZOJ4589】抢夺 题解