RaycastHit.barycentricCoordinate 重心坐标

var barycentricCoordinate : Vector3

Description描述

The barycentric coordinate of the triangle we hit.

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

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

这允许你沿着3个轴插值任何顶点数据。

// 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);
}
最后修改:2011年3月13日 Sunday 13:39

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。