Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added models component and renderer an integrated them into RRN compo…
…nent with berry bush as example
- Loading branch information
1 parent
4827fa4
commit 792bd32
Showing
6 changed files
with
305 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--[[ | ||
this component is used in conjunction with the models component renderer to render additional models for an entity | ||
for example, to easily add/remove berries to/from a berry bush when it ripens or is harvested | ||
]] | ||
|
||
local ModelsComponent = class() | ||
|
||
function ModelsComponent:initialize() | ||
self._sv.models = {} | ||
end | ||
|
||
function ModelsComponent:add_model(name, options) | ||
self:set_model_options(name, options) | ||
end | ||
|
||
function ModelsComponent:remove_model(name) | ||
self._sv.models[name] = nil | ||
self.__saved_variables:mark_changed() | ||
end | ||
|
||
function ModelsComponent:set_model_options(name, options) | ||
local model = self._sv.models[name] | ||
options = radiant.shallow_copy(options) | ||
if options.visible == nil then | ||
options.visible = (not model or model.visible == nil) or model.visible | ||
end | ||
self._sv.models[name] = options | ||
self.__saved_variables:mark_changed() | ||
end | ||
|
||
function ModelsComponent:set_model_visibility(name, visible) | ||
local model = self._sv.models[name] | ||
if model then | ||
model.visible = visible | ||
self.__saved_variables:mark_changed() | ||
end | ||
end | ||
|
||
return ModelsComponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"components": { | ||
"stonehearth:renewable_resource_node": { | ||
"half_renewed_model": { | ||
"model": "/stonehearth/entities/plants/berry_bush/berry_bush.qb", | ||
"matrix": "Matrix_2", | ||
"scale": 0.15, | ||
"offset": {"x": 5, "y": 0, "z": 5} | ||
}, | ||
"renewed_model": { | ||
"model": "/stonehearth/entities/plants/berry_bush/berry_bush.qb", | ||
"matrix": "Matrix_2", | ||
"material": "materials/glowy_voxel.material.json", | ||
"scale": 0.16, | ||
"rotation": 180, | ||
"offset": {"x": 5.5, "y": 0, "z": 5.5} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
local Point3 = _radiant.csg.Point3 | ||
|
||
local ModelsRenderer = class() | ||
local log = radiant.log.create_logger('models.renderer') | ||
|
||
function ModelsRenderer:initialize(render_entity, datastore) | ||
self._entity = render_entity:get_entity() | ||
self._entity_node = render_entity:get_node() | ||
self._node = self._entity_node:add_group_node('model node') | ||
|
||
self._datastore = datastore | ||
self._model_nodes = {} | ||
|
||
self._datastore_trace = self._datastore:trace('drawing models') | ||
:on_changed(function () | ||
self:_update() | ||
end) | ||
:push_object_state() | ||
end | ||
|
||
function ModelsRenderer:destroy() | ||
self:_destroy_model_nodes() | ||
if self._node then | ||
self._node:destroy() | ||
self._node = nil | ||
end | ||
if self._datastore_trace and self._datastore_trace.destroy then | ||
self._datastore_trace:destroy() | ||
self._datastore_trace = nil | ||
end | ||
end | ||
|
||
function ModelsRenderer:_destroy_model_nodes() | ||
for _, node in ipairs(self._model_nodes) do | ||
node:destroy() | ||
end | ||
self._model_nodes = {} | ||
end | ||
|
||
function ModelsRenderer:_update() | ||
self:_destroy_model_nodes() | ||
|
||
local data = self._datastore:get_data() | ||
local models = data.models | ||
|
||
for _, model in pairs(models) do | ||
self:_create_node(model) | ||
end | ||
end | ||
|
||
function ModelsRenderer:_create_node(options) | ||
if options and options.model and options.visible then | ||
local rotation = options.rotation or 0 | ||
local offset = radiant.util.to_point3(options.offset) or Point3.zero | ||
local scale = options.scale or 0.1 | ||
if options.scale_with_entity then | ||
scale = scale * self._entity:get_component('render_info'):get_scale() | ||
end | ||
local node = _radiant.client.create_qubicle_matrix_node(self._node, options.model, options.matrix or 'background', offset) | ||
if node then | ||
node:set_transform(0, 0, 0, 0, rotation, 0, scale, scale, scale) | ||
node:set_material(options.material or 'materials/voxel.material.json') | ||
table.insert(self._model_nodes, node) | ||
else | ||
--log:error('nil result from create_qubicle_matrix_node "%s" with rotation %s', model, rotation) | ||
end | ||
end | ||
end | ||
|
||
return ModelsRenderer |