Changing Material by Laser Pointer

Another way to extend functionality is to add a new action for the object.

We have a laser pointer that we can grab, throw, and use (turn on a laser beam). Now, let's add an alternative use action — change the pointed object's material. This action will be executed when we press and hold the Grip side button on one controller while using the laser pointer gripped by the opposite controller.

The ObjLaserPointer component implements the base functionality of the laser pointer.

The base class (interface) for all interactive objects is VRInteractable (vr/Components/VRInteractable.h). It defines all available interactions via virtual methods: grabIt, holdIt, useIt, throwIt. Components implementing VRInteractable must override these methods.

So, we need to add a new action and map it to a button combination (the Grip side button is pressed on one controller while the laser pointer is gripped by the opposite controller):

  1. Add a new useItAlt action (alternative use) to the VRInteractable interface:

    Source code (C++)
    
    #pragma once
    
    #include "Players/VRController.h"
    
    #include <UnigineComponentSystem.h>
    #include <UnigineNode.h>
    #include <UniginePhysics.h>
    
    class VRPlayer;
    
    class VRInteractable
    {
    public:
    	virtual ~VRInteractable() = default;
    
    	// interact methods
    	virtual void grabIt(VRPlayer *player, VRController::Side side) {}
    	virtual void holdIt(VRPlayer *player, VRController::Side side) {}
    	virtual void useIt(VRPlayer *player, VRController::Side side) {}
    	virtual void throwIt(VRPlayer *player, VRController::Side side) {}
    	${#HL}$virtual void useItAlt(VRPlayer *player, VRController::Side side) {} ${HL#}$
    };
    					
  2. Add an override of useItAlt() to the ObjLaserPointer component. In the update() method, implement the material switching logic:

    Source code (C++)
    
    #pragma once
    #include <UnigineWorld.h>
    #include "vr/Components/VRInteractable.h"
    
    class ObjLaserPointer : public VRInteractable, public Unigine::ComponentBase
    {
    public:
    	COMPONENT_DEFINE(ObjLaserPointer, Unigine::ComponentBase);
    	COMPONENT_INIT(init, InitOrder::COMMON_LOGIC);
    	COMPONENT_UPDATE(update, 1);
    
    	// parameters
    	PROP_PARAM(Node, laser);
    	PROP_PARAM(Node, laser_ray);
    	PROP_PARAM(Node, laser_hit);
    
    	${#HL}$// array of materials to be used for switching
    	PROP_ARRAY(Material, materials); ${HL#}$
    
    	// interact methods
    	void useIt(VRPlayer* player, VRController::Side side) override;
    	void grabIt(VRPlayer* player, VRController::Side side) override;
    	void throwIt(VRPlayer* player, VRController::Side side) override;
    	${#HL}$void useItAlt(VRPlayer* player, VRController::Side side) override; ${HL#}$
    
    protected:
    	void init();
    	void update();
    
    private:
    	Unigine::ObjectTextPtr obj_text;
    	Unigine::Math::Mat4 laser_ray_mat;
    	int grabbed{0};
    	${#HL}$bool alt_use = false;
    	int current_material = 0; ${HL#}$
    	Unigine::WorldIntersectionPtr intersection = Unigine::WorldIntersection::create();
    };
    					
    Source code (C++)
    
    #include "ObjLaserPointer.h"
    #include "../Global/ObjectLabeling.h"
    #include "vr/Components/Players/VRPlayer.h"
    #include "vr/Utils.h"
    
    #define RAY_OFFSET 0.05f
    
    REGISTER_COMPONENT(ObjLaserPointer);
    
    using namespace Unigine;
    using namespace Math;
    
    void ObjLaserPointer::init()
    {
    	if (!laser_ray || !laser)
    	{
    		Log::error("ObjLaserPointer::init: laser_ray or laser is not set\n");
    		return;
    	}
    	laser_ray_mat = laser_ray->getTransform();
    	grabbed = 0;
    	laser->setEnabled(false);
    }
    
    void ObjLaserPointer::update()
    {
    	if (!laser || !laser_ray)
    		return;
    	if (laser->isEnabled())
    	{
    		laser_ray->setTransform(laser_ray_mat);
    
    		Vec3 dir = Vec3(laser_ray->getWorldDirection(Math::AXIS_Y));
    		Vec3 p0 = laser_ray->getWorldPosition() + dir * RAY_OFFSET;
    		Vec3 p1 = p0 + dir * 1000;
    		ObjectPtr hit_obj = World::getIntersection(p0, p1, 1, intersection);
    		if (hit_obj)
    		{
    			laser_ray->setScale(vec3(laser_ray->getScale().x,
    				(float)length(intersection->getPoint() - p0) + RAY_OFFSET,
    				laser_ray->getScale().z));
    			laser_hit->setWorldPosition(intersection->getPoint());
    			laser_hit->setEnabled(1);
    ${#HL}$
    			// alternative use: cyclic material change
    			if (alt_use)
    			{
    				if (materials.nullCheck() || materials.size() == 0)
    					return;
    
    				current_material++;
    				if (current_material >= materials.size())
    					current_material = 0;
    
    				hit_obj->setMaterial(materials[current_material], 0);
    				alt_use = false;
    			} ${HL#}$
    		}
    		else
    			laser_hit->setEnabled(0);
    
    		if (VRPlayer::get() && VRPlayer::get()->getHead())
    			setWorldDirectionY(laser_ray, laser_ray->getWorldDirection(Math::AXIS_Y),
    				vec3(VRPlayer::get()->getHead()->getWorldPosition() - laser_ray->getWorldPosition()));
    
    		ObjectLabeling *labeling = ObjectLabeling::get();
    		if (labeling)
    		{
    			if (hit_obj && hit_obj->getProperty() && grabbed)
    				labeling->setTarget(hit_obj, laser_hit->getWorldPosition(), true);
    		}
    	}
    }
    
    void ObjLaserPointer::useIt(VRPlayer *player, VRController::Side side)
    {
    	laser->setEnabled(!laser->isEnabled());
    }
    
    void ObjLaserPointer::grabIt(VRPlayer *player, VRController::Side side)
    {
    	grabbed = 1;
    }
    
    void ObjLaserPointer::throwIt(VRPlayer *player, VRController::Side side)
    {
    	grabbed = 0;
    }
    
    ${#HL}$void ObjLaserPointer::useItAlt(VRPlayer *player, VRController::Side side)
    {
    	alt_use = true;
    } ${HL#}$
    					
  3. Add a new ALT_USE value to the Buttons enum in VRController (vr/Components/Players/VRController.h):

    VRController.h
    
    enum Buttons
    {
    	TELEPORT = 0,
    	USE,
    	GRAB,
    	MENU,
    	INVENTORY,
    	${#HL}$ALT_USE ${HL#}$
    };
    				
  4. Add the call to useItAlt in VRPlayer::postUpdate() (vr/Components/Players/VRPlayer.cpp), next to the existing useIt call:

    VRPlayer.cpp
    
    // use grabbed object
    if (getControllerButtonDown(side, VRController::Buttons::USE))
    {
    	for (int j = 0; j < components.size(); j++)
    		components[j]->useIt(this, side);
    	events.on_use.run(getGrabNode(side));
    }
    
    ${#HL}$// alternative use for grabbed object
    if (getControllerButtonDown(side, VRController::Buttons::ALT_USE))
    {
    	for (int j = 0; j < components.size(); j++)
    		components[j]->useItAlt(this, side);
    } ${HL#}$
    				
  5. Add a mapping for ALT_USE in VRHandController (vr/Components/Players/VRHandController.cpp). Add a new case to each of the three methods (getControllerButtonDown, getControllerButtonPressed, getControllerButtonUp). The condition: the grip button must be held on the opposite controller:

    VRHandController.cpp
    
    case Buttons::ALT_USE:
    {
    	const InputVRControllerPtr &other = get_valid_controller(
    		side == Side::LEFT ? Side::RIGHT : Side::LEFT);
    	if (!other)
    		return false;
    	return controller->isButtonPressed(Input::VR_BUTTON_GRIP)
    		&& other->isButtonDown(Input::VR_BUTTON_GRIP);
    }
    				
  6. Save all files, build and run the application (Ctrl + F5) to make the Component System update properties. Close and switch to UnigineEditor.
  7. Select the laser_pointer node, find the ObjLaserPointer property, specify the number of elements for the materials array (for example, 3), and drag the required materials from the Materials window to fill the array.

  8. Save changes (Ctrl + S) and run the application via SDK Browser.

The information on this page is valid for UNIGINE 2.22 SDK.

Last update: 2026-09-11 03:51:51