返回介绍

RaycastHit.triangleIndex 三角形索引

发布于 2019-12-18 15:38:23 字数 3512 浏览 1102 评论 0 收藏 0

JavaScript => public var triangleIndex: int;
C# => public int triangleIndex;

Description 描述

The index of the triangle that was hit.

碰到的三角形的索引。

Triangle index is only valid if the collider that was hit is a MeshCollider.

三角形索引仅对碰到的碰撞器是MeshCollider有效。

JavaScript:

#pragma strict
// This script draws a debug line around mesh triangles 
// as you move the mouse over them.
var camera: Camera;
function Start() {
	camera = GetComponent.<Camera>();
}
function Update() {
	var hit: RaycastHit;
	if (!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), hit))return ;
	var meshCollider: MeshCollider = hit.collider as MeshCollider;
	if (meshCollider == null || meshCollider.sharedMesh == null)return ;
	var mesh: Mesh = meshCollider.sharedMesh;
	var vertices: Vector3[] = mesh.vertices;
	var triangles: int[] = mesh.triangles;
	var p0: Vector3 = vertices[triangles[hit.triangleIndex * 3 + 0]];
	var p1: Vector3 = vertices[triangles[hit.triangleIndex * 3 + 1]];
	var p2: Vector3 = vertices[triangles[hit.triangleIndex * 3 + 2]];
	var hitTransform: Transform = hit.collider.transform;
	p0 = hitTransform.TransformPoint(p0);
	p1 = hitTransform.TransformPoint(p1);
	p2 = hitTransform.TransformPoint(p2);
	Debug.DrawLine(p0, p1);
	Debug.DrawLine(p1, p2);
	Debug.DrawLine(p2, p0);
}

C#:

// This script draws a debug line around mesh triangles 
// as you move the mouse over them.
using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
	Camera camera;
 
	void Start() {
		camera = GetComponent<Camera>();
	}
 
 
    void Update() {
        RaycastHit hit;
        if (!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit))
            return;
 
        MeshCollider meshCollider = hit.collider as MeshCollider;
        if (meshCollider == null || meshCollider.sharedMesh == null)
            return;
 
        Mesh mesh = meshCollider.sharedMesh;
        Vector3[] vertices = mesh.vertices;
        int[] triangles = mesh.triangles;
        Vector3 p0 = vertices[triangles[hit.triangleIndex * 3 + 0]];
        Vector3 p1 = vertices[triangles[hit.triangleIndex * 3 + 1]];
        Vector3 p2 = vertices[triangles[hit.triangleIndex * 3 + 2]];
        Transform hitTransform = hit.collider.transform;
        p0 = hitTransform.TransformPoint(p0);
        p1 = hitTransform.TransformPoint(p1);
        p2 = hitTransform.TransformPoint(p2);
        Debug.DrawLine(p0, p1);
        Debug.DrawLine(p1, p2);
        Debug.DrawLine(p2, p0);
    }
}

See Also: Physics.Raycast, Physics.Linecast, Physics.RaycastAll.

RaycastHit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文