/** * Copyright(c) Live2D Inc. All rights reserved. * * Use of this source code is governed by the Live2D Open Software license * that can be found at https://www.live2d.com/eula/live2d-open-software-license-agreement_en.html. */ using Live2D.Cubism.Core; using Live2D.Cubism.Rendering; using UnityEngine; namespace Live2D.Cubism.Framework.LookAt { /// /// Provides the center position for look-at calculations using an ArtMesh (CubismDrawable). /// [DisallowMultipleComponent] public class CubismLookCenterArtMesh : MonoBehaviour, ICubismLookCenter { /// /// Target drawable to calculate the center from. /// [SerializeField] public CubismDrawable TargetDrawable; /// /// Model root transform. /// private Transform _modelRootTransform; private void Start() { _modelRootTransform = this.transform; } /// /// Gets the position of the center. /// public Vector3 GetCenterPosition() { if (TargetDrawable == null) { return Vector3.zero; } var cubismRenderer = TargetDrawable.GetComponent(); if (cubismRenderer == null || cubismRenderer.Mesh == null) { return _modelRootTransform.InverseTransformPoint(TargetDrawable.transform.position); } var centerInMeshLocal = cubismRenderer.Mesh.bounds.center; var centerInWorld = TargetDrawable.transform.TransformPoint(centerInMeshLocal); var centerInModelLocal = _modelRootTransform.InverseTransformPoint(centerInWorld); return centerInModelLocal; } } }