-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSkyboxApp.swift
137 lines (106 loc) · 4.95 KB
/
SkyboxApp.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2022 Feng Yang
//
// I am making my contributions/submissions to this project solely in my
// personal capacity and am not conveying any rights to any intellectual
// property of any third parties.
import Cocoa
import ImGui
import Math
import ModelIO
import vox_render
import vox_toolkit
private class GUI: Script {
var show_demo_window: Bool = true
var show_another_window: Bool = false
var clear_color: SIMD3<Float> = .init(x: 0.28, y: 0.36, z: 0.5)
var f: Float = 0.0
var counter: Int = 0
override func onGUI() {
let view = Engine.canvas!
let io = ImGuiGetIO()!
io.pointee.DisplaySize.x = Float(view.bounds.size.width)
io.pointee.DisplaySize.y = Float(view.bounds.size.height)
let frameBufferScale = Float(view.window?.screen?.backingScaleFactor ?? NSScreen.main!.backingScaleFactor)
io.pointee.DisplayFramebufferScale = ImVec2(x: frameBufferScale, y: frameBufferScale)
io.pointee.DeltaTime = 1.0 / 60
ImGuiNewFrame()
// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()!
// You can browse its code to learn more about Dear ImGui!).
if show_demo_window {
ImGuiShowDemoWindow(&show_demo_window)
}
// 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window.
// Create a window called "Hello, world!" and append into it.
ImGuiBegin("Begin", &show_demo_window, 0)
// Display some text (you can use a format strings too)
ImGuiTextV("This is some useful text.")
// Edit bools storing our window open/close state
ImGuiCheckbox("Demo Window", &show_demo_window)
ImGuiCheckbox("Another Window", &show_another_window)
ImGuiSliderFloat("Float Slider", &f, 0.0, 1.0, nil, 1) // Edit 1 float using a slider from 0.0f to 1.0f
ImGuiColorEdit3("clear color", &clear_color, 0) // Edit 3 floats representing a color
if ImGuiButton("Button", ImVec2(x: 100, y: 20)) { // Buttons return true when clicked (most widgets return true when edited/activated)
counter += 1
}
// SameLine(offset_from_start_x: 0, spacing: 0)
ImGuiSameLine(0, 2)
ImGuiTextV(String(format: "counter = %d", counter))
let avg: Float = (1000.0 / io.pointee.Framerate)
let fps = io.pointee.Framerate
ImGuiTextV(String(format: "Application average %.3f ms/frame (%.1f FPS)", avg, fps))
ImGuiEnd()
// Rendering
ImGuiRender()
}
}
class SkyboxApp: NSViewController {
var canvas: Canvas!
var engine: Engine!
override func viewDidLoad() {
super.viewDidLoad()
canvas = Canvas(frame: view.frame)
canvas.setParentView(view)
engine = Engine(canvas: canvas)
let scene = Engine.sceneManager.activeScene!
scene.postprocessManager.autoExposure = true
let rootEntity = scene.createRootEntity()
rootEntity.addComponent(GUI.self)
let skyMaterial = SkyBoxMaterial()
// method1: load cubemap
// skyMaterial.textureCubeMap = try! Engine.textureLoader.loadTexture(with: "country")!
// method2: load hdr
let hdr = Engine.textureLoader.loadHDR(with: "assets/kloppenheim_06_4k.hdr")!
skyMaterial.textureCubeMap = hdr
skyMaterial.equirectangular = true
// method3: generate skymap
// let skyTexture = MDLSkyCubeTexture(name: "natrual", channelEncoding: .float16,
// textureDimensions: [512, 512], turbidity: 1.0, sunElevation: 1.0,
// sunAzimuth: 1.0, upperAtmosphereScattering: 1.0, groundAlbedo: 1.0)
// skyMaterial.textureCubeMap = try! Engine.textureLoader.loadTexture(with: skyTexture)!
let skySubpass = SkySubpass()
skySubpass.material = skyMaterial
skySubpass.mesh = PrimitiveMesh.createCuboid()
scene.background.mode = .Sky
scene.background.sky = skySubpass
let cameraEntity = rootEntity.createChild()
cameraEntity.transform.position = Vector3(1, 1, 1)
cameraEntity.transform.lookAt(targetPosition: Vector3())
cameraEntity.addComponent(Camera.self)
cameraEntity.addComponent(OrbitControl.self)
let light = rootEntity.createChild("light")
light.transform.position = Vector3(1, 3, 0)
light.transform.lookAt(targetPosition: Vector3())
light.addComponent(DirectLight.self)
let cubeEntity = rootEntity.createChild()
let renderer = cubeEntity.addComponent(MeshRenderer.self)
renderer.mesh = PrimitiveMesh.createCuboid(width: 0.1, height: 0.1, depth: 0.1)
let material = UnlitMaterial()
material.baseColor = Color(0.4, 0.0, 0.0)
renderer.setMaterial(material)
Engine.run()
}
override func viewDidDisappear() {
super.viewDidDisappear()
Engine.destroy()
}
}