mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2025-01-14 23:11:06 +00:00
18 lines
35 KiB
HTML
18 lines
35 KiB
HTML
<!doctype html>
|
||
<html lang="zh" dir="ltr" class="docs-wrapper docs-doc-page docs-version-1.0.0 plugin-docs plugin-id-default docs-doc-id-user-guide/multi-render/multi-material">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||
<meta name="generator" content="Docusaurus v2.0.0-beta.20">
|
||
<link rel="alternate" type="application/rss+xml" href="/cocos-service-pack/blog/rss.xml" title="Cocos Service Pack RSS Feed">
|
||
<link rel="alternate" type="application/atom+xml" href="/cocos-service-pack/blog/atom.xml" title="Cocos Service Pack Atom Feed"><title data-rh="true">多纹理材质 | Cocos Service Pack</title><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:url" content="https://smallmain.github.io/cocos-service-pack/docs/user-guide/multi-render/multi-material"><meta data-rh="true" name="docusaurus_locale" content="zh"><meta data-rh="true" name="docsearch:language" content="zh"><meta data-rh="true" name="docusaurus_version" content="1.0.0"><meta data-rh="true" name="docusaurus_tag" content="docs-default-1.0.0"><meta data-rh="true" name="docsearch:version" content="1.0.0"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-default-1.0.0"><meta data-rh="true" property="og:title" content="多纹理材质 | Cocos Service Pack"><meta data-rh="true" name="description" content="了解如何手动管理多纹理材质。"><meta data-rh="true" property="og:description" content="了解如何手动管理多纹理材质。"><link data-rh="true" rel="icon" href="/cocos-service-pack/img/favicon.ico"><link data-rh="true" rel="canonical" href="https://smallmain.github.io/cocos-service-pack/docs/user-guide/multi-render/multi-material"><link data-rh="true" rel="alternate" href="https://smallmain.github.io/cocos-service-pack/docs/user-guide/multi-render/multi-material" hreflang="zh"><link data-rh="true" rel="alternate" href="https://smallmain.github.io/cocos-service-pack/docs/user-guide/multi-render/multi-material" hreflang="x-default"><link rel="stylesheet" href="/cocos-service-pack/assets/css/styles.0d2d44fc.css">
|
||
<link rel="preload" href="/cocos-service-pack/assets/js/runtime~main.c235128b.js" as="script">
|
||
<link rel="preload" href="/cocos-service-pack/assets/js/main.a60a3891.js" as="script">
|
||
</head>
|
||
<body class="navigation-with-keyboard">
|
||
<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}()</script><div id="__docusaurus">
|
||
<div role="region"><a href="#" class="skipToContent_ZgBM">跳到主要内容</a></div><nav class="navbar navbar--fixed-top"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Navigation bar toggle" class="navbar__toggle clean-btn" type="button" tabindex="0"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/cocos-service-pack/"><div class="navbar__logo"><img src="/cocos-service-pack/img/logo.png" alt="Cocos Service Pack" class="themedImage_W2Cr themedImage--light_TfLj"><img src="/cocos-service-pack/img/logo.png" alt="Cocos Service Pack" class="themedImage_W2Cr themedImage--dark_oUvU"></div><b class="navbar__title text--truncate">Cocos Service Pack</b></a></div><div class="navbar__items navbar__items--right"><a class="navbar__item navbar__link" href="/cocos-service-pack/">首页</a><a class="navbar__item navbar__link navbar__link--active" href="/cocos-service-pack/docs/intro">文档</a><a href="https://smallmain.github.io/cocos-service-pack/demo/v1.0.0/web-desktop/index.html" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">演示</a><a class="navbar__item navbar__link" href="/cocos-service-pack/docs/intro">v1.0.0</a><a href="https://github.com/smallmain/cocos-service-pack" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link header-github-link">加星鼓励</a><div class="searchBox_dLyj"><div class="navbar__search searchBarContainer_NW3z"><input placeholder="搜索" aria-label="Search" class="navbar__search-input"><div class="loadingRing_RJI3 searchBarLoadingRing_YnHq"><div></div><div></div><div></div><div></div></div><div class="searchHintContainer_Pkmr"><kbd class="searchHint_iIMx">ctrl</kbd><kbd class="searchHint_iIMx">K</kbd></div></div></div><div class="toggle_S7eR colorModeToggle_vKtC"><button class="clean-btn toggleButton_rCf9 toggleButtonDisabled_Pu9x" type="button" disabled="" title="切换浅色/暗黑模式(当前为浅色模式)" aria-label="切换浅色/暗黑模式(当前为浅色模式)"><svg viewBox="0 0 24 24" width="24" height="24" class="lightToggleIcon_v35p"><path fill="currentColor" d="M12,9c1.65,0,3,1.35,3,3s-1.35,3-3,3s-3-1.35-3-3S10.35,9,12,9 M12,7c-2.76,0-5,2.24-5,5s2.24,5,5,5s5-2.24,5-5 S14.76,7,12,7L12,7z M2,13l2,0c0.55,0,1-0.45,1-1s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S1.45,13,2,13z M20,13l2,0c0.55,0,1-0.45,1-1 s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S19.45,13,20,13z M11,2v2c0,0.55,0.45,1,1,1s1-0.45,1-1V2c0-0.55-0.45-1-1-1S11,1.45,11,2z M11,20v2c0,0.55,0.45,1,1,1s1-0.45,1-1v-2c0-0.55-0.45-1-1-1C11.45,19,11,19.45,11,20z M5.99,4.58c-0.39-0.39-1.03-0.39-1.41,0 c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0s0.39-1.03,0-1.41L5.99,4.58z M18.36,16.95 c-0.39-0.39-1.03-0.39-1.41,0c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0c0.39-0.39,0.39-1.03,0-1.41 L18.36,16.95z M19.42,5.99c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06c-0.39,0.39-0.39,1.03,0,1.41 s1.03,0.39,1.41,0L19.42,5.99z M7.05,18.36c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06 c-0.39,0.39-0.39,1.03,0,1.41s1.03,0.39,1.41,0L7.05,18.36z"></path></svg><svg viewBox="0 0 24 24" width="24" height="24" class="darkToggleIcon_nQuB"><path fill="currentColor" d="M9.37,5.51C9.19,6.15,9.1,6.82,9.1,7.5c0,4.08,3.32,7.4,7.4,7.4c0.68,0,1.35-0.09,1.99-0.27C17.45,17.19,14.93,19,12,19 c-3.86,0-7-3.14-7-7C5,9.07,6.81,6.55,9.37,5.51z M12,3c-4.97,0-9,4.03-9,9s4.03,9,9,9s9-4.03,9-9c0-0.46-0.04-0.92-0.1-1.36 c-0.98,1.37-2.58,2.26-4.4,2.26c-2.98,0-5.4-2.42-5.4-5.4c0-1.81,0.89-3.42,2.26-4.4C12.92,3.04,12.46,3,12,3L12,3z"></path></svg></button></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div class="main-wrapper docsWrapper_mKqt"><button aria-label="回到顶部" class="clean-btn theme-back-to-top-button backToTopButton_RiI4" type="button"></button><div class="docPage_ualW"><aside class="theme-doc-sidebar-container docSidebarContainer_UQUJ"><div class="sidebar_RiAD"><nav class="menu thin-scrollbar menu_izAj"><ul class="theme-doc-sidebar-menu menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-1 menu__list-item"><a class="menu__link" href="/cocos-service-pack/docs/intro">介绍</a></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist" aria-expanded="true" href="/cocos-service-pack/docs/installation-guide/installation-intro">安装指南</a><button aria-label="打开/收起侧边栏菜单「安装指南」" type="button" class="clean-btn menu__caret"></button></div><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/cocos-service-pack/docs/installation-guide/installation-engine-plugin">使用引擎扩展安装</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/cocos-service-pack/docs/installation-guide/installation-manual">手动安装</a></li></ul></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist" aria-expanded="true" href="/cocos-service-pack/docs/start-guide/start-guide-intro">入门教程</a><button aria-label="打开/收起侧边栏菜单「入门教程」" type="button" class="clean-btn menu__caret"></button></div><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/cocos-service-pack/docs/start-guide/batcher-guide">新 UI 渲染批次合并指南</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/cocos-service-pack/docs/start-guide/advance-batcher-guide">进阶合批指南</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/cocos-service-pack/docs/start-guide/new-features">上手其它新特性</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/cocos-service-pack/docs/start-guide/breaking-change">破坏性变更</a></li></ul></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--active" aria-expanded="true" href="/cocos-service-pack/docs/user-guide/user-guide-intro">使用指南</a><button aria-label="打开/收起侧边栏菜单「使用指南」" type="button" class="clean-btn menu__caret"></button></div><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-2 menu__list-item"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--active" aria-expanded="true" tabindex="0" href="/cocos-service-pack/docs/user-guide/multi-render/multi-render-intro">多纹理渲染</a><button aria-label="打开/收起侧边栏菜单「多纹理渲染」" type="button" class="clean-btn menu__caret"></button></div><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link menu__link--active" aria-current="page" tabindex="0" href="/cocos-service-pack/docs/user-guide/multi-render/multi-material">多纹理材质</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-3 menu__list-item"><a class="menu__link" tabindex="0" href="/cocos-service-pack/docs/user-guide/multi-render/multi-batcher">多纹理合批</a></li></ul></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-2 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist" aria-expanded="false" tabindex="0" href="/cocos-service-pack/docs/user-guide/text-render/text-render-intro">文本渲染</a><button aria-label="打开/收起侧边栏菜单「文本渲染」" type="button" class="clean-btn menu__caret"></button></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-2 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist" aria-expanded="false" tabindex="0" href="/cocos-service-pack/docs/user-guide/dynamic-batcher/dynamic-batcher-intro">动态合图</a><button aria-label="打开/收起侧边栏菜单「动态合图」" type="button" class="clean-btn menu__caret"></button></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-2 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist" aria-expanded="false" tabindex="0" href="/cocos-service-pack/docs/user-guide/spine/spine-intro">Spine</a><button aria-label="打开/收起侧边栏菜单「Spine」" type="button" class="clean-btn menu__caret"></button></div></li></ul></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist" aria-expanded="false" href="/cocos-service-pack/docs/theory-guide/theory-guide-intro">原理文档</a><button aria-label="打开/收起侧边栏菜单「原理文档」" type="button" class="clean-btn menu__caret"></button></div></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-1 menu__list-item"><a class="menu__link" href="/cocos-service-pack/docs/uninstall-guide">卸载指南</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-1 menu__list-item"><a class="menu__link" href="/cocos-service-pack/docs/update-log">更新日志</a></li></ul></nav></div></aside><main class="docMainContainer_uL0j"><div class="container padding-top--md padding-bottom--lg"><div class="row"><div class="col docItemCol_DM6M"><div class="docItemContainer_vinB"><article><nav class="theme-doc-breadcrumbs breadcrumbsContainer_Xlws" aria-label="页面路径"><ul class="breadcrumbs" itemscope="" itemtype="https://schema.org/BreadcrumbList"><li class="breadcrumbs__item"><a aria-label="主页面" class="breadcrumbs__link" href="/cocos-service-pack/"><svg viewBox="0 0 24 24" class="breadcrumbHomeIcon_kU5B"><path d="M10 19v-5h4v5c0 .55.45 1 1 1h3c.55 0 1-.45 1-1v-7h1.7c.46 0 .68-.57.33-.87L12.67 3.6c-.38-.34-.96-.34-1.34 0l-8.36 7.53c-.34.3-.13.87.33.87H5v7c0 .55.45 1 1 1h3c.55 0 1-.45 1-1z" fill="currentColor"></path></svg></a></li><li itemscope="" itemprop="itemListElement" itemtype="https://schema.org/ListItem" class="breadcrumbs__item"><a class="breadcrumbs__link" itemprop="item" href="/cocos-service-pack/docs/user-guide/user-guide-intro"><span itemprop="name">使用指南</span></a><meta itemprop="position" content="1"></li><li itemscope="" itemprop="itemListElement" itemtype="https://schema.org/ListItem" class="breadcrumbs__item"><a class="breadcrumbs__link" itemprop="item" href="/cocos-service-pack/docs/user-guide/multi-render/multi-render-intro"><span itemprop="name">多纹理渲染</span></a><meta itemprop="position" content="2"></li><li itemscope="" itemprop="itemListElement" itemtype="https://schema.org/ListItem" class="breadcrumbs__item breadcrumbs__item--active"><span class="breadcrumbs__link" itemprop="name">多纹理材质</span><meta itemprop="position" content="3"></li></ul></nav><div class="tocCollapsible_bZGK theme-doc-toc-mobile tocMobile_TmEX"><button type="button" class="clean-btn tocCollapsibleButton_l22C">本页总览</button></div><div class="theme-doc-markdown markdown"><h1>多纹理材质</h1><p>服务包在引擎内置了可以直接使用的多纹理 Effect 着色器资源,并且你可以直接使用多纹理材质而不需要编写任何代码。</p><hr><h2 class="anchor anchorWithStickyNavbar_mojV" id="创建多纹理材质">创建多纹理材质<a class="hash-link" href="#创建多纹理材质" title="标题的直接链接"></a></h2><p>你可以正常创建一个材质文件,Effect 选择内置的多纹理 Effect 着色器 <code>multi-2d-universal</code> 即可。</p><p><img loading="lazy" alt="material-settings" src="/cocos-service-pack/assets/images/material-settings-a3019fbf351b67f8a041d16edc95e2fd.png" width="740" height="1224" class="img_E7b_"></p><p>勾选 <code>USE_MULTI_TEXTURE</code> 后可以看到上面有 <code>texture</code> - <code>texture8</code> 一共 8 个纹理插槽,将需要使用的纹理拖到上面的插槽即可完成多纹理材质的配置。</p><hr><h2 class="anchor anchorWithStickyNavbar_mojV" id="在组件中使用多纹理材质">在组件中使用多纹理材质<a class="hash-link" href="#在组件中使用多纹理材质" title="标题的直接链接"></a></h2><p>直接拖到组件的 <code>Materials</code> 属性上即可。</p><p><img loading="lazy" alt="material-comp" src="/cocos-service-pack/assets/images/material-comp-f59c86aeb8d92ad635a121ddffd9b0b4.png" width="1446" height="902" class="img_E7b_"></p><p>注意上图中已经在 <code>cc.Sprite</code> 组件拖入了刚刚创建的多纹理材质,并且其 <code>SpriteFrame</code> 属性设置的是材质中 <code>texture3</code> 插槽中的纹理。</p><p><img loading="lazy" alt="material-final" src="/cocos-service-pack/assets/images/material-final-35fbe06248d9d966d8898853fb663fe1.png" width="3642" height="1082" class="img_E7b_"></p><p>你不需要指定组件要使用的纹理插槽 id,组件内部在渲染前会自动查找纹理在材质中的 id。</p><div class="admonition admonition-caution alert alert--warning"><div class="admonition-heading"><h5><span class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>警告</h5></div><div class="admonition-content"><p>如果组件使用的纹理在材质中找不到,为了保证渲染的正常(毕竟性能只是锦上添花)会在组件的材质变体中将纹理设置到 <code>texture</code> 插槽中。</p><p>这会导致这个组件的多纹理材质变体之后都不能按预期进行合批,就像 “退化” 成了普通材质。</p><p>所以使用时必须要确保纹理在材质中,假如材质变体已经 “退化” 了,那你可以通过重新设置材质的方式使组件持有一个新的材质变体。</p></div></div><div class="admonition admonition-caution alert alert--warning"><div class="admonition-heading"><h5><span class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>特别注意</h5></div><div class="admonition-content"><p>Spine 组件使用多纹理材质时会强制勾选 <code>enableBatch</code>,因为不开启就不能合批,那也就没必要使用多纹理材质。</p></div></div><hr><h2 class="anchor anchorWithStickyNavbar_mojV" id="自定义多纹理材质">自定义多纹理材质<a class="hash-link" href="#自定义多纹理材质" title="标题的直接链接"></a></h2><p>上面介绍的多纹理材质都是使用的内置的多纹理 Effect 着色器,你可以直接在内置多纹理 Effect 着色器的基础上修改。</p><p>除了直接在内置着色器的基础上修改之外,任何着色器中如果存在一个宏 <code>USE_MULTI_TEXTURE = true</code>,则会被认为是多纹理材质。</p><p><a href="https://smallmain.github.io/cocos-service-pack/demo/v1.0.0/web-desktop/index.html" target="_blank" rel="noopener noreferrer">演示项目</a> 中有自定义材质的示范代码。</p><div class="admonition admonition-tip alert alert--success"><div class="admonition-heading"><h5><span class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" width="12" height="16" viewBox="0 0 12 16"><path fill-rule="evenodd" d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>提示</h5></div><div class="admonition-content"><p>是否为多纹理材质的判断逻辑流程:</p><ol><li>获取材质当前使用的 Technique 中的第一个 Pass</li><li>判断这个 Pass 是否 <code>USE_MULTI_TEXTURE = true</code></li><li>是的话,这个材质即为多纹理材质</li></ol><p>在某些情况下通过代码修改材质可能需要调用 <code>material.updateMultiSupport()</code> 来触发这个流程。</p></div></div><hr><h2 class="anchor anchorWithStickyNavbar_mojV" id="通过代码设置纹理插槽">通过代码设置纹理插槽<a class="hash-link" href="#通过代码设置纹理插槽" title="标题的直接链接"></a></h2><p>每个多纹理材质都对应着一个多纹理材质管理器,这是服务包新增的一个工具类,其主要用处是便捷、高性能地管理多纹理材质上面的纹理插槽。</p><p>通过 <code>material.getMultiHandler()</code> 可以获取到管理器实例,请使用这个实例来操作多纹理材质的纹理插槽。</p><p>比如:</p><div class="language-js codeBlockContainer_MPoW theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_B9tL"><pre tabindex="0" class="prism-code language-js codeBlock__0OG thin-scrollbar"><code class="codeBlockLines_gEuF"><span class="token-line" style="color:#393A34"><span class="token comment" style="color:#999988;font-style:italic">// 获取管理器实例</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token keyword" style="color:#00009f">const</span><span class="token plain"> handler </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> material</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">getMultiHandler</span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token comment" style="color:#999988;font-style:italic">// 设置 `texture` 纹理插槽</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">handler</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">setTexture</span><span class="token punctuation" style="color:#393A34">(</span><span class="token number" style="color:#36acaa">0</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> texture</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token comment" style="color:#999988;font-style:italic">// 置空 `texture2` 纹理插槽</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">handler</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">setTexture</span><span class="token punctuation" style="color:#393A34">(</span><span class="token number" style="color:#36acaa">1</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> </span><span class="token keyword null nil" style="color:#00009f">null</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token comment" style="color:#999988;font-style:italic">// 直接移除指定纹理</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">handler</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">removeTexture</span><span class="token punctuation" style="color:#393A34">(</span><span class="token plain">texture</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">getImpl</span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token comment" style="color:#999988;font-style:italic">// 将纹理自动设置到材质的空插槽</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">handler</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">autoSetTexture</span><span class="token punctuation" style="color:#393A34">(</span><span class="token plain">texture</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup_hRr1"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_W9eQ" aria-hidden="true"><svg class="copyButtonIcon_XEyF" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_i9w9" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>从上面的代码中可以看出操作纹理插槽的时候并不是传入插槽的名称,而是需要提供下标。</p><p>下标 <code>0</code> - <code>7</code> 分别对应着名称为 <code>texture</code> - <code>texture8</code> 的插槽。</p><p>可以使用这两个函数进行转换:</p><div class="language-js codeBlockContainer_MPoW theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_B9tL"><pre tabindex="0" class="prism-code language-js codeBlock__0OG thin-scrollbar"><code class="codeBlockLines_gEuF"><span class="token-line" style="color:#393A34"><span class="token comment" style="color:#999988;font-style:italic">// index to name</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">cc</span><span class="token punctuation" style="color:#393A34">.</span><span class="token property-access">sp</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">propertyIndex2Name</span><span class="token punctuation" style="color:#393A34">(</span><span class="token number" style="color:#36acaa">0</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">// return: "texture"</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token comment" style="color:#999988;font-style:italic">// name to index</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">cc</span><span class="token punctuation" style="color:#393A34">.</span><span class="token property-access">sp</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">propertyName2Index</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"texture"</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">// return: 0</span><br></span></code></pre><div class="buttonGroup_hRr1"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_W9eQ" aria-hidden="true"><svg class="copyButtonIcon_XEyF" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_i9w9" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><div class="admonition admonition-caution alert alert--warning"><div class="admonition-heading"><h5><span class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>注意</h5></div><div class="admonition-content"><p>注意区分材质 <code>cc.Material</code> 与材质变体 <code>cc.MaterialVariant</code>。</p></div></div><div class="admonition admonition-caution alert alert--warning"><div class="admonition-heading"><h5><span class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>警告</h5></div><div class="admonition-content"><p>请勿直接通过材质原始的 <code>setProperty</code> 接口修改多纹理材质的纹理插槽。</p><p>如果你必须这么做,需要调用 <code>material.getMultiHandler().syncTextures()</code> 来同步插槽数据到 <code>MultiHandler</code> 上。</p></div></div><hr><h2 class="anchor anchorWithStickyNavbar_mojV" id="强制设置材质的类型">强制设置材质的类型<a class="hash-link" href="#强制设置材质的类型" title="标题的直接链接"></a></h2><p>如果你想将某个材质强制视为多纹理材质或非多纹理材质,可以:</p><div class="language-js codeBlockContainer_MPoW theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_B9tL"><pre tabindex="0" class="prism-code language-js codeBlock__0OG thin-scrollbar"><code class="codeBlockLines_gEuF"><span class="token-line" style="color:#393A34"><span class="token comment" style="color:#999988;font-style:italic">// 视为多纹理材质</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">material</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">setMultiSupport</span><span class="token punctuation" style="color:#393A34">(</span><span class="token boolean" style="color:#36acaa">true</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token comment" style="color:#999988;font-style:italic">// 视为非多纹理材质</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">material</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">setMultiSupport</span><span class="token punctuation" style="color:#393A34">(</span><span class="token boolean" style="color:#36acaa">false</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup_hRr1"><button type="button" aria-label="复制代码到剪贴板" title="复制" class="clean-btn"><span class="copyButtonIcons_W9eQ" aria-hidden="true"><svg class="copyButtonIcon_XEyF" viewBox="0 0 24 24"><path d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg class="copyButtonSuccessIcon_i9w9" viewBox="0 0 24 24"><path d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>没有特殊情况不需要这么做。</p></div></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="文档分页导航"><a class="pagination-nav__link pagination-nav__link--prev" href="/cocos-service-pack/docs/user-guide/multi-render/multi-render-intro"><div class="pagination-nav__sublabel">上一页</div><div class="pagination-nav__label">多纹理渲染</div></a><a class="pagination-nav__link pagination-nav__link--next" href="/cocos-service-pack/docs/user-guide/multi-render/multi-batcher"><div class="pagination-nav__sublabel">下一页</div><div class="pagination-nav__label">多纹理合批</div></a></nav></div></div><div class="col col--3"><div class="tableOfContents_cNA8 thin-scrollbar theme-doc-toc-desktop"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#创建多纹理材质" class="table-of-contents__link toc-highlight">创建多纹理材质</a></li><li><a href="#在组件中使用多纹理材质" class="table-of-contents__link toc-highlight">在组件中使用多纹理材质</a></li><li><a href="#自定义多纹理材质" class="table-of-contents__link toc-highlight">自定义多纹理材质</a></li><li><a href="#通过代码设置纹理插槽" class="table-of-contents__link toc-highlight">通过代码设置纹理插槽</a></li><li><a href="#强制设置材质的类型" class="table-of-contents__link toc-highlight">强制设置材质的类型</a></li></ul></div></div></div></div></main></div></div></div>
|
||
<script src="/cocos-service-pack/assets/js/runtime~main.c235128b.js"></script>
|
||
<script src="/cocos-service-pack/assets/js/main.a60a3891.js"></script>
|
||
</body>
|
||
</html> |