关于Particle System is trying to spawn on a mesh with zero surface area的警告

今天测试反馈了个刷屏的警告”Particle System is trying to spawn on a mesh with zero surface area“根据翻译判断是使用了体积为0的mesh,但是当我在网上查找资料的时候,网上说的是因为 ParticleSystem 中 ShapeModule 要求引用的 Mesh 必须开启 Read/Write 选项.

经过我的实验和调试发现并不是这么回事,简单的就是因为某个特效的shape类型选择了mesh然而没有指定一个mesh给他,而且选择的Mode为Edge,如下:

满足以上几个箭头所指的条件后就会不断刷如下警告:

要想去掉这些警告,可以改变shape的类型或者指定一个mesh给它,也可以改变Mode的类型为Vertex都可以去掉这些烦人的警告,具体方法需要根据自己的特效情况设置.

如果需要查找项目中有问题的特效可以使用以下编辑器脚本:

using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;

public static class MeshToolForParticle
{
    [MenuItem("Tools/ParticleSystem/FindScenes", false, 20)]
    public static void FindScenes()
    {
        var guids = AssetDatabase.FindAssets("t:Scene");

        foreach (var guid in guids)
        {
            string path = AssetDatabase.GUIDToAssetPath(guid);
            var scene = EditorSceneManager.OpenScene(path, OpenSceneMode.Single);
            var roots = scene.GetRootGameObjects();
            bool found = false;
            foreach (var go in roots)
            {
                found |= Find(go);
            }

            if (found)
            {
                Debug.Log(path);
            }
        }
    }

    [MenuItem("Tools/ParticleSystem/FindPrefabs", false, 20)]
    public static void FindPrefabs()
    {
        var guids = AssetDatabase.FindAssets("t:Prefab");

        foreach (var guid in guids)
        {
            string path = AssetDatabase.GUIDToAssetPath(guid);
            var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(path);
            Find(prefab);
        }
    }

    private static bool Find(GameObject go)
    {
        bool found = false;
        var particles = go.GetComponentsInChildren<ParticleSystem>(true);
        foreach (var particle in particles)
        {
            if (particle.shape.enabled && particle.shape.shapeType == ParticleSystemShapeType.Mesh &&
                particle.shape.mesh == null)
            {
                string prefabPath = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(particle);
                Debug.LogWarning(string.Format("Path: {0}\nPrefab: {1}", particle.gameObject.GetGameObjectPath(), prefabPath));
                found = true;
            }
        }

        return found;
    }
}

把上面的脚本放到项目的Editor目录下然后点击菜单Tools/ParticleSystem/FindPrefabs,即可打印出有问题的特效名称.

Phong和Blinn-Phone光照模型的区别

Lambert光照模型虽然能模拟出粗糙物体表面的光照效果,但是在真实环境中很多光滑的物体它却不能很好的表现出来。

所以在1975年裴祥风提出了一种局部光照的经验模型——Phong光照模型

公式:Color = Ambient + Diffuse + Specular

其中, Specular 为镜面反射,C(specular) = C(light)*M(specular)saturate(v*r)^M(shininess)

v是视角方向,r是光线的反射方向, M(shininess) 为物体材质的光泽度。

后来在1977年 Jim Blinn对Phong光照模型算法进行了改进,提出了Blinn-Phong光照模型,和Phong光照模型相比 Blinn-Phong 只改进了镜面反射的算法。

镜面反射公式:C(specluar)= C(light)*M(specular)saturate(n*h)^M(shininess)

其中h=normalize(v+l),v是视角方向,l是光照方向,n是法线方向。

虽然这一个小小的改进并没有让表现效果在视觉上有很大的提升,但是在性能方面却有很大的提升,因为h是取决于视角方向以及灯光方向的,两者很远的时候h可以认为是常量,和位置及表面曲率没有关系,所以可以减少计算量。

而Phong要根据表面曲率去逐顶点或逐像素计算反射向量r,所以计算量比较大。