效果展示
1.首先先下载一个模型剖切插件Cross-Section插件 没有的下方链接自取
插件下载
2.下载之后导入到项目 导入之后的样子如下图
因项目需求需要剖切模型,要使用滑动条进行剖切,插件中有很多demo,其中就有使用滑动条的,可是只能进行单独一个面的剖切,换面操作模型会恢复原状,需求是可以同时操作三个面剖切,模型不恢复原状
最后只能在插件里面修改
修改了CappedSectionBox这个脚本
修改结果如下 代码很长就折叠了
//The purpose of this script is to manipulate the scale and position of the capped section box gizmo object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace WorldSpaceTransitions
{
public class CappedSectionBox : MonoBehaviour, ISizedSection
{
//public LayerMask layer = 13;
//[Space(10)]
public Collider xAxis;
public Collider xAxisNeg;
[Space(10)]
public Collider yAxis;
public Collider yAxisNeg;
[Space(10)]
public Collider zAxis;
public Collider zAxisNeg;
private enum GizmoAxis { X, Y, Z, Xneg, Yneg, Zneg, XYRotate, XZRotate, YZRotate, none };
private GizmoAxis selectedAxis;
private RaycastHit hit;
private Ray ray, ray1;
private Plane dragplane;
private float rayDistance, newRotY, rayDistancePrev, distance;
private Vector3 lookCamera, startDrag, startPos, startDragRot, lookHitPoint, startScale;
private bool dragging;
#region 自己加的改动 改成滑动条剖切
/// <summary>
/// 发射射线的物体 世界坐标的x轴 y轴 z轴 方向 各放一个位置作为起点
/// </summary>
public GameObject rayobj_X, rayobj_Y, rayobj_Z;
public Slider sliderX,sliderY,sliderZ;
// Use this for initialization
void Start()
{
//滑动条的数值如果不是很准确 就把 maxValue minValue调试到合适的值就可以了
sliderX.maxValue = xAxis.transform.position.x;
sliderX.minValue = -(xAxis.transform.position.x);
sliderX.value = sliderX.maxValue;// + this.transform.position.x;
sliderY.maxValue = yAxis.transform.position.y;
sliderY.minValue = -(yAxis.transform.position.y)/2;
sliderY.value = sliderY.maxValue;// + this.transform.position.y;
//不准确自己手动调吧 其他轴不准确的也自己调一下就行了
sliderZ.maxValue = zAxis.transform.position.z;
sliderZ.minValue = -zAxis.transform.position.z;
sliderZ.value = sliderZ.maxValue;
}
public void 滑动条按下(int aa)
{
if (aa==0)
{
dragplane = new Plane();
RaycastHit hit;
if (Physics.Raycast(rayobj_X.transform.position ,xAxis .transform.position , out hit, 1000f))
{
selectedAxis = GizmoAxis.X;
dragplane.SetNormalAndPosition(transform.right, transform.position);
}
distance = hit.distance;
startDrag = new Vector3(xAxis.transform.position.x, xAxis.transform.position.y, distance);
startPos = transform.position;
startScale = transform.localScale;
dragging = true;
}
else if (aa==1)
{
dragplane = new Plane();
RaycastHit hit;
if (Physics.Raycast(rayobj_Y.transform.position, yAxis.transform.position, out hit, 1000f))
{
selectedAxis = GizmoAxis.Y;
dragplane.SetNormalAndPosition(transform.up, transform.position);
}
distance = hit.distance;
startDrag = new Vector3(yAxis.transform.position.x, yAxis.transform.position.y, distance);
startPos = transform.position;
startScale = transform.localScale;
dragging = true;
}
else if (aa==2)
{
dragplane = new Plane();
RaycastHit hit;
if (Physics.Raycast(rayobj_Z.transform.position, zAxis.transform.position, out hit, 1000f))
{
selectedAxis = GizmoAxis.Z;
dragplane.SetNormalAndPosition(transform.forward, transform.position);
}
Debug.DrawLine(rayobj_Z.transform.position, zAxis.transform.position, Color.red,10f);
distance = hit.distance;
startDrag = new Vector3(zAxis.transform.position.x, zAxis.transform.position.y, zAxis.transform.position.z);
startPos = transform.position;
startScale = transform.localScale;
dragging = true;
}
}
public void 滑动条拖动(int aa)
{
if (aa == 0)
{
if (dragging)
{
float lsx = startScale.x;
float lsy = startScale.y;
float lsz = startScale.z;
Vector3 onDrag = new Vector3(sliderX.value, xAxis.transform.position.y, distance);
Vector3 translation = onDrag - startDrag;
Vector3 projectedTranslation = Vector3.zero;
projectedTranslation = Vector3.Project(translation, transform.right);
transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
lsx += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.right));
transform.localScale = new Vector3(Mathf.Clamp(lsx, 0.01f, Mathf.Infinity), Mathf.Clamp(lsy, 0.01f, Mathf.Infinity), Mathf.Clamp(lsz, 0.01f, Mathf.Infinity));
}
}
else if (aa==1)
{
if (dragging)
{
float lsx = startScale.x;
float lsy = startScale.y;
float lsz = startScale.z;
Vector3 onDrag = new Vector3(yAxis.transform.position.x , sliderY.value, distance);
Vector3 translation = onDrag - startDrag;
Vector3 projectedTranslation = Vector3.zero;
projectedTranslation = Vector3.Project(translation, transform.up);
transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
lsy += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.up));
transform.localScale = new Vector3(Mathf.Clamp(lsx, 0.01f, Mathf.Infinity), Mathf.Clamp(lsy, 0.01f, Mathf.Infinity), Mathf.Clamp(lsz, 0.01f, Mathf.Infinity));
}
}
else if (aa==2)
{
if (dragging)
{
float lsx = startScale.x;
float lsy = startScale.y;
float lsz = startScale.z;
Vector3 onDrag = new Vector3(zAxis.transform.position.x, zAxis.transform.position.y, (distance+sliderZ.value));
Vector3 translation = onDrag - startDrag;
Vector3 projectedTranslation = Vector3.zero;
projectedTranslation = Vector3.Project(translation, transform.forward);
transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
lsz += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.forward));
transform.localScale = new Vector3(Mathf.Clamp(lsx, 0.01f, Mathf.Infinity), Mathf.Clamp(lsy, 0.01f, Mathf.Infinity), Mathf.Clamp(lsz, 0.01f, Mathf.Infinity));
}
}
}
public void 滑动条停止()
{
dragging = false;
}
#endregion
// Update is called once per frame
void Update()
{
//if (Input.GetMouseButtonDown(0))
//{
// ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// dragplane = new Plane();
// RaycastHit hit;
// if (xAxis.Raycast(ray, out hit, 1000f))
// {
// selectedAxis = GizmoAxis.X;
// dragplane.SetNormalAndPosition(transform.up, transform.position);
// }
// else if (xAxisNeg.Raycast(ray, out hit, 1000f))
// {
// selectedAxis = GizmoAxis.Xneg;
// dragplane.SetNormalAndPosition(-transform.up, transform.position);
// }
// else if (yAxis.Raycast(ray, out hit, 1000f))
// {
// selectedAxis = GizmoAxis.Y;
// dragplane.SetNormalAndPosition(transform.forward, transform.position);
// }
// else if (yAxisNeg.Raycast(ray, out hit, 1000f))
// {
// selectedAxis = GizmoAxis.Yneg;
// dragplane.SetNormalAndPosition(-transform.forward, transform.position);
// }
// else if (zAxis.Raycast(ray, out hit, 1000f))
// {
// selectedAxis = GizmoAxis.Z;
// dragplane.SetNormalAndPosition(transform.up, transform.position);
// }
// else if (zAxisNeg.Raycast(ray, out hit, 1000f))
// {
// selectedAxis = GizmoAxis.Zneg;
// dragplane.SetNormalAndPosition(-transform.up, transform.position);
// }
// else
// {
// //Debug.Log(hit.collider.name);
// return;
// }
// distance = hit.distance;
// startDrag = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance));
// startPos = transform.position;
// startScale = transform.localScale;
// dragging = true;
//}
//if (dragging)
//{
// ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Vector3 onDrag = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance));
// Vector3 translation = onDrag - startDrag;
// Vector3 projectedTranslation = Vector3.zero;
// if (dragging)
// {
// float lsx = startScale.x;
// float lsy = startScale.y;
// float lsz = startScale.z;
// switch (selectedAxis)
// {
// case GizmoAxis.X:
// {
// projectedTranslation = Vector3.Project(translation, transform.right);
// transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
// lsx += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.right));
// break;
// }
// case GizmoAxis.Xneg:
// {
// projectedTranslation = Vector3.Project(translation, -transform.right);
// transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
// lsx += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, -transform.right));
// break;
// }
// case GizmoAxis.Y:
// {
// projectedTranslation = Vector3.Project(translation, transform.up);
// transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
// lsy += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.up));
// break;
// }
// case GizmoAxis.Yneg:
// {
// projectedTranslation = Vector3.Project(translation, -transform.up);
// transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
// lsy += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, -transform.up));
// break;
// }
// case GizmoAxis.Z:
// {
// projectedTranslation = Vector3.Project(translation, transform.forward);
// transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
// lsz += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.forward));
// break;
// }
// case GizmoAxis.Zneg:
// {
// projectedTranslation = Vector3.Project(translation, -transform.forward);
// transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
// lsz += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, -transform.forward));
// break;
// }
// }
// transform.localScale = new Vector3(Mathf.Clamp(lsx, 0.01f, Mathf.Infinity), Mathf.Clamp(lsy, 0.01f, Mathf.Infinity), Mathf.Clamp(lsz, 0.01f, Mathf.Infinity));
// //foreach (UVScaler uvs in gameObject.GetComponentsInChildren<UVScaler>()) uvs.SetUV();
// }
// if (Input.GetMouseButtonUp(0))
// {
// dragging = false;
// }
//}
}
public void HideCage(bool val)
{
foreach (PlaneHover ph in gameObject.GetComponentsInChildren<PlaneHover>()) ph.GetComponent<Renderer>().enabled = val;
}
public void HideCaps(bool val)
{
foreach (Transform quad in gameObject.transform) if (quad.name.Contains("hatch")) quad.GetComponent<Renderer>().enabled = val;
}
public void Size(Bounds b, GameObject g, BoundsOrientation orientation)
{
//Debug.Log(b.ToString());
float scale = 1f;
Vector3 clearance = 0.01f * Vector3.one;
transform.localScale = Vector3.one;
transform.localScale = scale * b.size + clearance;
transform.position = b.center;
}
}
}
CappedSectionBox
修改完之后就是效果展示的样子了
项目Hierarchy截图 用的就是插件里面带的demo
最后再把插件修改过后的资源包发一下吧,以免其他小伙伴出错
修改之后的插件资源包
亲测2021.3.4可以打包
二. 1.之后又更改了既可以滑动条模型剖切,也可以鼠标点击模型进行剖切
只改了一个脚本如下 复制替换即可
//The purpose of this script is to manipulate the scale and position of the capped section box gizmo object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace WorldSpaceTransitions
{
public class CappedSectionBox : MonoBehaviour, ISizedSection
{
//public LayerMask layer = 13;
//[Space(10)]
public Collider xAxis;
public Collider xAxisNeg;
[Space(10)]
public Collider yAxis;
public Collider yAxisNeg;
[Space(10)]
public Collider zAxis;
public Collider zAxisNeg;
private enum GizmoAxis { X, Y, Z, Xneg, Yneg, Zneg, XYRotate, XZRotate, YZRotate, none };
private GizmoAxis selectedAxis;
//private RaycastHit hit;
private Plane dragplane;
private float rayDistance, newRotY, rayDistancePrev, distance;
private Vector3 lookCamera, startDrag, startPos, startDragRot, lookHitPoint, startScale;
private bool dragging;
private bool dragging_;
#region 自己加的改动 改成滑动条剖切
/// <summary>
/// 发射射线的物体
/// </summary>
//public GameObject rayobj_X, rayobj_Y, rayobj_Z;
public Slider sliderX, sliderY, sliderZ;
private float floatminX, floatminY, floatminZ, floatmaxX, floatmaxY, floatmaxZ;
private float flokmax, flokmay, flokmaz;
private float blx, bly, blz;
// Use this for initialization
void Start()
{
//GetSliderData();
}
public void GetSliderData()
{
floatminX = xAxisNeg.transform.position.x;
floatminY = yAxisNeg.transform.position.y;
floatminZ = zAxisNeg.transform.position.z;
floatmaxX = xAxis.transform.position.x;
floatmaxY = yAxis.transform.position.y;
floatmaxZ = zAxis.transform.position.z;
sliderX.maxValue = floatmaxX;
sliderX.minValue = floatminX;
sliderX.value = floatmaxX;// + this.transform.position.x;
sliderY.maxValue = floatmaxY;
sliderY.minValue = floatminY;
sliderY.value = floatmaxY;// + this.transform.position.y;
sliderZ.maxValue = floatmaxZ;
sliderZ.minValue = floatminZ;
sliderZ.value = floatmaxZ;
flokmax = transform.localScale.x;
flokmay = transform.localScale.y;
flokmaz = transform.localScale.z;
blx = GetSpecific_value(floatmaxX, floatminX, flokmax);
bly = GetSpecific_value(floatmaxY, floatminY, flokmay);
blz = GetSpecific_value(floatmaxZ, floatminZ, flokmaz);
}
public void 滑动条按下(int aa)
{
if (aa == 0)
{
dragplane = new Plane();
RaycastHit hit;
Ray _ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (xAxis.Raycast(_ray, out hit, 1000f))
{
selectedAxis = GizmoAxis.X;
dragplane.SetNormalAndPosition(transform.right, transform.position);
}
distance = hit.distance;
startDrag = new Vector3(xAxis.transform.position.x, xAxis.transform.position.y, distance);
startPos = transform.position;
startScale = transform.localScale;
}
else if (aa == 1)
{
dragplane = new Plane();
RaycastHit hit;
Ray _ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//if (Physics.Raycast(rayobj_Y.transform.position, yAxis.transform.position, out hit, 1000f))
if (yAxis.Raycast(_ray, out hit, 1000f))
{
selectedAxis = GizmoAxis.Y;
dragplane.SetNormalAndPosition(transform.up, transform.position);
}
distance = hit.distance;
startDrag = new Vector3(yAxis.transform.position.x, yAxis.transform.position.y, distance);
startPos = transform.position;
startScale = transform.localScale;
}
else if (aa == 2)
{
dragplane = new Plane();
RaycastHit hit;
Ray _ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (zAxis.Raycast(_ray, out hit, 1000f))
{
selectedAxis = GizmoAxis.Z;
dragplane.SetNormalAndPosition(transform.forward, transform.position);
}
//Debug.DrawLine(Camera.main.transform.position, zAxis.transform.position, Color.red, 10f);
distance = hit.distance;
startDrag = new Vector3(zAxis.transform.position.x, zAxis.transform.position.y, zAxis.transform.position.z);
startPos = transform.position;
startScale = transform.localScale;
}
滑动条拖动(aa);
}
public void 滑动条拖动(int aa)
{
if (aa == 0)
{
float lsx = startScale.x;
float lsy = startScale.y;
float lsz = startScale.z;
Vector3 onDrag = new Vector3(sliderX.value, xAxis.transform.position.y, distance);
Vector3 translation = onDrag - startDrag;
Vector3 projectedTranslation = Vector3.zero;
projectedTranslation = Vector3.Project(translation, transform.right);
transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
lsx += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.right));
transform.localScale = new Vector3(Mathf.Clamp(lsx, 0.01f, Mathf.Infinity), Mathf.Clamp(lsy, 0.01f, Mathf.Infinity), Mathf.Clamp(lsz, 0.01f, Mathf.Infinity));
}
else if (aa == 1)
{
float lsx = startScale.x;
float lsy = startScale.y;
float lsz = startScale.z;
Vector3 onDrag = new Vector3(yAxis.transform.position.x, sliderY.value, distance);
Vector3 translation = onDrag - startDrag;
Vector3 projectedTranslation = Vector3.zero;
projectedTranslation = Vector3.Project(translation, transform.up);
transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
lsy += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.up));
transform.localScale = new Vector3(Mathf.Clamp(lsx, 0.01f, Mathf.Infinity), Mathf.Clamp(lsy, 0.01f, Mathf.Infinity), Mathf.Clamp(lsz, 0.01f, Mathf.Infinity));
}
else if (aa == 2)
{
float lsx = startScale.x;
float lsy = startScale.y;
float lsz = startScale.z;
Vector3 onDrag = new Vector3(zAxis.transform.position.x, zAxis.transform.position.y, sliderZ.value);
Vector3 translation = onDrag-startDrag;
Vector3 projectedTranslation = Vector3.zero;
projectedTranslation = Vector3.Project(translation, transform.forward);
transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
lsz += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.forward));
transform.localScale = new Vector3(Mathf.Clamp(lsx, 0.01f, Mathf.Infinity), Mathf.Clamp(lsy, 0.01f, Mathf.Infinity), Mathf.Clamp(lsz, 0.01f, Mathf.Infinity));
}
}
#endregion
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && EventSystem.current.IsPointerOverGameObject() == false)
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
dragplane = new Plane();
RaycastHit hit;
if (xAxis.Raycast(ray, out hit, 1000f))
{
selectedAxis = GizmoAxis.X;
dragplane.SetNormalAndPosition(transform.up, transform.position);
}
else if (xAxisNeg.Raycast(ray, out hit, 1000f))
{
selectedAxis = GizmoAxis.Xneg;
dragplane.SetNormalAndPosition(-transform.up, transform.position);
}
else if (yAxis.Raycast(ray, out hit, 1000f))
{
selectedAxis = GizmoAxis.Y;
dragplane.SetNormalAndPosition(transform.forward, transform.position);
}
else if (yAxisNeg.Raycast(ray, out hit, 1000f))
{
selectedAxis = GizmoAxis.Yneg;
dragplane.SetNormalAndPosition(-transform.forward, transform.position);
}
else if (zAxis.Raycast(ray, out hit, 1000f))
{
selectedAxis = GizmoAxis.Z;
dragplane.SetNormalAndPosition(transform.up, transform.position);
}
else if (zAxisNeg.Raycast(ray, out hit, 1000f))
{
selectedAxis = GizmoAxis.Zneg;
dragplane.SetNormalAndPosition(-transform.up, transform.position);
}
else
{
//Debug.Log(hit.collider.name);
return;
}
distance = hit.distance;
startDrag = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance));
startPos = transform.position;
startScale = transform.localScale;
dragging_ = true;
}
if (dragging_)
{
//Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector3 onDrag = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance));
Vector3 translation = onDrag - startDrag;
Vector3 projectedTranslation = Vector3.zero;
float lsx = startScale.x;
float lsy = startScale.y;
float lsz = startScale.z;
switch (selectedAxis)
{
case GizmoAxis.X:
{
projectedTranslation = Vector3.Project(translation, transform.right);
transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
lsx += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.right));
break;
}
//case GizmoAxis.Xneg:
// {
// projectedTranslation = Vector3.Project(translation, -transform.right);
// transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
// lsx += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, -transform.right));
// break;
// }
case GizmoAxis.Y:
{
projectedTranslation = Vector3.Project(translation, transform.up);
transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
lsy += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.up));
break;
}
//case GizmoAxis.Yneg:
// {
// projectedTranslation = Vector3.Project(translation, -transform.up);
// transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
// lsy += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, -transform.up));
// break;
// }
case GizmoAxis.Z:
{
projectedTranslation = Vector3.Project(translation, transform.forward);
transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
lsz += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.forward));
break;
}
//case GizmoAxis.Zneg:
// {
// projectedTranslation = Vector3.Project(translation, -transform.forward);
// transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
// lsz += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, -transform.forward));
// break;
// }
}
transform.localScale = new Vector3(Mathf.Clamp(lsx, 0.01f, Mathf.Infinity), Mathf.Clamp(lsy, 0.01f, Mathf.Infinity), Mathf.Clamp(lsz, 0.01f, Mathf.Infinity));
//比例值根据公式计算所得 lsx (框)的区间 是0-lsx 滑动条上面也有一个值的区间 min-max
//公式: (0*x)+y= 滑动条最小值 min
// (lsx*x)+y=滑动条最大值 max
//最后求出 x值 y值 带进去即可 因为模型大小都不一样,比例值也不一致,需要自行计算
sliderX.value = lsx * blx + floatminX;
sliderY.value = lsy * bly + floatminY;
sliderZ.value = lsz * blz + floatminZ;
if (Input.GetMouseButtonUp(0))
{
dragging_ = false;
}
}
}
public float GetSpecific_value(float MAX, float MIN, float LXYZ)
{
float AA = (MAX - MIN) / LXYZ;
return AA;
}
public void HideCage(bool val)
{
foreach (PlaneHover ph in gameObject.GetComponentsInChildren<PlaneHover>()) ph.GetComponent<Renderer>().enabled = val;
}
public void HideCaps(bool val)
{
foreach (Transform quad in gameObject.transform) if (quad.name.Contains("hatch")) quad.GetComponent<Renderer>().enabled = val;
}
public void Size(Bounds b, GameObject g, BoundsOrientation orientation)
{
//Debug.Log(b.ToString());
float scale = 1f;
Vector3 clearance = 0.01f * Vector3.one;
transform.localScale = Vector3.one;
transform.localScale = scale * b.size + clearance;
transform.position = b.center;
}
}
}
修改的脚本
2.再加一个自动设置剖切的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WorldSpaceTransitions;
public class AutoSheScript : MonoBehaviour
{
public GameObject pouqieObj;
public SectionSetup sectionSetup_;
public CappedSectionBox cappedSectionBox_;
// Start is called before the first frame update
void Start()
{
//模型标签"PouQie"
pouqieObj = GameObject.FindGameObjectWithTag("PouQie");
}
//执行剖切 剖切框就会自动设置好
public void Perform_Cutting()
{
sectionSetup_.model = pouqieObj;
sectionSetup_.RecalculateBounds();
sectionSetup_.CheckShaders();
sectionSetup_.CreateSectionMaterials();
cappedSectionBox_.GetSliderData();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown (KeyCode.Space))
{
Perform_Cutting();
}
}
}
自动设置剖切
3.新加的功能,设置剖切的百分比固定参数,有需要可以添加,没有需要就不用添加 ,方法加到上面**修改的脚本**里面的最后即可(如图所示)
4.下面是添加的方法复制即可
/// <summary>
/// 传的参数是数字 这是Y轴剖切的,其他轴可以自行添加
/// </summary>
/// <param name="aa"></param>
public void SetingValue(float aa)
{
滑动条按下(1);
float lsx = startScale.x;
float lsy = startScale.y;
float lsz = startScale.z;
Vector3 onDrag = new Vector3(yAxis.transform.position.x, CalculateBFB(aa), distance);
Vector3 translation = onDrag - startDrag;
Vector3 projectedTranslation = Vector3.zero;
projectedTranslation = Vector3.Project(translation, transform.up);
transform.position = startPos + 0.5f * (projectedTranslation.normalized * translation.magnitude);
lsy += translation.magnitude * Mathf.Sign(Vector3.Dot(projectedTranslation, transform.up));
transform.localScale = new Vector3(Mathf.Clamp(lsx, 0.01f, Mathf.Infinity), Mathf.Clamp(lsy, 0.01f, Mathf.Infinity), Mathf.Clamp(lsz, 0.01f, Mathf.Infinity));
}
float CalculateBFB(float aa)
{
float CAL = (sliderY.maxValue - sliderY.minValue) * aa/100+ sliderY.minValue;
return CAL;
}
固定参数的剖切方法
修改之后的效果