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

Fix VP2 rendering of UsdPreviewSurface with opacityThreshold #3947

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ limitations under the License.
<float name="roughness" ref="usdPreviewSurfaceLightingAPI2.specularRoughness"/>
<float3 name="specularColor" ref="usdPreviewSurfaceLightingAPI2.specularColor"/>
<float name="opacity" ref="opacityToTransparency.opacity"/>
<float name="opacityThreshold" ref="usdPreviewSurfaceLightingAPI2.opacityThreshold"/>
<bool name="useSpecularWorkflow" ref="usdPreviewSurfaceLightingAPI2.useSpecularWorkflow"/>

<!-- Maya Parameters for Lighting -->
Expand Down Expand Up @@ -86,6 +87,7 @@ limitations under the License.
<float3 name="specularColor" value="0.0,0.0,0.0"/>
<!-- The shader computes transparency from its "opacity" attribute. -->
<float name="opacity" value="1.0"/>
<float name="opacityThreshold" value="0.0"/>
<bool name="useSpecularWorkflow" value="false"/>

<!-- Default values for Maya-provided parameters. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ limitations under the License.
<float name="roughness" ref="usdPreviewSurfaceLightingAPI3.specularRoughness"/>
<float3 name="specularColor" ref="usdPreviewSurfaceLightingAPI3.specularColor"/>
<float name="opacity" ref="opacityToTransparency.opacity"/>
<float name="opacityThreshold" ref="usdPreviewSurfaceLightingAPI3.opacityThreshold"/>
<bool name="useSpecularWorkflow" ref="usdPreviewSurfaceLightingAPI3.useSpecularWorkflow"/>

<!-- Maya Parameters for Lighting -->
Expand Down Expand Up @@ -86,6 +87,7 @@ limitations under the License.
<float3 name="specularColor" value="0.0,0.0,0.0"/>
<!-- The shader computes transparency from its "opacity" attribute. -->
<float name="opacity" value="1.0"/>
<float name="opacityThreshold" value="0.0"/>
<bool name="useSpecularWorkflow" value="false"/>

<!-- Default values for Maya-provided parameters. -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,15 +379,24 @@ surfaceShader(vec3 Peye, vec3 Neye,
float specularAmount
)
{
float opacity = 1.0 - transparency.r;
if (opacity < opacityThreshold) {
discard;
}

mayaSurfaceShaderOutput result;
result.outGlowColor = vec3(0.0);

// Pre-multiply diffuse color by opacity if not done so already
diffuseColor *= opacity;
// Transparency and cutout.
float opacity = 1.0 - transparency.r;
if (opacityThreshold > 0.0) {
if (opacity < opacityThreshold) {
discard;
}
result.outTransparency = vec3(0.0);
result.outMatteOpacity = vec3(1.0);
} else {
result.outTransparency = transparency;
result.outMatteOpacity = vec3(opacity);

// Pre-multiply diffuse color by opacity if not done so already
diffuseColor *= opacity;
}

// Evaluate all lights.
result.outColor = evaluateLights(
Expand All @@ -406,12 +415,6 @@ surfaceShader(vec3 Peye, vec3 Neye,
Peye,
Neye);

// Transparency
result.outTransparency = transparency;

result.outGlowColor = vec3(0.0, 0.0, 0.0);
result.outMatteOpacity = vec3(opacity);

return result;
}

Expand Down Expand Up @@ -733,15 +736,24 @@ surfaceShader(vec3 Peye, vec3 Neye,
float specularAmount
)
{
float opacity = 1.0 - transparency.r;
if (opacity < opacityThreshold) {
discard;
}

mayaSurfaceShaderOutput result;
result.outGlowColor = vec3(0.0);

// Pre-multiply diffuse color by opacity if not done so already
diffuseColor *= opacity;
// Transparency and cutout.
float opacity = 1.0 - transparency.r;
if (opacityThreshold > 0.0) {
if (opacity < opacityThreshold) {
discard;
}
result.outTransparency = vec3(0.0);
result.outMatteOpacity = vec3(1.0);
} else {
result.outTransparency = transparency;
result.outMatteOpacity = vec3(opacity);

// Pre-multiply diffuse color by opacity if not done so already
diffuseColor *= opacity;
}

// Evaluate all lights.
result.outColor = evaluateLights(
Expand All @@ -760,12 +772,6 @@ surfaceShader(vec3 Peye, vec3 Neye,
Peye,
Neye);

// Transparency
result.outTransparency = transparency;

result.outGlowColor = vec3(0.0, 0.0, 0.0);
result.outMatteOpacity = vec3(opacity);

return result;
}

Expand Down Expand Up @@ -1087,15 +1093,24 @@ surfaceShader(float3 Peye, float3 Neye,
float specularAmount
)
{
float opacity = 1.0 - transparency.r;
if (opacity < opacityThreshold) {
discard;
}

mayaSurfaceShaderOutput result;
result.outGlowColor = float3(0.0, 0.0, 0.0);

// Pre-multiply diffuse color by opacity if not done so already
diffuseColor *= opacity;
// Transparency and cutout.
float opacity = 1.0 - transparency.r;
if (opacityThreshold > 0.0) {
if (opacity < opacityThreshold) {
discard;
}
result.outTransparency = float3(0.0, 0.0, 0.0);
result.outMatteOpacity = float3(1.0, 1.0, 1.0);
} else {
result.outTransparency = transparency;
result.outMatteOpacity = float3(opacity, opacity, opacity);

// Pre-multiply diffuse color by opacity if not done so already
diffuseColor *= opacity;
}

// Evaluate all lights.
result.outColor = evaluateLights(
Expand All @@ -1114,12 +1129,6 @@ surfaceShader(float3 Peye, float3 Neye,
Peye,
Neye);

// Transparency
result.outTransparency = transparency;

result.outGlowColor = float3(0.0, 0.0, 0.0);
result.outMatteOpacity = float3(opacity, opacity, opacity);

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,15 +379,24 @@ surfaceShader(vec3 Peye, vec3 Neye,
float specularAmount
)
{
float opacity = 1.0 - transparency.r;
if (opacity < opacityThreshold) {
discard;
}

mayaSurfaceShaderOutput result;
result.outGlowColor = vec3(0.0);

// Pre-multiply diffuse color by opacity if not done so already
diffuseColor *= opacity;
// Transparency and cutout.
float opacity = 1.0 - transparency.r;
if (opacityThreshold > 0.0) {
if (opacity < opacityThreshold) {
discard;
}
result.outTransparency = vec3(0.0);
result.outMatteOpacity = vec3(1.0);
} else {
result.outTransparency = transparency;
result.outMatteOpacity = vec3(opacity);

// Pre-multiply diffuse color by opacity if not done so already
diffuseColor *= opacity;
}

// Evaluate all lights.
result.outColor = evaluateLights(
Expand All @@ -406,12 +415,6 @@ surfaceShader(vec3 Peye, vec3 Neye,
Peye,
Neye);

// Transparency
result.outTransparency = transparency;

result.outGlowColor = vec3(0.0, 0.0, 0.0);
result.outMatteOpacity = vec3(opacity);

return result;
}

Expand Down Expand Up @@ -733,15 +736,24 @@ surfaceShader(vec3 Peye, vec3 Neye,
float specularAmount
)
{
float opacity = 1.0 - transparency.r;
if (opacity < opacityThreshold) {
discard;
}

mayaSurfaceShaderOutput result;

// Pre-multiply diffuse color by opacity if not done so already
diffuseColor *= opacity;
result.outGlowColor = vec3(0.0);

// Transparency and cutout.
float opacity = 1.0 - transparency.r;
if (opacityThreshold > 0.0) {
if (opacity < opacityThreshold) {
discard;
}
result.outTransparency = vec3(0.0);
result.outMatteOpacity = vec3(1.0);
} else {
result.outTransparency = transparency;
result.outMatteOpacity = vec3(opacity);

// Pre-multiply diffuse color by opacity if not done so already
diffuseColor *= opacity;
}

// Evaluate all lights.
result.outColor = evaluateLights(
Expand All @@ -760,12 +772,6 @@ surfaceShader(vec3 Peye, vec3 Neye,
Peye,
Neye);

// Transparency
result.outTransparency = transparency;

result.outGlowColor = vec3(0.0, 0.0, 0.0);
result.outMatteOpacity = vec3(opacity);

return result;
}

Expand Down Expand Up @@ -1087,15 +1093,24 @@ surfaceShader(float3 Peye, float3 Neye,
float specularAmount
)
{
float opacity = 1.0 - transparency.r;
if (opacity < opacityThreshold) {
discard;
}

mayaSurfaceShaderOutput result;

// Pre-multiply diffuse color by opacity if not done so already
diffuseColor *= opacity;
result.outGlowColor = float3(0.0, 0.0, 0.0);

// Transparency and cutout.
float opacity = 1.0 - transparency.r;
if (opacityThreshold > 0.0) {
if (opacity < opacityThreshold) {
discard;
}
result.outTransparency = float3(0.0, 0.0, 0.0);
result.outMatteOpacity = float3(1.0, 1.0, 1.0);
} else {
result.outTransparency = transparency;
result.outMatteOpacity = float3(opacity, opacity, opacity);

// Pre-multiply diffuse color by opacity if not done so already
diffuseColor *= opacity;
}

// Evaluate all lights.
result.outColor = evaluateLights(
Expand All @@ -1114,12 +1129,6 @@ surfaceShader(float3 Peye, float3 Neye,
Peye,
Neye);

// Transparency
result.outTransparency = transparency;

result.outGlowColor = float3(0.0, 0.0, 0.0);
result.outMatteOpacity = float3(opacity, opacity, opacity);

return result;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/usd/pxrUsdPreviewSurface/usdPreviewSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ MStatus PxrMayaUsdPreviewSurface::compute(const MPlug& plug, MDataBlock& dataBlo
CHECK_MSTATUS(status);
const float opacityThreshold = opacityThresholdData.asFloat();

if (opacity < opacityThreshold) {
opacity = 0.0f;
if (opacityThreshold > 0.0f) {
opacity = (opacity < opacityThreshold) ? 0.0f : 1.0f;
}

const float transparency = 1.0f - opacity;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,42 @@ def testDoubleSided(self):
mayaUtils.setBasicCamera(3)
self.assertSnapshotClose('doubleSided_disabled_back.png')

def testOpacityThreshold(self):
'''
Test UsdPreviewSurface transparency cut-out. The surface fragments should be
should be fully transparent or fully opaque when opacityThreshold is positive.
'''
cmds.file(new=True, force=True)
mayaUtils.loadPlugin('mayaUsdPlugin')

# Import the USD file.
testFile = testUtils.getTestScene('UsdPreviewSurface', 'TestOpacityThreshold.usda')
mayaUtils.createProxyFromFile(testFile)

# Frame the scene for the snapshot.
cmds.xform("persp", t= (0, 1, 2.25))
cmds.xform("persp", ro=[-25, 0, 0], ws=True)

# Create a light to cast a shadow.
white_light = cmds.directionalLight(rgb=(1, 1, 1))
white_transform = cmds.listRelatives(white_light, parent=True)[0]
cmds.xform(white_transform, ro=(-35, -35, 0), ws=True)

# Turn on texturing, lighting, shadows.
cmds.modelEditor(
mayaUtils.activeModelPanel(),
edit=True,
displayTextures=True,
displayLights='all',
shadows=True,
lights=False
)

# This option is needed for VP2 to discard shadow map samples.
cmds.setAttr('hardwareRenderingGlobals.transparentShadow', True)

# Snapshot and assert similarity.
self.assertSnapshotClose('opacityThreshold.png')

if __name__ == '__main__':
fixturesUtils.runTests(globals())
Loading
Loading