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
|
// This file is part of the 64k demo project.
// It implements the Hybrid3DEffect.
#include "gpu/effects/hybrid_3d_effect.h"
#include "generated/assets.h"
#include "util/asset_manager.h"
#include <cassert>
#include <cmath>
#include <iostream>
Hybrid3DEffect::Hybrid3DEffect(WGPUDevice device, WGPUQueue queue,
WGPUTextureFormat format)
: Effect(device, queue), width_(1280), height_(720) {
(void)format; // Passed to base, not directly used here.
}
void Hybrid3DEffect::resize(int width, int height) {
width_ = width;
height_ = height;
renderer_.resize(width_, height_);
}
void Hybrid3DEffect::init(MainSequence* demo) {
(void)demo;
WGPUTextureFormat format =
demo->format; // Get current format from MainSequence (might be different
// than constructor if resized)
renderer_.init(device_, queue_, format);
renderer_.resize(width_, height_);
// Texture Manager
texture_manager_.init(device_, queue_);
// Load Noise Asset
size_t size = 0;
const uint8_t* noise_data = GetAsset(AssetId::ASSET_NOISE_TEX, &size);
if (noise_data && size == 256 * 256 * 4) {
texture_manager_.create_texture("noise", 256, 256, noise_data);
renderer_.set_noise_texture(texture_manager_.get_texture_view("noise"));
} else {
std::cerr << "Failed to load NOISE_TEX asset." << std::endl;
}
// Setup Scene
scene_.clear();
Object3D center(ObjectType::BOX); // Use BOX for bumps
center.position = vec3(0, 0, 0);
center.color = vec4(1, 0, 0, 1);
scene_.add_object(center);
for (int i = 0; i < 8; ++i) {
ObjectType type = ObjectType::SPHERE;
if (i % 3 == 1)
type = ObjectType::TORUS;
if (i % 3 == 2)
type = ObjectType::BOX;
Object3D obj(type);
float angle = (i / 8.0f) * 6.28318f;
obj.position = vec3(std::cos(angle) * 4.0f, 0, std::sin(angle) * 4.0f);
obj.scale = vec3(0.7f, 0.7f, 0.7f); // Increased scale by 40%
if (type == ObjectType::SPHERE)
obj.color = vec4(0, 1, 0, 1);
else if (type == ObjectType::TORUS)
obj.color = vec4(0, 0.5, 1, 1);
else
obj.color = vec4(1, 1, 0, 1);
scene_.add_object(obj);
}
}
// Cubic ease-in/out function for non-linear motion
static float ease_in_out_cubic(float t) {
t *= 2.0f;
if (t < 1.0f) {
return 0.5f * t * t * t;
}
t -= 2.0f;
return 0.5f * (t * t * t + 2.0f);
}
void Hybrid3DEffect::render(WGPURenderPassEncoder pass, float time, float beat,
float intensity, float aspect_ratio) {
// Animate Objects
for (size_t i = 1; i < scene_.objects.size(); ++i) {
scene_.objects[i].rotation =
quat::from_axis(vec3(0, 1, 0), time * 2.0f + i);
scene_.objects[i].position.y = std::sin(time * 3.0f + i) * 1.5f;
}
// Create erratic, eased time variables for camera motion
float erratic_time_radius = ease_in_out_cubic(fmodf(time * 0.2f, 1.0f));
float erratic_time_height = ease_in_out_cubic(fmodf(time * 0.3f, 1.0f));
float erratic_time_orbit = ease_in_out_cubic(fmodf(time * 0.1f, 1.0f));
// Animate Camera
float cam_radius = 10.0f + std::sin(erratic_time_radius * 6.28318f) * 4.0f;
float cam_height = 5.0f + std::cos(erratic_time_height * 6.28318f) * 3.0f;
camera_.set_look_at(
vec3(std::sin(erratic_time_orbit * 6.28318f) * cam_radius, cam_height,
std::cos(erratic_time_orbit * 6.28318f) * cam_radius),
vec3(0, 0, 0),
vec3(0, 1, 0)
);
camera_.aspect_ratio = aspect_ratio;
// Draw
renderer_.draw(pass, scene_, camera_, time);
}
|