Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add comment #1447

Merged
merged 2 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 57 additions & 4 deletions src/foundation/materials/core/ShaderGraphResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ import { SinShaderNode } from '../nodes/SinShaderNode';
import { StepShaderNode } from '../nodes/StepShaderNode';
import { TimeShaderNode } from '../nodes/TimeShaderNode';

/**
* ShaderGraphResolver is a class that resolves the shader node graph and generates shader code.
*/
export class ShaderGraphResolver {
/**
* Create a vertex shader code from the given vertex nodes.
* @param vertexNodes - Vertex nodes
* @param varyingNodes - Varying nodes
* @param isFullVersion - Whether to generate a full version of the shader code
* @returns Vertex shader code
*/
static createVertexShaderCode(
vertexNodes: AbstractShaderNode[],
varyingNodes: AbstractShaderNode[],
Expand Down Expand Up @@ -70,7 +80,7 @@ export class ShaderGraphResolver {
let shaderBody = '';

// function definitions
shaderBody += ShaderGraphResolver.getFunctionDefinition(
shaderBody += ShaderGraphResolver.__getFunctionDefinition(
// sortedShaderNodes,
sortedShaderNodes.concat(varyingNodes.filter((node) => node.getShaderStage() !== 'Fragment')),
ShaderType.VertexShader
Expand All @@ -89,6 +99,13 @@ export class ShaderGraphResolver {
return shader;
}

/**
* Create a pixel shader code from the given pixel nodes.
*
* @param pixelNodes - Pixel nodes
* @param isFullVersion - Whether to generate a full version of the shader code
* @returns Pixel shader code
*/
static createPixelShaderCode(pixelNodes: AbstractShaderNode[], isFullVersion: boolean = true) {
const shaderNodes = pixelNodes.concat();

Expand All @@ -108,7 +125,7 @@ export class ShaderGraphResolver {
let shaderBody = '';

// function definitions
shaderBody += ShaderGraphResolver.getFunctionDefinition(
shaderBody += ShaderGraphResolver.__getFunctionDefinition(
sortedShaderNodes.filter((node) => node.getShaderStage() !== 'Vertex'),
ShaderType.PixelShader
);
Expand Down Expand Up @@ -144,6 +161,12 @@ export class ShaderGraphResolver {
return true;
}

/**
* Sort shader nodes topologically.
*
* @param shaderNodes - Shader nodes to sort
* @returns Sorted shader nodes
*/
private static __sortTopologically(shaderNodes: AbstractShaderNode[]) {
const sortedNodeArray: AbstractShaderNode[] = [];
const inputNumArray: Index[] = [];
Expand Down Expand Up @@ -210,7 +233,17 @@ export class ShaderGraphResolver {
return sortedNodeArray;
}

static getFunctionDefinition(shaderNodes: AbstractShaderNode[], shaderType: ShaderTypeEnum) {
/**
* Get function definition from shader nodes.
*
* @param shaderNodes - Shader nodes
* @param shaderType - Shader type
* @returns Function definition as a string
*/
private static __getFunctionDefinition(
shaderNodes: AbstractShaderNode[],
shaderType: ShaderTypeEnum
) {
let shaderText = '';
const existVertexFunctions: string[] = [];
for (let i = 0; i < shaderNodes.length; i++) {
Expand All @@ -225,6 +258,14 @@ export class ShaderGraphResolver {
return shaderText;
}

/**
* Construct shader code with shader nodes.
*
* @param shaderNodes - Shader nodes
* @param isVertexStage - Whether the shader is a vertex shader
* @param isFullVersion - Whether to generate a full version of the shader code
* @returns Shader code
*/
private static __constructShaderWithNodes(
shaderNodes: AbstractShaderNode[],
isVertexStage: boolean,
Expand Down Expand Up @@ -413,6 +454,12 @@ export class ShaderGraphResolver {
return shaderBody;
}

/**
* Generate shader code from JSON.
*
* @param json - JSON data of a shader node graph
* @returns Shader code
*/
public static generateShaderCodeFromJson(
json: any
): { vertexShader: string; pixelShader: string } | undefined {
Expand Down Expand Up @@ -561,7 +608,13 @@ function filterNodesForVarying(nodes: AbstractShaderNode[], endNodeName: string)
return varyingNodes.reverse();
}

export default function constructNodes(json: any) {
/**
* Construct shader nodes from JSON.
*
* @param json - JSON data of a shader node graph
* @returns Constructed shader nodes
*/
function constructNodes(json: any) {
// Create Node Instances
const nodeInstances: Record<number, AbstractShaderNode> = {};
const nodes: Record<string, any> = {};
Expand Down
3 changes: 2 additions & 1 deletion src/webgl/WebGLStrategyUniform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import { ModuleManager } from '../foundation/system/ModuleManager';
import { RnXR } from '../xr/main';
import { WebXRSystem } from '../xr/WebXRSystem';
import { Vector2 } from '../foundation/math/Vector2';
import { AnimationComponent, Scalar } from '../foundation';
import { AnimationComponent } from '../foundation/components/Animation/AnimationComponent';
import { Scalar } from '../foundation/math/Scalar';

declare const spector: any;

Expand Down
Loading