Use Unity for VR to write on the blackboard (upgrade) (ii)-----Add a blackboard Wipe

Source: Internet
Author: User

The function of the eraser is the same as the brush, but the color of the blackboard eraser is the original color of the artboard, and the pen sets the other color.

So the biggest difference when the handle holding the blackboard wipe and hold the pen when the function of the implementation is not the same; After this function, the erase function of the blackboard wipe will be completed in the subsequent chapters with the brush;

You can see that no matter what angle the blackboard rub to start near the artboard, the final eraser must be parallel with the artboard;

First look at the coordinate system of the artboard:

Look at the coordinate system of the blackboard wiping:

That is, no matter what rotation angle the blackboard rubs (Rotation) near the blackboard, the final result is: The transform.up of the blackboard is pointing to the artboard- Transform.forward direction, and in the process of close, according to the distance, we put this rotation to interpolate the value can be;

Now the question is, when do we start to interpolate? The implementation of the lab is that when the center of the blackboard Sassafras distance from the artboard is 0.5 of the long side of the blackboard rub, it begins to interpolate, when is the end? The simplest is 0, but because my blackboard rub bottom has a 0.02m of black cloth, so my is 0.02m when the end;

So first you need to write a mapping function:

 Public Static classhelper{/// <summary>    ///used to compare two Color32 types is not the same/// </summary>    /// <param Name= "origin" ></param>    /// <param name= "Compare" ></param>    /// <returns></returns>     Public Static BOOLIsEqual ( ThisColor32 origin, Color32 Compare) {        if(ORIGIN.G = = compare.g && ORIGIN.R = =COMPARE.R) {if(Origin.a = = Compare.a && origin.b = =compare.b) {return true; }        }        return false; }    /// <summary>    ///re-map Num's position between Low1 ~ High1 to Low2 ~ high2, and limit the return value to the value between Low2 ~ HIGH2; /// </summary>    /// <param name= "num" ></param>    /// <param name= "Low1" ></param>    /// <param name= "High1" ></param>    /// <param name= "Low2" ></param>    /// <param name= "HIGH2" ></param>    /// <returns></returns>     Public Static floatRemapnumberclamped (floatNumfloatLow1,floatHIGH1,floatLow2,floathigh2) {        returnmathf.clamp (Remapnumber (num, Low1, HIGH1, Low2, HIGH2), Mathf.min (Low2, HIGH2), Mathf.max (Low2, HIGH2)); }    /// <summary>    ///The value represented by num between Low1 ~ high1, mapped to the value represented by Low2 ~ HIGH2/// </summary>    /// <param name= "num" ></param>    /// <param name= "Low1" ></param>    /// <param name= "High1" ></param>    /// <param name= "Low2" ></param>    /// <param name= "HIGH2" ></param>    /// <returns></returns>     Public Static floatRemapnumber (floatNumfloatLow1,floatHIGH1,floatLow2,floathigh2) {        returnLow2 + (NUM-LOW1) * (HIGH2-LOW2)/(HIGH1-Low1); }}

When we limit num to 0, we get the interpolation coefficients, and when we start from 0.096, we interpolate from 0.02, the blackboard Sassafras distance from the artboard is 0.047, and after remapping, the result is 0.5;

Now you can write the grab Attach mechanism of the blackboard Sassafras:

In the upgrade (a) has completed the painter grab attach mechanism, as long as the direct rewrite of its processfixedupdate function can be;

usingUnityengine; Public classerasergrabattach:paintergrabattach{ Public Override voidprocessfixedupdate () {if(Grabbedobject)//only when the object is caught, Grabbedobject will not{grabbedObject.transform.rotation= ControllerAttachPoint.transform.rotation *Quaternion.euler (grabbedSnapHandle.transform.localEulerAngles); GrabbedObject.transform.position= ControllerAttachPoint.transform.position-(GrabbedSnapHandle.transform.position-grabbedObject.transform.position); floatDistance = board. Getdistancefromboardplane (transform.position);//blackboard Sassafras distance from the artboard            if(Distance >-0.096f)//when the blackboard Sassafras is close enough to the artboard            {                floatPercentofdistance = helper.remapnumberclamped (distance,-0.096f, -0.02f, 0f, 1f);//after mapping, the interpolation coefficients are obtainedquaternion q = quaternion.fromtorotation (grabbedObject.transform.up,-board.transform.forward);//The final goal is: The blackboard rub the transform.up pointing-transform.forwardQ *= grabbedObject.transform.rotation;//the rotation of the blackboard Sassafras when it achieves its final purpose.GrabbedObject.transform.rotation = Quaternion.slerp (grabbedObject.transform.rotation, Q, percentofdistance);//the rotation of the current blackboard Sassafras is obtained by interpolating the values .                if(Distance >0.01f)//if the blackboard Sassafras penetrates the artboard, it needs to be rectified.{Vector3 pos=board.                    Projectpointonboardplane (grabbedObject.transform.position); GrabbedObject.transform.position= Pos-board.transform.forward *0.01f; }            }                             }    }  }

Actually can also use quaternion.lookrotation but this function limit is bigger than quaternion.fromtorotation, the effect is not the latter good;

In the next article, we will refine all the deficiencies in the primary article;

Use Unity for VR to write on the blackboard (upgrade) (ii)-----Add a blackboard Wipe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.