I have an image that i use in my app as texture for a craft sphere. It draws circles at specific places around the surface of the sphere. At the moment to change the colour of the sphere i set the diffuse property to the desired colour.
In the future i would like to be able to set the colour of the background of the image separately from the rest of the image, i.e. the background colour would vary from sphere to sphere, while the colour of the circles do not change. This i cannot do by setting the diffuse property as it is global to all of the texture.
I have about 2000 spheres onscreen so the implementation needs to be efficient. I think probably the best way to do this would be to implement the texture as a Shade shader with an input for the background colour. I started trying to do this within Shade, but i confess i am lost and i do not know how to proceed. Could someone @John, @Simeon please give me some hints on the best way to do it? I am familiar with glsl shaders and did implemented it as a glsl shader before adopting craft.
Below is the codea function i use to generate the image/texture:
function makeDOMImage(backCol)
-- print(backCol)
local i,j
local img=image(255,255)
local mushroom_size = 0.23
local pmt_size = 0.05
local ring_size = 0.07
local twelveth = 0.083333
local sixth = 0.1666667
local p2=pmt_size*pmt_size
local r2=ring_size*ring_size
for i=1,img.width do
for j=1,img.height do
local col=backCol
local phi_prime = i/img.width
local theta = j/img.height
local up = 0.0
if theta<0.5 then --force theta > 0.5
phi_prime=phi_prime+twelveth
theta=1.0-theta
up=100.0
end
phi_prime=math.fmod(phi_prime, sixth)
if phi_prime>twelveth then phi_prime=sixth-phi_prime end
local a =math.sin(theta*3.1415)
a = a*a*4.0
local A = a*phi_prime*phi_prime
local tminusp=twelveth-phi_prime
local B = a*tminusp*tminusp
--the theta's of the dom positions are:
-- 0.980875, 1.2706, 1.872738, 2.162463, 2.579597, 3.1415923073180982
local omt=1.0-theta
local d1=omt*omt
local tm8=theta-0.82111139
d1 =math.min(d1, up+A+tm8*tm8)
local tm7=theta-0.687549
local d1=math.min (d1, B+tm7*tm7)
local tm6= theta-0.59587
local d1 =math.min(d1, A+tm6*tm6)
if (d1<r2) then col = vec4(0.5, 0.5, 0.5, 1.0)*255 end --ring
if (d1<p2) then col = vec4(0.9, 0.3, 0.3, 1.0)*255 end --pmt
if (j/img.height<mushroom_size) then col = vec4(0.5, 0.5, 0.5, 1.0)*255 end --mushroom
thiscol=color(col.r,col.g,col.b,col.a)
img:set(i, j, thiscol)
end
end
return img
end