Model loaded from a file in the asset folder

In case of error, error is set accordingly. If the error is on the mesh, mesh is set to a default new Cube. If the material is missing or it failed to load, material is set to the blueish new Material.

Introduced properties

Redefined properties

redef type SELF: ModelAsset

gamnit $ ModelAsset :: SELF

Type of this instance, automatically specialized in every class
redef init init

gamnit $ ModelAsset :: init

redef fun leaves: Array[LeafModel]

gamnit $ ModelAsset :: leaves

All LeafModel composing this model
redef fun load

gamnit $ ModelAsset :: load

Load this model in memory
redef fun named_parts: Map[Text, Model]

gamnit $ ModelAsset :: named_parts

Sub-models with names, usually declared in the asset file

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun center: Point3d[Float]

gamnit :: Model :: center

Center coordinates of all the vertices
fun center=(center: Point3d[Float])

gamnit :: Model :: center=

Center coordinates of all the vertices
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
init defaultinit(path: String)

app :: Asset :: defaultinit

fun dimensions: Point3d[Float]

gamnit :: Model :: dimensions

Dimensions of the bounding box containing all vertices
fun dimensions=(dimensions: Point3d[Float])

gamnit :: Model :: dimensions=

Dimensions of the bounding box containing all vertices
fun errors: Array[Error]

gamnit :: Model :: errors

Errors raised at loading
protected fun errors=(errors: Array[Error])

gamnit :: Model :: errors=

Errors raised at loading
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
abstract fun leaves: Array[LeafModel]

gamnit :: Model :: leaves

All LeafModel composing this model
fun load

gamnit :: Model :: load

Load this model in memory
fun max: Point3d[Float]

gamnit :: Model :: max

Maximum coordinates of all vertices on each axes
protected fun max=(max: Point3d[Float])

gamnit :: Model :: max=

Maximum coordinates of all vertices on each axes
fun min: Point3d[Float]

gamnit :: Model :: min

Minimum coordinates of all vertices on each axes
protected fun min=(min: Point3d[Float])

gamnit :: Model :: min=

Minimum coordinates of all vertices on each axes
fun named_parts: Map[Text, Model]

gamnit :: Model :: named_parts

Sub-models with names, usually declared in the asset file
protected fun named_parts=(named_parts: Map[Text, Model])

gamnit :: Model :: named_parts=

Sub-models with names, usually declared in the asset file
init new(path: Text): Model

gamnit :: Model :: new

Prepare to load a model from the assets folder
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun path: String

app :: Asset :: path

Path to this asset within the assets folder
protected fun path=(path: String)

app :: Asset :: path=

Path to this asset within the assets folder
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram gamnit::ModelAsset ModelAsset gamnit::Model Model gamnit::ModelAsset->gamnit::Model app::Asset Asset gamnit::ModelAsset->app::Asset core::Object Object gamnit::Model->core::Object app::Asset->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class Asset

app :: Asset

Resource from the assets folder
abstract class Model

gamnit :: Model

3D model composed of Mesh and Material, loaded from the assets folder by default

Class definitions

gamnit $ ModelAsset
# Model loaded from a file in the asset folder
#
# In case of error, `error` is set accordingly.
# If the error is on the mesh, `mesh` is set to a default `new Cube`.
# If the material is missing or it failed to load, `material` is set to the blueish `new Material`.
class ModelAsset
	super Model
	super Asset

	init do models.add self

	private var loaded = false

	redef fun load
	do
		if loaded then return

		var ext = path.file_extension
		if ext == "obj" then
			load_obj_file
		else
			errors.add new Error("Model at '{path}' failed to load: Extension '{ext or else "null"}' unrecognized")
		end

		if leaves_cache.is_empty then
			# Nothing was loaded, use a cube with the default material
			var leaf = placeholder_model
			leaves_cache.add leaf
		end

		loaded = true
	end

	private fun lazy_load
	do
		if loaded then return

		# Lazy load
		load

		# Print errors when lazy loading only
		if errors.length == 1 then
			print_error errors.first
		else if errors.length > 1 then
			print_error "Loading model at '{path}' raised {errors.length} errors:\n* "
			print_error errors.join("\n* ")
		end
	end

	private fun load_obj_file
	do
		# Read .obj description from assets
		var text_asset = new TextAsset(path)
		var content = text_asset.to_s
		if content.is_empty then
			errors.add new Error("Model failed to load: Asset empty at '{self.path}'")
			leaves_cache.add new LeafModel(new Cube, new Material)
			return
		end

		# Parse .obj description
		var parser = new ObjFileParser(content)
		var obj_def = parser.parse
		if obj_def == null then
			errors.add new Error("Model failed to load: .obj format error on '{self.path}'")
			leaves_cache.add new LeafModel(new Cube, new Material)
			return
		end

		# Check for errors
		if debug_gamnit then assert obj_def.is_coherent

		# Build models
		var converter = new BuildModelFromObj(path, obj_def)
		converter.fill_leaves self
		errors.add_all converter.errors
	end

	redef fun leaves
	do
		lazy_load
		return leaves_cache
	end

	private var leaves_cache = new Array[LeafModel]

	redef fun named_parts
	do
		lazy_load
		return named_leaves_cache
	end

	private var named_leaves_cache = new Map[String, Model]
end
lib/gamnit/depth/more_models.nit:31,1--124,3