Unity Shader Example 17 (Skybox)
来源:互联网 发布:淘宝授权书怎么找哇 编辑:程序博客网 时间:2024/06/04 12:28
cs脚本,挂到Camera上
using UnityEngine;using System.Collections;public class Test : MonoBehaviour { public Material skybox;// Will be called after all regular rendering is done//public void OnRenderObject () // OnPostRender is called after a camera has finished rendering the scene. public void OnRenderObject(){// Apply the line material Camera cam = GetComponent<Camera>(); GL.PushMatrix();// Set transformation matrix for drawing to// match our transform //GL.MultMatrix( cam.worldToCameraMatrix); //GL.LoadProjectionMatrix(cam.projectionMatrix); // 前 Vector3 vft0 = new Vector3(-1.0f, 1.0f, -1.0f); Vector3 vft3 = new Vector3(1.0f, 1.0f, -1.0f); Vector3 vft2 = new Vector3(1.0f, -1.0f, -1.0f); Vector3 vft1 = new Vector3(-1.0f, -1.0f, -1.0f); // 后 Vector3 vbk0 = new Vector3(-1.0f, 1.0f, 1.0f); Vector3 vbk1 = new Vector3(-1.0f, -1.0f, 1.0f); Vector3 vbk2 = new Vector3(1.0f, -1.0f, 1.0f); Vector3 vbk3 = new Vector3(1.0f, 1.0f, 1.0f); // 左 Vector3 vlf0 = new Vector3(-1.0f, 1.0f, -1.0f); Vector3 vlf1 = new Vector3(-1.0f, -1.0f, -1.0f); Vector3 vlf2 = new Vector3(-1.0f, -1.0f, 1.0f); Vector3 vlf3 = new Vector3(-1.0f, 1.0f, 1.0f); // 右 Vector3 vrt0 = new Vector3(1.0f, 1.0f, -1.0f); Vector3 vrt1 = new Vector3(1.0f, -1.0f, -1.0f); Vector3 vrt2 = new Vector3(1.0f, -1.0f, 1.0f); Vector3 vrt3 = new Vector3(1.0f, 1.0f, 1.0f); //上 Vector3 vup0 = new Vector3(-1.0f, 1.0f, -1.0f); Vector3 vup1 = new Vector3(1.0f, 1.0f, -1.0f); Vector3 vup2 = new Vector3(1.0f, 1.0f, 1.0f); Vector3 vup3 = new Vector3(-1.0f, 1.0f, 1.0f); // 下 Vector3 vdn0 = new Vector3(-1.0f, -1.0f, -1.0f); Vector3 vdn1 = new Vector3(1.0f, -1.0f, -1.0f); Vector3 vdn2 = new Vector3(1.0f, -1.0f, 1.0f); Vector3 vdn3 = new Vector3(-1.0f, -1.0f, 1.0f); // 立方体先转到相机位置,再变换到相机空间(没有根据相机来旋转立方体) Matrix4x4 m = Matrix4x4.TRS(cam.transform.position, Quaternion.identity, Vector3.one); skybox.SetMatrix("view", cam.worldToCameraMatrix * m); // 前 skybox.SetPass(1); GL.Begin(GL.QUADS); //1 GL.TexCoord(new Vector3(0, 1, 0)); GL.Vertex(vft0); //2 GL.TexCoord(new Vector3(0, 0, 0)); GL.Vertex(vft1); //3 GL.TexCoord(new Vector3(1, 0, 0)); GL.Vertex(vft2); //4 GL.TexCoord(new Vector3(1, 1, 0)); GL.Vertex(vft3); GL.End(); // 左 skybox.SetPass(2); GL.Begin(GL.QUADS); //1 GL.TexCoord(new Vector3(1, 1, 0)); GL.Vertex(vlf0); //2 GL.TexCoord(new Vector3(1, 0, 0)); GL.Vertex(vlf1); //3 GL.TexCoord(new Vector3(0, 0, 0)); GL.Vertex(vlf2); //4 GL.TexCoord(new Vector3(0, 1, 0)); GL.Vertex(vlf3); GL.End(); //后 skybox.SetPass(0); GL.Begin(GL.QUADS); //1 GL.TexCoord(new Vector3(1, 1, 0)); GL.Vertex(vbk0); //2 GL.TexCoord(new Vector3(1, 0, 0)); GL.Vertex(vbk1); //3 GL.TexCoord(new Vector3(0, 0, 0)); GL.Vertex(vbk2); //4 GL.TexCoord(new Vector3(0, 1, 0)); GL.Vertex(vbk3); GL.End(); // 右 skybox.SetPass(3); GL.Begin(GL.QUADS); //1 GL.TexCoord(new Vector3(0, 1, 0)); GL.Vertex(vrt0); //2 GL.TexCoord(new Vector3(0, 0, 0)); GL.Vertex(vrt1); //3 GL.TexCoord(new Vector3(1, 0, 0)); GL.Vertex(vrt2); //4 GL.TexCoord(new Vector3(1, 1, 0)); GL.Vertex(vrt3); GL.End(); // 上 skybox.SetPass(4); GL.Begin(GL.QUADS); //1 GL.TexCoord(new Vector3(0, 1, 0)); GL.Vertex(vup0); //2 GL.TexCoord(new Vector3(0, 0, 0)); GL.Vertex(vup1); //3 GL.TexCoord(new Vector3(1, 0, 0)); GL.Vertex(vup2); //4 GL.TexCoord(new Vector3(1, 1, 0)); GL.Vertex(vup3); GL.End(); // 下 skybox.SetPass(5); GL.Begin(GL.QUADS); //1 GL.TexCoord(new Vector3(0, 0, 0)); GL.Vertex(vdn0); //2 GL.TexCoord(new Vector3(0, 1, 0)); GL.Vertex(vdn1); //3 GL.TexCoord(new Vector3(1, 1, 0)); GL.Vertex(vdn2); //4 GL.TexCoord(new Vector3(1, 0, 0)); GL.Vertex(vdn3); GL.End(); GL.PopMatrix ();} }
.shader
Shader "Skybox/6 Sided Custom" {Properties {_Tint ("Tint Color", Color) = (.5, .5, .5, .5)[Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0_Rotation ("Rotation", Range(0, 360)) = 0[NoScaleOffset] _FrontTex ("Front [+Z] (HDR)", 2D) = "grey" {}[NoScaleOffset] _BackTex ("Back [-Z] (HDR)", 2D) = "grey" {}[NoScaleOffset] _LeftTex ("Left [+X] (HDR)", 2D) = "grey" {}[NoScaleOffset] _RightTex ("Right [-X] (HDR)", 2D) = "grey" {}[NoScaleOffset] _UpTex ("Up [+Y] (HDR)", 2D) = "grey" {}[NoScaleOffset] _DownTex ("Down [-Y] (HDR)", 2D) = "grey" {}}SubShader {Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }Cull Off ZWrite OffCGINCLUDE#include "UnityCG.cginc"half4 _Tint;half _Exposure;float _Rotation;float4x4 view;float4 RotateAroundYInDegrees (float4 vertex, float degrees){float alpha = degrees * UNITY_PI / 180.0;float sina, cosa;sincos(alpha, sina, cosa);float2x2 m = float2x2(cosa, -sina, sina, cosa);return float4(mul(m, vertex.xz), vertex.yw).xzyw;}struct appdata_t {float4 vertex : POSITION;float2 texcoord : TEXCOORD0;};struct v2f {float4 vertex : SV_POSITION;float2 texcoord : TEXCOORD0;};v2f vert (appdata_t v){v2f o;// 立方体先转到相机位置,再变换到相机空间(没有根据相机来旋转立方体)v.vertex = mul(view, v.vertex);v.vertex = RotateAroundYInDegrees(v.vertex, _Rotation);o.vertex = mul(UNITY_MATRIX_P, v.vertex);// 设置深度值为1,最大o.vertex.z = o.vertex.w;o.texcoord = v.texcoord;return o;}half4 skybox_simply_frag(v2f i, sampler2D smp){half4 tex = tex2D (smp, i.texcoord);return tex;}ENDCGPass {CGPROGRAM#pragma vertex vert#pragma fragment fragsampler2D _FrontTex;half4 frag (v2f i) : SV_Target { return skybox_simply_frag(i,_FrontTex); }ENDCG }Pass{CGPROGRAM#pragma vertex vert#pragma fragment fragsampler2D _BackTex;half4 frag (v2f i) : SV_Target { return skybox_simply_frag(i,_BackTex); }ENDCG }Pass{CGPROGRAM#pragma vertex vert#pragma fragment fragsampler2D _LeftTex;half4 frag (v2f i) : SV_Target { return skybox_simply_frag(i,_LeftTex); }ENDCG}Pass{CGPROGRAM#pragma vertex vert#pragma fragment fragsampler2D _RightTex;half4 frag (v2f i) : SV_Target { return skybox_simply_frag(i,_RightTex); }ENDCG}Pass{CGPROGRAM#pragma vertex vert#pragma fragment fragsampler2D _UpTex;half4 frag (v2f i) : SV_Target { return skybox_simply_frag(i,_UpTex); }ENDCG}Pass{CGPROGRAM#pragma vertex vert#pragma fragment fragsampler2D _DownTex;half4 frag (v2f i) : SV_Target { return skybox_simply_frag(i,_DownTex); }ENDCG}}}
0 0
- Unity Shader Example 17 (Skybox)
- Cg shader for Unity-specific skybox
- Unity Shader Example 1 : Texture
- Unity Shader Example 4 (扭曲)
- Unity Shader Example 7 (溶解)
- Unity Shader Example 18 (Fresnel)
- Unity Shader Example 19 (fog)
- Unity Shader Example 26 (Texture2DArray)
- Unity Shader Example 27 (Noise)
- Cg shader for skybox
- opengl skybox shader xyww
- Unity Shader Example 5 (地形混合)
- Unity Shader Example 6 (区域变色)
- Unity Shader Example 8 (光照贴图)
- Unity Shader Example 9 (均值模糊)
- Unity Shader Example 10 (高斯模糊)
- Unity Shader Example 11 (模板测试)
- Unity Shader Example 12 (Bloom 高光)
- 内存分配 指针和数组对比
- Activity的 LaunchMode和Flags
- android 自定义View学习总结-继承自ViewGroup
- AJAX学习1.1
- 二叉树两结点的最低共同父结点
- Unity Shader Example 17 (Skybox)
- 防止手机进入休眠状态而导致程序不能正常运行——WakeLock
- Gibbs Sampling\吉布斯采样(一)
- openpyxl - 一些简单用法
- 自定义轮播图SlideView
- rails中获取url信息的一些方法
- 不可变字符串(NSString)与可变字符串(NSMutableString)
- too many connections的一个实践
- Gibbs Sampling\吉布斯采样(二)