Skip to content
This repository has been archived by the owner on Jun 16, 2023. It is now read-only.

Commit

Permalink
added texcoordTransform for SkyboxMaterial & added PBR update to remo…
Browse files Browse the repository at this point in the history
…ve env intensity & gamma correction in UI
  • Loading branch information
rezaali committed May 20, 2023
1 parent 105fc3d commit 6d1f7d1
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 17 deletions.
4 changes: 4 additions & 0 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
E1E7681028A73661005D75AB /* AudioInputRendererView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioInputRendererView.swift; sourceTree = "<group>"; };
E1E7685028ADAF6F005D75AB /* FlockingRenderer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlockingRenderer.swift; sourceTree = "<group>"; };
E1E7685228ADB23B005D75AB /* FlockingRendererView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlockingRendererView.swift; sourceTree = "<group>"; };
E1E7ADCA2A14349700180894 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
E1EB6DE729690C5B0029E4FC /* PBREnhancedRenderer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PBREnhancedRenderer.swift; sourceTree = "<group>"; };
E1EB6DE929690DD90029E4FC /* PBREnhancedRendererView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PBREnhancedRendererView.swift; sourceTree = "<group>"; };
E1EC55D8268785630097C140 /* RayMarchingRenderer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RayMarchingRenderer.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -641,6 +642,7 @@
E1D714D228A6BADA00B517B6 /* Example */ = {
isa = PBXGroup;
children = (
E1E7ADCA2A14349700180894 /* Info.plist */,
E192F05F24875D6E00C7EE74 /* Utilities */,
66E678CD243EE2190070B294 /* Renderers */,
E1E767C028A6D313005D75AB /* Views */,
Expand Down Expand Up @@ -1053,6 +1055,7 @@
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Example/Info.plist;
INFOPLIST_KEY_NSCameraUsageDescription = "Augments your reality";
INFOPLIST_KEY_NSMicrophoneUsageDescription = "Audio Input Example";
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
Expand Down Expand Up @@ -1098,6 +1101,7 @@
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Example/Info.plist;
INFOPLIST_KEY_NSCameraUsageDescription = "Augments your reality";
INFOPLIST_KEY_NSMicrophoneUsageDescription = "Audio Input Example";
"INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphoneos*]" = YES;
Expand Down
3 changes: 3 additions & 0 deletions Example/Example/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"compression-type" : "automatic"
}
}
4 changes: 2 additions & 2 deletions Example/Example/Example.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.device.audio-input</key>
<true/>
<key>com.apple.security.device.camera</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
</dict>
</plist>
8 changes: 8 additions & 0 deletions Example/Example/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CADisableMinimumFrameDuration</key>
<true/>
</dict>
</plist>
1 change: 1 addition & 0 deletions Example/Example/Renderers/AR/ARPlanesRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import MetalKit

import Forge
import Satin
import SatinCore
import SwiftUI

class ARPlaneContainer: Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ import Metal
import MetalKit

import Forge
import SatinCore
import Satin
import SatinCore

import CoreImage
import ModelIO
import UniformTypeIdentifiers

class PBRStandardMaterialRenderer: BaseRenderer, MaterialDelegate {
override var texturesURL: URL { sharedAssetsURL.appendingPathComponent("Textures") }
Expand Down Expand Up @@ -182,7 +186,6 @@ class PBRStandardMaterialRenderer: BaseRenderer, MaterialDelegate {
material.setTexture(tmpTexture, type: .normal)
material.setTexture(tmpTexture, type: .roughness)


let baseURL = modelsURL.appendingPathComponent("Suzanne")
let maps: [PBRTextureIndex: URL] = [
.baseColor: baseURL.appendingPathComponent("albedo.png"),
Expand Down
5 changes: 4 additions & 1 deletion Sources/Satin/Core/Renderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,11 @@ open class Renderer {
}
}

if let cubemapTexture = cubemapTexture, let skyboxMaterial = material as? SkyboxMaterial {
if let cubemapTexture = cubemapTexture,
let skyboxMaterial = material as? SkyboxMaterial {
skyboxMaterial.texture = cubemapTexture
skyboxMaterial.texcoordTransform = reflectionTexcoordTransform
skyboxMaterial.environmentIntensity = environmentIntensity
}
}
} else {
Expand Down
22 changes: 21 additions & 1 deletion Sources/Satin/Materials/SkyboxMaterial.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Metal
import simd

open class SkyboxMaterial: BasicTextureMaterial {
override public var texture: MTLTexture? {
Expand All @@ -16,6 +17,18 @@ open class SkyboxMaterial: BasicTextureMaterial {
}
}

public var texcoordTransform: simd_float3x3 = matrix_identity_float3x3 {
didSet {
set("Texcoord Transform", texcoordTransform)
}
}

public var environmentIntensity: Float = 1.0 {
didSet {
set("Environment Intensity", environmentIntensity)
}
}

public var tonemapping: Tonemapping = .aces {
didSet {
if oldValue != tonemapping, let shader = shader as? SkyboxShader {
Expand Down Expand Up @@ -47,9 +60,16 @@ open class SkyboxMaterial: BasicTextureMaterial {
initalizeParameters(tonemapping: tonemapping, gammaCorrection: gammaCorrection)
}

private func initalizeParameters(tonemapping: Tonemapping = .aces, gammaCorrection: Float = 1.0) {
private func initalizeParameters(
tonemapping: Tonemapping = .aces,
gammaCorrection: Float = 1.0,
environmentIntensity: Float = 1.0,
texcoordTransform: simd_float3x3 = matrix_identity_float3x3
) {
self.tonemapping = tonemapping
self.gammaCorrection = gammaCorrection
self.environmentIntensity = environmentIntensity
self.texcoordTransform = texcoordTransform
}

public required init() {
Expand Down
9 changes: 5 additions & 4 deletions Sources/Satin/Objects/Scene.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,17 @@ open class Scene: Object, Environment {
}

public func setEnvironmentCubemap(texture: MTLTexture, qos: DispatchQoS.QoSClass = .background, reflectionSize: Int = 512, irrandianceSize: Int = 64, brdfSize: Int = 512, progress: Progress? = nil) {

cubemapTexture = texture
self.cubemapSize = texture.width
self.reflectionSize = reflectionSize
irradianceSize = irrandianceSize
self.brdfSize = brdfSize
DispatchQueue.global(qos: qos).async {
guard let environment = self.environment,
let commandQueue = environment.device.makeCommandQueue() else { return }

let device = environment.device
let device = texture.device

DispatchQueue.global(qos: qos).async {
guard let commandQueue = texture.device.makeCommandQueue() else { return }

var _brdfTexture: MTLTexture? = nil
var _reflectionTexture: MTLTexture? = nil
Expand Down
4 changes: 2 additions & 2 deletions Sources/Satin/Pipelines/Chunks/PbrStandardUniforms.metal
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
float roughness; // slider,0.0,1.0,0.0
float metallic; // slider,0.0,1.0,0.0
float specular; // slider,0.0,1.0,0.5
float environmentIntensity; // slider,0,1,1
float gammaCorrection; // slider,0,1,1
float environmentIntensity;
float gammaCorrection;
float3x3 baseColorTexcoordTransform;
float3x3 emissiveTexcoordTransform;
float3x3 roughnessTexcoordTransform;
Expand Down
4 changes: 2 additions & 2 deletions Sources/Satin/Pipelines/Chunks/PbrUniforms.metal
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
float transmission; // slider,0.0,1.0,0.0
float thickness; // slider,0.0,5.0,0.0
float ior; // slider,1.0,3.0,1.5
float environmentIntensity; // slider,0,1,1
float gammaCorrection; // slider,0,1,1
float environmentIntensity;
float gammaCorrection;
float3x3 baseColorTexcoordTransform;
float3x3 emissiveTexcoordTransform;
float3x3 roughnessTexcoordTransform;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void pbrIndirectLighting(
// Transmission
float ior = pixel.material.ior;
// float3 thickness = pixel.material.thickness;
float3 transmissionRay = getVolumeTransmissionRay(N, V, 1.0, ior);
float3 transmissionRay = pixel.material.reflectionTexcoordTransform * getVolumeTransmissionRay(N, V, 1.0, ior);
// float3 refractedRayExit = pixel.position + transmissionRay;

// Since Satin's render isn't ready for multiple passes we are going to default to refract into the cubemap
Expand Down
7 changes: 5 additions & 2 deletions Sources/Satin/Pipelines/Materials/Skybox/Shaders.metal
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
typedef struct {
float4 color; // color
float gammaCorrection; //slider,0.0,1.0,1.0
float environmentIntensity; //slider,0,1,1
float3x3 texcoordTransform;
} SkyboxUniforms;

typedef struct {
Expand Down Expand Up @@ -33,8 +35,9 @@ fragment float4 skyboxFragment(SkyVertexData in [[stage_in]],
texturecube<float> cubeTex [[texture(FragmentTextureCustom0)]],
sampler cubeTexSampler [[sampler(FragmentSamplerCustom0)]])
{
float4 color = cubeTex.sample(cubeTexSampler, in.uv);

float4 color = cubeTex.sample(cubeTexSampler, uniforms.texcoordTransform * in.uv);
color.rgb *= uniforms.environmentIntensity;

color.rgb = tonemap(color.rgb);

#ifndef TONEMAPPING_UNREAL
Expand Down

0 comments on commit 6d1f7d1

Please sign in to comment.