Created
September 25, 2020 01:37
-
-
Save sotanmochi/ffd01b85938c4b9ae52518f7c5190d35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2019 Soichiro Sugimoto. | |
// Licensed under the MIT License. See README in the project root for license information. | |
using UnityEngine; | |
using Live2D.Cubism.Rendering; | |
namespace Live2D_Extension | |
{ | |
public class CubismRenderControllerFor3DSpace : MonoBehaviour | |
{ | |
[SerializeField] CubismRenderController _CubismRenderController; | |
[SerializeField] GameObject _CubismModelDrawables; | |
[SerializeField] Material _ArtMeshMaterial; | |
[SerializeField] float _SortingOrderScale = 10000f; | |
[SerializeField] CubismSortingMode _SortingMode = CubismSortingMode.BackToFrontOrder; | |
MeshRenderer[] _MeshRenderers; | |
void Start() | |
{ | |
if (_ArtMeshMaterial != null) | |
{ | |
_MeshRenderers = _CubismModelDrawables.GetComponentsInChildren<MeshRenderer>(); | |
foreach (MeshRenderer renderer in _MeshRenderers) | |
{ | |
renderer.material = _ArtMeshMaterial; | |
} | |
} | |
} | |
void Update() | |
{ | |
UpdateSortingOrder(); | |
} | |
void UpdateSortingOrder() | |
{ | |
Vector3 cameraPos = Camera.main.transform.position; | |
Vector3 modelPos = _CubismRenderController.gameObject.transform.position; | |
Vector3 direction = cameraPos - modelPos; | |
float distance = direction.magnitude; | |
int sortingOrder = Mathf.RoundToInt(-_SortingOrderScale * distance); | |
_CubismRenderController.SortingOrder = sortingOrder; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment