// Update is called once per frame void Update() { int numPoints = KinectPCL.instance.numPoints; if (mesh.vertexCount != numPoints) { rebuildMesh(KinectPCL.instance.pointsWidth, KinectPCL.instance.pointsHeight); if (mesh.vertexCount == 0) { return; } colors = new Color[mesh.vertexCount]; vertices = new Vector3[mesh.vertexCount]; mat.SetInt("_ResX", KinectPCL.instance.pointsWidth); mat.SetInt("_ResY", KinectPCL.instance.pointsHeight); } //Debug.Log(mesh.vertexCount + " / " + numPoints); PCLPoint[] points = KinectPCL.instance.points; bool[] isBody = KinectPCL.instance.bodyMask; float lerpFactor = smoothFactor * Time.deltaTime; for (int i = 0; i < mesh.vertexCount; i++) { PCLPoint pp = points[i]; if (!pp.isValid) { continue; } Color targetColor = isBody[i] ? bodyColor : bgColor; Vector3 targetPoint = pp.position + offset; if (doSmooth) { colors[i] = Color.Lerp(mesh.colors[i], targetColor, lerpFactor); vertices[i] = Vector3.Lerp(vertices[i], targetPoint, lerpFactor); } else { colors[i] = targetColor; vertices[i] = targetPoint; } } mesh.vertices = vertices; mesh.colors = colors; //mesh.RecalculateBounds(); mesh.RecalculateNormals(); }
// Update is called once per frame public override void Update() { //transform.localPosition = pos; //transform.localRotation = Quaternion.Euler(rot); if (Input.GetKeyDown(KeyCode.G)) { regenerateRandom = !regenerateRandom; } if (Input.GetKeyDown(KeyCode.H)) { regenerateRandom = true; } if (Input.GetKeyUp(KeyCode.H)) { regenerateRandom = false; } if (lastDSXOffset != downSampleRandomXOffset || lastDSYOffset != downSampleRandomYOffset || regenerateRandom) { generateOffsetRandoms(); lastDSXOffset = downSampleRandomXOffset; lastDSYOffset = downSampleRandomYOffset; } animateOffsetRandoms(); if (lastDownSample != downSample) { updateDownSample(); lastDownSample = downSample; return; } ushort[] depthMap = manager.GetRawDepthMap(); bodyTree = new PointOctree <PCLPoint>(10, bodyCenter, .01f); //take previous frame roiTree = new PointOctree <PCLPoint>(10, Vector3.zero, .01f); //take previous frame roiPoints.Clear(); bodyPoints.Clear(); bodyCenter = new Vector3(); for (int ix = 0; ix < pointsWidth; ix++) { for (int iy = 0; iy < pointsHeight; iy++) { int dsIndex = iy * pointsWidth + ix; Vector2 drIndex = dsOffsetRandomsAnimated[dsIndex]; int dIndexX = (int)(drIndex.x * DEPTH_WIDTH); int dIndexY = (int)(drIndex.y * DEPTH_HEIGHT); int index = dIndexY * DEPTH_WIDTH + dIndexX; //Debug.DrawRay(new Vector3(dsOffsetRandomsAnimated[dsIndex].x, dsOffsetRandomsAnimated[dsIndex].y) * 10, Vector3.forward * .5f,Color.red) ; if (index >= depthMap.Length) { continue; } ushort um = (ushort)(depthMap[index] & 7); if (um == 0) { um = 255; } else { um = (byte)(um % 4); } ushort d = (ushort)(depthMap[index] >> 3); bool isValid = d > 0; Vector3 p; if (isValid) { float z_metric = d * 0.001f; float tx = z_metric * ((dIndexX - CENTRALPOINT_X) * FOCAL_X_INV); float ty = z_metric * ((dIndexY - CENTRALPOINT_Y) * FOCAL_Y_INV); p = new Vector3(tx, -ty, z_metric); } else { p = Vector3.zero; } bool isInROI = true; if (!isValid || p.z < minDepth || p.z > maxDepth || p.x < leftLimit || p.x > rightLimit || p.y < bottomLimit || p.y > topLimit) { isInROI = false; } Vector3 tPoint = transform.TransformPoint(p); int tIndex = iy * pointsWidth + ix; bodyMask[tIndex] = um != 255; bool isBody = bodyMask[tIndex]; if (isBody) { if (bodyRandomProba < 1) { isBody = Random.value <= bodyRandomProba; } } PCLPoint pp = new PCLPoint(tPoint, isBody, isValid, isInROI, manager.usersMapColors[index]); points[tIndex] = pp; if (isValid) { roiTree.Add(pp, tPoint); roiPoints.Add(pp); if (isBody) { bodyPoints.Add(pp); bodyTree.Add(pp, tPoint); bodyCenter += tPoint; } } if (debug && isValid) { if (isBody || !debugBodyOnly) { Color c = isInROI ? (isBody ? Color.yellow : Color.white) : Color.gray; Debug.DrawRay(tPoint, Vector3.forward * .1f, c); } } } } if (bodyPoints.Count > 0) { bodyCenter /= bodyPoints.Count; } }
// Update is called once per frame void Update() { //TMP if (Input.GetKey(KeyCode.G)) { generateOffsetRandoms(); } if (lastDownSample != downSample) { updateDownSample(); lastDownSample = downSample; return; } CameraSpacePoint[] realWorldMap = multiSourceManager.GetRealWorldData(); byte[] bodyIndexMap = multiSourceManager.GetBodyIndexData(); //Texture2D tex = multiSourceManager.GetColorTexture(); bodyTree = new PointOctree <PCLPoint>(10, bodyCenter, .01f); //take previous frame bodyPoints.Clear(); bodyCenter = new Vector3(); for (int ix = 0; ix < pointsWidth; ix++) { for (int iy = 0; iy < pointsHeight; iy++) { int dsIndex = iy * pointsWidth + ix; Vector2 rIndex = dsOffsetRandoms[dsIndex]; int index = Mathf.RoundToInt(rIndex.x * multiSourceManager.DepthHeight * multiSourceManager.DepthWidth) + Mathf.RoundToInt(rIndex.y * multiSourceManager.DepthWidth); //Vector3 dv = new Vector3(rIndex.x * 10, rIndex.y * 10); //Debug.DrawLine(dv, dv + Vector3.forward * .2f, Color.red); if (index >= realWorldMap.Length) { continue; } CameraSpacePoint csp = realWorldMap[index]; Vector3 p = new Vector3(csp.X, csp.Y, csp.Z); Vector3 tPoint = transform.TransformPoint(p); int tIndex = iy * pointsWidth + ix; bodyMask[tIndex] = bodyIndexMap[index] != 255; bool isBody = bodyMask[tIndex]; bool isValid = true; if (isBody) { if (bodyRandomProba < 1) { isBody = Random.value <= bodyRandomProba; } } if (float.IsNaN(tPoint.x) || float.IsInfinity(tPoint.x)) { isValid = false; tPoint = Vector3.zero; } PCLPoint pp = new PCLPoint(tPoint, isBody, isValid, true, isBody ? Color.yellow : Color.white); points[tIndex] = pp; if (isBody && isValid) { bodyPoints.Add(pp); bodyTree.Add(pp, tPoint); bodyCenter += tPoint; } if (debug && isValid) { if (isBody || !debugBodyOnly) { Color c = isBody ? Color.yellow : Color.white; Debug.DrawLine(tPoint, tPoint + Vector3.forward * .05f, c); } } } } if (bodyPoints.Count > 0) { bodyCenter /= bodyPoints.Count; } }
// Update is called once per frame void Update() { int numPoints = KinectPCL.instance.numPoints; PCLPoint[] points = KinectPCL.instance.points; if (numPoints != particles.Length) { particles = new ParticleSystem.Particle[numPoints]; shuffleIndices = new int[numPoints]; RandomUnique(shuffleIndices, numPoints); } int numParticles = ps.GetParticles(particles); if (numPoints == 0) { return; } if (numPoints > numParticles) { ParticleSystem.EmitParams parms = new ParticleSystem.EmitParams(); parms.position = Vector3.zero; parms.startSize = 0; // parms.startLifetime = 1; ps.Emit(parms, numPoints - numParticles); numParticles = ps.GetParticles(particles); } for (int i = 0; i < numParticles; i++) { int index = shuffleIndices[i]; ParticleSystem.Particle p = particles[index]; PCLPoint pp = points[i]; if (pp == null) { Debug.Log("PP Null !"); continue; } if (pp.isValid) { continue; } Vector3 point = transform.TransformPoint(pp.position); if (point.magnitude == 0) { continue; } //float age = p.startLifetime-p.lifetime; bool pointIsValid = !float.IsNaN(point.x); Color targetColor = pp.isBody ? bodyColor : bgColor; float targetSize = pp.isBody ? bodySize : bgSize; if (pointIsValid) { if (doSmooth) { float lerpFactor = smoothFactor * Time.deltaTime; p.position = Vector3.Lerp(p.position, point, lerpFactor); p.startColor = Color.Lerp(p.startColor, targetColor, lerpFactor); p.startSize = p.startSize + (targetSize - p.startSize) * lerpFactor; } else { p.position = point; p.startColor = targetColor; p.startSize = targetSize; } } particles[index] = p; } ps.SetParticles(particles, numParticles); }