20,741
edits
mNo edit summary |
m (→Canvas Code: add untested snippet) |
||
Line 96: | 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 = {}; | |||
var.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( path~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> | ||