Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

Script not responding to tap events

edited March 2017 in Questions And Answers

So I have a script that's meant to plant flowers when the user gazes and clicks on an area in the scene. But for some reason, it only works when I use my computers mouse and not the Hololens AirTap on the Xbox Controller. I also tried the Inputtest script to see what was going on, and none of the airtap commands that I sent were registering.

This is the code that is responsible for the events :

using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.Rendering;

public class PFGarden : MonoBehaviour {

    [SerializeField] List<GameObject> prefabs;

    const string SHADER_PATH = "Hidden/Internal-Colored";

    Material lineMaterial = null;
    MeshCollider col = null;
    Vector3[] vertices;
    int[] triangles;

    bool hit;
    Vector3 point;
    Vector3 normal;
    Quaternion rotation;

    void Update () {
        //var mouse = Input.mousePosition;
        //var ray = Camera.main.ScreenPointToRay(mouse);

        var headPosition = Camera.main.transform.position;
        var gazeDirection = Camera.main.transform.forward;

        RaycastHit info;
        hit = Physics.Raycast(headPosition,gazeDirection, out info);
        if(hit) {
            point = info.point;
            var t = info.triangleIndex * 3;
            var a = triangles[t];
            var b = triangles[t + 1];
            var c = triangles[t + 2];
            var va = vertices[a];
            var vb = vertices[b];
            var vc = vertices[c];

            Quaternion toQuat = Camera.main.transform.localRotation;
            toQuat.x = 0;
            toQuat.z = 0;
            //normal = transform.TransformDirection(Vector3.Cross(vb - va, vc - va));
            rotation = toQuat;
        }

        if(Input.GetMouseButtonUp(0) && hit) {
            var go = Instantiate(prefabs[Random.Range(0, prefabs.Count)]) as GameObject;
            go.transform.position = point;
            go.transform.localScale = Vector3.one * Random.Range(0.4f, 0.5f);
            go.transform.localRotation = Quaternion.LookRotation(Vector3.forward); // * Quaternion.AngleAxis(Random.Range(0f, 360f), Vector3.up);
        }

    }

    const int resolution = 16;
    const float pi2 = Mathf.PI * 2f;
    const float radius = 0.5f;
    Color color = new Color(0.6f, 0.75f, 1f);

    void OnRenderObject () {
        if(!hit) return;

        CheckInit();

        lineMaterial.SetPass(0);
        lineMaterial.SetInt("_ZTest", (int)CompareFunction.Always);

        GL.PushMatrix();
        GL.Begin(GL.LINES);
        GL.Color(color);

        for(int i = 0; i < resolution; i++) {
            var cur = (float)i / resolution * pi2;
            var next = (float)(i + 1) / resolution * pi2;
            var p1 = rotation * new Vector3(Mathf.Cos(cur), Mathf.Sin(cur), 0f);
            var p2 = rotation * new Vector3(Mathf.Cos(next), Mathf.Sin(next), 0f);
            GL.Vertex(point + p1 * radius);
            GL.Vertex(point + p2 * radius);
        }

        GL.End();
        GL.PopMatrix();
    }

    void OnEnable () {
        //col = GetComponent<MeshCollider>();
        var mesh = GetComponent<MeshFilter>().sharedMesh;
        vertices = mesh.vertices;
        triangles = mesh.triangles;
    }

    void CheckInit () {
        if(lineMaterial == null) {
            Shader shader = Shader.Find(SHADER_PATH);
            if (shader == null) return;
            lineMaterial = new Material(shader);
            lineMaterial.hideFlags = HideFlags.HideAndDontSave;
        }
    }

}

Best Answer

Answers

Sign In or Register to comment.