Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Examples shaders

pmax
Registered User
Quote
2014-10-15 20:58:37

I open this thread to post the code shaders.
looking on the internet I found the code GLSL shaders, I have modified to CopperCube.
Tried just filling in Winindows exe.

PS: Translation by Google.

TOON_Shader
var vertexShader = 
"\n" +
"varying vec3 normal;\n" +
"\n" +
"void main()\n" +
"{\n" +
" normal = gl_NormalMatrix * gl_Normal; \n" +
" gl_Position = ftransform(); \n" +
" \n" +
"}";

var fragmentShader =
"varying vec3 normal; \n" +
" \n" +
"void main() \n" +
"{ \n" +
" float intensity; \n" +
" vec4 color; \n" +
" vec3 n = normalize(normal); \n" +
" intensity = dot(vec3(gl_LightSource[0].position),n);\n" +
" \n" +
" if (intensity > 0.95) \n" +
" color = vec4(1.0,0.5,0.5,1.0);\n" +
" else if (intensity > 0.5) \n" +
" color = vec4(0.6,0.3,0.3,1.0);\n" +
" else if (intensity > 0.25) \n" +
" color = vec4(0.4,0.2,0.2,1.0); \n" +
" else \n" +
" color = vec4(0.2,0.1,0.1,1.0);\n" +
" gl_FragColor = color;\n" +
"\n" +
"}";

var newMaterial = ccbCreateMaterial(vertexShader, fragmentShader, 0, null);

var cube = ccbGetSceneNodeFromName('soldier');
ccbSetSceneNodeMaterialProperty(cube, 0, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(cube, 1, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(cube, 2, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(cube, 4, 'Type', newMaterial);

Example scene
https://www.dropbox.com/s/9iof99...
🔎︎



mrmdesign
Registered User
Quote
2014-10-15 21:13:01

Thank you pmax.
That is really good of you. Was it hard to convert?


niko
Moderator
Quote
2014-10-16 05:50:12

Looks nice!


bob
Guest
Quote
2014-10-16 12:06:18

Is that will be possible to adapt this in coppercube?:
https://developer.mozilla.org/fr...


pmax
Registered User
Quote
2014-10-17 19:25:12

Sorry I have not had much time to respond to the messages the company where I work has been flooded.

@ Mrmdesign, I just tried the code in different websites and modified by adding the symbols as in the example of Niko.

@ Bob, I do not know if you can do it when I have some time I will continue to look on the Internet other code.

PS: translated by Google


pmax
Registered User
Quote
2014-10-18 09:31:12

NEW_TOON_Shader
var vertexShader = 
"varying vec3 Normal;\n" +
"void main(void)\n" +
"{\n" +
"//passo al fragment shader il vettore normale\n" +
"Normal = normalize(gl_NormalMatrix * gl_Normal);\n" +
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" +
"}";

var fragmentShader =
"uniform vec3 DiffuseColor = vec3(0.0, 0.25, 1.0) ;//default (0.0, 0.25, 1.0)\n" +
"uniform vec3 PhongColor = vec3(0.75, 0.75, 1.0);//default (0.75, 0.75, 1.0)\n" +
"uniform float Edge = 0.5;//default 0.5\n" +
"uniform float Phong = 0.98;//default 0.98\n" +
"varying vec3 Normal;\n" +
"void main (void)\n" +
"{\n" +
"vec3 color = DiffuseColor;\n" +
"float f = dot( vec3(0,0,1),Normal);\n" +
"if ( abs(f) < Edge)\n" +
"color = vec3 (0);\n" +
"if (f>Phong)\n" +
"color = PhongColor;\n" +
"gl_FragColor = vec4(color, 1);\n" +
"}";

var newMaterial = ccbCreateMaterial(vertexShader, fragmentShader, 0, null);

var soldier = ccbGetSceneNodeFromName('soldier');
ccbSetSceneNodeMaterialProperty(soldier, 0, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 1, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 2, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 4, 'Type', newMaterial);

Example scene
https://www.dropbox.com/s/gjlpo9...
🔎︎



Specularmap
Guest
Quote
2014-10-18 17:50:58

Does not work. Black material.
What is wrong here ?!

var vertexShader =
"varying vec3 LightDir;\n" +
"varying vec3 EyeDir;\n" +
"varying vec3 Normal;\n" +
"uniform vec3 LightPosition;\n" +
"uniform float Scale;\n" +
"void main(void) \n" +
"{\n" +
"vec4 pos = gl_ModelViewMatrix * gl_Vertex;\n" +
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" +
"vec3 eyeDir = vec3(pos);\n" +
"gl_TexCoord[0] = gl_MultiTexCoord0;\n" +
"vec3 n = normalize(gl_NormalMatrix * gl_Normal);\n" +
"vec3 t = normalize(cross(vec3(1.141, 2.78, 3.14), n));\n" +
"vec3 b = cross(n, t);\n" +
"vec3 v;\n" +
"v.x = dot(LightPosition, t);\n" +
"v.y = dot(LightPosition, b);\n" +
"v.z = dot(LightPosition, n);\n" +
"LightDir = normalize(v);\n" +
"v.x = dot(eyeDir, t);\n" +
"v.y = dot(eyeDir, b);\n" +
"v.z = dot(eyeDir, n);\n" +
"EyeDir = normalize(v);\n" +
"}";

var fragmentShader =
"varying vec3 LightDir;\n" +
"varying vec3 EyeDir;\n" +
"varying vec3 Normal;\n" +
"const vec3 color = vec3(0.7, 0.6, 0.18);\n" +
"//const float Density = 16.0;\n" +
"//const float Size = 0.25;\n" +
"uniform float Density;\n" +
"uniform float Size;\n" +
"//uniform float Scale;\n" +
"const float SpecularFactor = 0.5;\n" +
"void main (void)\n" +
"{\n" +
" vec3 litColor;\n" +
" vec2 c = Density * (gl_TexCoord[0].xy);\n" +
" vec2 p = fract(c) - vec2(0.5);\n" +
" float d = (p.x * p.x) + (p.y * p.y);\n" +
" if (d >= Size)\n" +
" p = vec2(0.0);\n" +
" vec3 normDelta = vec3(-p.x, -p.y, 1.0);\n" +
" litColor = color * max(0.0, dot(normDelta, LightDir));\n" +
" float t = 2.0 * dot(LightDir, normDelta);\n" +
" vec3 reflectDir = t * normDelta;\n" +
" reflectDir = LightDir - reflectDir;\n" +
"// vec3 reflectDir = LightDir - 2.0 * dot(LightDir, normDelta) * normDelta;\n" +
" float spec = max(dot(EyeDir, reflectDir), 0.0);\n" +
" spec = spec * spec;\n" +
" spec = spec * spec;\n" +
" spec *= SpecularFactor;\n" +
" litColor = min(litColor + spec, vec3(1.0));\n" +
" gl_FragColor = vec4(litColor, 1.0);\n" +
"// gl_FragColor = vec4(Scale);\n" +
"}";

var newMaterial = ccbCreateMaterial(vertexShader, fragmentShader, 0, null);

var soldier = ccbGetSceneNodeFromName('soldier');
ccbSetSceneNodeMaterialProperty(soldier, 0, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 1, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 2, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 4, 'Type', newMaterial);


And should be like this:
🔎︎



pmax
Registered User
Quote
2014-10-18 18:27:49

where did you get that code? probably is not compatible with CopperCube.
I'm testing the code found on the Internet and it is not compatible behaves in that way.


Specularmap
Guest
Quote
2014-10-18 19:24:26

http://sourceforge.net/projects/...


Specularmap
Guest
Quote
2014-10-18 19:29:58

Maybe Niko will help us that this code would become compatible?


Specularmap
Guest
Quote
2014-10-18 22:17:40

This is called Normalmap but where it can be used in a scene is hard to say)
When the light around the model strange looks from the positive to the negative black and white

var vertexShader =
"varying vec3 ViewDirection;\n" +
"varying vec3 LightDirection;\n" +
"varying vec4 baseColor;\n" +
"void main(void)\n" +
"{\n" +
" gl_Position = ftransform();\n" +
" gl_TexCoord[0] = gl_MultiTexCoord0;\n" +
" ViewDirection = -vec3(gl_ModelViewMatrix * gl_Vertex);\n" +
" LightDirection = gl_LightSource[0].position.xyz;\n" +
" baseColor = gl_Color;\n" +
"}";

var fragmentShader =
"uniform sampler2D bump;\n" +
"uniform float Ka;\n" +
"uniform float Kd;\n" +
"uniform float Ks;\n" +
"uniform float Ke;\n" +
"varying vec4 baseColor;\n" +
"varying vec3 ViewDirection;\n" +
"varying vec3 LightDirection;\n" +
"void main(void)\n" +
"{\n" +
" float Ke = 1.84;\n" +
" float Ks = 0.7;\n" +
" float Kd = 0.9;\n" +
" vec3 bumpPert = normalize( -(0.5,0.5,0.5) + texture2D(bump, gl_TexCoord[0].st).rgb );\n" +
" vec3 LightDir = normalize(LightDirection);\n" +
" vec3 ViewDir = normalize(ViewDirection);\n" +
" vec3 nNormal = normalize(gl_NormalMatrix * bumpPert);\n" +
" float diffuse = clamp(dot(LightDir, nNormal), 0.0, 1.0);\n" +
" float specular = pow(clamp(dot(reflect(ViewDir, nNormal),-LightDir),0.0, 1.0), Ke);\n" +
" gl_FragColor = (Kd * diffuse * baseColor) + (Ks * specular)*baseColor;\n" +
"}";

var newMaterial = ccbCreateMaterial(vertexShader, fragmentShader, 0, null);

var soldier = ccbGetSceneNodeFromName('soldier');
ccbSetSceneNodeMaterialProperty(soldier, 0, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 1, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 2, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 4, 'Type', newMaterial);

🔎︎



pmax
Registered User
Quote
2014-10-18 23:18:56

X-RAY_shaders
var vertexShader = 
"varying vec3 P;\n" +
"varying vec3 N;\n" +
"varying vec3 I;\n" +
"void main()\n" +
"{\n" +
"//Transform vertex by modelview and projection matrices\n" +
"gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" +
"// Position in clip space\n" +
"P = vec3(gl_ModelViewMatrix * gl_Vertex);\n" +
"// Normal transform (transposed model-view inverse)\n" +
"N = gl_NormalMatrix * gl_Normal;\n" +
"// Incident vector\n" +
"I = P;\n" +
"// Forward current color and texture coordinates after applying texture matrix\n" +
"gl_FrontColor = gl_Color;\n" +
"gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;\n" +
"}";

var fragmentShader =
"varying vec3 P;\n" +
"varying vec3 N;\n" +
"varying vec3 I;\n" +
"uniform float EdgeFalloff;\n" +
"void main()\n" +
"{\n" +
"float opacity = dot(normalize(N), normalize(-I));\n" +
"opacity = abs(opacity);\n" +
"EdgeFalloff = length(N);\n" +
"opacity = 1.0 - pow(opacity, EdgeFalloff);\n" +
"gl_FragColor = opacity * (gl_Color.r+gl_Color.g+gl_Color.b);\n" +
"}";

var newMaterial = ccbCreateMaterial(vertexShader, fragmentShader, 0, null);

var mesh = ccbGetSceneNodeFromName('Mesh2');
ccbSetSceneNodeMaterialProperty(mesh, 0, 'Type', newMaterial);
var mesh1 = ccbGetSceneNodeFromName('Mesh2');
ccbSetSceneNodeMaterialProperty(mesh1, 1, 'Type', newMaterial);
var mesh2 = ccbGetSceneNodeFromName('Mesh2');
ccbSetSceneNodeMaterialProperty(mesh2, 2, 'Type', newMaterial);

Example scene
https://www.dropbox.com/s/qv2uuz...
🔎︎



Specularmap
Guest
Quote
2014-10-19 00:14:42

Your shader with a skull for some reason fails...
---
That, I think, the first working and the desired shader for CC5,like metal
float m = 5.0; shininess or power...
Minnaert_reflection:

var vertexShader =
"varying vec3 normal;\n" +
"varying vec3 vpos;\n" +
"void main (void)\n" +
"{\n" +
" // vertex normal\n" +
" normal = gl_NormalMatrix * gl_Normal;\n" +
" // vertex position (in eye-space)\n" +
" vpos = vec3(gl_ModelViewMatrix * gl_Vertex);\n" +
" gl_Position = ftransform();\n" +
"}";

var fragmentShader =
"varying vec3 normal;\n" +
"varying vec3 vpos;\n" +
"uniform float m;\n" +
"void main (void)\n" +
"{\n" +
"float m = 5.0;\n" +
" vec3 N = normalize(normal);\n" +
" vec3 V = normalize(-vpos);\n" +
" vec3 L = normalize(gl_LightSource[0].position.xyz - vpos);\n" +
" // diffuse color\n" +
" vec4 kd = gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse;\n" +
" // Minnaert reflection model\n" +
" float NdotV = dot(N,V);\n" +
" float NdotL = dot(N,L);\n" +
" gl_FragColor = pow(max(NdotV * NdotL, 0.0), m) * kd;\n" +
"}";

var newMaterial = ccbCreateMaterial(vertexShader, fragmentShader, 0, null);

var soldier = ccbGetSceneNodeFromName('soldier');
ccbSetSceneNodeMaterialProperty(soldier, 0, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 1, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 2, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 4, 'Type', newMaterial);

🔎︎



Specularmap
Guest
Quote
2014-10-19 01:04:15

Here is a working "xray"
but there is no transparency = opacity
Maybe Niko add opacity

var vertexShader =
"varying vec3 N;\n" +
"varying vec3 I;\n" +
"varying vec4 Cs;\n" +
"void main()\n" +
"{\n" +
" vec4 P = gl_ModelViewMatrix * gl_Vertex;\n" +
" I = P.xyz - vec3 (0);\n" +
" N = gl_NormalMatrix * gl_Normal;\n" +
" Cs = gl_Color;\n" +
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" +
"}";

var fragmentShader =
"// vertex to fragment shader io\n" +
"varying vec3 N;\n" +
"varying vec3 I;\n" +
"varying vec4 Cs;\n" +
"// globals\n" +
"uniform float edgefalloff;\n" +
"uniform float intensity;\n" +
"uniform float ambient;\n" +
"// entry point\n" +
"void main()\n" +
"{\n" +
"float ambient = 0.1;\n" +
"float edgefalloff = 0.6;\n" +
"float intensity = 2.0;\n" +
" float opac = dot(normalize(-N), normalize(-I));\n" +
" opac = abs(opac);\n" +
" opac = ambient + intensity*(1.0-pow(opac, edgefalloff));\n" +
" //opac = 1.0 - opac;\n" +
" gl_FragColor = opac * Cs;\n" +
" gl_FragColor.a = opac;\n" +
"}";

var newMaterial = ccbCreateMaterial(vertexShader, fragmentShader, 0, null);

var soldier = ccbGetSceneNodeFromName('soldier');
ccbSetSceneNodeMaterialProperty(soldier, 0, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 1, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 2, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 4, 'Type', newMaterial);


🔎︎



Specularmap
Guest
Quote
2014-10-19 02:17:05

Unfortunately, there is only one light source, even of only two.
Phong

var vertexShader =
"varying vec3 normal;\n" +
"varying vec3 vpos;\n" +
"void main()\n" +
"{\n" +
" // vertex normal\n" +
" normal = normalize(gl_NormalMatrix * gl_Normal);\n" +
" // vertex position\n" +
" vpos = vec3(gl_ModelViewMatrix * gl_Vertex);\n" +
" // vertex position\n" +
" gl_Position = ftransform();\n" +
"}";

var fragmentShader =
"varying vec3 normal;\n" +
"varying vec3 vpos;\n" +
"uniform float shininess;\n" +
"void main()\n" +
"{\n" +
"float shininess = 40;\n" +
" vec3 n = normalize(normal);\n" +
" vec4 diffuse = vec4(0.0);\n" +
" vec4 specular = vec4(0.0);\n" +
" // the material properties are embedded in the shader (for now)\n" +
" vec4 mat_ambient = vec4(1.0, 1.0, 1.0, 1.0);\n" +
" vec4 mat_diffuse = vec4(1.0, 1.0, 1.0, 1.0);\n" +
" vec4 mat_specular = vec4(1.0, 1.0, 1.0, 1.0);\n" +
" // ambient term\n" +
" vec4 ambient = mat_ambient * gl_LightSource[0].ambient;\n" +
" // diffuse color\n" +
" vec4 kd = mat_diffuse * gl_LightSource[0].diffuse;\n" +
" // specular color\n" +
" vec4 ks = mat_specular * gl_LightSource[0].specular;\n" +
" // diffuse term\n" +
" vec3 lightDir = normalize(gl_LightSource[0].position.xyz - vpos);\n" +
" float NdotL = dot(n, lightDir);\n" +
" if (NdotL > 0.0)\n" +
" diffuse = kd * NdotL;\n" +
" // specular term\n" +
" vec3 rVector = normalize(2.0 * n * dot(n, lightDir) - lightDir);\n" +
" vec3 viewVector = normalize(-vpos);\n" +
" float RdotV = dot(rVector, viewVector);\n" +
" if (RdotV > 0.0)\n" +
" specular = ks * pow(RdotV, shininess);\n" +
" gl_FragColor = ambient + diffuse + specular;\n" +
"}";

var newMaterial = ccbCreateMaterial(vertexShader, fragmentShader, 0, null);

var soldier = ccbGetSceneNodeFromName('soldier');
ccbSetSceneNodeMaterialProperty(soldier, 0, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 1, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 2, 'Type', newMaterial);
ccbSetSceneNodeMaterialProperty(soldier, 4, 'Type', newMaterial);

🔎︎



Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "I?ternational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact