public void OnGUI()
    {
        // Show initial GUI components
        EditorGUILayout.HelpBox(helpText, MessageType.Info);

        boundsMargin = EditorGUILayout.FloatField("Bounds Margin", boundsMargin);

        referenceMesh = (Mesh)EditorGUILayout.ObjectField("Reference Mesh", referenceMesh, typeof(Mesh), false);

        if (referenceMesh != null)
        {
            string referencePath = AssetDatabase.GetAssetPath(referenceMesh);
            string shadowPath    = ShadowAssetCreator.ConstructAssetPath(referenceMesh);

            // Show additional GUI components
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Reference Mesh location:");
            EditorGUILayout.LabelField(referencePath);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel("Shadow Mesh location:");
            EditorGUILayout.LabelField(shadowPath);
            EditorGUILayout.EndHorizontal();

            if (GUILayout.Button("Create shadow mesh"))
            {
                ShadowAssetCreator.CreateAsset(referenceMesh, boundsMargin, shadowPath);
            }
        }
    }
Beispiel #2
0
    protected void SetupGameObject(Transform transform)
    {
        // Destroy existing shadow game objects before performing recursion
        foreach (Transform child in transform)
        {
            if (child.name.Contains("Shadow"))
            {
                DestroyImmediate(child.gameObject);
            }
        }

        // Recursively setup (non-shadow game object) children first
        if (setupChildren)
        {
            foreach (Transform child in transform)
            {
                SetupGameObject(child);
            }
        }

        // Add shadow game object
        if (!transform.name.Contains("Shadow"))
        {
            // Examine current game object
            MeshFilter          meshFilter          = transform.GetComponent <MeshFilter>();
            SkinnedMeshRenderer skinnedMeshRenderer = transform.GetComponent <SkinnedMeshRenderer>();

            if (meshFilter != null && meshFilter.sharedMesh != null)
            {
                // Add shadow volume component
                ShadowVolume shadowVolume = transform.gameObject.GetComponent <ShadowVolume>();

                if (shadowVolume == null)
                {
                    shadowVolume = transform.gameObject.AddComponent <ShadowVolume>();
                }

                shadowVolume.IsSimple = isSimple;
                shadowVolume.Layer    = layer;

                if (createShadowMeshes)
                {
                    Mesh shadowMesh = ShadowAssetCreator.CreateAsset(meshFilter.sharedMesh, boundsMargin);

                    shadowVolume.ShadowMesh = shadowMesh;
                }
            }
            else if (skinnedMeshRenderer != null && skinnedMeshRenderer.sharedMesh != null)
            {
                // Create skinned shadow game object
                GameObject shadowGameObject = new GameObject("Skinned Shadow");

                // Set Transform
                shadowGameObject.transform.parent        = transform;
                shadowGameObject.transform.localPosition = Vector3.zero;
                shadowGameObject.transform.localRotation = Quaternion.identity;
                shadowGameObject.transform.localScale    = Vector3.one;

                // Set SkinnedMeshRenderer
                SkinnedMeshRenderer shadowRenderer = shadowGameObject.AddComponent <SkinnedMeshRenderer>();

                if (createShadowMeshes)
                {
                    Mesh shadowMesh = ShadowAssetCreator.CreateAsset(skinnedMeshRenderer.sharedMesh, boundsMargin);

                    if (shadowMesh != null)
                    {
                        shadowRenderer.bones      = skinnedMeshRenderer.bones;
                        shadowRenderer.sharedMesh = shadowMesh;
                    }
                }

                // Set SkinnedShadowVolume
                SkinnedShadowVolume shadowVolume = shadowGameObject.AddComponent <SkinnedShadowVolume>();

                shadowVolume.IsSimple         = isSimple;
                shadowVolume.gameObject.layer = layer;
            }
        }
    }