Stingray Forum (Read Only)
Welcome to Autodesk’s Stingray Forums. Share your knowledge, ask questions, and explore popular Stingray topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ShadingEnvironment - how to make a change work

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Anonymous
903 Views, 6 Replies

ShadingEnvironment - how to make a change work

Hi,

 

I have been trying to change the ShadingEnvironment

 

The code I am using as a template is the example included in the source documentation, but I have changed "sun_direction" to "skydome_intensity" and changed the set_vector3 to set_scalar.

 

function render()

     stingray.ShadingEnvironment.blend(self.shading_environment, {"default", 1.0})

     stingray.ShadingEnvironment.set_scalar(self.shading_environment, "skydome_intensity", 0.2) 

     stingray.ShadingEnvironment.apply(self.shading_environment)

     stingray.Application.render_world(self.world, self.camera, self.viewport, self.shading_environment)

end

 

Whatever value I change "skydome_intensity" to has no effect to the look of the scene render. It just ignores the value changes.

 

Any ideas on where I am going wrong?

 

Thanks

 

Brian

6 REPLIES 6
Message 2 of 7
lumonix
in reply to: Anonymous

the lua commands you are using only work on 'old' shading env objects.

Does your level have a 'new' shading env entity?

 

(one that has components etc?)

 

If so, then these lua commands don't work on it.

(which imo is kinda of a bug on our end, but that is still being debated)

 

I'm going to dump a bunch of lua code here to show you how to set the properties for the 'new' shading env entity.

This is pretty sucky and hopefully we will provide a nicer interface for this in the future (or just make the old lua calls work with the new entity)

 

-- Cached Engine Modules
local Application = stingray.Application
local Keyboard = stingray.Keyboard
local Vector3 = stingray.Vector3
local Quaternion = stingray.Quaternion
local World = stingray.World
local Unit = stingray.Unit
local Window = stingray.Window
local World = stingray.World
local Level = stingray.Level
local EntityManager = stingray.EntityManager

Game = Game or {}
local Game = Game

local shading_env_entity = nil
local exposure_component = nil

function gather_shading_env_params()
	-- this tries to find shading environment variables from the shading env entity
	-- in the future, there may be better API to handle this, but for now we loop all entities.
	local data_component_manager = EntityManager.data_component(Game.world)
	local all_entity_handles = World.entities(Game.world)
	 
	for _, entity_handle in ipairs(all_entity_handles) do
	    local all_data_component_handles = {data_component_manager:instances(entity_handle)} -- Returns values on stack, so wrap in {} to get array.
	 
	    for _, data_component_handle in ipairs(all_data_component_handles) do
	        -- Missing properties return nil, so this is "safe".
	        local shading_environment_mapping_resource_name = data_component_manager:get_property(entity_handle, data_component_handle, {"shading_environment_mapping"})
	 
	        if shading_environment_mapping_resource_name == "core/stingray_renderer/shading_environment_components/exposure" then

	        	if (shading_env_entity == nil) then
	        		shading_env_entity = entity_handle
	        	end

	        	exposure_component = data_component_handle;
	        end
	    end
	end
end

function update_shading_env_entity()
	-- Arrays are immutable - must replace whole array, not a component.
	-- Thus, colors, vectors and ranges are set using whole arrays.
	-- Refer to "core/stingray_renderer/shading_environment_components/fog.component" for available properties.
	--local fog_rgb_linear = {1.0, 0.5, 0.0}
	--local fog_alpha = 1.0
	--local fog_depth_range = {20, 150}
	--data_component_manager:set_property(entity_handle, data_component_handle, {"fog_color", "rgb"}, fog_rgb_linear)
	--data_component_manager:set_property(entity_handle, data_component_handle, {"fog_color", "alpha"}, fog_alpha)
	--data_component_manager:set_property(entity_handle, data_component_handle, {"fog_depth_range"}, fog_depth_range)         
	if (shading_env_entity ~= nil) then
		local data_component_manager = EntityManager.data_component(Game.world)

		if (exposure_component ~= nil) then
			local exposure_val = 0.2
			data_component_manager:set_property(shading_env_entity, exposure_component, {"exposure"}, exposure_val)
		end
	end
end

function init()
	if LEVEL_EDITOR_TEST and not LEVEL_EDITOR_TEST_READY then
		print("Waiting for test level initialization...")
		return
	end

	Game.world = Application.new_world()

	Game.viewport = Application.create_viewport(Game.world, "default")

	local level = World.load_level(Game.world, "content/levels/empty")
	Level.spawn_background(level)
	Level.trigger_level_loaded(level)

	stingray.Window.set_clip_cursor(true)
	stingray.Window.set_mouse_focus(true)
	stingray.Window.set_show_cursor(true)

	-- even though we have a entitiy shading env in our levels, we still need the 'old' shading environment to pass to render_world in render()
	-- values set in the shading env entity will automatically override the values in the old shading env.
	-- so to get values to change in your level, you need to update them in the entity
	if Level.has_data(level, "shading_environment") then
		print("level has shading env")
		World.set_shading_environment(Game.world, Game.shading_environment, Level.get_data(level, "shading_environment"))
	else
		Game.shading_environment = World.create_shading_environment(Game.world, "core/stingray_renderer/environments/midday/midday")
	end

	Game.camera_unit = World.spawn_unit(Game.world, "core/units/camera")

	gather_shading_env_params()

	--Game.model_unit = stingray.World.unit_by_name(Game.world, "some_model");
	--model_position = stingray.Unit.local_position(Game.model_unit, 1);
end

function update(dt)
	if LEVEL_EDITOR_TEST and not LEVEL_EDITOR_TEST_READY then return end

	update_shading_env_entity()

	Game.world:update(dt)
end

function render()
	if LEVEL_EDITOR_TEST and not LEVEL_EDITOR_TEST_READY then return end

	local camera = Unit.camera(Game.camera_unit, "camera")
	Application.render_world(Game.world, camera, Game.viewport, Game.shading_environment)
end
Message 3 of 7
Anonymous
in reply to: lumonix

Hi Lumonix,

 

Thanks for the reply and explanation of why the code didn't work. 

 

I will do as you suggest and use your code as a template to move forward.

 

You help is appreciated.

 

Cheers

 

Brian

 

Message 4 of 7
Anonymous
in reply to: Anonymous

Is this code still relevant in the latest version (1.1.247) ?

 

I'm trying to do something similar to change the skydome intensity from a script but can't get it to work. Some variables, like Game, seem to not exist anymore so I don't know where to get the world from.

 

 

Message 5 of 7
Anonymous
in reply to: Anonymous

Hi Elifer,

 

Yes the code still works.

 

I have created my own Shading Environment class called ShadingEnvParam.lua which is attached to this post. Rename it from ShadingEnvParam.lua.txt to ShadingEnvParam.lua, we are not allowed to upload lua attachments! Hopefully this code will help you.

 

To use the class just add the following lines of code into your project at the relevant places. You mention the variable Game not existing. Game is just a table variable used to hold many of the initial objects created at init(). So, add the following 5 lines of code into your project.

 

 

-- to include the class into your codebase

require 'ShadingEnvParam'

 

-- create a variable we will use to change the sky dome
local shadingVar = nil

 

-- in the init() function you need to initialize shadingVar

-- Game.world is a variable initialized before (usually in init) with Game.world = Application.new_world()

shadingVar = ShadingEnvParam.Init(Game.world)

 

-- in the update() function add these 2 lines before  Game.world:update(dt)

 shadingVar:ChangeComponent("core/stingray_renderer/shading_environment_components/global_lighting")
 shadingVar:Update({{"skydome_intensity"}, 0.1}) 

 

 

And that should do it. I added those exact 5 lines of code into the project available here https://forums.autodesk.com/t5/stingray/stingray-l​ua-memento-project-video/td-p/5803936 and it worked fine. I got a nice dark sky.

 

Hope this helps you.

 

Brian

 

Message 6 of 7
dan.matlack
in reply to: Anonymous

@Anonymous Please let us know if the above helps you out or if you still need some assistance. 🙂

______________________________________
Dan Matlack
Senior Content Manager || Games Solutions
Autodesk, Inc.
Message 7 of 7
Anonymous
in reply to: dan.matlack

Sorry for the delay in answering. I actually managed to get the original code working by removing references to Game, and getting the world from SimpleProject instead.

 

I will try the code you posted anyway, see if it's a cleaner solution.

 

Thanks!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report