diff --git a/extensions/shader-graph/package.json b/extensions/shader-graph/package.json index a67248a..5563771 100755 --- a/extensions/shader-graph/package.json +++ b/extensions/shader-graph/package.json @@ -32,6 +32,7 @@ "@types/fs-extra": "^9.0.5", "@types/node": "^16.0.1", "adm-zip": "^0.5.10", + "globby": "^8.0.2", "less": "^4.2.0", "ts-node": "^10.9.1", "tslib": "^2.5.3", diff --git a/extensions/shader-graph/src/block-forge/block.ts b/extensions/shader-graph/src/block-forge/block.ts index 2a36802..9b5fe79 100644 --- a/extensions/shader-graph/src/block-forge/block.ts +++ b/extensions/shader-graph/src/block-forge/block.ts @@ -400,6 +400,10 @@ export function removeDeclareBlock(type: string) { // @ts-ignore window.removeDeclareBlock = removeDeclareBlock; +export function removeAllDeclareBlock() { + blockMap.clear(); +} + /** * 注册一个 block 类型 * @param block diff --git a/extensions/shader-graph/src/block-forge/forge.ts b/extensions/shader-graph/src/block-forge/forge.ts index 67719e2..e59bc82 100644 --- a/extensions/shader-graph/src/block-forge/forge.ts +++ b/extensions/shader-graph/src/block-forge/forge.ts @@ -353,6 +353,7 @@ export class HTMLGraphForgeElement extends HTMLElement { const $svg = $graph.shadowRoot.querySelector('#lines')!; function searchG(htmlArray: (HTMLElement | SVGGElement)[]) { + if (!htmlArray) return; const length = Math.min(htmlArray.length, 4); for (let i = 0; i < length; i++) { const $elem = htmlArray[i]; diff --git a/extensions/shader-graph/src/block-forge/index.ts b/extensions/shader-graph/src/block-forge/index.ts index 5990b06..5bdf5b3 100644 --- a/extensions/shader-graph/src/block-forge/index.ts +++ b/extensions/shader-graph/src/block-forge/index.ts @@ -18,6 +18,7 @@ export { hasDeclareBlock, getDeclareBlock, removeDeclareBlock, + removeAllDeclareBlock, } from './block'; export { diff --git a/extensions/shader-graph/src/contributions/scene.ts b/extensions/shader-graph/src/contributions/scene.ts index 4c0a906..09781a8 100644 --- a/extensions/shader-graph/src/contributions/scene.ts +++ b/extensions/shader-graph/src/contributions/scene.ts @@ -25,6 +25,13 @@ function createSlot(slot: SlotDefine) { } exports.methods = { + /** + * 注册后需要让场景进程同步一下节点数据 + */ + afterDeclared() { + shaderGraph.reset(); + }, + async queryShaderNode() { const { shaderNodeMap, shaderPropertyMap } = await Editor.Module.importProjectModule('db://shader-graph/graph/index.ts') as IModuleOptions; diff --git a/extensions/shader-graph/src/importer/shader-graph.ts b/extensions/shader-graph/src/importer/shader-graph.ts index 14abf44..d157a15 100644 --- a/extensions/shader-graph/src/importer/shader-graph.ts +++ b/extensions/shader-graph/src/importer/shader-graph.ts @@ -51,6 +51,11 @@ export class ShaderGraph { public cacheSourceMap: Map = new Map(); _initedGraph = false; + + reset() { + this._initedGraph = false; + } + async initGraph() { if (this._initedGraph && this.shaderContext?.shaderTemplatesDir) { return; diff --git a/extensions/shader-graph/src/shader-graph/declare/block.ts b/extensions/shader-graph/src/shader-graph/declare/block.ts index 0d6a7a0..b51f84c 100644 --- a/extensions/shader-graph/src/shader-graph/declare/block.ts +++ b/extensions/shader-graph/src/shader-graph/declare/block.ts @@ -2,7 +2,7 @@ import type { BlockTemplateData } from '../internal'; import type { BlockData, IBlockDescription, IPinDescription, PinData } from '../../block-forge/interface'; import type { PropertyDefine, NodeDefine, SlotDefine } from '../../../@types/shader-node-type'; -import { declareBlock, declareEnum, declareDynamicEnumToType } from '../../block-forge'; +import { declareBlock, declareEnum, declareDynamicEnumToType, removeAllDeclareBlock } from '../../block-forge'; import { generatePinID } from '../utils'; type SlotTag = 'input' | 'out' | 'prop'; @@ -171,6 +171,7 @@ export function declareShaderNodeBlock(shaderNodeMap: Map) { // 清空缓存 normalBlockCacheMap.clear(); pinMap.clear(); + removeAllDeclareBlock(); for (const [blockType, item] of shaderNodeMap) { const inputPins: PinData[] = []; const inputPinDescriptions: IPinDescription[] = []; diff --git a/extensions/shader-graph/src/shader-graph/declare/index.ts b/extensions/shader-graph/src/shader-graph/declare/index.ts index db81cd0..cad93e1 100644 --- a/extensions/shader-graph/src/shader-graph/declare/index.ts +++ b/extensions/shader-graph/src/shader-graph/declare/index.ts @@ -28,6 +28,7 @@ async function declareGraphBlock() { applyBlockToMenu(); MessageMgr.Instance.send(MessageType.Declared); + MessageMgr.Instance.callSceneMethod('afterDeclared'); } function iteratePropertyDefines(handle: (define: PropertyDefine) => void) {