Modify the shader to show the right color

Use the common MeshData instead of trimesh.
This commit is contained in:
Diego Prado Gesto 2019-03-01 10:56:17 +01:00
parent 9be7ddde2f
commit 102bef9a4f
2 changed files with 35 additions and 15 deletions

View File

@ -4,6 +4,9 @@
import random import random
from typing import Optional, TYPE_CHECKING from typing import Optional, TYPE_CHECKING
from PyQt5.QtCore import QBuffer
from UM.Mesh.MeshBuilder import MeshBuilder
from UM.Qt.QtApplication import QtApplication from UM.Qt.QtApplication import QtApplication
from UM.Math.Vector import Vector from UM.Math.Vector import Vector
from UM.Math.Color import Color from UM.Math.Color import Color
@ -47,36 +50,45 @@ class PickingPass(RenderPass):
# Fill up the batch with objects that can be sliced. ` # Fill up the batch with objects that can be sliced. `
for node in DepthFirstIterator(self._scene.getRoot()): #type: ignore #Ignore type error because iter() should get called automatically by Python syntax. for node in DepthFirstIterator(self._scene.getRoot()): #type: ignore #Ignore type error because iter() should get called automatically by Python syntax.
if node.callDecoration("isSliceable") and node.getMeshData() and node.isVisible(): if node.callDecoration("isSliceable") and node.getMeshData() and node.isVisible():
tri_node = node.getMeshData().toTrimesh() faces = node.getMeshData().getIndices()
for index, face in enumerate(tri_node.faces): vertices = node.getMeshData().getVertices()
normal_vertex = tri_node.face_normals[index] normals = node.getMeshData().getNormals()
print("Faces:", faces)
print("Vertices", vertices)
for index, face in enumerate(faces):
normal_vertex = normals[index]
triangle_mesh = vertices[face]
print(face, normal_vertex, triangle_mesh)
batch.addItem(transformation = node.getWorldTransformation(), mesh = node.getMeshData(), uniforms = { "selection_color": self._getFaceColor(face, normal_vertex)}) batch.addItem(transformation = node.getWorldTransformation(), mesh = node.getMeshData(), uniforms = { "selection_color": self._getFaceColor(face, normal_vertex)})
self.bind() self.bind()
batch.render(self._scene.getActiveCamera()) batch.render(self._scene.getActiveCamera())
self.release() self.release()
def _getFaceColor(self, face, normal_vertex): def _getFaceColor(self, face: Vector, normal_vertex: Vector) -> Color:
while True: while True:
r = random.randint(0, 255) r = random.randint(0, 255)
g = random.randint(0, 255) g = random.randint(0, 255)
b = random.randint(0, 255) b = random.randint(0, 255)
a = 0 a = 255
color = Color(r, g, b, a) color = Color(r, g, b, a)
if color not in self._selection_map: if color not in self._selection_map:
break break
self._selection_map[color] = normal_vertex print("Adding color: {color} - {normal}".format(color = color, normal = normal_vertex))
self._selection_map[color] = {"face": id(face), "normal_vertex": normal_vertex}
return color return color
## Get the normal vector at a certain pixel coordinate. ## Get the normal vector at a certain pixel coordinate.
def getPickedNormalVertex(self, x: int, y: int) -> float: def getPickedNormalVertex(self, x: int, y: int) -> Optional[Vector]:
print(self._selection_map)
output = self.getOutput() output = self.getOutput()
print("Creating image")
output.save("thumbnail.png")
window_size = self._renderer.getWindowSize() window_size = self._renderer.getWindowSize()
px = (0.5 + x / 2.0) * window_size[0] px = (0.5 + x / 2.0) * window_size[0]
@ -87,7 +99,10 @@ class PickingPass(RenderPass):
pixel = output.pixel(px, py) pixel = output.pixel(px, py)
print("######### ", x, y, pixel, Color.fromARGB(pixel)) print("######### ", x, y, pixel, Color.fromARGB(pixel))
return self._selection_map.get(Color.fromARGB(pixel), None) face = self._selection_map.get(Color.fromARGB(pixel), None)
if not face:
return None
return face.get("normal_vertex", None)
## Get the distance in mm from the camera to at a certain pixel coordinate. ## Get the distance in mm from the camera to at a certain pixel coordinate.
def getPickedDepth(self, x: int, y: int) -> float: def getPickedDepth(self, x: int, y: int) -> float:

View File

@ -17,6 +17,7 @@ vertex =
fragment = fragment =
uniform highp vec3 u_viewPosition; uniform highp vec3 u_viewPosition;
uniform lowp vec4 u_color;
varying highp vec3 v_vertex; varying highp vec3 v_vertex;
@ -29,8 +30,9 @@ fragment =
encoded.g = floor((distance_to_camera - encoded.r * 65536.0) / 256.0); encoded.g = floor((distance_to_camera - encoded.r * 65536.0) / 256.0);
encoded.b = floor(distance_to_camera - encoded.r * 65536.0 - encoded.g * 256.0); encoded.b = floor(distance_to_camera - encoded.r * 65536.0 - encoded.g * 256.0);
gl_FragColor.rgb = encoded / 255.; //gl_FragColor.rgb = encoded / 255.;
gl_FragColor.a = 1.0; //gl_FragColor.a = 1.0;
gl_FragColor = u_color;
} }
vertex41core = vertex41core =
@ -53,6 +55,7 @@ vertex41core =
fragment41core = fragment41core =
#version 410 #version 410
uniform highp vec3 u_viewPosition; uniform highp vec3 u_viewPosition;
uniform lowp vec4 u_color;
in highp vec3 v_vertex; in highp vec3 v_vertex;
@ -67,8 +70,9 @@ fragment41core =
encoded.g = floor((distance_to_camera - encoded.r * 65536.0) / 256.0); encoded.g = floor((distance_to_camera - encoded.r * 65536.0) / 256.0);
encoded.b = floor(distance_to_camera - encoded.r * 65536.0 - encoded.g * 256.0); encoded.b = floor(distance_to_camera - encoded.r * 65536.0 - encoded.g * 256.0);
frag_color.rgb = encoded / 255.; //frag_color.rgb = encoded / 255.;
frag_color.a = 1.0; //frag_color.a = 1.0;
frag_color = u_color;
} }
[defaults] [defaults]
@ -78,6 +82,7 @@ u_modelMatrix = model_matrix
u_viewProjectionMatrix = view_projection_matrix u_viewProjectionMatrix = view_projection_matrix
u_normalMatrix = normal_matrix u_normalMatrix = normal_matrix
u_viewPosition = view_position u_viewPosition = view_position
u_color = selection_color
[attributes] [attributes]
a_vertex = vertex a_vertex = vertex