I want to change the color of the target object that I created with V_Real Builder every time it is repeated
1 view (last 30 days)
Show older comments
clc;clear;close all
Myworld = vrworld('MyCicle1.x3d');
%%
open(Myworld);
%%
figMy = view(Myworld, '-internal');
vrdrawnow;
%%
GMy=get(Myworld)
%%
NMy=nodes(Myworld);
%% Accessing VRML Nodes
Cicle = vrnode(Myworld,'ObjectCicle');
%%
Cicle.translation = [0 0 0];
x1 = 1:15 ;
y1 = 0 + zeros(size(x1));
z1 = 0 + zeros(size(x1));
Ch=Myworld.ObjectCicle.children;
c1=getfield(Ch);
c2=getfield(c1.appearance);
c3=getfield(c2.material);
for i=1:length(x1)
Cicle.translation = [x1(i) y1(i) z1(i)];
c3.diffuseColor = rand(1,3);
vrdrawnow;
pause(1)
end
0 Comments
Answers (1)
Shaunak
on 24 Apr 2025
To change the color of your target object in the VRML/X3D scene at each iteration, you need to access the actual material node in your virtual world, not just a copy of its properties. In your current code, ‘c3’ is a copy, so updating ‘c3.diffuseColor’ does not reflect in the visualization. Instead, you should get the handle to the material node using its DEF name (e.g., 'MyMaterial') and update its property directly.
Here's how you can modify your code:
clc; clear; close all
Myworld = vrworld('MyCicle1.x3d');
open(Myworld);
figMy = view(Myworld, '-internal');
vrdrawnow;
% Access the main object node
Cicle = vrnode(Myworld, 'ObjectCicle');
% Access the material node by its DEF name (as defined in the X3D file)
matNode = vrnode(Myworld, 'MyMaterial');
x1 = 1:15;
y1 = zeros(size(x1));
z1 = zeros(size(x1));
for i = 1:length(x1)
Cicle.translation = [x1(i), y1(i), z1(i)];
matNode.diffuseColor = rand(1,3); % Set the color directly on the material node
vrdrawnow;
pause(1)
end
You can refer to the following MathWorks documentation for more information on accessing and updating VRML node properties:
Hope this helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!