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

原文地址:https://www.cnblogs.com/langzou/p/6012123.html