zoukankan      html  css  js  c++  java
  • 人物影子制作

    在人物对象的下面创建一个空物体,命名为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,大功告成

    效果图:

  • 相关阅读:
    基础算法:求目标值 &字符串反转
    算法计算出股票最佳交易时间点
    Python 设计模式—命令模式
    Python 设计模式—代理模式
    有趣的算法题~单调栈
    令人头大的字符串—算法处理
    WebUI 自动化测试的经典设计模式:PO
    双指针—滑动窗口算法解析
    Python 设计模式—观察者模式
    多线程之读写锁原理
  • 原文地址:https://www.cnblogs.com/mcyushao/p/9806621.html
Copyright © 2011-2022 走看看