Quantcast
Channel: Questions in topic: "webcam"
Viewing all articles
Browse latest Browse all 256

can anybody help with script that takes webcam texture pixel information to deform mesh vertices?

$
0
0
i'm trying to use a webcam texture -- get its pixel information- to then assign to mesh vertices so that while the image color/alpha changes, the mesh will deform at the same time. i am using a mac, so things are slightly different, but i have managed to get a mesh to create on play and the webcam to work. i now need to figure out how to get the pixel info and assign it to vertices, and this is where i'm stuck, because i'm not sure how Unity uses textures and shaders. its pretty mysterious to me. anyway, any help would be GREATLY appreciated. here's the code as it is now: using UnityEngine; public class WebCamMeshTest : MonoBehaviour { public bool getDeviceByName = false; public int deviceIndex = 0; public string deviceName = ""; public bool setRequestResolution = false; public int resolutionWidth = 800; public int resolutionHeight = 600; private int resolution = 80 ; public bool setFPS = false; public int requestedFPS = 30; public bool mirrorHorizontal = false; [HideInInspector] public bool running = false; private WebCamTexture wcTexture; private bool foundDevice = false; private Mesh mesh; private int totalPixels; private Vector3[] vertices; private Color[] colors; private Vector3[] normals; private Vector2[] uv; //public Gradient coloring; void Start() { totalPixels = resolution * resolution; //totalPixels = resolution * resolution; mesh = new Mesh(); mesh.name = "Surface Mesh"; GetComponent().mesh = mesh; vertices = new Vector3[(resolution+1) * (resolution+1)]; colors = new Color[vertices.Length]; Vector2[] uv = new Vector2[vertices.Length]; Vector3[] normals = new Vector3[vertices.Length]; int vertexIndex = 0; float stepSize = 1f / resolution; for (int v = 0, z = 0; z <= resolution; z++) { for (int x = 0; x <= resolution; x++, v++) { vertices[v] = new Vector3(x * stepSize - 0.5f, 0f, z * stepSize - 0.5f); colors[v] = renderer.material.mainTexture.GetPixels(); //colors[v] = coloring.Evaluate(sample); normals[v] = Vector3.up; uv[v] = new Vector2(x * stepSize, z * stepSize); } } mesh.vertices = vertices; mesh.colors = colors; mesh.normals = normals; mesh.uv = uv; int[] triangles = new int[resolution * resolution * 6]; for (int t = 0, v = 0, y = 0; y < resolution; y++, v++) { for (int x = 0; x < resolution; x++, v++, t += 6) { triangles[t] = v; triangles[t + 1] = v + resolution + 1; triangles[t + 2] = v + 1; triangles[t + 3] = v + 1; triangles[t + 4] = v + resolution + 1; triangles[t + 5] = v + resolution + 2; } } mesh.triangles = triangles; if(WebCamTexture.devices.Length > 0) { wcTexture = new WebCamTexture(); if(setRequestResolution) { wcTexture.requestedWidth = resolution; wcTexture.requestedHeight = resolution; //wcTexture.requestedWidth = resolutionWidth; //wcTexture.requestedHeight = resolutionHeight; } if(setFPS) { wcTexture.requestedFPS = requestedFPS; } if(getDeviceByName) { foreach (WebCamDevice a in WebCamTexture.devices) { if(a.name == deviceName) { foundDevice = true; } } if(!foundDevice) { Debug.LogWarning("WebCam - Cannot find named device"); } else { wcTexture.deviceName = deviceName; } } else { if(deviceIndex < WebCamTexture.devices.Length) { wcTexture.deviceName = WebCamTexture.devices[deviceIndex].name; deviceName = WebCamTexture.devices[deviceIndex].name; foundDevice = true; } else { Debug.LogWarning("WebCam - Index out of range. Webcams Detected: " + WebCamTexture.devices.Length.ToString()); } } if(foundDevice) { renderer.material.mainTexture = wcTexture; wcTexture.Play(); if(!wcTexture.isPlaying) { Webcam[] wcs = GameObject.FindObjectsOfType(); foreach(Webcam a in wcs) { if(a.running) { if(a.deviceName == deviceName) { renderer.material.mainTexture = a.renderer.material.mainTexture; } } } } else { running = true; } if(mirrorHorizontal) { Vector2 tempScale = renderer.material.mainTextureScale; Vector2 tempOffset = renderer.material.mainTextureOffset; tempOffset.x += tempScale.x; tempScale.x *= -1; renderer.material.mainTextureScale = tempScale; renderer.material.mainTextureOffset = tempOffset; } } } else { Debug.LogWarning("WebCam - No Webcam Device Detected"); } } void OnDestroy() { if(running) { wcTexture.Stop(); } } }

Viewing all articles
Browse latest Browse all 256

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>