人物影子制作
在人物对象的下面创建一个空物体,命名为PlayerShadow,在其下建一个Camera,如下设置:
在playerShadow添加三个脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharShadowScript : MonoBehaviour {
public Transform display;
private GameObject ShadowCamera;
GameObject map;
RenderTexture mTex = null;
private void Start()
{
map = transform.GetChild(1).gameObject;
ShadowCamera = transform.GetChild(0).gameObject;
if (!display)
display = transform.parent;
mTex = new RenderTexture(128, 128, 0);
mTex.name = "PlayerShadow" + GetInstanceID();
Camera mCamera = ShadowCamera.GetComponent<Camera>();
mCamera.targetTexture = mTex;
}
private void Update()
{
if(display != null)
{
map.GetComponent<Renderer>().material.mainTexture = mTex;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KeepStartPositionOffset : MonoBehaviour {
public Transform targetObject;
Vector3 positionOffset = new Vector3();
private void Start()
{
if(!targetObject)
{
targetObject = transform.parent;
}
if(targetObject)
{
positionOffset = transform.position - targetObject.position;
}
}
private void Update()
{
if(targetObject)
{
transform.position = targetObject.position + positionOffset;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCameraFoward : MonoBehaviour {
public Transform cameraTransform;
Quaternion initRotation;
private void Start()
{
if(cameraTransform == null)
{
cameraTransform = GameObject.Find("MainCamTarget").transform;
}
initRotation = transform.rotation;
}
private void FixedUpdate()
{
if(cameraTransform == null)
{
cameraTransform = GameObject.Find("MainCamTarget").transform;
}
transform.rotation = Quaternion.RotateTowards(cameraTransform.rotation, initRotation, Mathf.Infinity);
}
}
创建一个RenderTexture命名为playerShadow,创建一个Material和一个Shader,shader代码:
Shader "MyShader/PlayerShadow"
{
Properties
{
_Color("Main Color",Color) = (1,1,1,1)
_MainTex("Base (RGB) Alpha(A)", 2D) = "white" {}
_Cutoff("Base Alpha cutoff",Range(0,.1)) = .02
_SpcTex("Base (RGB) Alpha (A)",2D) = "white"{}
}
SubShader
{
Tags{"Queue" = "Transparent" "IgnoreProjector" = "True" "RanderType" = "TransparentCutout"}
Lighting off
ZWrite off
Cull off
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};
struct v2f
{
float4 vertex : SV_POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};
sampler2D _MainTex;
sampler2D _SpecTex;
float4 _MainTex_ST;
float _Cutoff;
v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.color = v.color;
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
float4 _Color;
half4 frag(v2f i) : SV_Target
{
half4 col = tex2D(_MainTex,i.texcoord);
half4 col1 = tex2D(_SpecTex, i.texcoord);
if (col.a < _Cutoff)
{
clip(col.a - _Cutoff);
}
else
{
col.rgb = col.rgb * float3(0, 0, 0);
col.rgb = col.rgb + _Color;
col.a = _Color.a;
}
return col;
}
ENDCG
}
}
}
将这个RenderTexture赋给刚创建的Camera的TargetTexture,大功告成
效果图:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了