分享unity中 激光剑的制作 来几张图吧

 

这个是没有 在摄像机上加载后期处理效果的 照片

主要是 视觉上主要是用到一个插件

 

这个图片上挂载了一个

VolumetricLineBehavior

这个

    [RequireComponent(typeof(MeshFilter))] 是在要求物体身上必须要有组件meshfilter 和,meshrenderer组件才可以生效,否则会报错
然后有很多
  这个方法是  获取 值和设置值,这个值是光剑剑刃开始的位置一般 只设置z轴,x,y为0
 

这个 方法比较 重要, 就是设置开始位置和结束位置,主要是下面的mesh.vertices=vertexpositions;那一段代码

设置顶点坐标 vertices 线性normals,法线 就是和 顶点坐标反着来,然后再从定点重新计算mesh的边界积, 也就是重新计算边界盒

   private MeshFilter m_meshFilter;   网格过滤器,是在私有成员中定义的 主要是修改网格中顶点 对mesh组件不熟悉的请移步
https://www.cnblogs.com/jeason1997/p/4825981.html
 
这个方法是设置所有材质的属性,这个shader是里面的Additive
有四个属性 一个贴图

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

#ifndef VOL_LINE_SIMPLE_SHADER_INC
#define VOL_LINE_SIMPLE_SHADER_INC
	
	#include "UnityCG.cginc"
	
	float _LineWidth;
	float _LineScale;
	
	struct a2v
	{
		float4 vertex : POSITION;
		float3 otherPos : NORMAL; // object-space position of the other end
		half2 texcoord : TEXCOORD0;
		float2 texcoord1 : TEXCOORD1;
	};
	
	struct v2f
	{
		float4 pos : SV_POSITION;
		half2 uv : TEXCOORD0;
	};
	
	v2f vert (a2v v)
	{
		v2f o;
		o.uv = v.texcoord;
		
#if UNITY_VERSION >= 540
		float4 clipPos = UnityObjectToClipPos(v.vertex);
		float4 clipPos_other = UnityObjectToClipPos(float4(v.otherPos, 1.0));
#else
		float4 clipPos = UnityObjectToClipPos(v.vertex);
		float4 clipPos_other = UnityObjectToClipPos(float4(v.otherPos, 1.0));
#endif

		float aspectRatio = _ScreenParams.x / _ScreenParams.y;
		float invAspectRatio = _ScreenParams.y / _ScreenParams.x;

		float2 ssPos = float2(clipPos.x * aspectRatio, clipPos.y);
		float2 ssPos_other = float2(clipPos_other.x * aspectRatio, clipPos_other.y);

		float scaledLineWidth = _LineWidth * _LineScale;

#ifndef FOV_SCALING_OFF
		float t = unity_CameraProjection._m11;
		float fov = atan(1.0f / t) * 114.59155902616464175359630962821; // = 2 * 180 / UNITY_PI = 2 * rad2deg
		scaledLineWidth = scaledLineWidth * 60.0 / fov;
#endif
		
		// screen-space offset vector:
		float2 lineDirProj = scaledLineWidth * normalize(
			ssPos.xy/clipPos.w - // screen-space pos of current end
			ssPos_other.xy/clipPos_other.w // screen-space position of the other end
		) * sign(clipPos.w) * sign(clipPos_other.w);
		
		float2 offset =
			v.texcoord1.x * lineDirProj +
			v.texcoord1.y * float2(lineDirProj.y, -lineDirProj.x)
		;
		
		clipPos.x += offset.x / aspectRatio;
		clipPos.y += offset.y;
		o.pos = clipPos;

		return o;
	}
	
#if !defined(VOL_LINE_SHDMODE_FAST)
	fixed _LightSaberFactor;
	fixed4 _Color;
#endif
	sampler2D _MainTex;

	fixed4 frag(v2f i) : SV_Target
	{
		fixed4 tx = tex2D(_MainTex, i.uv);
		
#ifdef VOL_LINE_SHDMODE_FAST
		return tx;
#else
		return
			tx.a > _LightSaberFactor
			?
			fixed4(1.0, 1.0, 1.0, tx.a)
			:
			tx * _Color
		;
#endif
	}
	
#endif

 

父物体挂载的脚本 light saber 继承于

VRTK_InteractableObject
namespace VRTK.Examples
{
    using UnityEngine;

    public class LightSaber : VRTK_InteractableObject
    {
        private bool beamActive = false;   
	 	private float maxCollisionForce = 4000f;
		private float impactMagnifier = 120f;
		 private float collisionForce = 0f;
		VolumetricLines.VolumetricLineBehavior m_VoluLine;      
  		private VRTK_ControllerReference controllerReference;

		public override void StartUsing(VRTK_InteractUse usingObject)
		{
			base.StartUsing(usingObject);

			m_VoluLine.gameObject.SetActive(!beamActive);
			beamActive = !beamActive;


			Debug.Log("1");
		

		}
			 public override void Grabbed(VRTK_InteractGrab grabbingObject)
        {
            base.Grabbed(grabbingObject);
            controllerReference = VRTK_ControllerReference.GetControllerReference(grabbingObject.controllerEvents.gameObject);
        }

	public void m_Sowrd(float fas)
	{
		//判断  然后  手柄加震动
 	 if (VRTK_ControllerReference.IsValid(controllerReference) && IsGrabbed())
            { 
                collisionForce = VRTK_DeviceFinder.GetControllerVelocity(controllerReference).magnitude * impactMagnifier;
                var hapticStrength = collisionForce / maxCollisionForce;
                VRTK_ControllerHaptics.TriggerHapticPulse(controllerReference, hapticStrength, 0.5f, 0.01f);
            }
            else
            {
                collisionForce = fas*impactMagnifier;
            }
	}
	  protected override void OnEnable()
        {
            base.OnEnable();
            controllerReference = null;
		}

        public override void StopUsing(VRTK_InteractUse usingObject)
        {
            base.StopUsing(usingObject);

			Debug.Log("1");
		}
		public override void Ungrabbed(VRTK_InteractGrab previousGrabbingObject = null)
		{
			base.Ungrabbed(previousGrabbingObject);
			controllerReference = null;

			m_VoluLine.gameObject.SetActive(false);
			beamActive = false;
		}

		protected void Start()
        {
			 m_VoluLine = transform.Find("Sword").GetComponent<VolumetricLines.VolumetricLineBehavior>();
			m_VoluLine.gameObject.SetActive(false);

		}


        protected override void Update()
        {
            base.Update();

		}


     
    }
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

跳至工具栏