// Update is called once per frame void Update() { //MOVEMENT INPUT Vector3 moveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); Vector3 moveVelocity = moveInput.normalized * mvtSpeed; controller.Move(moveVelocity); //LOOK INPUT Ray ray = view.ScreenPointToRay(Input.mousePosition); Plane groundPlane = new Plane(Vector3.up, Vector3.zero); float rayDist; if (groundPlane.Raycast(ray, out rayDist)) { Vector3 intersect = ray.GetPoint(rayDist); //Debug.DrawLine(ray.origin, intersect, Color.red); controller.LookAt(intersect); } //WEAPON INPUT if (Input.GetMouseButton(0)) { canonController.Shoot(); } }
// Update is called once per frame void Update() { imageOrig = webCam.QueryFrame(); if (imageOrig != null) { imageGray = new Mat(); imageHSV = new Mat(); Mat imageAverage = new Mat(); Mat imageMedian = new Mat(); Mat imageGaussian = new Mat(); Mat imageBilateral = new Mat(); imageResult = new Mat(); CvInvoke.Flip(imageOrig, imageOrig, FlipType.Horizontal); CvInvoke.Resize(imageOrig, imageOrig, new Size(imSize, imSize * webCam.Height / webCam.Width)); CvInvoke.CvtColor(imageOrig, imageGray, ColorConversion.Bgr2Gray); CvInvoke.CvtColor(imageOrig, imageHSV, ColorConversion.Bgr2Hsv); // Draw Original Image //CvInvoke.Imshow(imNameOrig, imageOrig); // Draw Original Image CvInvoke.Imshow(imNameColor, imageHSV); Mat filteredImg = new Mat(); //CvInvoke.BilateralFilter( imageHSV, filteredImg, 3, 75, 75 ); //CvInvoke.GaussianBlur( imageHSV, filteredImg, new Size(7,7), 0 ); CvInvoke.MedianBlur(imageHSV, filteredImg, 7); Image <Hsv, System.Byte> rangeImg = filteredImg.ToImage <Hsv, System.Byte>(); // Yellow 40-70; 0-255; 0-255 Hsv bottomHsv = new Hsv(18, 127, 127); // 0-179, 0-255, 0-255 Hsv topHsv = new Hsv(35, 240, 240); if (detectColorBlue) { // BLue 230-180; 0-255; 0-255 bottomHsv = new Hsv(80, 70, 70); // 0-179, 0-255, 0-255 topHsv = new Hsv(120, 250, 250); } //rangeImg.Data[0,0,0]; Mat imagBW = rangeImg.InRange(bottomHsv, topHsv).Mat; int elementSize = 5; Mat structureElement = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(elementSize, elementSize), new Point(-1, -1)); CvInvoke.Erode(imagBW, imagBW, structureElement, new Point(-1, -1), 1, BorderType.Constant, new MCvScalar(0)); CvInvoke.Dilate(imagBW, imagBW, structureElement, new Point(-1, -1), 1, BorderType.Constant, new MCvScalar(0)); // Contours VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); int biggestContourIndex = -1; double biggestContourArea = -1; Mat hierarchy = new Mat(); CvInvoke.FindContours(imagBW, contours, hierarchy, RetrType.List, ChainApproxMethod.ChainApproxNone); if (contours.Size > 0) { biggestContourIndex = 0; biggestContourArea = CvInvoke.ContourArea(contours[biggestContourIndex]); } for (int i = 1; i < contours.Size; i++) { double currentArea = CvInvoke.ContourArea(contours[i]); if (currentArea > biggestContourArea) { biggestContourIndex = i; biggestContourArea = currentArea; } } if (contours.Size > 0) { VectorOfPoint biggestContour = contours[biggestContourIndex]; CvInvoke.DrawContours(imageOrig, contours, biggestContourIndex, new MCvScalar(255, 0, 0), 5); if (previousBiggestContourArea > 0) // Object entering { if (biggestContourArea > previousBiggestContourArea * 1.6) { // Going Foward //Debug.Log( "Front" ); if (canon) { canon.Shoot(); } } else if (biggestContourArea * 1.6 < previousBiggestContourArea) { // Going backward //Debug.Log( "Back" ); } } previousBiggestContourArea = biggestContourArea; //* Centroid MCvMoments moment = CvInvoke.Moments(contours[biggestContourIndex]); int cx = (int)(moment.M10 / moment.M00); int cy = (int)(moment.M01 / moment.M00); imageOrig = drawPoint(imageOrig, cx, cy, 5); if (canon) { canon.setHorizontalPosition(cx / (float)imSize); } //*/ //* Top Point Point top = biggestContour[0]; for (int i = 1; i < biggestContour.Size; i++) { Point p = biggestContour[i]; if (top.Y > p.Y) { top = p; } } if (canon) { canon.setVerticalPosition(((float)imageOrig.SizeOfDimemsion[0] - top.Y) / (float)imageOrig.SizeOfDimemsion[0]); } imageOrig = drawPoint(imageOrig, top.X, top.Y, 5, 255, 0, 0); //*/ } else { // Object leaving previousBiggestContourArea = -1; } CvInvoke.Imshow("BW", imagBW); CvInvoke.Imshow(imNameOrig, imageOrig); // Filtering /*CvInvoke.Blur( imageHSV, imageAverage, new Size(5,5), new Point(-1,-1) ); * CvInvoke.Imshow("Average", imageAverage); * CvInvoke.MedianBlur( imageHSV, imageMedian, 5 ); * CvInvoke.Imshow("Median", imageMedian); * CvInvoke.GaussianBlur( imageHSV, imageGaussian, new Size(5,5), 0 ); * CvInvoke.Imshow("Gaussian", imageGaussian); * CvInvoke.BilateralFilter( imageHSV, imageBilateral, 3, 75, 75 ); * CvInvoke.Imshow("Bilateral", imageBilateral);*/ //CvInvoke.Imshow(imNameResult, imageResult); // Storing writer.Write(imageOrig); } else { webCam = new VideoCapture(imAddress); } if (Input.GetKeyDown(KeyCode.Escape)) { Application.Quit(); } }