In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "Unity how to achieve the effect of writing on the blackboard in VR", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve the effect of writing on the blackboard in VR" this article.
The details are as follows
I. tools
1. The developer uses Unity version 5.6.2.
The physical interaction in 2.VR uses the VRTK plug-in, which integrates better physical interaction functions.
3.HTC Vive
II. Overview
Function: on a whiteboard, draw any desired figure on the whiteboard with a pen of different colors
Because it is only an elementary article, it is simply implemented with two scripts, and there are no functions such as blackboard erasers, nor can two pens draw at the same time. These functions will be written in future upgrades.
Third, knowledge points
In fact, this function is very simple, just simply using two functions in the Unity Texure2D class:
Public void SetPixels32 (int x, int y, int blockWidth, int blockHeight, Color32 [] colors, int miplevel = 0)
The first four parameters are equivalent to a rectangle. X and y are the points in the lower left corner of the rectangle. BlockWidth and blockHeight are the width and height of the rectangle, respectively. The range represented by this rectangle is the location of blockWidth*blockHeight pixels. We might as well call this rectangle a color block.
The size of the parameter colors must be equal to blockWidth*blockHeight, because this method is to assign a value to each pixel in the rectangular range from left to right, from bottom to top, line by line, starting with the coordinates (XMagol y).
That is, colors [0] ~ colors [blockWidth-1] is assigned to pixels with coordinates (xQuery y) ~ (x + blockWidth,y), and so on.
The last parameter, because the picture we are using turns off the Generate Min Maps option, so use the default optional parameter 0
Public void Apply (bool updateMipmaps = true, bool makeNoLongerReadable = false)
When the changes to the picture are complete, you need to call this method so that the changes can be really applied to the picture.
Fourth, scene construction
1. Drawing board
Create a Quad in the scene and set its Scale in the x and y directions to 1.92 and 1.08 (or other sizes) respectively; note that the Quad must use Mesh Collider as the collision body, otherwise the texture coordinates obtained by the rays will be wrong, and set a Tag to Board for it
two。 Pen
Build a pen of the right size, create an empty sub-object, name it SnapPoint, and set the Z direction of SnapPoint to point to the tip of the pen, that is, the position of the handle holding the pen, and make sure that the posture of the pen is equivalent to that of the person holding the pen normally.
3. Other
Create an object to hold the pen so that the pen is in an easier position to hold
What represents the artboard in my scene is the Board object under WhiteBoard.
Fifth, code implementation function
This script is hung on the object that represents the artboard:
Using System.Linq;using UnityEngine;/// artboard / / public class Board: MonoBehaviour {/ / when the brush moves fast, in order not to appear intermittent points, it is necessary to interpolate between the two points. Lerp is the interpolation coefficient [Range (0,1)] public float lerp = 0.05f; / / initialize the background image public Texture2D initailizeTexture; / / the current background image private Texture2D currentTexture / / the position of the brush is mapped to the UV coordinates of the artboard picture private Vector2 paintPos; private bool isDrawing = false;// whether the current brush is on the artboard / / the position of the brush when leaving private int lastPaintX; private int lastPaintY; / / the size of the color block represented by the brush private int painterTipsWidth = 30; private int painterTipsHeight = 15; / / the size of the background picture of the current artboard private int textureWidth; private int textureHeight; / / the color of the brush private Color32 [] painterColor Private Color32 [] currentColor; private Color32 [] originColor; private void Start () {/ / get the size of the original picture Texture2D originTexture = GetComponent (). Material.mainTexture as Texture2D; textureWidth = originTexture.width;//1920 textureHeight = originTexture.height;//1080 / / set the current picture currentTexture = new Texture2D (textureWidth, textureHeight, TextureFormat.RGBA32, false, true); currentTexture.SetPixels32 (originTexture.GetPixels32 ()); currentTexture.Apply (); / / assign a value to the blackboard GetComponent (). Material.mainTexture = currentTexture / / initialize the color of the brush painterColor = Enumerable.Repeat (new Color32 (255,0,0,255), painterTipsWidth * painterTipsHeight). ToArray ();} private void LateUpdate () {/ / calculate a starting point of the current brush, int texPosX = (int) (paintPos.x * (float) textureWidth-(float) (painterTipsWidth / 2)); int texPosY = (int) (paintPos.y * (float) textureHeight-(float) (painterTipsHeight / 2)) If (isDrawing) {/ / change the pixel value of the block where the brush is located currentTexture.SetPixels32 (texPosX, texPosY, painterTipsWidth, painterTipsHeight, painterColor); / / if you move the brush quickly, the phenomenon will occur intermittently, so interpolation if (lastPaintX! = 0 & lastPaintY! = 0) {int lerpCount = (int) (1 / lerp); for (int I = 0; I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.