-
-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feat/shaderloader' into feat/editor_module
- Loading branch information
Showing
12 changed files
with
229 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
attribute vec3 POSITION; | ||
attribute vec2 TEXCOORD_0; | ||
|
||
varying vec2 v_uv; | ||
uniform vec4 camera_ProjectionParams; | ||
|
||
void main() { | ||
gl_Position = vec4(POSITION, 1.0); | ||
gl_Position.y *= camera_ProjectionParams.x; | ||
|
||
v_uv = TEXCOORD_0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** @internal */ | ||
export class PathUtils { | ||
private static _urlSchema = "files://"; | ||
static shaderIncludeRegex = /\s#include\s+"([^\\"]+)"/gm; | ||
|
||
static pathResolve(path: string, base: string): string { | ||
return new URL(path, PathUtils._urlSchema + base).href.substring(PathUtils._urlSchema.length); | ||
} | ||
|
||
static isRelativePath(path: string): boolean { | ||
return path[0] === "."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { | ||
AssetPromise, | ||
LoadItem, | ||
Loader, | ||
ResourceManager, | ||
ShaderFactory, | ||
resourceLoader, | ||
// @ts-ignore | ||
ShaderLib | ||
} from "@galacean/engine-core"; | ||
import { PathUtils } from "./PathUtils"; | ||
|
||
@resourceLoader("ShaderChunk", ["glsl"]) | ||
class ShaderChunkLoader extends Loader<void[]> { | ||
load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<void[]> { | ||
const { virtualPath, url } = item; | ||
const shaderVirtualPath = item.params?.shaderVirtualPath ?? "/"; | ||
const chunkPath = virtualPath ?? new URL(url).pathname; | ||
|
||
return this.request<string>(url, { ...item, type: "text" }).then((code: string) => { | ||
ShaderFactory.registerInclude(chunkPath.substring(1), code); | ||
|
||
return _loadChunksInCode(code, shaderVirtualPath, resourceManager); | ||
}); | ||
} | ||
} | ||
|
||
/** @internal */ | ||
export function _loadChunksInCode( | ||
code: string, | ||
shaderVirtualPath: string, | ||
resourceManager: ResourceManager | ||
): Promise<void[]> { | ||
const shaderChunkPaths: string[] = []; | ||
const matches = code.matchAll(PathUtils.shaderIncludeRegex); | ||
for (const match of matches) { | ||
const chunkPath = PathUtils.pathResolve(match[1], shaderVirtualPath); | ||
if (!ShaderLib[chunkPath.substring(1)]) { | ||
shaderChunkPaths.push(chunkPath); | ||
} | ||
} | ||
|
||
return Promise.all( | ||
shaderChunkPaths.map((chunkPath) => { | ||
return resourceManager.load<void>({ | ||
type: "ShaderChunk", | ||
url: chunkPath, | ||
virtualPath: chunkPath, | ||
params: { shaderVirtualPath } | ||
}); | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
AssetPromise, | ||
AssetType, | ||
LoadItem, | ||
Loader, | ||
ResourceManager, | ||
Shader, | ||
resourceLoader | ||
} from "@galacean/engine-core"; | ||
import { _loadChunksInCode } from "./ShaderChunkLoader"; | ||
|
||
@resourceLoader(AssetType.Shader, ["gs", "gsl"]) | ||
class ShaderLoader extends Loader<Shader> { | ||
private static _builtinRegex = /^\s*\/\/\s*@builtin\s+(\w+)/; | ||
|
||
load(item: LoadItem, resourceManager: ResourceManager): AssetPromise<Shader> { | ||
const { virtualPath, url } = item; | ||
const shaderVirtualPath = virtualPath ?? "/"; | ||
|
||
return this.request<string>(url, { ...item, type: "text" }).then((code: string) => { | ||
const builtinShader = this.getBuiltinShader(code); | ||
// TODO: delete the snippets below when breaking change version released | ||
if (builtinShader) { | ||
return Shader.find(builtinShader); | ||
} | ||
|
||
return _loadChunksInCode(code, shaderVirtualPath, resourceManager).then(() => { | ||
return Shader.create(code); | ||
}); | ||
}); | ||
} | ||
|
||
private getBuiltinShader(code: string) { | ||
const match = code.match(ShaderLoader._builtinRegex); | ||
if (match && match[1]) return match[1]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.