zoukankan      html  css  js  c++  java
  • PreviewRenderUtility的Example

    用PreviewRenderUtility就不用自己再去创建摄像机来做模型在Editor下的预览了

    需要注意,默认是用场景天空盒和场景灯光。参考ShaderForge编辑器的做法,配置自定义灯光

    这些Unity都有提供接口,不用自己再去创建

    using UnityEngine;
    using System.Collections;
    using UnityEditor;
    using UnityEditorInternal;
    
    public class Foo : EditorWindow
    {
        Mesh mPreviewMesh;
        Material mPreviewMaterial;
        PreviewRenderUtility mPreviewRenderUtility;
    
    
        [MenuItem("Tools/Foo")]
        static void Setup()
        {
            GetWindow<Foo>();
        }
    
        void OnGUI()
        {
            if (mPreviewRenderUtility == null)
            {
                mPreviewRenderUtility = new PreviewRenderUtility();
                mPreviewRenderUtility.m_Camera.farClipPlane = 500;
                mPreviewRenderUtility.m_Camera.clearFlags = CameraClearFlags.SolidColor;
                mPreviewRenderUtility.m_Camera.transform.position = new Vector3(0, 0, -10);
    
                var go = GameObject.CreatePrimitive(PrimitiveType.Cube);
                var meshFilter = go.GetComponent<MeshFilter>();
                mPreviewMesh = meshFilter.sharedMesh;
                mPreviewMaterial = go.GetComponent<MeshRenderer>().sharedMaterial;
    
                DestroyImmediate(go);
            }
    
            var drawRect = new Rect(0, 0, 100, 100);
            mPreviewRenderUtility.BeginPreview(drawRect, GUIStyle.none);
    
            InternalEditorUtility.SetCustomLighting(mPreviewRenderUtility.m_Light, new Color(0.6f, 0.6f, 0.6f, 1f));
    
            mPreviewRenderUtility.DrawMesh(mPreviewMesh, Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(30, 45, 0), Vector3.one), mPreviewMaterial, 0);
    
            mPreviewRenderUtility.m_Camera.Render();
            var texture = mPreviewRenderUtility.EndPreview();
    
            InternalEditorUtility.RemoveCustomLighting();
    
            GUI.Box(drawRect, texture);
        }
    }

    效果:

    天空盒的话可以自行配置一个带Cubemap的shader:

  • 相关阅读:
    rm 、git rm 、git rm --cached的区别
    实行敏捷开发阶段性总结
    [转]Pycharm 断点调试方法
    scp -v 查看具体的过程
    根据字典的值大小进行排序
    文件服务器满故障排查总结
    看谷歌白板面试有感
    [译]从列表或字典创建Pandas的DataFrame对象
    查找docker log久远数据方法
    docker log 批量删除报错: find: `/var/lib/docker/containers/': 没有那个文件或目录
  • 原文地址:https://www.cnblogs.com/hont/p/6618528.html
Copyright © 2011-2022 走看看