float getAngle(int t, Camera camera) { Mesh m = getMesh(); Vector3 a = worldVertices[m.triangles[t * 3 + 0]]; Vector3 b = worldVertices[m.triangles[t * 3 + 1]]; Vector3 c = worldVertices[m.triangles[t * 3 + 2]]; Plane plane = new Plane(a, b, c); Vector3 norm = plane.normal; Vector3 dirProjector = camera.transform.forward; float angle = Vector3.Angle(norm, -dirProjector); if (debug) { Debug.Log("normal:" + norm); Math3DUtils.CreateSphere(a, Color.gray); Debug.DrawRay(a, norm, Color.white, 100); Debug.DrawRay(a, -dirProjector, Color.black, 100); Debug.Log("dir:" + this.transform.forward); Debug.Log("Angle:" + angle); // mod 90 } return(angle); }
// Draw a corner if the 3 spawnedWalls intersect at a single point private void DrawOneCorner(GameObject s0, GameObject s1, GameObject s2) { Vector3 point; bool success = Math3DUtils.planesIntersectAtSinglePoint(s0, s1, s2, out point); if (success) { GameObject go = Math3DUtils.CreateSphere(point, Color.yellow, 0.1f); ui3dGOs.Add(go); go.name = "Corner"; GameObject line0 = DrawCornerLine(point, s0, s1, s2); GameObject line1 = DrawCornerLine(point, s1, s2, s0); GameObject line2 = DrawCornerLine(point, s2, s0, s1); if (line0) { line0.transform.parent = go.transform; } if (line1) { line1.transform.parent = go.transform; } if (line2) { line2.transform.parent = go.transform; } AddPointToWall(s0, point); AddPointToWall(s1, point); AddPointToWall(s2, point); } }
public string fn = null; //dir + image name eg "2020-12-24_143059/2020-12-24_143059....jpg" // Draw frame and put it 1m far from camera void Start() { this.name = "Projector " + fn; //load texture if (!String.IsNullOrEmpty(fn)) //otherwise display already loaded texture projectcube.jpg { string path = UnityEngine.Application.persistentDataPath + "/" + fn; if (!System.IO.File.Exists(path)) { Debug.LogError("file not found: " + path); } byte[] fileData = System.IO.File.ReadAllBytes(path); Texture2D tex = new Texture2D(1, 1); tex.LoadImage(fileData); //load also width/height plane.GetComponent <Renderer>().material.mainTexture = tex; //project texture this.GetComponent <Projector>().material = new Material(Shader.Find("Projector/Multiply")); Material thisProjectorMat = this.GetComponent <Projector>().material; thisProjectorMat.SetTexture("_ShadowTex", tex); thisProjectorMat.SetTexture("_FalloffTex", tex); this.GetComponent <Projector>().material = thisProjectorMat; w = tex.width; h = tex.height; } else { w = 4618; h = 3464; } this.GetComponent <Camera>().aspect = this.GetComponent <Projector>().aspectRatio = (float)w / h; this.GetComponent <Camera>().fieldOfView = this.GetComponent <Projector>().fieldOfView = this.vfov; float hfov = getHorizontalFov(); Debug.Log("vfov:" + this.vfov + " hfov:" + hfov + " w:" + w + " h:" + h + " fn:" + fn); var halfw = getHalfWidth(); var halfh = getHalfHeight(); Vector3 centerplane = this.transform.position + this.transform.forward * far; //an alternative is to use go.transform.Translate plane.transform.position = centerplane; plane.transform.localScale = new Vector3(halfw * 2f, halfh * 2f, 1f); Vector3 ne = ProjectOnPlaneViewport(new Vector2(1, 1)); Vector3 se = ProjectOnPlaneViewport(new Vector2(1, 0)); Vector3 sw = ProjectOnPlaneViewport(new Vector2(0, 0)); Vector3 nw = ProjectOnPlaneViewport(new Vector2(0, 1)); Debug.DrawLine(this.transform.position, centerplane, Color.blue, 99f); Debug.DrawLine(this.transform.position, ne, Color.white, 99f); Debug.DrawLine(this.transform.position, se, Color.white, 99f); Debug.DrawLine(this.transform.position, sw, Color.white, 99f); Debug.DrawLine(this.transform.position, nw, Color.white, 99f); Math3DUtils.CreateSphere(ne, Color.red, 0.01f).transform.parent = this.transform; Math3DUtils.CreateSphere(se, Color.red, 0.01f).transform.parent = this.transform; Math3DUtils.CreateSphere(sw, Color.red, 0.01f).transform.parent = this.transform; Math3DUtils.CreateSphere(nw, Color.red, 0.01f).transform.parent = this.transform; //https://forum.unity.com/threads/solved-image-projection-shader.254196/ }