返回介绍

RaycastHit.barycentricCoordinate 重心坐标

发布于 2019-12-18 15:38:22 字数 3709 浏览 1152 评论 0 收藏 0

JavaScript => public var barycentricCoordinate: Vector3;
C# => public Vector3 barycentricCoordinate;

Description 描述

The barycentric coordinate of the triangle we hit.

碰到的三角形的重心坐标。

This lets you interpolate any of the vertex data along the 3 axes.

这可以让你插值沿3轴的任意顶点数据。

JavaScript:

	// Attach this script to a camera and it will 
	// draw a debug line pointing outward from the normal 
	function Update () { 
	   // Only if we hit something, do we continue 
	   var hit : RaycastHit; 
	   if (!Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), hit)) 
	      return; 
 
	   // Just in case, also make sure the collider also has a renderer 
	   // material and texture 
	   var meshCollider = hit.collider as MeshCollider; 
	   if (meshCollider == null || meshCollider.sharedMesh == null) 
	      return; 
 
	   var mesh : Mesh = meshCollider.sharedMesh; 
	   var normals = mesh.normals; 
	   var triangles = mesh.triangles; 
 
	   // Extract local space normals of the triangle we hit 
	   var n0 = normals[triangles[hit.triangleIndex * 3 + 0]]; 
	   var n1 = normals[triangles[hit.triangleIndex * 3 + 1]];    
	   var n2 = normals[triangles[hit.triangleIndex * 3 + 2]];    
 
	   // interpolate using the barycentric coordinate of the hitpoint 
	   var baryCenter = hit.barycentricCoordinate; 
 
	   // Use barycentric coordinate to interpolate normal 
	   var interpolatedNormal = n0 * baryCenter.x + n1 * baryCenter.y + n2 * baryCenter.z; 
	   // normalize the interpolated normal 
	   interpolatedNormal =  interpolatedNormal.normalized; 
 
	   // Transform local space normals to world space 
	   var hitTransform : Transform = hit.collider.transform; 
	   interpolatedNormal = hitTransform.TransformDirection(interpolatedNormal); 
 
	   // Display with Debug.DrawLine 
	   Debug.DrawRay(hit.point, interpolatedNormal); 
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Update() {
        RaycastHit hit;
        if (!Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            return;
 
        MeshCollider meshCollider = hit.collider as MeshCollider;
        if (meshCollider == null || meshCollider.sharedMesh == null)
            return;
 
        Mesh mesh = meshCollider.sharedMesh;
        Vector3[] normals = mesh.normals;
        int[] triangles = mesh.triangles;
        Vector3 n0 = normals[triangles[hit.triangleIndex * 3 + 0]];
        Vector3 n1 = normals[triangles[hit.triangleIndex * 3 + 1]];
        Vector3 n2 = normals[triangles[hit.triangleIndex * 3 + 2]];
        Vector3 baryCenter = hit.barycentricCoordinate;
        Vector3 interpolatedNormal = n0 * baryCenter.x + n1 * baryCenter.y + n2 * baryCenter.z;
        interpolatedNormal = interpolatedNormal.normalized;
        Transform hitTransform = hit.collider.transform;
        interpolatedNormal = hitTransform.TransformDirection(interpolatedNormal);
        Debug.DrawRay(hit.point, interpolatedNormal);
    }
}

RaycastHit

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

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

发布评论

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