I’m looking for a way to clip with plane arrays for my project.
The test code works, but not in my project.
The effect I am going for is each array of planes has it’s own object to clip.
How do I clip one cube with two plane arrays and keep the positive sides?
My test code.
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class clipcube : MonoBehaviour
{
public GameObject Cube;
public Vector4[] PlanePos;
public MaterialPropertyBlock BlockOne;
public List<Plane> planes = new List<Plane>();
// Start is called before the first frame update
void Start()
{
PlanePos = new Vector4[10];
BlockOne = new MaterialPropertyBlock();
planes.Add(new Plane(new Vector3(5,0,0), new Vector3(5,0, -1), new Vector3(5, 1, -1)));
planes.Add(new Plane(new Vector3(-5, 1, -1), new Vector3(-5, 0, -1), new Vector3(-5, 0, 0)));
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < planes.Count; i++)
{
BlockOne.SetInt("_Int", planes.Count);
Array.Clear(PlanePos, 0, PlanePos.Length);
PlanePos[i] = new Vector4(planes[i].normal.x, planes[i].normal.y, planes[i].normal.z, planes[i].distance);
Matrix4x4 matrix = Matrix4x4.TRS(Cube.transform.position, Cube.transform.rotation, Cube.transform.lossyScale);
Graphics.DrawMesh(Cube.GetComponent<MeshFilter>().mesh, matrix, Cube.GetComponent<Renderer>().sharedMaterial, 0, Camera.main, 0, BlockOne, false, false);
BlockOne.SetVectorArray("_Plane", PlanePos);
}
}
}
My shader
Shader "Custom/Clipping"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
[HDR]_Emission ("Emission", color) = (0,0,0)
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
float3 worldPos;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
int _Int = 0;
float4 _Plane[50];
half3 _Emission;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
for (int i = 0; i < _Int; i++)
{
float distance = dot(IN.worldPos, _Plane[i].xyz);
distance = distance + _Plane[i].w;
clip(distance);
}
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
o.Emission = _Emission * tex2D(_MainTex, IN.uv_MainTex);
}
ENDCG
}
FallBack "Diffuse"
}