123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace Knife.HologramEffect
- {
- [ExecuteAlways]
- public class MatrixProvider : MonoBehaviour
- {
- [SerializeField] private string propertyName = "_CustomMatrix";
- [SerializeField] private Renderer[] targetRenderers;
- [SerializeField] private bool eliminateRootBoneMatrix;
- private MaterialPropertyBlock materialPropertyBlock;
- private void OnEnable()
- {
- if (this == null)
- return;
- if (materialPropertyBlock == null)
- materialPropertyBlock = new MaterialPropertyBlock();
- if (targetRenderers != null)
- {
- foreach (var r in targetRenderers)
- {
- if (r != null)
- {
- Matrix4x4 matrix = transform.localToWorldMatrix;
- if(eliminateRootBoneMatrix)
- {
- SkinnedMeshRenderer skinnedMeshRenderer = r as SkinnedMeshRenderer;
- if(skinnedMeshRenderer != null)
- {
- Transform rootBone = skinnedMeshRenderer.rootBone;
- if(rootBone != null)
- {
- matrix = rootBone.localToWorldMatrix * matrix;
- }
- }
- }
- r.GetPropertyBlock(materialPropertyBlock);
- materialPropertyBlock.SetMatrix(propertyName, matrix);
- r.SetPropertyBlock(materialPropertyBlock);
- }
- }
- }
- }
- private void OnValidate()
- {
- OnEnable();
- }
- }
- }
|