1/**************************************************************************/
2/* mesh_texture.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "mesh_texture.h"
32
33#include "scene/resources/mesh.h"
34
35int MeshTexture::get_width() const {
36 return size.width;
37}
38
39int MeshTexture::get_height() const {
40 return size.height;
41}
42
43RID MeshTexture::get_rid() const {
44 return RID();
45}
46
47bool MeshTexture::has_alpha() const {
48 return false;
49}
50
51void MeshTexture::set_mesh(const Ref<Mesh> &p_mesh) {
52 mesh = p_mesh;
53}
54
55Ref<Mesh> MeshTexture::get_mesh() const {
56 return mesh;
57}
58
59void MeshTexture::set_image_size(const Size2 &p_size) {
60 size = p_size;
61}
62
63Size2 MeshTexture::get_image_size() const {
64 return size;
65}
66
67void MeshTexture::set_base_texture(const Ref<Texture2D> &p_texture) {
68 base_texture = p_texture;
69}
70
71Ref<Texture2D> MeshTexture::get_base_texture() const {
72 return base_texture;
73}
74
75void MeshTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
76 if (mesh.is_null() || base_texture.is_null()) {
77 return;
78 }
79 Transform2D xform;
80 xform.set_origin(p_pos);
81 if (p_transpose) {
82 SWAP(xform.columns[0][1], xform.columns[1][0]);
83 SWAP(xform.columns[0][0], xform.columns[1][1]);
84 }
85 RenderingServer::get_singleton()->canvas_item_add_mesh(p_canvas_item, mesh->get_rid(), xform, p_modulate, base_texture->get_rid());
86}
87
88void MeshTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
89 if (mesh.is_null() || base_texture.is_null()) {
90 return;
91 }
92 Transform2D xform;
93 Vector2 origin = p_rect.position;
94 if (p_rect.size.x < 0) {
95 origin.x += size.x;
96 }
97 if (p_rect.size.y < 0) {
98 origin.y += size.y;
99 }
100 xform.set_origin(origin);
101 xform.set_scale(p_rect.size / size);
102
103 if (p_transpose) {
104 SWAP(xform.columns[0][1], xform.columns[1][0]);
105 SWAP(xform.columns[0][0], xform.columns[1][1]);
106 }
107 RenderingServer::get_singleton()->canvas_item_add_mesh(p_canvas_item, mesh->get_rid(), xform, p_modulate, base_texture->get_rid());
108}
109
110void MeshTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const {
111 if (mesh.is_null() || base_texture.is_null()) {
112 return;
113 }
114 Transform2D xform;
115 Vector2 origin = p_rect.position;
116 if (p_rect.size.x < 0) {
117 origin.x += size.x;
118 }
119 if (p_rect.size.y < 0) {
120 origin.y += size.y;
121 }
122 xform.set_origin(origin);
123 xform.set_scale(p_rect.size / size);
124
125 if (p_transpose) {
126 SWAP(xform.columns[0][1], xform.columns[1][0]);
127 SWAP(xform.columns[0][0], xform.columns[1][1]);
128 }
129 RenderingServer::get_singleton()->canvas_item_add_mesh(p_canvas_item, mesh->get_rid(), xform, p_modulate, base_texture->get_rid());
130}
131
132bool MeshTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
133 r_rect = p_rect;
134 r_src_rect = p_src_rect;
135 return true;
136}
137
138bool MeshTexture::is_pixel_opaque(int p_x, int p_y) const {
139 return true;
140}
141
142void MeshTexture::_bind_methods() {
143 ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshTexture::set_mesh);
144 ClassDB::bind_method(D_METHOD("get_mesh"), &MeshTexture::get_mesh);
145 ClassDB::bind_method(D_METHOD("set_image_size", "size"), &MeshTexture::set_image_size);
146 ClassDB::bind_method(D_METHOD("get_image_size"), &MeshTexture::get_image_size);
147 ClassDB::bind_method(D_METHOD("set_base_texture", "texture"), &MeshTexture::set_base_texture);
148 ClassDB::bind_method(D_METHOD("get_base_texture"), &MeshTexture::get_base_texture);
149
150 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
151 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "base_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_base_texture", "get_base_texture");
152 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "image_size", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_image_size", "get_image_size");
153}
154
155MeshTexture::MeshTexture() {
156}
157