Multimesh3d camera orthogonal, error render top down 2D
Asked Answered
R

3

0

Good morning Community.

I tell you my problem.

I am using multimesh3d, to create multiple instances with a shader that makes cuts to the image, depending on the position and size of the subimage. My code Shader
`shader_type spatial;
uniform sampler2D main_texture;

uniform float transparency : hint_range(0.0, 1.0) = 0.0;
varying float x;
varying float y;
varying float ancho;
varying float alto;
varying float zorder;
void vertex() {
x= INSTANCE_CUSTOM.r;
y= INSTANCE_CUSTOM.g;
ancho = INSTANCE_CUSTOM.b;
alto = INSTANCE_CUSTOM.a;
}
void fragment() {

vec2 frameSize = vec2(ancho, alto);	
vec2 frameOffset = vec2(x,y);

vec2 texCoord = UV * frameSize + frameOffset;

vec4 texColor = texture(main_texture, texCoord);
// Aplica el color del segmento al resultado final
texColor.a *= 1.0 - transparency;
ALBEDO = texColor.rgb * texColor.rgb;
ALPHA = texColor.a;

}`
But I have the problem when viewing the instances with an orthogonal 3D camera, with position (0,0,40) and rotation (0,0,0) I am simulating a 2D visualization in 3D.

The problem arises when rendering the instances, for each instance that is drawn. I use the Z coordinate, to use it as depth, that is, while Z is larger, it should be rendered above Z, which is smaller.

But the problem arises there since it does not respect the rendering order, I understand that it is due to the camera mode in orthographic mode, in position 0,0,0. What it does is always render the smaller instance above the larger instance.

I don't understand the reason, and if anyone knows how to configure the camera or perhaps the shader so that the depth of the instance is taken correctly

If we move the camera to see another perspective, I see that it really draws it well, the problem is when the camera remains at 0,0,0

Camara with rotation

Camara without rotation

I hope you can help me.
regards.

Recidivate answered 7/11, 2023 at 0:13 Comment(0)
G
0

zorder I see you declaring it but never assigning it and never using it, so I'm not sure exactly what you are doing.

Guncotton answered 7/11, 2023 at 1:11 Comment(0)
R
0

I solved it after trying quite a few things, it was fixed using ALPHA_SCISSOR_THRESHOLD = 0.5;

Caracara The z order is not necessary with the z coordinate of the transform that is used to declare the instance is resolved, the detail was to add ALPHA_SCISSOR_THRESHOLD, so that it segments based on the orthogonal camera.

The shader code would look like this:

`
shader_type spatial;

uniform sampler2D main_texture;

uniform float transparency : hint_range(0.0, 1.0) = 0.0;
varying float x;
varying float y;
varying float ancho;
varying float alto;
void vertex() {
x= INSTANCE_CUSTOM.r;
y= INSTANCE_CUSTOM.g;
ancho = INSTANCE_CUSTOM.b;
alto = INSTANCE_CUSTOM.a;
}
void fragment() {
vec2 frameSize = vec2(ancho, alto);
vec2 frameOffset = vec2(x,y);
vec2 texCoord = UV * frameSize + frameOffset;
vec4 texColor = texture(main_texture, texCoord);
// Aplica el color del segmento al resultado final
texColor.a *= 1.0 - transparency;
ALPHA_SCISSOR_THRESHOLD = 0.5;
ALBEDO = texColor.rgb * texColor.rgb;
ALPHA = texColor.a ;
}
`

regardssssssssssssss. 🙂

Recidivate answered 7/11, 2023 at 2:10 Comment(0)
M
0

Recidivate Hello again. I don't exactly understand what you are looking for.
Looking at your shader, the vaying zorder is doing nothing.
since you are in 3D, the position of the camera matters, objects will be rendered in front of the camera, they will just not be deformed by perspective (multiplied by the perspective matrix). but objects behind the camera should not show.

there are many problems with the shader you posted, I fixed it:
the main problem was the lack of ALPHA_SCISSOR_THRESHOLD, it now renders objects according to their position in Z.

shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back;
uniform sampler2D mainTexture : source_color, filter_linear_mipmap,repeat_enable;//CHANGE THIS IF THE IMAGE LOOKS BLURRY
uniform float alpha_scissor_threshold = 0.5;
uniform vec2 uvs = vec2(0.125, 0.125);//this is the size of each cell, if they are all the same size
varying vec2 texCoord;

void vertex()
{
	texCoord = UV * uvs + vec2(INSTANCE_CUSTOM.r, INSTANCE_CUSTOM.g);//you have 2 floats to do whatever you want with, R and G  control the shift in X and Y
}

void fragment()
{
	vec4 texColor = texture(mainTexture, texCoord);
	ALBEDO = texColor.rgb;//if you need to apply color correction, it is better to do it in Environment or in the texture
	ALPHA = texColor.a;//this shader is missing you transparency uniform. you will be unable to get both transparency and correct sorting.
	ALPHA_SCISSOR_THRESHOLD = alpha_scissor_threshold;
}

this is the test script, it renders 4 instances shifted in X, Y and Z:

extends MultiMeshInstance3D

func _ready():
	var mmesh : MultiMesh = self.multimesh
	mmesh.instance_count = 4
	for i in mmesh.instance_count:
		mmesh.set_instance_custom_data(i, Color(0.125, 0.0, 0.0, 0.0))
		mmesh.set_instance_transform(i, self.transform.translated(Vector3(i * 0.2, i * 0.1, i * -1.0)))


as you can see, objects are rendered from left to right, but objects in front cover the ones behind.

Mendicity answered 7/11, 2023 at 2:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.