ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperCube > Programming and Scripting
forum topic indicator awful glsl flashlight
person icon
SamGrady
Guest
Quote
2023-11-25 18:52:16


/*
<behavior jsname="behavior_FPSflashlight" description="FPS Flashlight">
<property name="radius" type="float" default="10.0" />
<property name="softness" type="float" default="2.0" />
</behavior>
*/
behavior_FPSflashlight = function(){}
behavior_FPSflashlight.prototype.onAnimate = function(node, timeMs){
this.name = ccbGetSceneNodeProperty(node, "Name");
ccbSetCopperCubeVariable(this.name + ".radius", this.radius);
ccbSetCopperCubeVariable(this.name + ".softness", this.softness);
for(i = 0; i < ccbGetSceneNodeMaterialCount(node); ++i)
ccbSetSceneNodeMaterialProperty(node, i, 'Type', this.material(this.name));
this.onAnimate = function(){};
}
behavior_FPSflashlight.prototype.material = function(name){
var vertexShader =
"uniform mat4 mWorldViewProj; \n" +
"uniform mat4 mTransWorld; \n" +
"uniform mat4 mInvWorld; \n" +
"uniform vec3 camPos; \n" +
"varying float depth; \n" +
"varying vec3 fragToCam; \n" +
"varying vec3 fragNormal; \n" +
"void main(void) { \n" +
" gl_Position = mWorldViewProj * gl_Vertex; \n" +
" gl_FrontColor = gl_BackColor = vec4(1.0, 1.0, 1.0, 1.0); \n" +
" gl_TexCoord[0] = gl_MultiTexCoord0; \n" +
" vec4 worldPos = mTransWorld * gl_Vertex; \n" +
" vec3 fragPos = vec3(worldPos); \n" +
" fragToCam = camPos - fragPos; \n" +
" fragNormal = normalize(vec3(mInvWorld * vec4(gl_Normal, 0.0))); \n" +
" depth = gl_Position.w; \n" +
"}";
var fragmentShader =
"uniform sampler2D myTexture; \n" +
"uniform float radius; \n" +
"uniform float softness; \n" +
"varying float depth; \n" +
"varying vec3 fragToCam; \n" +
"varying vec3 fragNormal; \n" +
"void main(void) { \n" +
" vec2 texCoord = gl_TexCoord[0].st; \n" +
" vec4 col = texture2D(myTexture, texCoord); \n" +
" vec3 normal = normalize(fragNormal); \n" +
" vec3 lightDir = normalize(fragToCam); \n" +
" float intensity = max(dot(normal, lightDir), 0.0); \n" +
" vec2 st = gl_FragCoord.xy / vec2(800.0, 600.0); \n" +
" vec2 center = vec2(0.5, 0.5); \n" +
" float dist = length(st - center) * depth; \n" +
" float smoothStep = smoothstep(radius + softness, radius - softness, dist); \n" +
" col.rgb *= intensity; \n" +
" col.rgb -= mix(depth, depth / 50, smoothStep) / 50; \n" +
" gl_FragColor = col; \n" +
"}";
var shaderCallBack = function(){
let pos = ccbGetSceneNodeProperty(ccbGetActiveCamera(), "PositionAbs");
let radius = ccbGetCopperCubeVariable(name + ".radius");
let softness = ccbGetCopperCubeVariable(name + ".softness");
ccbSetShaderConstant(1, "camPos", pos.x, pos.y, pos.z, null);
ccbSetShaderConstant(2, "radius", radius, null, null, null);
ccbSetShaderConstant(2, "softness", softness, null, null, null);
};
return ccbCreateMaterial(vertexShader, fragmentShader, 13, shaderCallBack);
}



i don't even add comments on this crap
anyway no one need this
[img]https://i.ibb.co/ckV

person icon
SamGrady
Guest
Quote
2023-11-25 18:52:49

embedded external image
🔎︎


person icon
SamGrady
Guest
Quote
2023-11-25 18:57:32

i forget about screen resolution

person icon
SamGrady
Guest
Quote
2023-11-25 18:59:08

but the forum doesn't allow me to add a fixed code nowemoji icon_hmpf

person icon
SamGrady
Guest
Quote
2023-11-25 18:59:39



var fragmentShader =
"uniform sampler2D myTexture; \n" +
"uniform float radius; \n" +
"uniform float softness; \n" +
"uniform vec2 screen; \n" +
"varying float depth; \n" +
"varying vec3 fragToCam; \n" +
"varying vec3 fragNormal; \n" +
"void main(void) { \n" +
" vec2 texCoord = gl_TexCoord[0].st; \n" +
" vec4 col = texture2D(myTexture, texCoord); \n" +
" vec3 normal = normalize(fragNormal); \n" +
" vec3 lightDir = normalize(fragToCam); \n" +
" float intensity = max(dot(normal, lightDir), 0.0); \n" +
" vec2 st = gl_FragCoord.xy / vec2(screen.x, screen.y); \n" +
" vec2 center = vec2(0.5, 0.5); \n" +
" float dist = length(st - center) * depth; \n" +
" float smoothStep = smoothstep(radius + softness, radius - softness, dist); \n" +
" col.rgb *= intensity; \n" +
" col.rgb -= mix(depth, depth / 50, smoothStep) / 50; \n" +
" gl_FragColor = col; \n" +
"}";
var shaderCallBack = function(){
let pos = ccbGetSceneNodeProperty(ccbGetActiveCamera(), "PositionAbs");
let radius = ccbGetCopperCubeVariable(name + ".radius");
let softness = ccbGetCopperCubeVariable(name + ".softness");
ccbSetShaderConstant(1, "camPos", pos.x, pos.y, pos.z, null);
ccbSetShaderConstant(2, "radius", radius, null, null, null);
ccbSetShaderConstant(2, "softness", softness, null, null, null);
ccbSetShaderConstant(2, "screen", ccbGetScreenWidth(), ccbGetScreenHeight(), null, null);
};



person icon
Guru
Guest
Quote
2023-11-25 19:16:48

One major drawback with flashlight shaders and any shader is that they don't work with realtime shadows or any other inbuilt dynamic lighting, I mean I don't want to use flashlight alone, but also want that all the lights in the scene remains active with the flashlight.

person icon
Guest
Guest
Quote
2023-11-25 21:11:27

@SamGrady, please post a project demo ccb. Thanks.

person icon
Robbo
Guest
Quote
2023-11-25 23:49:37

Guru wrote:
One major drawback with flashlight shaders and any shader is that they don't work with realtime shadows or any other inbuilt dynamic lighting, I mean I don't want to use flashlight alone, but also want that all the lights in the scene remains active with the flashlight.


I could be wrong but I think this should work with JIC's Screen shader such that all backend lighting is calculated as normal and only the render to texture in front of the camera adds in the spotlight so they merge together as one....at least I think it should work, started working on this idea yesterday....let you know...

person icon
just_in_case
Moderator
Quote
2023-11-26 07:27:43

@guru, Yup, you guys can create flashlight and any effect that deals with the screen, though am not sure if it will be able to simulate the actual depth effect or not but definitely it can be used with any kind of post processing and basic shaders like flashlight and all.

person icon
Robbo
Guest
Quote
2023-11-27 07:23:54

I found the same thing JIC mentioned - whatever object you select as the effected node will be used to send through the vertex positions, so if you use a 3d plane mesh in front of the camera then it will look at that and not the actual scene behind it so you cannot get any depth info from that as it will be fixed.

No wonder I never could get depth info from post processing shader code in CC either.

The only way at the moment I think is to get depth info yourself and send that into the shader - will not be vertex by vertex position but a general distance amount only where the line collides with the scene in front of the camera.

I have done this before as a test and it works ok....not a true flashlight but not bad either.


Create reply:










 

  

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


icon_holyicon_cryicon_devilicon_lookicon_grinicon_kissicon_monkeyicon_hmpf
icon_sadicon_happyicon_smileicon_uhicon_blink   






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