Last active
January 4, 2016 05:19
-
-
Save dermotbalson/8574361 to your computer and use it in GitHub Desktop.
Lighting shader maker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--# Notes | |
--[[ | |
== NOTES ON LIGHTING LIBRARY | |
by ignatz (December 2013) | |
== HELP! I DON'T UNDERSTAND ALL THIS STUFF! | |
I've written an ebook on lighting, here | |
https://www.dropbox.com/s/rrpxu6hghcsdrhx/Lighting%20in%203D.pdf | |
I suggest you read it first, it will make things much clearer | |
Lighting uses "shaders". If you don't know what they are, and want to find out, then | |
you should read this ebook *before* the lighting ebook | |
https://www.dropbox.com/s/z05aql3qp3dsly5/Codea%20shaders.pdf | |
== WHAT THE LIBRARY DOES | |
The library helps you add lighting to your programs - | |
* ambient, | |
* diffuse and | |
* specular light | |
using | |
* directional | |
and/or | |
* a point or spot light | |
It can also produce | |
* fog and | |
* flickering | |
You can use it in two ways, | |
* to generate and use lighting shaders on the fly, or | |
* to produce shader code which you can copy to your own app | |
== HOW TO USE IT | |
1. Installation | |
You can either put this code in a Codea project and make it a dependency for any project that needs lighting, or include it directly in your project. All the code is in its own table called LL, so it should not interfere with your own variable naming. | |
2. Usage | |
The code in Main will give you a good idea of how to use it. | |
You configure lights for a mesh with LL.AddLights(p) where p is a table (explained below) | |
You draw lights by using LL.draw(m) in the draw function, for each mesh m affected by lighting (ie use this | |
instead of m:draw()) | |
First set up your mesh normally with vertices, texture mappings and a texture (if required). | |
The table passed through to configure the lights can include the following items. Note that when I write | |
mesh = your mesh, then if your mesh is m, then you need to write either of these | |
p["mesh"]=m | |
p.mesh=m | |
mesh = your mesh --must be provided | |
reflectiveness = 0-1 --must be provided | |
--ambient --both these must be provided | |
ambientColor = any colour | |
ambientStrength = 0-1 (usually 0.1-0.3) | |
--directional light (if applicable) | |
directional=true/false (assumed false if omitted) | |
directionalDirection = vec3 eg vec3(-0.5, 0, 1) | |
directionalColor = any color | |
directionalStrength = 0-1 eg 0.6 | |
--point/spot lights (can be omitted) | |
point = true/false | |
spot = true/false | |
--if either is true, then include these | |
pointPosition = vec3 | |
pointRange = number of pixels distance at which light cannot be seen | |
pointColor = any colour | |
pointStrength = 0-1 | |
--if the light is a spot, then include these too | |
spotRadius = radius of light beam in degrees, eg 5 or 10 | |
spotDirection = vec3 the direction in which the light is pointed | |
--for specular light only | |
specular = true/false (to turn specular light on and off) | |
specularColor = any color - usually white because specular light is bright | |
specularFocus = 1-7 (how concentrated and small the reflection is, higher is more focussed) | |
specularShine = 0-1 how reflective the mesh is | |
eyePosition = vec3 usually this is the camera position | |
--fog items | |
fog = true/false | |
fogColor = any colour, eg color(220) | |
fogRange = number of pixels distance at which light cannot be seen | |
--other - normals | |
The lighting library will calculate "normals" for your mesh unless you provide them. If you want them | |
to be averaged across all shared vertices, such as for a sphere, set this | |
normals = "average" | |
3. Special functions | |
Having created a shader with LL.AddLights(p), you can get the vertex shader and fragment shader with | |
v,s=LL.GetShader(tabName,shaderName) --> returns vertex shader, fragment shader as two strings | |
If you want to print the shader in a code tab, use this | |
LL.PrintShader(tabName,shaderName) | |
The variables tabName and shaderName are optional, as defaults will be used if you omit them. | |
--]] | |
--# Main | |
function setup() | |
--mesh first | |
m=CreateCube() | |
m:setColors(color(255)) | |
--user parameters and fixed light settings | |
parameter.boolean("Directional",true,RunTest) | |
parameter.boolean("Point",true,function(a) if a then Spot=false Specular=true end RunTest() end) | |
parameter.boolean("Spot",true,function(a) if a then Point=false Specular=true end RunTest() end) | |
--parameter.boolean("Diffuse",true,RunTest) | |
parameter.boolean("Specular",true,function(a) if Point or Spot then Specular=true end RunTest() end) | |
parameter.boolean("Texture",false,RunTest) | |
parameter.boolean("Flicker",false,RunTest) | |
parameter.boolean("Fog",false,RunTest) | |
parameter.action("Print_Shader",PrintShader) | |
ready=true | |
RunTest() | |
end | |
function RunTest() | |
if not ready then return end | |
--build a table with all the lighting elements we need | |
p={} | |
p["mesh"]=m --first the mesh | |
--p["normals"]="average" | |
p["ambientColor"]=color(255,255,0) | |
p["ambientStrength"]=0.2 | |
p["directional"]=Directional | |
if Directional then | |
p["directional"]=true | |
p["directionalDirection"]=vec3(-0.5, 0, 1) | |
p["directionalColor"]=color(255) | |
p["directionalStrength"]=0.6 | |
end | |
p["point"]=Point | |
p["spot"]=Spot | |
if Point or Spot then | |
p["pointPosition"]=vec3(1, 0, 0) --these are common to point and spot | |
p["pointRange"]=8 | |
p["pointColor"]=color(255) | |
p["pointStrength"]=0.8 | |
p["directionalStrength"]=0.6 --turn off directional if point or light shining | |
p["flicker"]=Flicker --only applies to point and spot | |
if Spot then | |
p["spotRadius"]=5 | |
p["spotDirection"]=vec3(-0.2, 0, -1) | |
end | |
end | |
if Texture then m.texture=readImage("Cargo Bot:Starry Background") | |
else m.texture=nil end | |
p["reflectiveness"]=1 | |
--p["diffuse"]=Diffuse | |
p["specular"]=Specular | |
p["eyePosition"]=vec3(0,0,0) | |
p["specularColor"]=color(255,0,0) | |
p["specularFocus"]=4 --1 to 7 | |
p["specularShine"]=0.8 | |
p["fog"]=Fog | |
p["fogColor"]=color(220) | |
p["fogRange"]=9 | |
LL.AddLights(p) | |
--visual effects | |
rot={x=0,y=0,z=0} --rotation of cube | |
tween(15, rot, {x=360}, { easing = tween.easing.linear, loop = tween.loop.pingpong }) | |
tween(13, rot, {y=360}, { easing = tween.easing.linear, loop = tween.loop.pingpong }) | |
tween(11, rot, {z=360}, { easing = tween.easing.linear, loop = tween.loop.pingpong }) | |
--make cube go forward and backward | |
cubePos={z=-3} | |
tween(9, cubePos, {z=-10}, { easing = tween.easing.linear, loop = tween.loop.pingpong }) | |
end | |
function PrintShader() | |
LL.PrintShader("TestShader","MyShader") --tab name and shader name (both can be omitted, will use default names) | |
end | |
function draw() | |
if Fog then background(220) else background(0) end | |
perspective() | |
camera(0, 0, 0, 0, 0, -10) | |
pushMatrix() | |
--move and draw cube | |
translate(0, 0, cubePos.z) | |
rotate(rot.x, 1, 0, 0) rotate(rot.y,0,1,0) rotate(rot.z,0,0,1) | |
--m.shader.mModel = modelMatrix() | |
LL.draw(m) | |
popMatrix() | |
end | |
function CreateCube() --NEW | |
local m = mesh() | |
--vertices for the corners of the cube (stolen from 3d lab) | |
local vertices = { | |
vec3(-0.5, -0.5, 0.5), -- Left bottom front | |
vec3( 0.5, -0.5, 0.5), -- Right bottom front | |
vec3( 0.5, 0.5, 0.5), -- Right top front | |
vec3(-0.5, 0.5, 0.5), -- Left top front | |
vec3(-0.5, -0.5, -0.5), -- Left bottom back | |
vec3( 0.5, -0.5, -0.5), -- Right bottom back | |
vec3( 0.5, 0.5, -0.5), -- Right top back | |
vec3(-0.5, 0.5, -0.5), -- Left top back | |
} | |
-- now construct a cube out of the vertices above | |
m.vertices = { | |
-- Front | |
vertices[1], vertices[2], vertices[3], | |
vertices[1], vertices[3], vertices[4], | |
-- Right | |
vertices[2], vertices[6], vertices[7], | |
vertices[2], vertices[7], vertices[3], | |
-- Back | |
vertices[6], vertices[5], vertices[8], | |
vertices[6], vertices[8], vertices[7], | |
-- Left | |
vertices[5], vertices[1], vertices[4], | |
vertices[5], vertices[4], vertices[8], | |
-- Top | |
vertices[4], vertices[3], vertices[7], | |
vertices[4], vertices[7], vertices[8], | |
-- Bottom | |
vertices[5], vertices[6], vertices[2], | |
vertices[5], vertices[2], vertices[1], | |
} | |
--now texture it | |
-- all the unique texture positions needed | |
local q=.03 | |
local texvertices = { vec2(q,q), | |
vec2(1-q,q), | |
vec2(q,1-q), | |
vec2(1-q,1-q) } | |
-- apply the texture coordinates to each triangle | |
m.texCoords = { | |
-- Front | |
texvertices[1], texvertices[2], texvertices[4], | |
texvertices[1], texvertices[4], texvertices[3], | |
-- Right | |
texvertices[1], texvertices[2], texvertices[4], | |
texvertices[1], texvertices[4], texvertices[3], | |
-- Back | |
texvertices[1], texvertices[2], texvertices[4], | |
texvertices[1], texvertices[4], texvertices[3], | |
-- Left | |
texvertices[1], texvertices[2], texvertices[4], | |
texvertices[1], texvertices[4], texvertices[3], | |
-- Top | |
texvertices[1], texvertices[2], texvertices[4], | |
texvertices[1], texvertices[4], texvertices[3], | |
-- Bottom | |
texvertices[1], texvertices[2], texvertices[4], | |
texvertices[1], texvertices[4], texvertices[3], | |
} | |
return m | |
end | |
--# Lighting | |
--Lighting 2.0 | |
LL={} | |
function LL.ClearLights() | |
LL={} | |
end | |
function LL.AddLights(p) | |
local m=p.mesh | |
--first check for changes which require a fresh shader | |
local newShader=false | |
if m.shader==nil or LL.directional~=p.directional or LL.point~=p.point or LL.spot~=p.point or | |
LL.specular~=p.specular or LL.flicker~=p.flicker or | |
(LL.texture~=p.texture and (LL.texture==nil or p.texture==nil)) | |
then newShader=true end | |
--copy all the new details into LL | |
LL.texture=m.texture | |
for i,j in pairs(p) do | |
LL[i]=j | |
end | |
--set defaults | |
LL.ambientColor=LL.ambientColor or color(255) | |
LL.ambientStrength=LL.ambientStrength or 0.2 | |
LL.reflectivity=LL.reflectivity or 1 | |
if LL.directional then | |
LL.directionalColor=LL.directionalColor or color(255) | |
LL.directionalStrength=LL.directionalStrength or 1 | |
end | |
LL.specularColor=LL.specularColor or color(255) | |
LL.specularShine=LL.specularShine or 1 | |
LL.specularFocus=LL.specularFocus or 3 | |
--create normals if not already created | |
if p.normals=="average" then | |
m.normals=LL.CalculateAverageNormals(p.mesh.vertices) | |
elseif m.normals[1]==nil then | |
m.normals=LL.CalculateNormals(p.mesh.vertices) | |
end | |
output.clear() | |
--create shader if necessary | |
if newShader then | |
p.mesh.shader=LL.MakeShader() | |
end | |
--set shader settings | |
--ambient | |
local c,s=LL.ambientColor,LL.ambientStrength | |
m.shader.ambientColor = color(c.r*s,c.g*s,c.b*s,255) --print("AmbientColor=",m.shader.ambientColor) | |
--directional | |
if LL.directional then --print("Directional?",LL.directional) | |
local c,s=LL.directionalColor,LL.directionalStrength | |
m.shader.directColor = color(c.r*s,c.g*s,c.b*s,255) --print("DirectColor",m.shader.directColor) | |
m.shader.directStrength=LL.directionalStrength | |
local d=LL.directionalDirection:normalize() | |
m.shader.directDirection = vec4(d.x,d.y,d.z,0) --print("DirectDirection",m.shader.directDirection) | |
end | |
--point and spot | |
if LL.point or LL.spot then --print("Point/Spot?",LL.point,LL.spot) | |
m.shader.point1Color = LL.pointColor --print("PointColor",LL.pointColor) | |
m.shader.point1Strength=LL.pointStrength --print("PointStrength",LL.pointStrength) | |
local d=LL.pointPosition | |
m.shader.point1Position=vec4(d.x,d.y,d.z,1) --print("PointPosition",m.shader.point1Position) | |
d=LL.spotDirection:normalize() | |
m.shader.point1Direction=vec4(d.x,d.y,d.z,0) --print("PointDirection",m.shader.point1Direction) | |
m.shader.point1Range=LL.pointRange --print("Range",LL.pointRange) | |
if LL.spot then | |
m.shader.point1InBeam=math.cos(math.rad(LL.spotRadius)) --print("RadiusCos",m.shader.point1InBeam) | |
end | |
end | |
--specular | |
if LL.specular then --print("Spec?",LL.specular) | |
m.shader.shine=LL.specularShine --print("SpecShine",LL.specularShine) | |
m.shader.specularColor=LL.specularColor --print("SpecColor",LL.specularColor) | |
local a={16,32,64,128,256} | |
m.shader.specularPower=a[math.max(1,math.min(#a,LL.specularFocus))] | |
--print("SpecPower=",m.shader.specularPower) | |
local d=LL.eyePosition | |
m.shader.eyePosition=vec4(d.x,d.y,d.z,1) --print("Eye=",m.shader.eyePosition) | |
end | |
--other | |
m.shader.reflec=LL.reflectiveness --print("Reflect=",LL.reflectiveness) | |
if LL.flicker==nil then m.shader.point1Flicker=0 end | |
if LL.fog then | |
m.shader.fogColor=LL.fogColor | |
m.shader.fogRange=LL.fogRange | |
end | |
--m.shader.transparency=LL.transparency --- ??? | |
end | |
function LL.MakeShader() | |
local s="023" | |
if LL.texture then s=s.."1" end | |
if LL.specular then s=s.."4" end | |
if LL.directional then s=s.."5" end | |
if LL.point then s=s.."6" elseif LL.spot then s=s.."7" end | |
if LL.fog then s=s.."8" end | |
if LL.flicker and (LL.point or LL.spot) then s=s.."9" end | |
LL.vs=LL.BuildShaderSection(s,LL.VertexShader) | |
LL.fs=LL.BuildShaderSection(s,LL.FragmentShader) | |
return shader(LL.vs,LL.fs) | |
end | |
function LL.GetShader() | |
print(type(LL.vs)) | |
print(type(LL.fs)) | |
return LL.vs,LL.fs | |
end | |
function LL.PrintShader(tabName,shaderName) | |
tabName=tabName or "Shader" | |
shaderName=shaderName or "Shader" | |
saveProjectTab(tabName,shaderName.. | |
"={\nvertexShader=[[\n"..LL.vs.."\n\n]],\n\nfragmentShader=[[\n"..LL.fs.."\n]]\n}") | |
end | |
function LL.draw(m) | |
m.shader.mModel=modelMatrix() | |
if LL.flicker then m.shader.point1Flicker=0.75+noise(ElapsedTime*5)/4 end | |
m:draw() | |
end | |
function LL.CalculateNormals(vertices) | |
--this assumes flat surfaces, and hard edges between triangles | |
local norm = {} | |
for i=1, #vertices,3 do --calculate normal for each set of 3 vertices | |
local n = ((vertices[i+1] - vertices[i]):cross(vertices[i+2] - vertices[i])):normalize() | |
norm[i] = n --then apply it to all 3 | |
norm[i+1] = n | |
norm[i+2] = n | |
end | |
return norm | |
end | |
function LL.CalculateAverageNormals(vertices) | |
--average normals at each vertex | |
--first get a list of unique vertices, concatenate the x,y,z values as a key | |
local norm,unique= {},{} | |
for i=1, #vertices do | |
unique[vertices[i].x ..vertices[i].y..vertices[i].z]=vec3(0,0,0) | |
end | |
--calculate normals, add them up for each vertex and keep count | |
for i=1, #vertices,3 do --calculate normal for each set of 3 vertices | |
local n = (vertices[i+1] - vertices[i]):cross(vertices[i+2] - vertices[i]) | |
for j=0,2 do | |
local v=vertices[i+j].x ..vertices[i+j].y..vertices[i+j].z | |
unique[v]=unique[v]+n | |
end | |
end | |
--calculate average for each unique vertex | |
for i=1,#unique do | |
unique[i] = unique[i]:normalize() | |
end | |
--now apply averages to list of vertices | |
for i=1, #vertices do --calculate average | |
norm[i] = unique[vertices[i].x ..vertices[i].y..vertices[i].z] | |
end | |
return norm | |
end | |
function LL.typeOf(x) | |
if x==nil then return "nil" end | |
if type(x) == "table" and x.is_a then return("class") end | |
local txt | |
if typeTable==nil then | |
typeTable = {[getmetatable(vec2()).__index ] = "vec2", [getmetatable(vec3()).__index ] = "vec3", | |
[getmetatable(vec4()).__index ] = "vec4"} | |
end | |
local i = getmetatable(x) | |
if i then txt = typeTable[i.__index] end | |
if txt then return txt end | |
txt = type(x) | |
return txt | |
end | |
--# Lighting2 | |
--Shadermaker1 | |
function LL.BuildShaderSection(s,sh) | |
local t="" | |
for line in sh:gmatch("[^\r\n]+") do | |
local n=string.find(line,"//#") | |
if n then | |
local ss="" | |
local z=string.sub(line,n+3,999) | |
for i=1,string.len(z),2 do | |
local a,b=string.sub(z,i,i),string.sub(z,i+1,i+1) | |
if string.find(s,b) then | |
if a=="o" or a=="a" then ss="\n"..LL.trim(string.sub(line,1,n-1)) | |
elseif a=="n" then ss="" break end | |
elseif a=="a" then ss="" break end | |
end | |
if ss then t=t..ss end | |
else t=t.."\n"..line end | |
end | |
--remove extra line feeds | |
t=string.gsub(t,"//\n//","//") | |
t=string.gsub(t,"//\n//","//") | |
t=string.gsub(t,"//\n//","//") | |
t=string.gsub(t,"//","") | |
return t | |
end | |
-- trim whitespace from right end of string | |
function LL.trim(s) | |
return s:match( "(.-)%s*$" ) | |
--return s:find'^%s*$' and '' or s:match'^(.*%S)' | |
end | |
--shaderOptions={standard=0,texture=1,ambient=2,diffuse=3,specular=4, | |
-- directional=5,point=6,spot=7,fog=8,flicker=9,transparency="A"} | |
---------------------------------------------------- | |
LL.VertexShader=[[ | |
uniform mat4 modelViewProjection; //#a0 | |
uniform mat4 mModel; //#a3 | |
uniform vec4 directColor; //#a5 | |
uniform vec4 directDirection; //#a5 | |
uniform vec4 point1Position; //#o6o7 | |
uniform vec4 point1Color; //#o6o7 | |
// //#a0 | |
attribute vec4 position; //#a0 | |
attribute vec4 color; //#a0 | |
attribute vec2 texCoord; //#a1 | |
attribute vec3 normal; //#a3 | |
// //#a0 | |
varying lowp vec4 vColor; //#a0 | |
varying highp vec2 vTexCoord; //#a1 | |
varying lowp vec4 vPosition; //#a3 | |
varying lowp vec4 vNormal; //#a3 | |
varying vec4 vDirectDiffuse; //#a5 | |
varying vec4 vPoint1Diffuse; //#o6o7 | |
// //#a0 | |
void main() //#a0 | |
{ //#a0 | |
vColor = color; //#a0 | |
gl_Position = modelViewProjection * position; //#a0 | |
vTexCoord = texCoord; //#a1 | |
vNormal = mModel * vec4( normal, 0.0 ); //#a3 | |
vec4 norm = normalize(vNormal); //#a3 | |
vPosition = mModel * position; //#a3 | |
vDirectDiffuse = directColor * max( 0.0, dot( norm, directDirection )); //#a5 | |
vec4 d = normalize( point1Position - vPosition ); //#o6o7 | |
vPoint1Diffuse = point1Color * max( 0.0, dot( norm, d )); //#o6o7 | |
} //#a0 | |
]] | |
---------------------------------------------------- | |
LL.FragmentShader=[[ | |
precision highp float; //#a0 | |
uniform vec4 ambientColor; //#a2 | |
uniform lowp sampler2D texture; //#a1 | |
uniform float reflec; //#a2 | |
// //#a0/ | |
uniform vec4 directColor; //#a4a5 | |
uniform float directStrength; //#a4a5 | |
uniform vec4 directDirection; //#a4a5 | |
// //#a0/ | |
uniform vec4 eyePosition; //#a4 | |
uniform vec4 specularColor; //#a4 | |
uniform float specularPower; //#a4 | |
uniform float shine; //#a4 | |
// //#a0/ | |
uniform vec4 point1Position; //#o6o7 | |
uniform float point1Range; //#o6o7 | |
uniform vec4 point1Color; //#o6o7 | |
uniform float point1Strength; //#o6o7 | |
uniform vec4 point1Direction; //#a7 | |
uniform float point1InBeam; //#a7 | |
// //#a0/ | |
uniform float fogRange; //#a8 | |
uniform vec4 fogColor; //#a8 | |
uniform float transparency; //#aA | |
uniform float point1Flicker; //#a9 | |
// //#a0/ | |
varying lowp vec4 vColor; //#a0 | |
varying highp vec2 vTexCoord; //#a1 | |
varying lowp vec4 vPosition; //#a3 | |
varying lowp vec4 vNormal; //#a3 | |
varying vec4 vDirectDiffuse; //#a5 | |
varying vec4 vPoint1Diffuse; //#o6o7 | |
// //#a0/ | |
vec4 normalizedNormal = normalize(vNormal); //#a3 | |
// //#a0/ | |
vec4 GetSpecularColor(vec4 lightPosition, vec4 lightColor, bool IsDirectional) //#a4 | |
{ //#a4 | |
vec4 lightDirection; //#a4 | |
if (IsDirectional) lightDirection = lightPosition; //#a4 | |
else lightDirection = vec4( normalize( lightPosition - vPosition )); //#a4 | |
vec4 cameraDirection = normalize( eyePosition - vPosition ); //#a4 | |
vec4 halfAngle = normalize( cameraDirection + lightDirection ); //#a4 | |
vec4 specularColor = min(lightColor + 0.5, 1.0); //#a4 | |
float spec = pow( max( 0.0, dot( normalizedNormal, halfAngle)), specularPower ); //#a4 | |
return specularColor * spec * shine; //#a4 | |
} //#a4 | |
// //#a0/ | |
void main() //#a0 | |
{ //#a0 | |
lowp vec4 ambient=vec4(0.,0.,0.,0.); //#a2 | |
lowp vec4 diffuse=vec4(0.,0.,0.,0.); //#a2 | |
lowp vec4 specular=vec4(0.,0.,0.,0.); //#a2 | |
lowp vec4 pixel = vColor; //#a0n1 | |
lowp vec4 pixel = texture2D( texture, vTexCoord); //#a1 | |
ambient = pixel * ambientColor; //#a0 | |
// //#a0/ | |
if ( pixel.a!=0.0 || transparency==0.0 ) { //#aA | |
diffuse = diffuse + pixel * vDirectDiffuse; //#a5 | |
specular=specular + pixel * directStrength * GetSpecularColor(directDirection, specularColor, true); //#a4a5 | |
float r = point1Flicker * point1Range; //#a9 | |
float r = point1Range; //#o6o7n9 | |
float point1Attenuation = max( 0.0, 1.0 - length( point1Position.xyz - vPosition.xyz ) / r ); //#o6o7 | |
float fracSpot=1.0; //#o6o7 | |
vec4 lightToPixel = normalize( vPosition - point1Position ); //#a7 | |
float cos = dot( point1Direction, lightToPixel ); //#a7 | |
if (cos>=point1InBeam && cos<=1.0) fracSpot = max( 0.0, 1.0 - ( 1.0 - cos ) / ( 1.0 - point1InBeam )); //#a7 | |
else fracSpot = 0.0; //#a7 | |
diffuse = diffuse + pixel * fracSpot * point1Attenuation * vPoint1Diffuse; //#o6o7 | |
specular=specular + pixel * fracSpot * point1Attenuation * point1Strength * //#o6o7 | |
GetSpecularColor(point1Position, specularColor, false); //#o6o7 | |
vec4 totalColor = clamp( reflec * (ambient + diffuse + specular),0.,1.); //#a2 | |
float f = max(0.0, 1.0 - length( eyePosition.xyz - vPosition.xyz )/fogRange); //#a8 | |
totalColor = totalColor * f + fogColor * (1.0-f); //#a8 | |
totalColor.a=1.; //#a2 | |
gl_FragColor=totalColor; //#a2 | |
gl_FragColor=pixel; //#a0n2 | |
else { //#aA | |
if (transparency == 1.0) gl_FragColor = pixel; //#aA | |
else discard; //#aA | |
} //#aA | |
} //#a0 | |
]] | |
--# TestShader | |
MyShader={ | |
vertexShader=[[ | |
uniform mat4 modelViewProjection; | |
uniform mat4 mModel; | |
uniform vec4 directColor; | |
uniform vec4 directDirection; | |
attribute vec4 position; | |
attribute vec4 color; | |
attribute vec2 texCoord; | |
attribute vec3 normal; | |
varying lowp vec4 vColor; | |
varying highp vec2 vTexCoord; | |
varying lowp vec4 vPosition; | |
varying lowp vec4 vNormal; | |
varying vec4 vDirectDiffuse; | |
void main() | |
{ | |
vColor = color; | |
gl_Position = modelViewProjection * position; | |
vTexCoord = texCoord; | |
vNormal = mModel * vec4( normal, 0.0 ); | |
vec4 norm = normalize(vNormal); | |
vPosition = mModel * position; | |
vDirectDiffuse = directColor * max( 0.0, dot( norm, directDirection )); | |
} | |
]], | |
fragmentShader=[[ | |
precision highp float; | |
uniform vec4 ambientColor; | |
uniform lowp sampler2D texture; | |
uniform float reflec; | |
uniform vec4 directColor; | |
uniform float directStrength; | |
uniform vec4 directDirection; | |
uniform vec4 eyePosition; | |
uniform vec4 specularColor; | |
uniform float specularPower; | |
uniform float shine; | |
uniform float fogRange; | |
uniform vec4 fogColor; | |
varying lowp vec4 vColor; | |
varying highp vec2 vTexCoord; | |
varying lowp vec4 vPosition; | |
varying lowp vec4 vNormal; | |
varying vec4 vDirectDiffuse; | |
vec4 normalizedNormal = normalize(vNormal); | |
vec4 GetSpecularColor(vec4 lightPosition, vec4 lightColor, bool IsDirectional) | |
{ | |
vec4 lightDirection; | |
if (IsDirectional) lightDirection = lightPosition; | |
else lightDirection = vec4( normalize( lightPosition - vPosition )); | |
vec4 cameraDirection = normalize( eyePosition - vPosition ); | |
vec4 halfAngle = normalize( cameraDirection + lightDirection ); | |
vec4 specularColor = min(lightColor + 0.5, 1.0); | |
float spec = pow( max( 0.0, dot( normalizedNormal, halfAngle)), specularPower ); | |
return specularColor * spec * shine; | |
} | |
void main() | |
{ | |
lowp vec4 ambient=vec4(0.,0.,0.,0.); | |
lowp vec4 diffuse=vec4(0.,0.,0.,0.); | |
lowp vec4 specular=vec4(0.,0.,0.,0.); | |
lowp vec4 pixel = texture2D( texture, vTexCoord); | |
ambient = pixel * ambientColor; | |
diffuse = diffuse + pixel * vDirectDiffuse; | |
specular=specular + pixel * directStrength * GetSpecularColor(directDirection, specularColor, true); | |
vec4 totalColor = clamp( reflec * (ambient + diffuse + specular),0.,1.); | |
float f = max(0.0, 1.0 - length( eyePosition.xyz - vPosition.xyz )/fogRange); | |
totalColor = totalColor * f + fogColor * (1.0-f); | |
totalColor.a=1.; | |
gl_FragColor=totalColor; | |
} | |
]] | |
} | |
--# ccConfig | |
--[[ | |
########################################### | |
##Codea Community Project Config Settings## | |
########################################### | |
##You can use # to comment out a line | |
##Use 1 for true and 0 for false | |
########################################### | |
# Add project info below # | |
#========================================== | |
ProjectName: Lighting shader maker | |
Version: 1.01 | |
Comments: | |
Author: ignatz | |
##License Info: http://choosealicense.com | |
##Supported Licneses: MIT, GPL, Apache, NoLicense | |
License: MIT | |
#========================================== | |
########################################### | |
# Settings # | |
[Settings]================================= | |
##Codea Community Configuration settings | |
##Format: Setting state | |
Button 1 | |
ParamButton 0 | |
NotifyCCUpdate 1 | |
ResetUserOption 0 | |
AddHeaderInfo 1 | |
Connect 1 | |
[/Settings]================================ | |
########################################### | |
# Screenshots # | |
[Screenshots]============================== | |
##Screenshots from your project. | |
##Format: url | |
##Example: http://www.dropbox.com/screenshot.jpg | |
[/Screenshots]============================= | |
########################################### | |
# Video # | |
[Video]==================================== | |
##Link to a YouTube.com video. | |
##Format: url | |
##Example: http://www.youtube.com/videolink | |
[/Video]=================================== | |
########################################### | |
# Dependencies # | |
[Dependencies]============================= | |
##Include the names of any dependencies here | |
##Format: Dependency | |
##Example: Codea Community | |
[/Dependencies]============================ | |
############################################ | |
# Tabs # | |
[Tabs]====================================== | |
##Select which tabs are to be uploaded. | |
##Keyword 'not' excludes a tab or tabs. Keyword 'add' includes a tab or tabs. | |
##not * will exclude all tabs to allow for individual selection | |
##not *tab1 will look for any tab with tab1 on the end. | |
##not tab1* will look for any tab with tab1 at the beginning. | |
##Format: (add/not)(*)tab(*) | |
##Example: not Main --this will remove main. | |
##Example: not *tab1 --this will remove any tab with "tab1" on the end. | |
##Example: add Main --This will add Main back in. | |
[/Tabs]===================================== | |
############################################# | |
# Assets # | |
[Assets]===================================== | |
##Directory, path and url info for any assets besides the standard Codea assets. | |
##Format: Folder:sprite URL | |
##Example: Documents:sprite1 http://www.somewebsite.com/img.jpg | |
[/Assets]==================================== | |
--]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment