1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Portable game to destroy asteroids
16 module model_viewer
is
17 app_name
"Model Viewer"
18 app_namespace
"org.nitlanguage.model_viewer"
19 app_version
(1, 0, git_revision
)
21 android_manifest_activity
"""android:screenOrientation="landscape""""
31 # All available models
32 var models: Array[Model] = [
33 new LeafModel(new Cube, new SmoothMaterial.default),
34 new LeafModel(new Mesh.uv_sphere(4.0, 32, 16), new SmoothMaterial.default),
35 new LeafModel(new Mesh.uv_sphere(4.0, 32, 16), new NormalsMaterial),
36 new Model("models
/Tree_01.obj
"),
37 new Model("models
/Oak_Fall_01.obj
"),
38 new Model("models
/Quandtum_BA-2_v
1_1.obj
"),
41 # Index of the current model in `models`
44 # Texture "Previous model
"
45 var ui_prev = new Texture("ui
/prev
.png
")
47 # Texture "Next model
"
48 var ui_next = new Texture("ui
/next
.png
")
55 var logo = new Texture("splash
.png
")
56 show_splash_screen logo
58 if model_path.has_prefix("assets
/") then model_path = model_path.substring_from(7)
59 # Load all models passed as command line argument
60 for arg in args.to_a.reversed do
62 var model = new Model(arg)
66 world_camera.near = 0.1
67 world_camera.far = 100.0
69 for model in models do model.load
70 for texture in asset_textures_by_name.values do texture.load
72 # Display the first model
73 model = models[model_index]
76 # Use 800 px in height as screen reference
77 ui_camera.reset_height 800.0
79 var prev_sprite = new Sprite(ui_prev,
80 ui_camera.bottom_left.offset(200, 40, 0))
81 prev_sprite.scale = 0.5
82 ui_sprites.add prev_sprite
84 var next_sprite = new Sprite(ui_next,
85 ui_camera.bottom_right.offset(-165, 40, 0))
86 next_sprite.scale = 0.5
87 ui_sprites.add next_sprite
90 # Set the currently displayed model
91 fun model=(model: Model)
93 var actor = new Actor(model, new Point3d[Float](0.0, 0.0, 0.0))
95 model = model.leaves.first
96 actor.center.x -= model.mesh.center.x
97 actor.center.y -= model.mesh.center.y
98 actor.center.z -= model.mesh.center.z
100 var height = model.mesh.dimensions.y
101 world_camera.reset_height(height * 2.5)
107 # Cycle to the next or previous model, changing the index by `d`
108 fun cycle_model(d: Int)
110 model_index = (model_index + d + models.length) % models.length
111 model = models[model_index]
114 redef fun accept_event(event)
116 var display = display
117 if display == null then return super
119 if event isa QuitEvent then
121 else if event isa KeyEvent and event.is_down then
122 if event.is_arrow_right then
124 else if event.is_arrow_left then
127 else if event isa PointerEvent and event.depressed then
128 if event.x.to_i > display.width / 2 then
136 private var clock = new Clock
142 var t = clock.total.to_f
145 actors.first.rotation = t
147 # Move the light source
148 var dist_to_light = 20.0
150 light.position.x = dist_to_light * t.cos
151 light.position.y = 4.0
152 light.position.z = dist_to_light * t.sin