Skip to content

Commit

Permalink
Merge branch 'dev' into physx
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Mar 11, 2024
2 parents d891ea8 + f48f5f9 commit 4cf2596
Show file tree
Hide file tree
Showing 58 changed files with 437 additions and 352 deletions.
10 changes: 5 additions & 5 deletions examples/geometries/box_geometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ namespace {
auto geometry = BoxGeometry::create(params);
auto material = MeshBasicMaterial::create();

Mesh mesh(geometry, material);
mesh.add(createWireframe(*geometry));
auto mesh = Mesh::create(geometry, material);
mesh->add(createWireframe(*geometry));

return mesh;
}
Expand Down Expand Up @@ -79,16 +79,16 @@ int main() {
canvas.animate([&]() {
float dt = clock.getDelta();

mesh.rotation.y += 0.8f * dt;
mesh.rotation.x += 0.5f * dt;
mesh->rotation.y += 0.8f * dt;
mesh->rotation.x += 0.5f * dt;

renderer.render(scene, camera);

ui.render();

if (paramsChanged) {
paramsChanged = false;
updateGroupGeometry(mesh, params);
updateGroupGeometry(*mesh, params);
}
});
}
41 changes: 14 additions & 27 deletions examples/geometries/heightmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,47 +104,36 @@ int main() {
hudText.material()->as<MaterialWithColor>()->color.setHex(Color::black);
hud.add(hudText);

std::promise<std::shared_ptr<Material>> promise;
auto future = promise.get_future();
auto material = MeshPhongMaterial::create();
auto mesh = Mesh::create(BufferGeometry::create(), material);
scene->add(mesh);

auto f1 = std::async([&] {
TextureLoader tl;
auto texture = tl.load("data/textures/terrain/aalesund_terrain.png");
auto material = MeshPhongMaterial::create({{"map", texture}});
promise.set_value(material);

canvas.invokeLater([&] {
hudText.setText("Material loaded..", opts);
});
});

auto f2 = std::async([&] {
auto data = loadHeights();
auto future = std::async(std::launch::async, [&] {
const auto data = loadHeights();

auto geometry = PlaneGeometry::create(5041, 5041, 1023, 1023);
geometry->applyMatrix4(Matrix4().makeRotationX(-math::PI / 2));

auto pos = geometry->getAttribute<float>("position");
for (unsigned i = 0, j = 0, l = data.size(); i < l; ++i, j += 3) {
pos->setY(i, data[i]);
}

canvas.invokeLater([&] {
hudText.setText("Geometry loaded..", opts);
});
TextureLoader tl;
auto texture = tl.load("data/textures/terrain/aalesund_terrain.png");

auto material = future.get();
auto mesh = Mesh::create(geometry, material);
canvas.invokeLater([&, texture, geometry] {

canvas.invokeLater([&, mesh] {
hudText.setText("Terrain loaded..", opts);
material->map = texture;
material->needsUpdate();
mesh->setGeometry(geometry);

scene->add(mesh);
hudText.setText("Terrain loaded..", opts);
});

canvas.invokeLater([&] {
hud.remove(hudText);
}, 2);
},
2);
});

canvas.onWindowResize([&](WindowSize size) {
Expand All @@ -165,6 +154,4 @@ int main() {
hud.apply(renderer);
});

f1.get();
f2.get();
}
2 changes: 1 addition & 1 deletion examples/libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ add_library(pathfinding INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/pathfinding/heuristics/ClosestSquaredHeuristic.hpp"
"${CMAKE_CURRENT_SOURCE_DIR}/pathfinding/heuristics/ManhattanHeuristic.hpp"
)
target_include_directories(pathfinding INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(pathfinding INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")
63 changes: 42 additions & 21 deletions examples/lights/point_light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,42 @@ namespace {
return plane;
}

auto addLights(Scene& scene) {
auto light1 = PointLight::create(Color::yellow);
light1->castShadow = true;
light1->shadow->bias = -0.005f;
light1->distance = 8;
light1->position.y = 4;

auto light2 = PointLight::create(Color::white);
light2->castShadow = true;
light2->shadow->bias = -0.005f;
light2->distance = 8;
light2->position.y = 4;

auto light3 = PointLight::create(Color::purple);
light3->castShadow = true;
light3->shadow->bias = -0.005f;
light3->distance = 10;
light3->position.y = 7;

auto lightHelper1 = PointLightHelper::create(*light1, 0.25f);
auto lightHelper2 = PointLightHelper::create(*light2, 0.25f);
auto lightHelper3 = PointLightHelper::create(*light3, 0.25f);

light1->name = "light1";
light2->name = "light2";
light3->name = "light3";

scene.add(light1);
scene.add(light2);
scene.add(light3);

scene.add(lightHelper1);
scene.add(lightHelper2);
scene.add(lightHelper3);
}

}// namespace

int main() {
Expand All @@ -49,25 +85,7 @@ int main() {

OrbitControls controls{*camera, canvas};

auto light1 = PointLight::create(Color::yellow);
light1->castShadow = true;
light1->shadow->bias = -0.005f;
light1->distance = 8;
light1->position.y = 4;
scene->add(light1);

auto lightHelper1 = PointLightHelper::create(*light1, 0.25f);
scene->add(lightHelper1);

auto light2 = PointLight::create(Color::white);
light2->castShadow = true;
light2->shadow->bias = -0.005f;
light2->distance = 8;
light2->position.y = 4;
scene->add(light2);

auto lightHelper2 = PointLightHelper::create(*light2, 0.25f);
scene->add(lightHelper2);
addLights(*scene);

auto knot = createTorusKnot();
scene->add(knot);
Expand All @@ -82,10 +100,13 @@ int main() {
renderer.setSize(size);
});

auto light1 = scene->getObjectByName("light1");
auto light2 = scene->getObjectByName("light2");

Clock clock;
canvas.animate([&]() {
float dt = clock.getDelta();
float t = clock.elapsedTime;
const float dt = clock.getDelta();
const float t = clock.elapsedTime;

knot->rotation.y += 0.5f * dt;

Expand Down
2 changes: 1 addition & 1 deletion examples/lights/spot_light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int main() {
scene->add(helper);

auto target = Object3D::create();
light->target = target;
light->setTarget(*target);
scene->add(target);

auto knot = createTorusKnot();
Expand Down
2 changes: 1 addition & 1 deletion examples/misc/morphtargets_sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main() {

mesh->material()->as<MaterialWithMorphTargets>()->morphTargets = true;

auto points = Points::create(mesh->shared_geometry(), pointsMaterial);
auto points = Points::create(mesh->geometry()->shared_from_this(), pointsMaterial);
points->copyMorphTargetInfluences(&mesh->morphTargetInfluences());
mesh->add(points);
});
Expand Down
38 changes: 31 additions & 7 deletions examples/objects/bones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ namespace {

auto prevBone = Bone::create();
bones.emplace_back(prevBone);
prevBone->position.y = -sizing.halfHeight;

for (unsigned i = 0; i < sizing.segmentCount; i++) {

Expand All @@ -82,6 +81,7 @@ namespace {
{"flatShading", true}});

auto mesh = SkinnedMesh::create(geometry, material);
mesh->castShadow = true;
auto skeleton = Skeleton::create(bones);

mesh->add(bones[0]);
Expand All @@ -104,6 +104,7 @@ namespace {
Sizing sizing{segmentHeight, segmentCount, height, halfHeight};

auto geometry = createGeometry(sizing);
geometry->applyMatrix4(Matrix4().makeTranslation(0,halfHeight,0));
auto bones = createBones(sizing);

auto mesh = createMesh(geometry, bones);
Expand All @@ -112,33 +113,56 @@ namespace {
return mesh;
}

auto initPlane() {
int gridSize = 100;
auto grid = GridHelper::create(gridSize, 10, Color::yellow);

auto geometry = PlaneGeometry::create(gridSize, gridSize);
auto material = ShadowMaterial::create();
material->color = 0x000000;
material->opacity = 0.2f;

auto plane = Mesh::create(geometry, material);
plane->rotation.x = -math::PI / 2;
plane->receiveShadow = true;
grid->add(plane);

return grid;
}

}// namespace

int main() {

Canvas canvas("Bones");
GLRenderer renderer(canvas.size());
renderer.checkShaderErrors = true;
renderer.shadowMap().enabled = true;

Scene scene;
scene.background = Color(0x444444);

PerspectiveCamera camera(75, canvas.size().aspect(), 0.1, 200);
camera.position.set(0, 30, 30);
camera.position.set(0, 50, 50);

auto light1 = PointLight::create(0xffffff);
light1->position.set(0, 200, 0);
light1->castShadow = true;
auto light2 = PointLight::create(0xffffff);
light2->position.set(100, 200, 100);
light2->castShadow = true;
auto light3 = PointLight::create(0xffffff);
light3->position.set(-100, -200, -100);
light3->castShadow = true;

scene.add(light1);
scene.add(light2);
scene.add(light3);

auto mesh = initBones();
scene.add(mesh);
auto plane = initPlane();
scene.add(plane);

auto bones = initBones();
scene.add(bones);

OrbitControls controls{camera, canvas};
controls.enableZoom = false;
Expand Down Expand Up @@ -172,9 +196,9 @@ int main() {
ui.render();

if (animate) {
for (unsigned i = 0; i < mesh->skeleton->bones.size(); i++) {
for (unsigned i = 0; i < bones->skeleton->bones.size(); i++) {

mesh->skeleton->bones[i]->rotation.z = std::sin(time) * 2 / float(mesh->skeleton->bones.size());
bones->skeleton->bones[i]->rotation.z = std::sin(time) * 2 / float(bones->skeleton->bones.size());
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion examples/objects/decal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ int main() {

Vector3 scale = Vector3::ONES() * math::randFloat(0.6f, 1.2f);

auto mat = decalMat->clone()->as<MeshPhongMaterial>();
auto mat = decalMat->clone()->as_shared<MeshPhongMaterial>();
mat->color.randomize();
orientation.z = math::PI * math::randFloat();
auto m = Mesh::create(DecalGeometry::create(*mesh, position, orientation, scale), mat);
Expand Down
8 changes: 4 additions & 4 deletions examples/objects/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int main() {
material->map = loader.load("data/textures/three.png");
material->map->offset.set(0.5, 0.5);

auto pickMaterial = material->clone()->as<SpriteMaterial>();
auto pickMaterial = material->clone()->as_shared<SpriteMaterial>();

auto sprites = createSprites(material);
scene->add(sprites);
Expand Down Expand Up @@ -110,7 +110,7 @@ int main() {
Sprite* lastPicked = nullptr;
canvas.animate([&]() {
if (lastPicked) {
lastPicked->material = material;
lastPicked->setMaterial(material);
lastPicked->scale.set(1, 1, 1);
}

Expand All @@ -120,12 +120,12 @@ int main() {
raycaster.setFromCamera(mouse, *camera);
auto intersects = raycaster.intersectObjects(sprites->children, true);
if (!intersects.empty()) {
auto& intersection = intersects.front();
const auto& intersection = intersects.front();
helper->position.copy(intersection.point);
helper->visible = true;

lastPicked = intersection.object->as<Sprite>();
lastPicked->material = pickMaterial;
lastPicked->setMaterial(pickMaterial);
lastPicked->scale.set(1.2, 1.2, 1.2);
}

Expand Down
14 changes: 14 additions & 0 deletions examples/textures/data_texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ int main() {
box.position.x = -1;
scene.add(box);

auto grid1 = GridHelper::create(5);
grid1->rotateX(math::PI / 2);
grid1->position.z = -2.5;
scene.add(grid1);

auto grid2 = GridHelper::create(5);
grid2->rotateX(math::PI / 2).rotateZ(math::PI / 2);
grid2->position.x = -2.5;
scene.add(grid2);

auto grid3 = GridHelper::create(5);
grid3->position.y = -2.5;
scene.add(grid3);

canvas.onWindowResize([&](WindowSize size) {
camera.aspect = size.aspect();
camera.updateProjectionMatrix();
Expand Down
7 changes: 6 additions & 1 deletion include/threepp/core/BufferGeometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace threepp {

class BufferGeometry: public EventDispatcher {
class BufferGeometry: public EventDispatcher, public std::enable_shared_from_this<BufferGeometry> {

public:
const unsigned int id{++_id};
Expand All @@ -35,6 +35,11 @@ namespace threepp {

BufferGeometry();

BufferGeometry(const BufferGeometry&) = delete;
BufferGeometry& operator=(const BufferGeometry&) = delete;
BufferGeometry(BufferGeometry&&) = delete;
BufferGeometry& operator=(BufferGeometry&&) = delete;

[[nodiscard]] virtual std::string type() const {

return "BufferGeometry";
Expand Down
Loading

0 comments on commit 4cf2596

Please sign in to comment.