csharp 把相机朝向的物体的面向相机面剔除
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 把相机朝向的物体的面向相机面剔除相关的知识,希望对你有一定的参考价值。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HideOneSide : MonoBehaviour
{
public List<Transform> VerticeTransforms;
private Mesh _mesh;
private MeshFilter _meshFilter;
private MeshRenderer _meshRenderer;
public bool EnableHideSide = false;
// Use this for initialization
void Start ()
{
CreateMesh();
}
void CreateMesh()
{
if(gameObject.GetComponent<MeshFilter>()==null)
_meshFilter = gameObject.AddComponent<MeshFilter>();
_mesh = new Mesh();
UpdateMesh();
_mesh.RecalculateNormals();
_meshFilter.mesh = _mesh;
if(gameObject.GetComponent<MeshRenderer>()==null)
_meshRenderer = gameObject.AddComponent<MeshRenderer>();
}
void UpdateMesh()
{
CreateVertices();
CreateTriangles();
}
void Update()
{
UpdateMesh();
if(EnableHideSide)
HideFrontSide();
}
void HideFrontSide()
{
Vector3[] vertices = _mesh.vertices;
Vector3 a = vertices[0];
Vector3 b = vertices[1];
Vector3 c = vertices[2];
Vector3 worldNormal = Vector3.Cross(b - a, c - b);
worldNormal = Camera.main.projectionMatrix*worldNormal;
float angle = Vector3.Angle(worldNormal, Camera.main.transform.forward);
if (Mathf.Abs(angle) < 90)
{
}
else
{
_mesh.triangles = null;
}
}
void CreateVertices()
{
if (VerticeTransforms != null)
{
List<Vector3> vertices = new List<Vector3>();
for (int i = 0; i < VerticeTransforms.Count; i++)
{
vertices.Add(VerticeTransforms[i].transform.localPosition);
}
_mesh.SetVertices(vertices);
}
}
void CreateTriangles()
{
List<int> triangles = new List<int>();
for (int i = 0; i < VerticeTransforms.Count/3; i ++)
{
triangles.Add(3 * i);
triangles.Add(3 * i + 1);
triangles.Add(3 * i + 2);
}
_mesh.SetTriangles(triangles,0);
}
}
以上是关于csharp 把相机朝向的物体的面向相机面剔除的主要内容,如果未能解决你的问题,请参考以下文章
什么是openGL中的顺时针面
OpenGL制作物面相机
aframe动态添加元素位置是错误的
园景人|SketchUp表现技法 汇总
剔除算法总结
Unity3d怎么控制第三人称的朝向