/**************************************************************************/1/* csg.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "csg.h"3132// CSGBrush3334void CSGBrush::build_from_faces(const Vector<Vector3> &p_vertices, const Vector<Vector2> &p_uvs, const Vector<bool> &p_smooth, const Vector<Ref<Material>> &p_materials, const Vector<bool> &p_flip_faces) {35faces.clear();3637int vc = p_vertices.size();3839ERR_FAIL_COND((vc % 3) != 0);4041const Vector3 *rv = p_vertices.ptr();42int uvc = p_uvs.size();43const Vector2 *ruv = p_uvs.ptr();44int sc = p_smooth.size();45const bool *rs = p_smooth.ptr();46int mc = p_materials.size();47const Ref<Material> *rm = p_materials.ptr();48int ic = p_flip_faces.size();49const bool *ri = p_flip_faces.ptr();5051HashMap<Ref<Material>, int> material_map;5253faces.resize(p_vertices.size() / 3);5455for (int i = 0; i < faces.size(); i++) {56Face &f = faces.write[i];57f.vertices[0] = rv[i * 3 + 0];58f.vertices[1] = rv[i * 3 + 1];59f.vertices[2] = rv[i * 3 + 2];6061if (uvc == vc) {62f.uvs[0] = ruv[i * 3 + 0];63f.uvs[1] = ruv[i * 3 + 1];64f.uvs[2] = ruv[i * 3 + 2];65}6667if (sc == vc / 3) {68f.smooth = rs[i];69} else {70f.smooth = false;71}7273if (ic == vc / 3) {74f.invert = ri[i];75} else {76f.invert = false;77}7879if (mc == vc / 3) {80Ref<Material> mat = rm[i];81if (mat.is_valid()) {82HashMap<Ref<Material>, int>::ConstIterator E = material_map.find(mat);8384if (E) {85f.material = E->value;86} else {87f.material = material_map.size();88material_map[mat] = f.material;89}9091} else {92f.material = -1;93}94}95}9697materials.resize(material_map.size());98for (const KeyValue<Ref<Material>, int> &E : material_map) {99materials.write[E.value] = E.key;100}101102_regen_face_aabbs();103}104105void CSGBrush::copy_from(const CSGBrush &p_brush, const Transform3D &p_xform) {106faces = p_brush.faces;107materials = p_brush.materials;108109for (int i = 0; i < faces.size(); i++) {110for (int j = 0; j < 3; j++) {111faces.write[i].vertices[j] = p_xform.xform(p_brush.faces[i].vertices[j]);112}113}114115_regen_face_aabbs();116}117118119