RaycastHit.textureCoord2
Declaration
public Vector2 textureCoord2;Описание
Координаты вторичной ультрафиолетовой текстуры в точке удара.
Это можно использовать для 3D Если коллайдер не является коллайдером с сеткой, Vector2.zero Если сетка не содержит вторичного набора uv, то будет возвращено uv из первичного набора uv. Это свойство доступно вне основного потока.
Примечание: A textureCoord2 требует, чтобы коллайдер был MeshCollider.
using UnityEngine; using UnityEngine.InputSystem;
public class Example : MonoBehaviour { // Attach this script to a camera and it will paint black pixels in 3D // on whatever the user clicks. Make sure the mesh you want to paint // on has a mesh collider attached.
Camera cam;
void Start() { cam = GetComponent<Camera>(); }
void Update() { // Only when we press the mouse if (!Mouse.current.leftButton.isPressed) { return; }
// Only if we hit something, do we continue RaycastHit hit; if (!Physics.Raycast(cam.ScreenPointToRay(Mouse.current.position.ReadValue()), out hit)) { return; }
// Just in case, also make sure the collider also has a renderer // material and texture. Also we should ignore primitive colliders. Renderer rend = hit.transform.GetComponent<Renderer>();
MeshCollider meshCollider = hit.collider as MeshCollider;
if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null) { return; }
// Now draw a pixel where we hit the object Texture2D tex = rend.material.mainTexture as Texture2D; Vector2 pixelUV = hit.textureCoord2; pixelUV.x *= tex.width; pixelUV.y *= tex.height;
tex.SetPixel(Mathf.FloorToInt(pixelUV.x), Mathf.FloorToInt(pixelUV.y), Color.black);
tex.Apply(); } }