Skip to content

Commit

Permalink
make newly added hydra additions a bit swifty.
Browse files Browse the repository at this point in the history
Signed-off-by: furby™ <devs@wabi.foundation>
  • Loading branch information
furby-tm committed Dec 31, 2024
1 parent 2a64ff4 commit 23a1291
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 22 deletions.
10 changes: 10 additions & 0 deletions Sources/PixarUSD/Base/Gf/Frustum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,14 @@ public extension Gf.Frustum
{
GetProjectionType()
}

func computeViewMatrix() -> Gf.Matrix4d
{
ComputeViewMatrix()
}

func computeProjectionMatrix() -> Gf.Matrix4d
{
ComputeProjectionMatrix()
}
}
5 changes: 5 additions & 0 deletions Sources/PixarUSD/Imaging/Hdx/HdxTokens.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ public extension Hdx
}
}
}

public extension Tf.Token
{
static let sRGB = Hdx.ColorCorrectionTokens.sRGB.token
}
84 changes: 75 additions & 9 deletions Sources/PixarUSD/UsdImaging/UsdImagingGL/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
{
UsdImagingGL.Engine.CreateEngine()
}

static func createEngine(params: Parameters) -> UsdImagingGL.EngineSharedPtr
{
UsdImagingGL.Engine.CreateEngine(params)
Expand Down Expand Up @@ -69,14 +69,55 @@
SetEnablePresentation(value)
}

func setRenderer(aov: Hd.AovTokens)
func setRendererAov(_ aovName: Hd.AovTokens)
{
SetRendererAov(aovName.token)
}

func render(rootPrim: Usd.Prim, params: UsdImagingGL.RenderParams)
{
Render(rootPrim, params)
}

func setCameraState(modelViewMatrix: Gf.Matrix4d, projectionMatrix: Gf.Matrix4d)
{
SetCameraState(modelViewMatrix, projectionMatrix)
}

func setRenderViewport(_ viewport: Gf.Vec4d)
{
SetRenderViewport(viewport)
}

enum CameraUtilWindowConformPolicy
{
case matchVertically
case matchHorizontally
case fit
case crop
case dontConform
}

func setWindowPolicy(_ policy: CameraUtilWindowConformPolicy)
{
switch policy
{
case .matchVertically: SetWindowPolicy(Pixar.CameraUtilConformWindowPolicy(0))
case .matchHorizontally: SetWindowPolicy(Pixar.CameraUtilConformWindowPolicy(1))
case .fit: SetWindowPolicy(Pixar.CameraUtilConformWindowPolicy(2))
case .crop: SetWindowPolicy(Pixar.CameraUtilConformWindowPolicy(3))
case .dontConform: SetWindowPolicy(Pixar.CameraUtilConformWindowPolicy(4))
}
}

func setLightingState(lights: Pixar.GlfSimpleLightVector, material: Pixar.GlfSimpleMaterial, sceneAmbient: Pixar.GfVec4f)
{
SetRendererAov(aov.token)
SetLightingState(lights, material, sceneAmbient)
}

func render(root: Usd.Prim, params: UsdImagingGL.RenderParams)
func getAovTexture(_ aovName: Hd.AovTokens) -> Pixar.HgiTextureHandle
{
Render(root, params)
GetAovTexture(aovName.token)
}
}

Expand All @@ -87,14 +128,39 @@
pointee.setEnablePresentation(value)
}

func setRenderer(aov token: Hd.AovTokens)
func setRendererAov(_ aovName: Hd.AovTokens)
{
pointee.setRendererAov(aovName)
}

func render(rootPrim: Usd.Prim, params: UsdImagingGL.RenderParams)
{
pointee.render(rootPrim: rootPrim, params: params)
}

func setCameraState(modelViewMatrix: Gf.Matrix4d, projectionMatrix: Gf.Matrix4d)
{
pointee.setCameraState(modelViewMatrix: modelViewMatrix, projectionMatrix: projectionMatrix)
}

func setRenderViewport(_ viewport: Gf.Vec4d)
{
pointee.setRenderViewport(viewport)
}

func setWindowPolicy(_ policy: UsdImagingGL.Engine.CameraUtilWindowConformPolicy)
{
self.pointee.setWindowPolicy(policy)
}

func setLightingState(lights: Pixar.GlfSimpleLightVector, material: Pixar.GlfSimpleMaterial, sceneAmbient: Pixar.GfVec4f)
{
pointee.setRenderer(aov: token)
pointee.setLightingState(lights: lights, material: material, sceneAmbient: sceneAmbient)
}

func render(root prim: Usd.Prim, params: UsdImagingGL.RenderParams)
func getAovTexture(_ aovName: Hd.AovTokens) -> Pixar.HgiTextureHandle
{
pointee.render(root: prim, params: params)
pointee.getAovTexture(aovName)
}
}
#endif // canImport(UsdImagingGLEngine)
2 changes: 1 addition & 1 deletion Sources/UsdView/Hydra/GL/Hydra+GLRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import PixarUSD
)

engine.setEnablePresentation(false)
engine.setRenderer(aov: .color)
engine.setRendererAov(.color)
#endif // canImport(UsdImagingGL)
}

Expand Down
24 changes: 12 additions & 12 deletions Sources/UsdView/Hydra/Hydra+RenderEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public enum Hydra
)

engine.setEnablePresentation(false)
engine.setRenderer(aov: .color)
engine.setRendererAov(.color)

viewCamera = Hydra.Camera(isZUp: Hydra.RenderEngine.isZUp(for: stage))
setupCamera()
Expand All @@ -63,33 +63,33 @@ public enum Hydra
let cameraTransform = viewCamera.getTransform()
let cameraParams = viewCamera.getShaderParams()
let frustum = computeFrustum(cameraTransform: cameraTransform, viewSize: viewSize, cameraParams: cameraParams)
let modelViewMatrix = frustum.ComputeViewMatrix()
let projMatrix = frustum.ComputeProjectionMatrix()
engine.pointee.SetCameraState(modelViewMatrix, projMatrix)
let viewMatrix = frustum.computeViewMatrix()
let projMatrix = frustum.computeProjectionMatrix()
engine.setCameraState(modelViewMatrix: viewMatrix, projectionMatrix: projMatrix)

// viewport setup.
let viewport = Gf.Vec4d(0, 0, viewSize.width, viewSize.height)
engine.pointee.SetRenderViewport(viewport)
engine.pointee.SetWindowPolicy(Pixar.CameraUtilConformWindowPolicy(0))
engine.setRenderViewport(viewport)
engine.setWindowPolicy(.matchHorizontally)

// light and material setup.
let lights = computeLights(cameraTransform: cameraTransform)
engine.pointee.SetLightingState(lights.GetLights(), material, sceneAmbient)
engine.setLightingState(lights: lights.getLights(), material: material, sceneAmbient: sceneAmbient)

var params = UsdImagingGL.RenderParams()
params.frame = Usd.TimeCode(timeCode)
params.clearColor = .init(Float(0.0), Float(0.0), Float(0.0), Float(0.0))
params.colorCorrectionMode = Hdx.ColorCorrectionTokens.sRGB.token
params.clearColor = .init(0.0, 0.0, 0.0, 0.0)
params.colorCorrectionMode = .sRGB
params.enableIdRender = false
params.showGuides = true
params.showRender = true
params.showProxy = true

// render the frame.
engine.render(root: stage.getPseudoRoot(), params: params)
engine.render(rootPrim: stage.getPseudoRoot(), params: params)

// return the color output.
return engine.pointee.GetAovTexture(Hd.AovTokens.color.token)
return engine.getAovTexture(.color)
}

public func setupCamera()
Expand Down Expand Up @@ -127,7 +127,7 @@ public enum Hydra
func computeLights(cameraTransform: Gf.Matrix4d) -> Pixar.GlfSimpleLightCollector
{
var lights = Pixar.GlfSimpleLightCollector()
lights.AddLight(computeCameraLight(cameraTransform: cameraTransform))
lights.addLight(computeCameraLight(cameraTransform: cameraTransform))

return lights
}
Expand Down

0 comments on commit 23a1291

Please sign in to comment.