Howto:Dynamic Liveries via Canvas: Difference between revisions

Jump to navigation Jump to search
m
cat
m (cat)
(20 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Canvas Navigation}}


== Objective ==
== Objective ==
Line 13: Line 14:


{{FGCquote
{{FGCquote
   |But in the long-term, I would prefer turning this into a Nasal class that can manage multiple Canvases per aircraft, so that we can also re-implement the existing livery system accordingly, so that it stops interfering with canvas. Which would also mean that people can reuse the same class for placing bullet holes or doing other fancy things (e.g. immatriculation)
   |But in the long-term, I would prefer turning this into a Nasal class that can manage multiple Canvases per aircraft, so that we can also re-implement the existing livery system accordingly, so that it stops interfering with canvas. Which would also mean that people can reuse the same class for placing bullet holes or doing other fancy things (e.g. [[Immatriculation]])
   |{{cite web |url=http://forum.flightgear.org/viewtopic.php?p=218789#p218789
   |{{cite web |url=http://forum.flightgear.org/viewtopic.php?p=218789#p218789
     |title=<nowiki>Re: Two Images to a Texture</nowiki>
     |title=<nowiki>Re: Two Images to a Texture</nowiki>
Line 46: Line 47:


=== Canvas Code ===
=== Canvas Code ===
From the code snippets above and [[Howto:Using_raster_images_and_nested_canvases]], I've put together my first attempt at some Canvas code. It appears that the canvas is the right size, and both .jpg and .png textures are loaded. But the placement line did not seem to make any difference to the texture on the named model object ("Fuselage"). When the native XML/Nasal livery object was commented out, the image was successfully displayed on the fuselage, but incorrectly sized and apparently without UV mapping (see image below).
From the code snippets above and [[Howto:Using raster images and nested canvases]], I've put together my first attempt at some Canvas code. It appears that the canvas is the right size, and both .jpg and .png textures are loaded. But the placement line did not seem to make any difference to the texture on the named model object ("Fuselage"). When the native XML/Nasal livery object was commented out, the image was successfully displayed on the fuselage, but incorrectly sized and apparently without UV mapping (see image below).


<syntaxhighlight lang="nasal">
<syntaxhighlight lang="nasal">
Line 64: Line 65:
# This replaces the texture on the aircraft and attaches the Canvas texture
# This replaces the texture on the aircraft and attaches the Canvas texture
myCanvas.addPlacement({"node": "Fuselage"});
myCanvas.addPlacement({"node": "Fuselage"});
 
# Put a raster image into the canvas
# hash with all images added
root.createChild("image")
var layers = {};
     .setFile("Aircraft/EF2000/Models/EF2000.png")
# texture path
    .setSize(2048,2048);
var path="Aircraft/EF2000/Models/";
foreach(var image; ['EF2000.png','EF2000-dirt.png']) {
if(contains(layers, image)) print("Warning: replacing texture (added twice): ", image);
# Put a raster image into the canvas and save the image in a hash: layers['EF2000.png].hide();
layers[image] = root.createChild("image")
     .setFile( path~image )
    .setSize(2048,2048)
}
 
# object-oriented settimer() replacement
# see: http://wiki.flightgear.org/List_of_Nasal_extension_functions#maketimer.28.29_.282.11.2B.29
var timer_hide = maketimer(3.0, func() {
  layers['EF2000-dirt.png'].hide();
});
 
var timer_show = maketimer(5.0, func() {
  layers['EF2000-dirt.png'].show();
});
 
# start those two timers to hide/show the dirt texture with 2 second delays
timer_hide.start();
timer_show.start();


# Create a Canvas dialog window to hold the canvas and show that it's working
# Create a Canvas dialog window to hold the canvas and show that it's working
Line 74: Line 96:
var window = canvas.Window.new([512,512],"dialog");
var window = canvas.Window.new([512,512],"dialog");
window.setCanvas(myCanvas);
window.setCanvas(myCanvas);
</syntaxhighlight>
Here's another slightly-restructured version (untested), to better encapsulate the concept of a "managed texture" so that the code can be reused for other aircraft/purposes, e.g. for placing bullet holes etc:
<syntaxhighlight lang="nasal">
var ManagedTexture = {
# this create a new TextureManager object
new: func(name, size, path) {
# create a temporary object that inherits from TextureManager
var m = {parents:[ManagedTexture]};
# Create a standalone Canvas (not attached to any GUI dialog/aircraft etc)
m.canvas = canvas.new({
"name": name, # The name is optional but allow for easier identification
"size": [size[0], size[1]], # Size of the underlying texture (should be a power of 2, required) [Resolution]
"view": [size[0], size[1]], # Virtual resolution (Defines the coordinate system of the canvas [Dimensions]
                        # which will be stretched the size of the texture, required)
"mipmapping": 1      # Enable mipmapping (optional)
});
# create our top-level/root group that contains all other Canvas elements
m.root = m.canvas.createGroup();
# hash with all layers/images added
m.layers = {};
m.basepath = path;
# return the new object to the caller
return m;
},
replaceTexture: func(name) {
# Add a placement by replacing the textured face specified (name) in the 3D model
# This replaces the texture on the aircraft and attaches the Canvas texture
m.canvas.addPlacement({"node": name});
},
addDynamicLayer: func(filename, callback=nil) {
if(contains(layers, image)) print("Warning: replacing texture (added twice): ", image);
# Put a raster image into the canvas and save the image in a hash: layers['EF2000.png].hide();
m.layers[image] = root.createChild("image")
    .setFile( m.basepath~image )
    .setSize(2048,2048)
return m.layers[image];
},
}; # of ManagedTexture
# now, create a new managed texture, specifying the path to use for texture lookups
var ExhaustDirt = ManagedTexture.new( name:"ExhaustDirt",
size:[2048,2048],
path:'Aircraft/EF2000/Models/' );
ExhaustDirt.replaceTexture('Fuselage');
# Create a Canvas dialog window to hold the canvas and show that it's working
# the Canvas is now standalone, i.e. continues to live once the dialog is closed!
var window = canvas.Window.new([512,512],"dialog");
window.setCanvas(ExhaustDirt.canvas);
foreach(var layer; [ {file:'EF2000.png'},
{file:'EF2000-dirt.png'} ]) {
ExhaustDirt.addDynamicLayer(layer.file);
}
var timer_hide = maketimer(3.0, func() {
  ExhaustDirt.layers['EF2000-dirt.png'].hide();
});
var timer_show = maketimer(5.0, func() {
  ExhaustDirt.layers['EF2000-dirt.png'].show();
});
# start those two timers to hide/show the dirt texture with 2 second delays
timer_hide.start();
timer_show.start();
</syntaxhighlight>
</syntaxhighlight>


Line 225: Line 325:
   }}
   }}
}}
}}
[[Category:Canvas]]
[[Category:Aircraft enhancement]]

Navigation menu