Shader to Map (GD 3)
Asked Answered
C

4

0

Hey! I was working on procedural generation to world maps, and I'm at a point where I'm stuck I have a shader that can take in noise and apply a gradient to it for a nice looking island, but I'm not sure how I can output that back to be read into a height map.

This is an example of the shader-

Is this even possible? The only way I can think to do it is by returning the color to a float, similar to vertex displacement with shaders.

The real problem with this is that as far as I can tell the only way to average values between openSimplexNoise and a gradient is through shaders, so if there's an alternative method for doing so, that would also be very helpful.

Combine answered 28/4, 2023 at 14:58 Comment(0)
R
0

Combine You don't need to go via the shader. You can do remapping/thresholding/sampling completely on the cpu side (bilinear pixel sampling is trivial to implement). It'd be a bit slower but still fine if it's an one-off operation.

With a shader, simply render a quad/sprite into a custom viewport and transfer viewport's texture into an Image object. Image's pixels can then be accessed by the cpu and used for whatever you need them.

Ruff answered 28/4, 2023 at 15:14 Comment(0)
C
0

Ruff
Thanks!

My only problem is that, despite doing everything I can tell its not returning the data properly. For some reason, even though I've locked it and waited for after the first render to sample, it seems to be returning an all transparent image every time. I can see it in the editor, but in game its completely broken.

func generateIsland(per, oct):
	var gridName = {}

	yield(VisualServer, "frame_post_draw")
	var mat = $ShaderProcess/ShaderProcess.get_material()
	var island = shaderProcess.get_texture().get_data()
	island.lock()
	$Sprite.texture = ImageTexture.new().create_from_image(island)
	
	for x in width:
		for z in height:
			var rand = (island.get_pixel(x, z))
			gridName[Vector2(x, z)] = rand.r
			
			if(rand != Color(0,0,0,0)):
				print(rand)
	
	return gridName

thats my code, when I run it I have no output and the testing sprite is always transparent.

Any ideas?

Combine answered 1/5, 2023 at 14:58 Comment(0)
R
0

Combine Try to set viewport's update mode to Always.

Ruff answered 1/5, 2023 at 15:37 Comment(0)
C
0

Ruff
that did it thanks!

Combine answered 1/5, 2023 at 16:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.