I’m interested in in being able to define the Craft scene’s default (main) camera viewport so that I can partition the display into a region that displays a 3D scene (a viewport) at the same time as other 2D objects (e.g. Obj C bridge created textbox and web browser). I thought I could do so with the following example code:
(Note: Mutant is a 3D model I imported; in the example below I was hoping to confine the 3D viewport to the the lower half of the middle 1/3 of the display)
However, Codea gives me an error saying that I can’t use the viewport method on the scene.camera. Does the camera:viewport() method only work on the cameras added as components to entities and not the main scene camera itself?
If not, is there another away I can change the main camera’s viewport? It would be nice to be able to set the main camera’s viewport so that I wouldn’t need to worry about my 3D objects bumping into my 2D objects on the screen.
I appreciate any suggestions the community might have.
@SugarRay the object returned by scene.camera is actually an entity, what you want is the camera component which is scene.camera:get(craft.camera). Also viewport() takes normalized screen coordinates (so for SCREEN/3 you use 1.0/3.0)
I therefore revised my above code to show what worked. To further illustrate that the viewport() method on the camera component takes relative values for all its parameters, I also revised my code to show that.
I’ve attached a screenshot of the finished product: an objective C bridge created WKWebView web browser window at the top left of the display, an objective C bridge created textbox to the top right of the display, and a Codea Craft 3D scene containing a mutant entity’s at the bottom of the display (specifically with left border at 0, bottom border starting at 60 y pixels up from the bottom of the display, and extending halfway up and the full width of the display) containing a mutant entity:
Mutant = scene:entity()
Mutant.model = craft.model(asset.documents.MutantMesh)
Mutant.material = craft.material(asset.builtin.Materials.Basic)
Mutant.material.map = readImage(asset.documents.Mutant_diffuse)
Mutant.scale = vec3(0.005,0.005,0.005)
– change the main camera position by “camera” property of scene
scene.camera.z = -4
– to actually change the camera’s function (e.g. it’s viewport) need to actually get the camera element of camera element
scene.cameraElement = scene.camera:get(craft.camera)
– to change the scene camera’s viewport (i.e. where 3D scene drawn on display) pass relative values x,y,width,height
scene.cameraElement:viewport((0/HEIGHT),(60/HEIGHT),(1*(HEIGHT/HEIGHT)),(0.5*(HEIGHT/HEIGHT)))
Sorry, I realized there are other errors in last line last line; it shoud read:
scene.cameraElement:viewport((0/WIDTH),(60/HEIGHT),(1(WIDTH/WIDTH)),(0.5*(HEIGHT/HEIGHT))
Hi, @Bri_G. I was just trying to emphasize to other novices (like me :-)) that the viewport function takes relative dimensions since most other Craft functions (coordinates, sizes) do not use relative dimensions in their parameters.