Unity Shader Example 17 (Skybox)

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 Off
	
	CGINCLUDE
	#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;
	}
	ENDCG
	
	Pass {
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		sampler2D _FrontTex;
		half4 frag (v2f i) : SV_Target { return skybox_simply_frag(i,_FrontTex); }
		ENDCG 
	}
	Pass{
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		sampler2D _BackTex;
		half4 frag (v2f i) : SV_Target { return skybox_simply_frag(i,_BackTex); }
		ENDCG 
	}
	Pass{
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		sampler2D _LeftTex;
		half4 frag (v2f i) : SV_Target { return skybox_simply_frag(i,_LeftTex); }
		ENDCG
	}
	Pass{
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		sampler2D _RightTex;
		half4 frag (v2f i) : SV_Target { return skybox_simply_frag(i,_RightTex); }
		ENDCG
	}	
	Pass{
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		sampler2D _UpTex;
		half4 frag (v2f i) : SV_Target { return skybox_simply_frag(i,_UpTex); }
		ENDCG
	}	
	Pass{
		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		sampler2D _DownTex;
		half4 frag (v2f i) : SV_Target { return skybox_simply_frag(i,_DownTex); }
		ENDCG
	}
}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值