【小沐学Unity3d】3ds Max 减面工具:Simplyon(Unity3d,Python)

1、简介

Simplygon 带有一个 Unity 插件,它公开了优化功能,例如缩减、聚合、重新划分网格、冒名顶替者(SingleView、BillboardCloud / Vegetation)、遮挡网格以及支持以下内置着色器的材质烘焙:

  • 标准着色器
  • 通用渲染管线 (URP) 预构建着色器
  • 高清渲染管线 (HDRP) 预构建着色器

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、下载安装

https://www.simplygon.com/downloads
将Simlygon插件导入Unity。

2.1 安装Simlygon插件

Simplygon 安装完成后,按照 Unity 文档部分提供的指南从 tarball 文件安装包,然后浏览以查找 tarball(例如)。C:\Program Files\Simplygon<MajorVersion>\Unity\bin<UnityVersion>Simplygon.Unity2022Plugin.tgz
在这里插入图片描述
现在,每次启动 Unity 时,Simplygon Unity 插件都应自动加载。转到 Window -> Simplygon 以显示 Simplygon UI(如果尚不存在)。Simplygon UI 可以停靠到 Unity 中的大多数停靠区域;只需单击并按住 Simplygon 标签并将其放置在您喜欢的位置即可。

  • (1)找到Simplygon的dll文件,并将其复制到Assets文件夹下:
    在这里插入图片描述
  • (2)导入Simplygon后有:
    在这里插入图片描述

2.2 安装USD插件

每个 Unity 项目都需要手动安装 Simplygon Unity 插件,由于该插件依赖于 Pixar 的 USD(通用场景描述)文件格式,因此也会安装 Unity 提供的 USD 包。有关如何安装 Simplygon Unity 插件的信息,请参阅以下说明。

Simplygon Unity 插件利用 USD 文件格式作为 Unity 和 Simplygon 之间的中间格式。Unity 中未内置 USD 支持,因此支持 Unity 包与 Simplygon 插件一起安装。

  • (1)打开包管理器Window→Package Manager,把Packages切换到Packages:Unity Registry, 并搜索USD:
    在这里插入图片描述
  • (2)如果能搜到USD的包,就点击安装。如果当前Unity版本没有集成USD包资源,就选 择从Git仓库添加(URL地址:com.unity.formats.usd):
    在这里插入图片描述
    然后点击Add添加,等待USD包安装完成。
    在这里插入图片描述
    开始安装USD包:
    在这里插入图片描述

3、使用测试

  • (1)选择Window→Simplygon打开Simplygon面板:
    在这里插入图片描述

  • (2)点击Add LOD Component,依次点击Template > Basic > Reduction with material baking。
    此处有两种模式:Advanced(高级的)和Basic(基本的)。根据官方介绍,Basic模 式是针对新手的模式,在优化参数设置上屏蔽了大多数的可选项,如果你是新手或者 你希望简便快速的操作,就可以选择Basic。而Advanced模式下提供了所有优化配置 项的设置,如果你希望对模型做出选择性的针对性的优化,就可以使用Advanced选项。
    在这里插入图片描述
    ①Reduction: 减少面片
    ②Reduction with material baking:减少面片数并合并材质和贴图(若物体有多个材质)。
    ③Remeshing with material baking:官方解释是用更原始的轻量级Mesh网格代替原来的Mesh并合并材质和贴图。这个我没用过,可以自行 体验。
    ④Aggregation:合并网格(若有多个Mesh或者子物体含有Mesh,最后会合并成一个)。
    ⑤Aggregation with material baking:合并网格并合并材质和贴图。

  • (3)参数设置完后点击黄色大Logo执行优化。
    在这里插入图片描述
    Run Mode选择Rum In This Process。看到ReductionSettings下可以设置目标三角面比率(若看不到后面数字将窗口拉大点即可),默认为0.5,即生成模型面数是原来的0.5倍;MappingImageSettings下可以设置贴图尺寸,默认是1024 x 1024。

  • (4)简化结果比较:左边是原始资产,右边是优化的资产:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

4、Python测试

https://documentation.simplygon.com/SimplygonSDK_10.2.10100.0/api/examples/reduction/reduction.html

  • (1)首先安装包里有Simplyon SDK的Python库,如下:
    在这里插入图片描述
    在这里插入图片描述
  • (2)编写Python代码文件进行测试:
  • test.py
# Copyright (c) Microsoft Corporation. 
# Licensed under the MIT License. 

import math
import os
import sys
import glob
import gc
import threading

from pathlib import Path
from simplygon10 import simplygon_loader
from simplygon10 import Simplygon


def LoadScene(sg: Simplygon.ISimplygon, path: str):
    # Create scene importer 
    sgSceneImporter = sg.CreateSceneImporter()
    sgSceneImporter.SetImportFilePath(path)
    
    # Run scene importer. 
    importResult = sgSceneImporter.Run()
    if Simplygon.Failed(importResult):
        raise Exception('Failed to load scene.')
    sgScene = sgSceneImporter.GetScene()
    return sgScene

def SaveScene(sg: Simplygon.ISimplygon, sgScene: Simplygon.spScene, path: str):
    # Create scene exporter. 
    sgSceneExporter = sg.CreateSceneExporter()
    outputScenePath = ''.join(['output\\', 'Reduction', '_', path])
    sgSceneExporter.SetExportFilePath(outputScenePath)
    sgSceneExporter.SetScene(sgScene)
    
    # Run scene exporter. 
    exportResult = sgSceneExporter.Run()
    if Simplygon.Failed(exportResult):
        raise Exception('Failed to save scene.')

def CheckLog(sg: Simplygon.ISimplygon):
    # Check if any errors occurred. 
    hasErrors = sg.ErrorOccurred()
    if hasErrors:
        errors = sg.CreateStringArray()
        sg.GetErrorMessages(errors)
        errorCount = errors.GetItemCount()
        if errorCount > 0:
            print('CheckLog: Errors:')
            for errorIndex in range(errorCount):
                errorString = errors.GetItem(errorIndex)
                print(errorString)
            sg.ClearErrorMessages()
    else:
        print('CheckLog: No errors.')
    
    # Check if any warnings occurred. 
    hasWarnings = sg.WarningOccurred()
    if hasWarnings:
        warnings = sg.CreateStringArray()
        sg.GetWarningMessages(warnings)
        warningCount = warnings.GetItemCount()
        if warningCount > 0:
            print('CheckLog: Warnings:')
            for warningIndex in range(warningCount):
                warningString = warnings.GetItem(warningIndex)
                print(warningString)
            sg.ClearWarningMessages()
    else:
        print('CheckLog: No warnings.')
    
    # Error out if Simplygon has errors. 
    if hasErrors:
        raise Exception('Processing failed with an error')

def RunReduction(sg: Simplygon.ISimplygon):
    # Load scene to process.     
    print("Load scene to process.")
    sgScene = LoadScene(sg, 'airplane/11805_airplane_v2_L2.obj')
    
    # Create the reduction processor. 
    sgReductionProcessor = sg.CreateReductionProcessor()
    sgReductionProcessor.SetScene( sgScene )
    sgReductionSettings = sgReductionProcessor.GetReductionSettings()
    
    # Set reduction target to triangle ratio with a ratio of 50%. 
    sgReductionSettings.SetReductionTargets( Simplygon.EStopCondition_All, True, False, False, False )
    sgReductionSettings.SetReductionTargetTriangleRatio( 0.5 )
    
    # Start the reduction process.     
    print("Start the reduction process.")
    sgReductionProcessor.RunProcessing()
    
    # Save processed scene.     
    print("Save processed scene.")
    SaveScene(sg, sgScene, 'Output.fbx')
    
    # Check log for any warnings or errors.     
    print("Check log for any warnings or errors.")
    CheckLog(sg)

if __name__ == '__main__':
        sg = simplygon_loader.init_simplygon()
        if sg is None:
            exit(Simplygon.GetLastInitializationError())

        RunReduction(sg)

        sg = None
        gc.collect()

运行结果如下:
在这里插入图片描述
输出简化模型如下:
在这里插入图片描述

结语

如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;╮( ̄▽ ̄)╭
如果您感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进;o_O???
如果您需要相关功能的代码定制化开发,可以留言私信作者;(✿◡‿◡)
感谢各位大佬童鞋们的支持!( ´ ▽´ )ノ ( ´ ▽´)っ!!!

猜你喜欢

转载自blog.csdn.net/hhy321/article/details/135119653