/// <summary> /// Finds the color below scanner item[i, j]. /// </summary> /// <param name="i">The row index.</param> /// <param name="j">The column index.</param> public int FindColor(int i, int j, ref GameObject[,] currScanners, bool isGrid = true) { var scanner = currScanners[i, j]; var scannerRenderer = scanner.GetComponent <Renderer>(); if (ScannerIsAboveTexture(scanner) && hitTex) { var texBounds = keystonedQuad.GetComponent <Renderer>().bounds; var localScanPos = scanner.transform.position - texBounds.min; Color pixel = hitTex.GetPixelBilinear(localScanPos.x / texBounds.size.x, localScanPos.z / texBounds.size.z); // int _locX = Mathf.RoundToInt(hit.textureCoord.x * hitTex.width); // int _locY = Mathf.RoundToInt(hit.textureCoord.y * hitTex.height); // Color pixel = hitTex.GetPixel(_locX, _locY); int currID = colorClassifier.GetClosestColorId(pixel); if (isGrid) { if (_useBuffer) { currID = GetIdAverage(i, j, currID); } // Save colors for 3D visualization if (setup || _isCalibrating) { allColors[i + numOfScannersX * j] = pixel; } } Color minColor; // Display 3D colors & use scanned colors for scanner color if (_isCalibrating && isGrid) { minColor = pixel; } else { minColor = colorClassifier.GetColor(currID); } if (_showDebugLines) { // Could improve by drawing only if sphere locations change Vector3 origin = _colorSpaceParent.transform.position; Debug.DrawLine(origin + new Vector3(pixel.r, pixel.g, pixel.b), origin + new Vector3(sampleColors[currID].r, sampleColors[currID].g, sampleColors[currID].b), pixel, 1, false); } // Paint scanner with the found color if (scannerRenderer.material.color != minColor) { scannerRenderer.material.color = minColor; } return(currID); } else { if (scannerRenderer.material.color != Color.magenta) { scannerRenderer.material.color = Color.magenta; } return(-1); } }
/// <summary> /// Finds the color below scanner item[i, j]. /// </summary> /// <param name="i">The row index.</param> /// <param name="j">The column index.</param> public int FindColor(int i, int j, ref GameObject[,] currScanners, bool isGrid = true) { if (Physics.Raycast(currScanners [i, j].transform.position, Vector3.down, out hit, 6)) { // Get local tex coords w.r.t. triangle if (!hitTex) { Debug.Log("No hit texture"); currScanners [i, j].GetComponent <Renderer> ().material.color = Color.magenta; return(-1); } else { int _locX = Mathf.RoundToInt(hit.textureCoord.x * hitTex.width); int _locY = Mathf.RoundToInt(hit.textureCoord.y * hitTex.height); Color pixel = hitTex.GetPixel(_locX, _locY); int currID = colorClassifier.GetClosestColorId(pixel); if (isGrid) { if (_useBuffer) { currID = GetIdAverage(i, j, currID); } // Save colors for 3D visualization if (setup || _isCalibrating) { allColors [i + numOfScannersX * j] = pixel; } } Color minColor; // Display 3D colors & use scanned colors for scanner color if (_isCalibrating && isGrid) { minColor = pixel; } else { minColor = colorClassifier.GetColor(currID); } if (_showDebugLines) { // Could improve by drawing only if sphere locations change Vector3 origin = _colorSpaceParent.transform.position; Debug.DrawLine(origin + new Vector3(pixel.r, pixel.g, pixel.b), origin + new Vector3(sampleColors [currID].r, sampleColors [currID].g, sampleColors [currID].b), pixel, 1, false); } // Display rays cast at the keystoned quad if (_showRays) { Debug.DrawLine(scannersList [i, j].transform.position, hit.point, pixel, 200, false); Debug.Log(hit.point); } // Paint scanner with the found color currScanners [i, j].GetComponent <Renderer> ().material.color = minColor; return(currID); } } else { currScanners [i, j].GetComponent <Renderer> ().material.color = Color.magenta; //paint scanner with Out of bounds / invalid color return(-1); } }
public int UpdateColor() { RaycastHit hit; hitTex = SingletonTMono <Scanners> .Instance.hitTex; colorClassifier = SingletonTMono <Scanners> .Instance.colorClassifier; if (Physics.Raycast(transform.position, Vector3.down, out hit, 6, 1 << 9)) { // Get local tex coords w.r.t. triangle if (!hitTex) { Debug.Log("No hit texture"); if (needUpdateColor) { GetComponent <Renderer>().material.color = Color.magenta; } return(-1); } else { int _locX = Mathf.RoundToInt(hit.textureCoord.x * hitTex.width); int _locY = Mathf.RoundToInt(hit.textureCoord.y * hitTex.height); Color pixel = hitTex.GetPixel(_locX, _locY); int currID = colorClassifier.GetClosestColorId(pixel); //if (isGrid) //{ // if (_useBuffer) // currID = GetIdAverage(i, j, currID); // // Save colors for 3D visualization // if (setup || _isCalibrating) // allColors[i + numOfScannersX * j] = pixel; //} Color minColor; // Display 3D colors & use scanned colors for scanner color //if (SingletonTMono<Scanners>.Instance._isCalibrating && isGrid) if (SingletonTMono <Scanners> .Instance._isCalibrating) { minColor = pixel; } else { minColor = colorClassifier.GetColor(currID); } if (SingletonTMono <Scanners> .Instance._showDebugLines) { // Could improve by drawing only if sphere locations change Vector3 origin = SingletonTMono <Scanners> .Instance._colorSpaceParent.transform.position; Debug.DrawLine(origin + new Vector3(pixel.r, pixel.g, pixel.b), origin + new Vector3(SingletonTMono <Scanners> .Instance.sampleColors[currID].r, SingletonTMono <Scanners> .Instance.sampleColors[currID].g, SingletonTMono <Scanners> .Instance.sampleColors[currID].b), pixel, 1, false); } // Display rays cast at the keystoned quad if (SingletonTMono <Scanners> .Instance._showRays) { Debug.DrawLine(transform.position, hit.point, pixel, 200, false); Debug.Log(hit.point); } if (needUpdateColor) { GetComponent <Renderer>().material.color = minColor; } // Paint scanner with the found color color = minColor; return(currID); } } else { if (needUpdateColor) { GetComponent <Renderer>().material.color = Color.magenta; //paint scanner with Out of bounds / invalid color } return(-1); } }