unity3D HTC VIVE開發-物體高亮功能實現


在VR開發時,有時需要用到物體高亮的功能。這里使用Highlighting System v3.0.1.unitypackage插件實現。
Highlighting System v3.0.1的介紹訪問看這里:
https://forum.unity3d.com/threads/highlighting-system-released.143043/

因為是VR環境下,所以也需要SteamVR Plugin.unitypackage插件。
實現步驟

step1: 導入插件

按照 Assets->Import Package->Custom Package導入就可以了。

step2:新建cube 和 VR環境

新建一個cube,刪除已有的"Main Camera"對象,加入SteamVR下的“Camera Rig”, "Status"和“SteamVR”3個prefabs。

step3:給cube增加高亮腳本

新建腳本SpectrumController.cs 並掛在Cube對象下。

using UnityEngine;
using System.Collections;

public class SpectrumController : HighlighterController
{
	public float speed = 200f;
	
	private readonly int period = 1530;
	private float counter = 0f;

    // 
    new void Update()
	{
        base.Update();
        h.On(Color.blue);
        int val = (int)counter;

		Color col = new Color(GetColorValue(1020, val), GetColorValue(0, val), GetColorValue(510, val), 1f);
		
		h.ConstantOnImmediate(col);

        counter += Time.deltaTime * speed;
		counter %= period;
	}

	// Some color spectrum magic
	float GetColorValue(int offset, int x)
	{
		int o = 0;
		x = (x - offset) % period;
		if (x < 0) { x += period; }
		if (x < 255) { o = x; }
		if (x >= 255 && x < 765) { o = 255; }
		if (x >= 765 && x < 1020) { o = 1020 - x; }
		return (float) o / 255f;
	}
}

step4:給Camera Rig 下Camera(eye)增加腳本

Camera(eye) 增加“Highlighting Mobile”腳本。

step5: bug解決

到這里已經實現了基本功能,但是這個有個bug需要解決:在vive里面,高亮的輪廓在水平軸上是反向的。
各位試過就知道是怎么回事了。

修改:在HighlightingBase.cs文件下,Line 551開始修改

GL.PushMatrix();
GL.LoadOrtho();

mat1.SetPass(pass1);
GL.Begin(GL.QUADS);
// Unity uses a clockwise winding order for determining front-facing polygons. Important for stencil buffer!
GL.TexCoord2(0f, y1); GL.Vertex3(0f, 0f, z);    // Bottom-Left
GL.TexCoord2(0f, y2); GL.Vertex3(0f, 1f, z);    // Top-Left
GL.TexCoord2(1f, y2); GL.Vertex3(1f, 1f, z);    // Top-Right
GL.TexCoord2(1f, y1); GL.Vertex3(1f, 0f, z);    // Bottom-Right
GL.End();

mat2.SetPass(pass2);
GL.Begin(GL.QUADS);
//GL.TexCoord2(0f, 0f); GL.Vertex3(0f, 0f, z);
//GL.TexCoord2(0f, 1f); GL.Vertex3(0f, 1f, z);
//GL.TexCoord2(1f, 1f); GL.Vertex3(1f, 1f, z);
//GL.TexCoord2(1f, 0f); GL.Vertex3(1f, 0f, z);

GL.TexCoord2(0f, 1f); GL.Vertex3(0f, 0f, z);
GL.TexCoord2(0f, 0f); GL.Vertex3(0f, 1f, z);
GL.TexCoord2(1f, 0f); GL.Vertex3(1f, 1f, z);
GL.TexCoord2(1f, 1f); GL.Vertex3(1f, 0f, z);

GL.End();

GL.PopMatrix();

http://www.cnblogs.com/langzou/p/6012123.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM