Skip to content

Commit

Permalink
Add support for velocity to prepass output block (#16191)
Browse files Browse the repository at this point in the history
  • Loading branch information
Popov72 authored Feb 17, 2025
1 parent 421e300 commit a013140
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class PrePassOutputBlock extends NodeMaterialBlock {
this.registerInput("viewNormal", NodeMaterialBlockConnectionPointTypes.AutoDetect, true);
this.registerInput("worldNormal", NodeMaterialBlockConnectionPointTypes.AutoDetect, true);
this.registerInput("reflectivity", NodeMaterialBlockConnectionPointTypes.AutoDetect, true);
this.registerInput("velocity", NodeMaterialBlockConnectionPointTypes.AutoDetect, true);
this.registerInput("velocityLinear", NodeMaterialBlockConnectionPointTypes.AutoDetect, true);

this.inputs[2].addExcludedConnectionPointFromAllowedTypes(NodeMaterialBlockConnectionPointTypes.Vector3 | NodeMaterialBlockConnectionPointTypes.Vector4);
this.inputs[3].addExcludedConnectionPointFromAllowedTypes(NodeMaterialBlockConnectionPointTypes.Vector3 | NodeMaterialBlockConnectionPointTypes.Vector4);
Expand All @@ -36,6 +38,8 @@ export class PrePassOutputBlock extends NodeMaterialBlock {
NodeMaterialBlockConnectionPointTypes.Color3 |
NodeMaterialBlockConnectionPointTypes.Color4
);
this.inputs[7].addExcludedConnectionPointFromAllowedTypes(NodeMaterialBlockConnectionPointTypes.Vector3 | NodeMaterialBlockConnectionPointTypes.Vector4);
this.inputs[8].addExcludedConnectionPointFromAllowedTypes(NodeMaterialBlockConnectionPointTypes.Vector3 | NodeMaterialBlockConnectionPointTypes.Vector4);
}

/**
Expand Down Expand Up @@ -95,6 +99,20 @@ export class PrePassOutputBlock extends NodeMaterialBlock {
return this._inputs[6];
}

/**
* Gets the velocity component
*/
public get velocity(): NodeMaterialConnectionPoint {
return this._inputs[7];
}

/**
* Gets the linear velocity component
*/
public get velocityLinear(): NodeMaterialConnectionPoint {
return this._inputs[8];
}

private _getFragData(isWebGPU: boolean, index: number) {
return isWebGPU ? `fragmentOutputs.fragData${index}` : `gl_FragData[${index}]`;
}
Expand All @@ -109,6 +127,8 @@ export class PrePassOutputBlock extends NodeMaterialBlock {
const viewDepth = this.viewDepth;
const reflectivity = this.reflectivity;
const screenDepth = this.screenDepth;
const velocity = this.velocity;
const velocityLinear = this.velocityLinear;

state.sharedData.blocksWithDefines.push(this);

Expand Down Expand Up @@ -186,6 +206,26 @@ export class PrePassOutputBlock extends NodeMaterialBlock {
state.compilationString += ` fragData[PREPASS_REFLECTIVITY_INDEX] = ${vec4}(0.0, 0.0, 0.0, 1.0);\r\n`;
}
state.compilationString += `#endif\r\n`;
state.compilationString += `#ifdef PREPASS_VELOCITY\r\n`;
if (velocity.connectedPoint) {
state.compilationString += ` fragData[PREPASS_VELOCITY_INDEX] = ${vec4}(${velocity.associatedVariableName}.rgb, ${
velocity.connectedPoint.type === NodeMaterialBlockConnectionPointTypes.Vector4 ? velocity.associatedVariableName + ".a" : "1.0"
});\r\n`;
} else {
// We have to write something on the reflectivity output or it will raise a gl error
state.compilationString += ` fragData[PREPASS_VELOCITY_INDEX] = ${vec4}(0.0, 0.0, 0.0, 1.0);\r\n`;
}
state.compilationString += `#endif\r\n`;
state.compilationString += `#ifdef PREPASS_VELOCITY_LINEAR\r\n`;
if (velocityLinear.connectedPoint) {
state.compilationString += ` fragData[PREPASS_VELOCITY_LINEAR_INDEX] = ${vec4}(${velocityLinear.associatedVariableName}.rgb, ${
velocityLinear.connectedPoint.type === NodeMaterialBlockConnectionPointTypes.Vector4 ? velocityLinear.associatedVariableName + ".a" : "1.0"
});\r\n`;
} else {
// We have to write something on the reflectivity output or it will raise a gl error
state.compilationString += ` fragData[PREPASS_VELOCITY_LINEAR_INDEX] = ${vec4}(0.0, 0.0, 0.0, 1.0);\r\n`;
}
state.compilationString += `#endif\r\n`;

state.compilationString += `#if SCENE_MRT_COUNT > 1\r\n`;
state.compilationString += `${this._getFragData(isWebGPU, 1)} = fragData[1];\r\n`;
Expand Down
16 changes: 16 additions & 0 deletions packages/dev/core/src/Materials/Node/nodeMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,22 @@ export class NodeMaterial extends PushMaterial {
result.push(Constants.PREPASS_POSITION_TEXTURE_TYPE);
}

if (prePassOutputBlock.localPosition.isConnected) {
result.push(Constants.PREPASS_LOCAL_POSITION_TEXTURE_TYPE);
}

if (prePassOutputBlock.reflectivity.isConnected) {
result.push(Constants.PREPASS_REFLECTIVITY_TEXTURE_TYPE);
}

if (prePassOutputBlock.velocity.isConnected) {
result.push(Constants.PREPASS_VELOCITY_TEXTURE_TYPE);
}

if (prePassOutputBlock.velocityLinear.isConnected) {
result.push(Constants.PREPASS_VELOCITY_LINEAR_TEXTURE_TYPE);
}

return result;
}

Expand Down

0 comments on commit a013140

Please sign in to comment.