rosenfranz
Posts: 17
Joined: Sun Oct 26, 2014 11:19 am

Pi3D - 3D-Object on top of movable, screen-filling 2D-Plane

Mon Nov 24, 2014 2:15 pm

Hellohello.

I am trying to create a very simple scene with a colored, movable(!) 2d plane that lies in the background and serves as a canvas for a 3d Model that is positioned in front of it. In order to work with exact pixel values I use a 2d-Camera for the background plane.

As the plane is filling the whole screen it also covers the 3d model. This also is discribed in the pi3d FAQ, see "6. How do I display 2D images in front of a 3D scene? (or behind, for that matter)". That couldn't solve my problem, though. I tried to assign a huge z-value to my background plane plus I am using 2 different cameras as described in the FAQ. Still no 3d model.

Any ideas how this could be solved?

Code: Select all

#!/usr/bin/python
from __future__ import absolute_import, division, print_function, unicode_literals
""" Peter Hess' converted shader for pi3d dynamic texturing """
import demo
import pi3d

DISPLAY = pi3d.Display.create(w=1024, h=600, frames_per_second=30)

#map color values from 0 - 255 to 0.0 - 1.0
def map_color((r, g, b), in_min, in_max, out_min, out_max):
	rgb = (r, g, b)
	r = (r - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
	g = (g - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
	b = (b - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
	return (r, g, b)

#colors
yellow_int = (252, 213, 0)
yellow = map_color(yellow_int, 0, 255, 0.0, 1.0)
white = (1.0, 1.0, 1.0)

bgndSh = pi3d.Shader("mat_flat")
iconSh = pi3d.Shader("2d_flat")

CAMERA = pi3d.Camera()
CAMERA2D = pi3d.Camera(is_3d=False)
#tex = pi3d.Texture("textures/eden_logo.png", blend=True)

#colored background plane
planeBgnd = pi3d.Plane(camera=CAMERA2D, w=1024, h=600, z=100)
planeBgnd.set_material(yellow)
planeBgnd.set_shader(bgndSh)

#LOGO
endlessKnot = pi3d.Model(file_string='models/teapot.obj', camera=CAMERA, name='endless_knot', z=10)
endlessKnot.set_shader(bgndSh)
endlessKnot.set_material(white)

#planeIcon = pi3d.Plane(camera=CAMERA, x=0, y=0, z=200)

mykeys = pi3d.Keyboard()
while DISPLAY.loop_running():

	endlessKnot.draw()
	planeBgnd.draw()
	
	endlessKnot.rotateIncY( 1 )


	if mykeys.read() == 27:
		mykeys.close()
		DISPLAY.destroy()
		break

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D - 3D-Object on top of movable, screen-filling 2D-Pl

Mon Nov 24, 2014 5:38 pm

The orthographic camera works differently from the normal perspective one (I went round in circles getting it to work but ended up with a near, far = (0, 10000) and the mapping between the two includes a factor
10000/(10000 - z_ortho)
or z_ortho = 10000 (1 - 1/z). This seems to work

Code: Select all

...
def map_color(rgb, in_min, in_max, out_min, out_max):
   #rgb = (r, g, b) #had to do this (maybe python3 specific but seems more logical to me)
   r, g, b = rgb
...
#colored background plane
planeBgnd = pi3d.Plane(camera=CAMERA2D, w=1024, h=600, z=9900) #########
# see https://groups.google.com/forum/#!searchin/pi3d/2d/pi3d/LTVDjEQe5fs/lYaRI7xK4E8J
planeBgnd.set_material(yellow)
planeBgnd.set_fog((0.5, 0.5, 0.5, 1.0), 30000) #################### default fog is mid-grey at 5000 #####
# see http://pi3d.github.io/html/_modules/pi3d/Shape.html#Shape
# and http://pi3d.github.io/html/pi3d.html#pi3d.Shape.Shape.set_fog
planeBgnd.set_shader(bgndSh)
I will expand the FAQ entry and make it a bit more explicit!
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

rosenfranz
Posts: 17
Joined: Sun Oct 26, 2014 11:19 am

Re: Pi3D - 3D-Object on top of movable, screen-filling 2D-Pl

Tue Nov 25, 2014 10:16 am

Thanks a lot!
I have to admit that I (so far) did not really understand your workaround, Paddy. But I'll try to get my head around it.
Ah and thanks for improving my color mapping function. I'm quite new to python so there's still a lot to learn…
This is part of my masters project and further questions are quite likely to arise on the path.

So I really appreciate the dedication of people like you Paddy, to helping people use Pi3D which is an awesome package.

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Pi3D - 3D-Object on top of movable, screen-filling 2D-Pl

Tue Nov 25, 2014 12:58 pm

I've added a bit to the FAQ that might help understand it.

Obviously it doesn't matter in the context of your code but something you are bound to come across in python is the 'list comprehension' which can speed up for loops quite a bit, so your function becomes

Code: Select all

def map_color(rgb, in_min, in_max, out_min, out_max):
   return [(component - in_min) * (out_max - out_min) / (in_max - in_min) + out_min for component in rgb]
# or put in line
yellow = [(c - 0) * (1.0 - 0.0) / (255 - 0) + 0.0 for c in yellow_int]
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”