少年修仙传客户端代码仓库
hch
2025-06-12 204ef05a831c9484e2abc561d27ecbff7c797453
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using UnityEngine;
using System.Collections;
 
namespace DynamicShadowProjector {
    public class FollowTargetObject : MonoBehaviour {
        public enum TextureAlignment {
            None = 0,
            TargetAxisX,
            TargetAxisY,
            TargetAxisZ,
        }
        public enum UpdateFunction {
            OnPreCull = 0,
            LateUpdate,
            UpdateTransform,
        }
        // Serialize Fields
        [SerializeField]
        private Transform m_target;
        [SerializeField]
        private Transform m_targetDirection = null;
        [SerializeField]
        private TextureAlignment m_textureAlignment = TextureAlignment.None;
        [SerializeField]
        private UpdateFunction m_updateFunction = UpdateFunction.LateUpdate;
 
        // public properties
        public Transform target
        {
            get { return m_target; }
            set { m_target = value; }
        }
        public Transform targetDirection
        {
            get { return m_targetDirection; }
            set { m_targetDirection = value; }
        }
        public TextureAlignment textureAlignment
        {
            get { return m_textureAlignment; }
            set { m_textureAlignment = value; }
        }
        public UpdateFunction updateFunction
        {
            get { return m_updateFunction; }
            set { m_updateFunction = value; }
        }
 
        public void UpdateTransform()
        {
            if (m_textureAlignment != TextureAlignment.None || m_targetDirection != null) {
                // rotate projector to align texture parallel to target object.
                Vector3 up;
                switch (m_textureAlignment) {
                case TextureAlignment.TargetAxisX:
                    up = m_target.right;
                    break;
                case TextureAlignment.TargetAxisZ:
                    up = m_target.forward;
                    break;
                default:
                    up = m_target.up;
                    break;
                }
                Vector3 z = m_targetDirection != null ? m_targetDirection.forward : transform.forward;
                transform.LookAt(transform.position + z, up);
            }
            Vector3 targetPos = transform.TransformPoint(m_localTargetPosition);
            transform.position += m_target.position - targetPos;
        }
 
        private Vector3 m_localTargetPosition;
 
        // message handlers
        void Awake()
        {
            if (m_target != null) {
                m_localTargetPosition = transform.InverseTransformPoint(m_target.position);
            }
        }
 
        void LateUpdate()
        {
#if UNITY_EDITOR
            if (!Application.isPlaying) {
                return;
            }
#endif
            if (m_updateFunction == UpdateFunction.LateUpdate) {
                UpdateTransform();
            }
        }
 
        void OnPreCull()
        {
#if UNITY_EDITOR
            if (!Application.isPlaying) {
                return;
            }
#endif
            if (m_updateFunction == UpdateFunction.OnPreCull) {
                UpdateTransform();
            }
        }
    }
}