Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine;
  5. //RightControllerAnchor
  6. public class QuadCreator1_virkeIkke : MonoBehaviour
  7. {
  8.     public float width = 1;
  9.     public float height = 1;
  10.    
  11.     [SerializeField]
  12.     private GameObject hand;
  13.     public bool findingPoints = false;
  14.  
  15.     private Vector3[] vertices = new Vector3[4];
  16.     private Mesh mesh;
  17.     private int index = 0;
  18.     public Material myMaterial;
  19.  
  20.  
  21.     public GameObject myPrefab; //wand
  22.     private GameObject instantiateObj; //wand refrence
  23.  
  24.     public GameObject markerPrefab; //marker
  25.     public GameObject[] instantiateMarker; //markers refrence
  26.  
  27.  
  28.  
  29.  
  30.     public void Start()
  31.     {
  32.         MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
  33.         //meshRenderer.sharedMaterial = new Material(Shader.Find("Standard"));
  34.         meshRenderer.sharedMaterial = myMaterial;
  35.  
  36.         MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
  37.  
  38.         mesh = new Mesh();
  39.  
  40.         vertices = new Vector3[4]
  41.         {
  42.             new Vector3(0, 0, 0), //BOTTOM LEFT
  43.             new Vector3(width, 0, 0),//BOTTOM RIGHT
  44.  
  45.             new Vector3(0, 0, height), //TOP LEFT
  46.             new Vector3(width, 0, height)//TOP RIGHT
  47.         };
  48.         mesh.vertices = vertices;
  49.  
  50.         int[] tris = new int[6]
  51.         {
  52.             // lower left triangle
  53.             0, 2, 1,
  54.             // upper right triangle
  55.             2, 3, 1
  56.         };
  57.         mesh.triangles = tris;
  58.  
  59.         Vector3[] normals = new Vector3[4]
  60.         {
  61.             -Vector3.forward,
  62.             -Vector3.forward,
  63.             -Vector3.forward,
  64.             -Vector3.forward
  65.         };
  66.         mesh.normals = normals;
  67.  
  68.         Vector2[] uv = new Vector2[4]
  69.         {
  70.             new Vector2(0, 0),
  71.             new Vector2(1, 0),
  72.             new Vector2(0, 1),
  73.             new Vector2(1, 1)
  74.         };
  75.         mesh.uv = uv;
  76.  
  77.         meshFilter.mesh = mesh;
  78.     }
  79.     public void Update()
  80.     {
  81.  
  82.         if (OVRInput.GetDown(OVRInput.Button.Four) && !findingPoints)
  83.         {
  84.             findingPoints = true;
  85.             instantiateObj=(GameObject) Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
  86.          /*   for(int i = 0; i < 4; i++)
  87.             {
  88.                 UpdateVertex(Vector3(0,0,0))
  89.             }
  90.             */
  91.         }
  92.  
  93.  
  94.         if (findingPoints)
  95.         {
  96.             instantiateObj.transform.position = hand.transform.position;
  97.             instantiateObj.transform.rotation = hand.transform.rotation;
  98.             instantiateObj.transform.Rotate(50, 0, 0);
  99.  
  100.  
  101.  
  102.  
  103.             if (OVRInput.GetDown(OVRInput.Button.Three))
  104.             {
  105.                 if (index==0)
  106.                 {
  107.                     PutMarker(instantiateObj.transform.GetChild(0).gameObject.transform.position);
  108.                     //UpdateVertex(instantiateObj.transform.GetChild(0).gameObject.transform.position);
  109.                     //UpdateMesh();
  110.  
  111.                     index=index+1;
  112.                 }
  113.                 if (index >= 4) //stop mode
  114.                 {
  115.                     //index = 0;
  116.                     findingPoints = false;
  117.                     Destroy(instantiateObj);
  118.  
  119.                     for (int i = 0; i < 4; i++)
  120.                     {
  121.                         Destroy(instantiateMarker[i]);
  122.                     }
  123.                 }
  124.             }
  125.  
  126.  
  127.         }  
  128.     }
  129.  
  130.     private void PutMarker(Vector3 markerPos)
  131.     {
  132.         instantiateMarker[index] = Instantiate(markerPrefab, markerPos, Quaternion.identity);
  133.     }
  134.  
  135.     private void UpdateMesh()
  136.     {
  137.         mesh.vertices = vertices;
  138.     }
  139.     private void UpdateVertex(Vector3 newPosition)
  140.     {
  141.         vertices[index] = newPosition;
  142.        // instantiateMarker[index] = Instantiate(markerPrefab, newPosition, Quaternion.identity);
  143.        /*
  144.         index++;
  145.  
  146.         if (index >= 4) //stop mode
  147.         {
  148.             index = 0;
  149.             findingPoints = false;
  150.             Destroy(instantiateObj);
  151.            
  152.             for (int i = 0; i < 4; i++)
  153.             {
  154.                 Destroy(instantiateMarker[i]);
  155.             }
  156.         }*/
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement