[SOLVED] set transparency of a texture

Isn’t up there already a shader ?

Instancing way :

  1. n = NodePath(’’)
  2. n.node().stealChildren(station.node())
  3. n.reparentTo(station)
  4. instance n instead

Well I never used a shader in Panda before :smiley:

When I try to apply the shader I get this error:

spob.setShader(Shader.load("../mcscripts/transparent.sha"))
TypeError: NodePath.setShader() argument 1 must be Shader, not NoneType

However the loading of the shader file works fine it seems.

EDIT
No wait that’s the error:

:gobj(error): Could not read shader file: ../mcscripts/transparent.sha

Does that mean the path is wrong or the file internally?

EDIT2
Alright, with loader.loadShader the path works. But the shader doesnt.
Knowing nothing about shaders I thought I’d do:

glowtex = loader.loadTexture("../mcstructures/"
+StobList[self.StobIdentifier][4+i*13]+"/"
+StobList[self.StobIdentifier][4+i*13]+"_glow.png")
               
colortex = loader.loadTexture("../mcstructures/"
+StobList[self.StobIdentifier][4+i*13]+"/"
+StobList[self.StobIdentifier][4+i*13]+".png")

spob.setShader(loader.loadShader("../mcscripts/transparent.sha"))
spob.setShaderInput("tex_0", colortex)
spob.setShaderInput("tex_1", glowtex)
spob.setShaderInput("k_fade", 0)

spob_glow_sequence = Sequence(
LerpFunc(self.changeSpobTexAlpha, fromData=0.0, toData=1.0, duration=1.0, extraArgs=[spob]),
LerpFunc(self.changeSpobTexAlpha, fromData=1.0, toData=0.0, duration=1.0, extraArgs=[spob])).loop()
self.EnvIntervals.append(spob_glow_sequence)

def changeSpobTexAlpha(self, alphavalue, spob):
	spob.setShaderInput("k_fade", alphavalue)

This is the error:

:gobj(error): /Applications/Mare Ceti/assets/mcscripts/transparent.sha: (26) : error C0000: syntax error, unexpected identifier, expecting ',' or ';' at token "second_color"
:gobj(error): /Applications/Mare Ceti/assets/mcscripts/transparent.sha: (26) : error C0501: type name expected at token "second_color"
:gobj(error): Shader encountered an error.

The (26) is the line number. The error message tells you that a semicolon is missing at the end of first_color definition line.

Yeah, makes sense.
Now the error says:

:gobj(error): /Applications/Mare Ceti/assets/mcscripts/transparent.sha: (25) : error C1008: undefined variable "first_color"
:gobj(error): /Applications/Mare Ceti/assets/mcscripts/transparent.sha: (26) : error C1008: undefined variable "second_color"
:gobj(error): /Applications/Mare Ceti/assets/mcscripts/transparent.sha: (27) : error C1008: undefined variable "first_color"
:gobj(error): /Applications/Mare Ceti/assets/mcscripts/transparent.sha: (27) : error C1008: undefined variable "second_color"
:gobj(error): Shader encountered an error.

Tried using:

spob.setShader(loader.loadShader("../mcscripts/transparent.sha"))
					spob.setShaderInput("first_color", colortex)
					spob.setShaderInput("second_color", glowtex)
					spob.setShaderInput("k_fade", 0)

instead of tex_0 and tex_1, but didn’t help.

Of course, each variable must have type definition, put float4 in front of first_color and second_color.
Next you’d need to strip k_ from the shader input.

Alright, fixed that. The good news is that there are no more errors in the console and that something is happening on screen. But its not that the glow texture is fading in and out. The base texture is there and other plain colors fade in an out like a rainbow.

These are the two states that are being blended:

and

The former being the base texture without glow.

use only the first element of fade :

//Cg

void vshader( 
  in float4 vtx_color: COLOR, 
  out float4 l_color0 : COLOR0, 
  in float4 vtx_position : POSITION, 
  out float4 l_position : POSITION, 
  in float2 vtx_texcoord0 : TEXCOORD0, 
  out float2 l_texcoord0 : TEXCOORD0, 
  in uniform float4x4 mat_modelproj)
{
  l_color0 = vtx_color;
  l_position = mul(mat_modelproj, vtx_position);
  l_texcoord0 = vtx_texcoord0;
}

void fshader(
  in float4 l_color0 : COLOR0,
  in float2 l_texcoord0: TEXCOORD0,
  in uniform sampler2D tex_0: TEXUNIT0,
  in uniform sampler2D tex_1: TEXUNIT1,
  in uniform float4 k_fade,
  out float4 o_color : COLOR)
{
  float4 first_color = tex2D(tex_0, l_texcoord0);
  float4 second_color = tex2D(tex_1, l_texcoord0);
  o_color = lerp(second_color, first_color, k_fade.x);
}
from pandac.PandaModules import *
from direct.interval.IntervalGlobal import *
import direct.directbase.DirectStart
import os

t1 = loader.loadTexture('maps/envir-ground.jpg')
ts = TextureStage('2')

s = loader.loadModel('smiley').getChild(0)
s.reparentTo(render)
s.setTexture(ts,t1)

sha = loader.loadShader('fade.sha')
s.setShader(sha)
s.setShaderInput('fade',0)

def applyFade(fade):
#     print fade
    s.setShaderInput('fade',fade)

lerp1 = LerpFunctionInterval(applyFade,duration=3)
lerp2 = LerpFunctionInterval(applyFade,duration=3,fromData=1,toData=0)

Sequence(
  lerp1, Wait(1),
  lerp2, Wait(1)
).loop()

base.accept('escape',os._exit,[0])
base.cam.setY(-10)
run()

Alright, this works. The shader:

//Cg

void vshader(
  in float4 vtx_color: COLOR,
  out float4 l_color0 : COLOR0,
  in float4 vtx_position : POSITION,
  out float4 l_position : POSITION,
  in float2 vtx_texcoord0 : TEXCOORD0,
  out float2 l_texcoord0 : TEXCOORD0,
  in uniform float4x4 mat_modelproj)
{
  l_color0 = vtx_color;
  l_position = mul(mat_modelproj, vtx_position);
  l_texcoord0 = vtx_texcoord0;
}

void fshader(
  in float4 l_color0 : COLOR0,
  in float2 l_texcoord0: TEXCOORD0,
  in uniform sampler2D tex_0: TEXUNIT0,
  in uniform sampler2D tex_1: TEXUNIT1,
  in uniform float4 k_fade,
  out float4 o_color : COLOR)
{
  float4 first_color = tex2D(tex_0, l_texcoord0);
  float4 second_color = tex2D(tex_1, l_texcoord0);
  o_color = lerp(second_color, first_color, k_fade.x);
}

And in Python:

glowtex = loader.loadTexture("../mcstructures/"
+StobList[self.StobIdentifier][4+i*13]+"/"
+StobList[self.StobIdentifier][4+i*13]+"_glow.png")
colortex = loader.loadTexture("../mcstructures/"
+StobList[self.StobIdentifier][4+i*13]+"/"
+StobList[self.StobIdentifier][4+i*13]+".png")
					
spob = spob.getChild(0)
ts = TextureStage("2")
spob.setTexture(ts, glowtex)
					
spob.setShader(loader.loadShader("../mcscripts/transparent.sha"))
spob.setShaderInput("fade", 0)
					
spob_glow_sequence = Sequence(
LerpFunc(self.changeSpobTexAlpha, fromData=0.0, toData=1.0, duration=1.25, extraArgs=[spob]),Wait(1.25),
LerpFunc(self.changeSpobTexAlpha, fromData=1.0, toData=0.0, duration=0.25, extraArgs=[spob]))
self.EnvIntervals.append(spob_glow_sequence)
spob_glow_sequence.loop()
					
spob_rotation_interval = spob.hprInterval(240,Vec3(-140,0,0))
spob_rotation_interval.loop()
self.EnvIntervals.append(spob_rotation_interval)
				
self.SpobObjects.append(spob)

def changeSpobTexAlpha(self, alphavalue, spob):
	spob.setShaderInput("fade", alphavalue)

Problem is that the selection frames now look like this:

instead of:

So you already apply the shader to station’s children only, leaving the frame untouched at all.
Does the frame parented to the ModelRoot ?

Isn’t this the list of the root of the station, not the child ?

self.SpobObjects.append(spob)

It should be called earlier than :

spob = spob.getChild(0)

Wow that did it. Man you’re good.

The technique I’m using now could also be applied to ships, so that’s cool.

Wow I totally forgot about this thread.

Setting it to 0.98 doesnt seam to fix it for evening->night transition. I dont know why. This all is pretty weird. Not to mention weird render artifacts when you change the cameras Pitch and look at it from ‘below’

Bump

heres the same sample with setting the alpha valu to 0.98 instead of 1:
filedropper.com/gradientsequence

Weird artifact when looking at different angles

This doesnt work.

I have skyboxes like a matryoshka doll (one slightly bigger than the other), and when you have an interval changing the alpha of 2 at the same time and rotate the camera around, that render artifact still happens.
I tried values up to 0.8.

Okay, heres a sample with files: filedropper.com/skydometimelapse

the old link is dead