Skip to content

Commit

Permalink
Merge pull request #47 from dmurdoch/transparency
Browse files Browse the repository at this point in the history
Transparency
  • Loading branch information
dmurdoch authored Jun 10, 2023
2 parents 42fc150 + 2fc0c66 commit ef14812
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 24 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: rgl2gltf
Type: Package
Title: Read and Write '.gltf' and '.glb' Files
Version: 1.0.3
Version: 1.0.4
Authors@R: c(person("Duncan", "Murdoch", role = c("aut", "cre"),
email = "murdoch.duncan@gmail.com"),
person("Morten S.", "Mikkelsen", role = c("cph")))
Expand All @@ -24,5 +24,5 @@ Suggests:
png,
jpeg
VignetteBuilder: knitr, rmarkdown
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
Roxygen: list(markdown = TRUE)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# rgl2gltf 1.0.4

* Support transparency.

# rgl2gltf 1.0.3

* Added print support for sparse accessors; `rgl` added
Expand Down
10 changes: 10 additions & 0 deletions R/PBR.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ setPBRshaders <- function(gltf, gltfMat,
}
defines[["MANUAL_SRGB"]] <- 1

if (is.null(alphaMode <- gltfMat$alphaMode))
alphaMode <- "OPAQUE"
if (is.null(alphaCutoff <- gltfMat$alphaCutoff))
alphaCutoff <- 0.5

if (alphaMode != "MASK")
alphaCutoff <- -1

uniforms[["u_alphaCutoff"]] <- alphaCutoff

if (length(defines))
defines <- paste("#define", names(defines), unlist(defines))
else
Expand Down
8 changes: 8 additions & 0 deletions R/gltfObj.R
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ Gltf <- R6Class("gltf",
result <- list()
else {
material <- self$getMaterial(n)
if (is.null(alphaMode <- material$alphaMode))
alphaMode <- "OPAQUE"
result <- list(color = "white", alpha = 1)
if (!is.null(pbrm <- material$pbrMetallicRoughness)) {
if (!is.null(col <- unlist(pbrm$baseColorFactor))) {
Expand All @@ -459,6 +461,10 @@ Gltf <- R6Class("gltf",
result$texture <- texturefile
texture <- self$getTexture(texnum)
result$gltftexCoord <- texture$texCoord
if (alphaMode == "OPAQUE")
result$textype <- "rgb"
else
result$textype <- "rgba"
}
}
if (!is.null(col <- unlist(material$emissiveFactor)))
Expand All @@ -476,6 +482,8 @@ Gltf <- R6Class("gltf",
result <- lapply(result, unlist)
} else
result$specular <- "gray10"
if (alphaMode == "OPAQUE")
result$alpha <- 1
}
result
},
Expand Down
10 changes: 5 additions & 5 deletions R/gltfWidget.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
animationDependency <- makeDependency(name = "gltfAnimate",
animationDependency <- function() makeDependency(name = "gltfAnimate",
src = "javascript/gltfAnimate",
script = "gltfAnimate.js",
package = "rgl2gltf",
Expand Down Expand Up @@ -35,7 +35,7 @@ animationControl <- function(gltf, ani = 0, value = gltf$timerange(ani)[1], tran
value = value,
animation = animation,
buffer = buffer$as.list(),
dependencies = list(animationDependency)),
dependencies = list(animationDependency())),
class = "rglControl")
}

Expand All @@ -52,7 +52,7 @@ weightedControl <- function(subid, nodes, weights, translations,
nodes = unname(nodes),
weights = unname(weights),
backtransform = backtransforms,
dependencies = list(animationDependency)),
dependencies = list(animationDependency())),
class = "rglControl")
}

Expand All @@ -61,7 +61,7 @@ skeletonControl <- function(subid) {
structure(list(type = "rgl2gltfSkeleton",
value = 0,
subid = unname(subid),
dependencies = list(animationDependency)),
dependencies = list(animationDependency())),
class = "rglControl")
}

Expand All @@ -76,7 +76,7 @@ shaderControl <- function(id, joints, usedjoints, backtransform) {
id = unname(id),
joints = unname(joints[keep]),
backtransform = backtransforms,
dependencies = list(animationDependency)),
dependencies = list(animationDependency())),
class = "rglControl")
}

Expand Down
8 changes: 4 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ are not included in the package tarball.
Briefly,

- `2CylinderEngine.glb` was produced by Okino Computer Graphics.
- `AlphaBlendModeTest.glb`, `NormalTangentTest.glb` and `NormalTangentMirrorTest.glb` were created
by Ed Mackey and are owned by Analytical Graphics, Inc., licensed under
CC-BY 4.0 https://creativecommons.org/licenses/by/4.0/.
- `AntiqueCamera.glb` was produced by Maximilian Kamps and UX3D.
- `Avocado.glb` was produced by Microsoft.
- `BarramundiFish.glb` was produced by Microsoft.
- `BoxAnimated.glb` was produced by Cesium.
- `BoxAnimated.glb` and `RiggedSimple.glb` were produced by Cesium.
- `BrainStem.glb` was created by Keith Hunter and is owned by Smith Micro Software, Inc.
- `DamagedHelmet.glb` was created by theblueturtle_.
- `NormalTangentTest.glb` and `NormalTangentMirrorTest.glb` were created
by Ed Mackey and are owned by Analytical Graphics, Inc., licensed under
CC-BY 4.0 https://creativecommons.org/licenses/by/4.0/.

Files `src/mikktspace.c` and `src/mikktspace.h` are written by Morten
S. Mikkelsen and used under the permissive license included
Expand Down
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ included in the package tarball.

Briefly,

- `2CylinderEngine.glb` was produced by Okino Computer Graphics.
- `AntiqueCamera.glb` was produced by Maximilian Kamps and UX3D.
- `Avocado.glb` was produced by Microsoft.
- `BarramundiFish.glb` was produced by Microsoft.
- `BoxAnimated.glb` was produced by Cesium.
- `BrainStem.glb` was created by Keith Hunter and is owned by Smith
Micro Software, Inc. 
- `DamagedHelmet.glb` was created by theblueturtle\_.
- `NormalTangentTest.glb` and `NormalTangentMirrorTest.glb` were
created by Ed Mackey and are owned by Analytical Graphics, Inc.,
licensed under CC-BY 4.0
<https://creativecommons.org/licenses/by/4.0/>.
- `2CylinderEngine.glb` was produced by Okino Computer Graphics.
- `AlphaBlendModeTest.glb`, `NormalTangentTest.glb` and
`NormalTangentMirrorTest.glb` were created by Ed Mackey and are owned
by Analytical Graphics, Inc., licensed under CC-BY 4.0
<https://creativecommons.org/licenses/by/4.0/>.
- `AntiqueCamera.glb` was produced by Maximilian Kamps and UX3D.
- `Avocado.glb` was produced by Microsoft.
- `BarramundiFish.glb` was produced by Microsoft.
- `BoxAnimated.glb` and `RiggedSimple.glb` were produced by Cesium.
- `BrainStem.glb` was created by Keith Hunter and is owned by Smith
Micro Software, Inc. 
- `DamagedHelmet.glb` was created by theblueturtle\_.

Files `src/mikktspace.c` and `src/mikktspace.h` are written by Morten S.
Mikkelsen and used under the permissive license included within them,
Expand Down
Binary file added inst/localtests/AlphaBlendModeTest.glb
Binary file not shown.
8 changes: 7 additions & 1 deletion inst/shaders/pbr-frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ uniform float u_OcclusionStrength;
uniform vec2 u_MetallicRoughnessValues;
varying vec4 vCol;

uniform float u_alphaCutoff;

// debugging flags used for shader output of intermediate PBR variables
uniform vec4 u_ScaleDiffBaseMR;
uniform vec4 u_ScaleFGDSpec;
Expand Down Expand Up @@ -329,5 +331,9 @@ void main()
color = mix(color, vec3(metallic), u_ScaleDiffBaseMR.z);
color = mix(color, vec3(perceptualRoughness), u_ScaleDiffBaseMR.w);

gl_FragColor = vec4(pow(color,vec3(1.0/2.2)), baseColor.a);
float alpha = baseColor.a;
if (u_alphaCutoff >= 0.0)
alpha = alpha < u_alphaCutoff ? 0.0 : 1.0;

gl_FragColor = vec4(pow(color,vec3(1.0/2.2)), alpha);
}

0 comments on commit ef14812

Please sign in to comment.