转载自 http://www.taidous.com/thread-52133-1-1.html
Shader "Unlit / Pano360Shader"
{
Properties
{
_MainTex("Base(RGB)", 2D) = "white"{}
_Color("Main Color", Color) = (1,1,1,0.5)
}
SubShader
{
Tags{ "RenderType" = "Opaque" }
//This is used to print the texture inside of the sphere
Cull Front
CGPROGRAM
#pragma surface surf SimpleLambert
half4 LightingSimpleLambert(SurfaceOutput s, half3 lightDir, half atten)
{
half4 c;
c.rgb = s.Albedo;
return c;
}
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
float4 myColor : COLOR;
};
fixed3 _Color;
void surf(Input IN, inout SurfaceOutput o)
{
//This is used to mirror the image correctly when printing it inside of the sphere
IN.uv_MainTex.x = 1 - IN.uv_MainTex.x;
fixed3 result = tex2D(_MainTex, IN.uv_MainTex)*_Color;
o.Albedo = result.rgb;
o.Alpha = 1;
}
ENDCG
}
Fallback "Diffuse"
}
遮罩Shader
[AppleScript] 纯文本查看 复制代码
Shader "ImageEffect/MaskIcon"
{
Properties
{
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Mask("Base (RGB)", 2D) = "white" {}
_Color("Tint", Color) = (1,1,1,1)
_StencilComp("Stencil Comparison", Float) = 8
_Stencil("Stencil ID", Float) = 0
_StencilOp("Stencil Operation", Float) = 0
_StencilWriteMask("Stencil Write Mask", Float) = 255
_StencilReadMask("Stencil Read Mask", Float) = 255
_ColorMask("Color Mask", Float) = 15
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
Stencil
{
Ref[_Stencil]
Comp[_StencilComp]
Pass[_StencilOp]
ReadMask[_StencilReadMask]
WriteMask[_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest[unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
ColorMask[_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "UnityUI.cginc"
#pragma multi_compile __ UNITY_UI_ALPHACLIP
struct a2v
{
fixed2 uv : TEXCOORD0;
half4 vertex : POSITION;
float4 color : COLOR;
};
fixed4 _Color;
struct v2f
{
fixed2 uv : TEXCOORD0;
half4 vertex : SV_POSITION;
float4 color : COLOR;
};
sampler2D _MainTex;
sampler2D _Mask;
v2f vert(a2v i)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, i.vertex);
o.uv = i.uv;
o.color = i.color * _Color;
return o;
}
fixed4 frag(v2f i) : COLOR
{
half4 color = tex2D(_MainTex, i.uv) * i.color;
half4 mask = tex2D(_Mask, i.uv);
color.a *= mask.a;
return color;
}
ENDCG
}
}
}
C#代码
[AppleScript] 纯文本查看 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pano360 : MonoBehaviour {
public Transform container;
float turnSpeedMouse=50;
/// <summary>
/// 第一次按下的位置
/// </summary>
private Vector2 first = Vector2.zero;
/// <summary>
/// 鼠标的拖拽位置(第二次的位置)
/// </summary>
private Vector2 second = Vector2.zero;
/// <summary>
/// 旋转的角度
/// </summary>
private float angle = 3f;
void OnGUI()
{
if (Event.current.type == EventType.MouseDown)
{
//记录鼠标按下的位置
first = Event.current.mousePosition;
}
if (Event.current.type == EventType.MouseDrag)
{
//记录鼠标拖动的位置
second = Event.current.mousePosition;
if (second.x < first.x)
{
//拖动的位置的x坐标比按下的位置的x坐标小时,响应向左事件
this.transform.Rotate(Vector3.up , angle);
container.Rotate(new Vector3(0 , angle * (-1) , 0f) * Time.deltaTime * turnSpeedMouse);
transform.Rotate(new Vector3(angle , 0 , 0) * Time.deltaTime * turnSpeedMouse);
}
if (second.x > first.x)
{
//拖动的位置的x坐标比按下的位置的x坐标大时,响应向右事件
this.transform.Rotate(Vector3.down , angle);
container.Rotate(new Vector3(0 , angle * (1) , 0f) * Time.deltaTime * turnSpeedMouse);
transform.Rotate(new Vector3(angle , 0 , 0) * Time.deltaTime * turnSpeedMouse);
}
first = second;
}
}
}
本文介绍了一种用于全景图展示的特殊Shader实现方式,通过Unlit/Pano360Shader达到内部纹理映射的效果,并实现了基于Unity的鼠标拖拽全景视图功能。此外,还提供了一个用于图像遮罩的MaskIcon Shader示例。
772

被折叠的 条评论
为什么被折叠?



