Skip to content

Commit a013140

Browse files
authored
Add support for velocity to prepass output block (#16191)
1 parent 421e300 commit a013140

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

packages/dev/core/src/Materials/Node/Blocks/Fragment/prePassOutputBlock.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export class PrePassOutputBlock extends NodeMaterialBlock {
2525
this.registerInput("viewNormal", NodeMaterialBlockConnectionPointTypes.AutoDetect, true);
2626
this.registerInput("worldNormal", NodeMaterialBlockConnectionPointTypes.AutoDetect, true);
2727
this.registerInput("reflectivity", NodeMaterialBlockConnectionPointTypes.AutoDetect, true);
28+
this.registerInput("velocity", NodeMaterialBlockConnectionPointTypes.AutoDetect, true);
29+
this.registerInput("velocityLinear", NodeMaterialBlockConnectionPointTypes.AutoDetect, true);
2830

2931
this.inputs[2].addExcludedConnectionPointFromAllowedTypes(NodeMaterialBlockConnectionPointTypes.Vector3 | NodeMaterialBlockConnectionPointTypes.Vector4);
3032
this.inputs[3].addExcludedConnectionPointFromAllowedTypes(NodeMaterialBlockConnectionPointTypes.Vector3 | NodeMaterialBlockConnectionPointTypes.Vector4);
@@ -36,6 +38,8 @@ export class PrePassOutputBlock extends NodeMaterialBlock {
3638
NodeMaterialBlockConnectionPointTypes.Color3 |
3739
NodeMaterialBlockConnectionPointTypes.Color4
3840
);
41+
this.inputs[7].addExcludedConnectionPointFromAllowedTypes(NodeMaterialBlockConnectionPointTypes.Vector3 | NodeMaterialBlockConnectionPointTypes.Vector4);
42+
this.inputs[8].addExcludedConnectionPointFromAllowedTypes(NodeMaterialBlockConnectionPointTypes.Vector3 | NodeMaterialBlockConnectionPointTypes.Vector4);
3943
}
4044

4145
/**
@@ -95,6 +99,20 @@ export class PrePassOutputBlock extends NodeMaterialBlock {
9599
return this._inputs[6];
96100
}
97101

102+
/**
103+
* Gets the velocity component
104+
*/
105+
public get velocity(): NodeMaterialConnectionPoint {
106+
return this._inputs[7];
107+
}
108+
109+
/**
110+
* Gets the linear velocity component
111+
*/
112+
public get velocityLinear(): NodeMaterialConnectionPoint {
113+
return this._inputs[8];
114+
}
115+
98116
private _getFragData(isWebGPU: boolean, index: number) {
99117
return isWebGPU ? `fragmentOutputs.fragData${index}` : `gl_FragData[${index}]`;
100118
}
@@ -109,6 +127,8 @@ export class PrePassOutputBlock extends NodeMaterialBlock {
109127
const viewDepth = this.viewDepth;
110128
const reflectivity = this.reflectivity;
111129
const screenDepth = this.screenDepth;
130+
const velocity = this.velocity;
131+
const velocityLinear = this.velocityLinear;
112132

113133
state.sharedData.blocksWithDefines.push(this);
114134

@@ -186,6 +206,26 @@ export class PrePassOutputBlock extends NodeMaterialBlock {
186206
state.compilationString += ` fragData[PREPASS_REFLECTIVITY_INDEX] = ${vec4}(0.0, 0.0, 0.0, 1.0);\r\n`;
187207
}
188208
state.compilationString += `#endif\r\n`;
209+
state.compilationString += `#ifdef PREPASS_VELOCITY\r\n`;
210+
if (velocity.connectedPoint) {
211+
state.compilationString += ` fragData[PREPASS_VELOCITY_INDEX] = ${vec4}(${velocity.associatedVariableName}.rgb, ${
212+
velocity.connectedPoint.type === NodeMaterialBlockConnectionPointTypes.Vector4 ? velocity.associatedVariableName + ".a" : "1.0"
213+
});\r\n`;
214+
} else {
215+
// We have to write something on the reflectivity output or it will raise a gl error
216+
state.compilationString += ` fragData[PREPASS_VELOCITY_INDEX] = ${vec4}(0.0, 0.0, 0.0, 1.0);\r\n`;
217+
}
218+
state.compilationString += `#endif\r\n`;
219+
state.compilationString += `#ifdef PREPASS_VELOCITY_LINEAR\r\n`;
220+
if (velocityLinear.connectedPoint) {
221+
state.compilationString += ` fragData[PREPASS_VELOCITY_LINEAR_INDEX] = ${vec4}(${velocityLinear.associatedVariableName}.rgb, ${
222+
velocityLinear.connectedPoint.type === NodeMaterialBlockConnectionPointTypes.Vector4 ? velocityLinear.associatedVariableName + ".a" : "1.0"
223+
});\r\n`;
224+
} else {
225+
// We have to write something on the reflectivity output or it will raise a gl error
226+
state.compilationString += ` fragData[PREPASS_VELOCITY_LINEAR_INDEX] = ${vec4}(0.0, 0.0, 0.0, 1.0);\r\n`;
227+
}
228+
state.compilationString += `#endif\r\n`;
189229

190230
state.compilationString += `#if SCENE_MRT_COUNT > 1\r\n`;
191231
state.compilationString += `${this._getFragData(isWebGPU, 1)} = fragData[1];\r\n`;

packages/dev/core/src/Materials/Node/nodeMaterial.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,22 @@ export class NodeMaterial extends PushMaterial {
11371137
result.push(Constants.PREPASS_POSITION_TEXTURE_TYPE);
11381138
}
11391139

1140+
if (prePassOutputBlock.localPosition.isConnected) {
1141+
result.push(Constants.PREPASS_LOCAL_POSITION_TEXTURE_TYPE);
1142+
}
1143+
1144+
if (prePassOutputBlock.reflectivity.isConnected) {
1145+
result.push(Constants.PREPASS_REFLECTIVITY_TEXTURE_TYPE);
1146+
}
1147+
1148+
if (prePassOutputBlock.velocity.isConnected) {
1149+
result.push(Constants.PREPASS_VELOCITY_TEXTURE_TYPE);
1150+
}
1151+
1152+
if (prePassOutputBlock.velocityLinear.isConnected) {
1153+
result.push(Constants.PREPASS_VELOCITY_LINEAR_TEXTURE_TYPE);
1154+
}
1155+
11401156
return result;
11411157
}
11421158

0 commit comments

Comments
 (0)