Skip to content

Commit efa79ee

Browse files
committed
working shadows for me planet shader.. But bunch o' cleaning needed now.. :/
1 parent 484fa0b commit efa79ee

11 files changed

Lines changed: 169 additions & 29 deletions

File tree

Blobtory/Scripts/Main.py

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from panda3d.core import loadPrcFile, loadPrcFileData
2-
from direct.showbase.ShowBase import ShowBase, PTAFloat, AmbientLight, DirectionalLight
1+
from direct.interval.LerpInterval import LerpPosInterval
2+
from panda3d.core import loadPrcFile, loadPrcFileData, LPoint3f
3+
from direct.showbase.ShowBase import ShowBase, PTAFloat, AmbientLight, DirectionalLight, Shader, Texture, TextureStage, \
4+
SamplerState, FrameBufferProperties, WindowProperties, GraphicsPipe, GraphicsOutput, NodePath
35

46
from Blobtory.Scripts.Pipeline.WindowCreator import WindowCreator
57

@@ -24,20 +26,76 @@ def __init__(self):
2426
self.planetGen.cubeformer.mouseTime.setData(PTAFloat([10, 0, 0, 1]))
2527
self.accept("a", self.planetGen.RegenPlanet)
2628

29+
sphere = self.loader.loadModel("assets/models/icosphere")
30+
sphere.reparentTo(self.render)
31+
myShader: Shader = Shader.load(Shader.SL_GLSL,
32+
vertex="assets/shaders/defaults/default.vert",
33+
fragment="assets/shaders/defaults/default.frag")
34+
sphere.setShader(myShader, 1)
35+
sphere.setScale(100)
36+
sphere.setPos((0,50,600))
37+
38+
myToonLightTex = self.winCreator.base.loader.loadTexture(
39+
"assets/textures/ramps/rampToonLight.png")
40+
myToonLightTex.setWrapU(Texture.WM_clamp)
41+
myToonLightTex.setWrapV(Texture.WM_clamp)
42+
myToonLightTex.setMagfilter(SamplerState.FT_nearest)
43+
myToonLightTex.setMinfilter(SamplerState.FT_nearest)
44+
45+
stageToonLight = TextureStage("ToonLight")
46+
stageToonLight.setSort(1)
47+
sphere.setTexture(stageToonLight, myToonLightTex)
48+
49+
winprops = WindowProperties(size=(512, 512))
50+
props = FrameBufferProperties()
51+
props.setRgbColor(1)
52+
props.setAlphaBits(1)
53+
props.setDepthBits(1)
54+
LBuffer = self.graphicsEngine.makeOutput(
55+
self.pipe, "offscreen buffer", -2,
56+
props, winprops,
57+
GraphicsPipe.BFRefuseWindow,
58+
self.win.getGsg(), self.win)
59+
60+
Ldepthmap = Texture()
61+
LBuffer.addRenderTexture(Ldepthmap, GraphicsOutput.RTMBindOrCopy,
62+
GraphicsOutput.RTPDepthStencil)
63+
64+
self.accept("v", self.bufferViewer.toggleEnable)
65+
2766
alight = AmbientLight('alight')
2867
alight.setColor((0.1, 0.1, 0.1, 1))
2968
alnp = self.render.attachNewNode(alight)
3069
self.render.setLight(alnp)
3170

32-
dlight = DirectionalLight('my dlight')
33-
dlnp = self.render.attachNewNode(dlight)
34-
self.render.setLight(dlnp)
71+
dlight1 = DirectionalLight('my dlight1')
72+
dlight1.setShadowCaster(True, 512, 512)
73+
#dlight1.show_frustum()
74+
dlnp1 = self.render.attachNewNode(dlight1)
75+
dlnp1.setPos(0,0,512)
76+
dlnp1.setHpr(0, 270, 0)
77+
self.render.setLight(dlnp1)
78+
79+
bmin, bmax = self.render.get_tight_bounds(dlnp1)
80+
size=128
81+
bmin, bmax = LPoint3f(-size,0, -size), LPoint3f(size, size,size)
82+
print(bmin,bmax)
83+
lens = dlight1.get_lens(0)
84+
lens.set_film_offset((bmin.xz + bmax.xz) * 0.5)
85+
lens.set_film_size(bmax.xz - bmin.xz)
86+
lens.set_near_far(bmin.y, bmax.y)
87+
88+
i = LerpPosInterval(sphere,
89+
2,
90+
(0,-100,600),(0,100,600))
91+
i.loop()
3592

36-
dlight = DirectionalLight('my dlight')
37-
dlight.setColor((0.05, 0.05, 0.05, 1))
38-
dlnp = self.render.attachNewNode(dlight)
39-
dlnp.setHpr(0, 180, 0)
40-
self.render.setLight(dlnp)
93+
dlight2 = DirectionalLight('my dlight2')
94+
dlight2.setColor((0.05, 0.05, 0.05, 1))
95+
dlight2.setShadowCaster(True, 512, 512)
96+
dlnp2 = self.render.attachNewNode(dlight2)
97+
dlnp2.setHpr(0, 180, 0)
98+
self.render.setLight(dlnp2)
4199

42100

43101
loadPrcFileData('', 'framebuffer-multisample 1')

Blobtory/Scripts/planet_former/MarchingCubes.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from panda3d.core import Texture, Shader, NodePath, LVecBase3i, ShaderAttrib, GeomEnums, GeomNode, \
44
GeomVertexFormat, GeomVertexData, GeomTriangles, Geom, OmniBoundingVolume, Material, LColor, SamplerState, \
5-
TextureStage
5+
TextureStage, TransparencyAttrib
66

77
from assets.shaders.compute.includes import MarchTable
88
from Blobtory.Scripts.planet_former.CubeFormer import CubeFormer
@@ -139,11 +139,23 @@ def GenerateMesh(self):
139139
myDiffTex = self.winCreator.base.loader.loadTexture(
140140
"assets/textures/ramps/rampTerrainDiffuse.png",
141141
"assets/textures/ramps/rampTerrainSpecular.png")
142-
#myDiffTex.setMagfilter(SamplerState.FT_linear)
143-
#myDiffTex.setMinfilter(SamplerState.FT_linear)
142+
myDiffTex.setWrapU(Texture.WM_clamp)
143+
myDiffTex.setWrapV(Texture.WM_clamp)
144144

145145
stageDiff = TextureStage("Diffuse")
146146
stageDiff.setSort(1)
147147
self.geomPath.setTexture(stageDiff, myDiffTex)
148+
#self.geomPath.setTransparency(TransparencyAttrib.MAlpha)
149+
150+
myToonLightTex = self.winCreator.base.loader.loadTexture(
151+
"assets/textures/ramps/rampToonLight.png")
152+
myToonLightTex.setWrapU(Texture.WM_clamp)
153+
myToonLightTex.setWrapV(Texture.WM_clamp)
154+
myToonLightTex.setMagfilter(SamplerState.FT_nearest)
155+
myToonLightTex.setMinfilter(SamplerState.FT_nearest)
156+
157+
stageToonLight = TextureStage("ToonLight")
158+
stageToonLight.setSort(2)
159+
self.geomPath.setTexture(stageToonLight, myToonLightTex)
148160

149161

assets/shaders/compute/alphaNoiseSphere.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ uniform float radius;
88
uniform float offset;
99
uniform vec3 midPoint;
1010
uniform vec4 mouseTime;
11-
const int craterCount = 0;
11+
const int craterCount = 20;
1212

1313
layout(rgba32f, binding = 0) uniform image3D vertexBufferWAlpha;
1414

@@ -45,7 +45,7 @@ void main() {
4545

4646
// point.w = (snoise(polarCoord.yz/(mouseX*10))*radius - r);
4747

48-
float noiseOuter = 1-abs(fractalNoise(normalize(movedPoint)*1)+1)*mouseTime.w*90;
48+
float noiseOuter = 1-abs(fractalNoise(normalize(movedPoint)*1)+1)*mouseTime.w*50;
4949
float planetRadius = radius+noiseOuter;
5050
point.w = smoothstep(0, 1, (planetRadius - lengthFromCenter)/10);
5151

assets/shaders/compute/cubemarcher.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void main() {
7171
vec3 normal = normalize(cross(v2-v0, v1-v0));
7272
//vec3 normal = normalize(v2+v0+v1);
7373
int type = int(distance(vec3(512), v1)/60);
74-
if(type > 6) if(dot(normalize(vec3(512)-v1), normal)<0.5) type = 5;
74+
if(type > 6) if(dot(normalize(vec3(512)-v1), normal)<0.8) type = 5;
7575
imageStore(normalBuffer, int(triangleIDIndex/3), vec4(normal, type));
7676

7777
i+=3;
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#version 130
22

3-
#pragma include "../utils/p3d_light_sources"
3+
#pragma include "../utils/p3d_light_sources.glsl"
44

55
#define MAX_ITERATION 20
66

@@ -9,12 +9,20 @@ uniform sampler2D p3d_Texture0;
99
// Input from vertex shader
1010
in vec2 texcoord;
1111
in vec3 normal;
12+
in vec3 viewspacePos;
1213

1314
out vec4 outputColor;
1415

1516
void main() {
16-
vec4 color = texture(p3d_Texture0, texcoord);
17+
//vec4 color = texture(p3d_Texture0, texcoord);
1718
vec3 rgb_normal = (normalize(normal) + 1) * 0.5;
1819

19-
outputColor = saturate(dot(normal, p3d_LightSource[0]));
20+
vec3 lightDir = p3d_LightSource[0].position.xyz;
21+
if (p3d_LightSource[0].position.w != 0) lightDir -= viewspacePos;
22+
lightDir = normalize(-lightDir);
23+
24+
float lambertian = max(dot(lightDir, normal), 0.0);
25+
lambertian = texture(p3d_Texture0, vec2(lambertian, 0.5)).x;
26+
27+
outputColor = vec4(lambertian)*vec4(1, 0.5, 0.5, 1);
2028
}

assets/shaders/defaults/default.vert

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// Uniform inputs
44
uniform mat4 p3d_ModelViewProjectionMatrix;
5+
uniform mat4 p3d_ModelViewMatrix;
56

67
// Vertex inputs
78
in vec4 p3d_Vertex;
@@ -11,10 +12,14 @@ in vec3 p3d_Normal;
1112
// Output to fragment shader
1213
out vec2 texcoord;
1314
out vec3 normal;
15+
out vec3 viewspacePos;
1416

1517
void main() {
1618
gl_Position = p3d_ModelViewProjectionMatrix*p3d_Vertex;
1719

20+
vec4 viewspacePos4 = p3d_ModelViewMatrix * p3d_Vertex;
21+
viewspacePos = vec3(viewspacePos4) / viewspacePos4.w;
22+
1823
normal = p3d_Normal;
1924

2025
texcoord = p3d_MultiTexCoord0;

assets/shaders/planets/planet.frag

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#pragma include "../utils/p3d_light_sources.glsl"
44
#pragma include "../utils/p3d_material.glsl"
55

6+
uniform sampler2D p3d_Texture1;
7+
68
// Input from vertex shader
79
in vec3 viewspacePos;
810
in vec4 fragPos;
@@ -16,6 +18,8 @@ in float specStrength;
1618
in vec3 cam_pos;
1719
in vec3 cam_dir;
1820

21+
in vec4[4] shadow_uv;
22+
1923
out vec4 outputColor;
2024

2125
vec3 remap(vec3 iMin, vec3 iMax, vec3 oMin, vec3 oMax, vec3 v) {
@@ -28,6 +32,47 @@ float remap(float iMin, float iMax, float oMin, float oMax, float v) {
2832
return mix(oMin, oMax, t);
2933
}
3034

35+
36+
float textureProjSoft(sampler2DShadow tex, vec4 uv, float bias, float blur) {
37+
float result = textureProj(tex, uv, bias);
38+
result += textureProj(tex, vec4(uv.xy + vec2( -0.326212, -0.405805)*blur, uv.z-bias, uv.w));
39+
result += textureProj(tex, vec4(uv.xy + vec2(-0.840144, -0.073580)*blur, uv.z-bias, uv.w));
40+
result += textureProj(tex, vec4(uv.xy + vec2(-0.695914, 0.457137)*blur, uv.z-bias, uv.w));
41+
result += textureProj(tex, vec4(uv.xy + vec2(-0.203345, 0.620716)*blur, uv.z-bias, uv.w));
42+
result += textureProj(tex, vec4(uv.xy + vec2(0.962340, -0.194983)*blur, uv.z-bias, uv.w));
43+
result += textureProj(tex, vec4(uv.xy + vec2(0.473434, -0.480026)*blur, uv.z-bias, uv.w));
44+
result += textureProj(tex, vec4(uv.xy + vec2(0.519456, 0.767022)*blur, uv.z-bias, uv.w));
45+
result += textureProj(tex, vec4(uv.xy + vec2(0.185461, -0.893124)*blur, uv.z-bias, uv.w));
46+
result += textureProj(tex, vec4(uv.xy + vec2(0.507431, 0.064425)*blur, uv.z-bias, uv.w));
47+
result += textureProj(tex, vec4(uv.xy + vec2(0.896420, 0.412458)*blur, uv.z-bias, uv.w));
48+
result += textureProj(tex, vec4(uv.xy + vec2(-0.321940, -0.932615)*blur, uv.z-bias, uv.w));
49+
result += textureProj(tex, vec4(uv.xy + vec2(-0.791559, -0.597705)*blur, uv.z-bias, uv.w));
50+
return result/13.0;
51+
}
52+
53+
//float shadowCoef(vec4 vPos, sampler2DShadow shadowMap, mat4 shadowViewMatrix) {
54+
// int index = 3;
55+
// // find the appropriate depth map to look up in
56+
// // based on the depth of this fragment
57+
// if(gl_FragCoord.z < far_d.x) index = 0;
58+
// else if(gl_FragCoord.z < far_d.y) index = 1;
59+
// else if(gl_FragCoord.z < far_d.z) index = 2;
60+
//
61+
// // transform this fragment's position from view space to
62+
// // scaled light clip space such that the xy coordinates
63+
// // lie in [0;1]. Note that there is no need to divide by w
64+
// // for othogonal light sources
65+
// vec4 shadow_coord = shadowViewMatrix*vPos;
66+
// // set the current depth to compare with
67+
// shadow_coord.w = shadow_coord.z;
68+
//
69+
// // tell glsl in which layer to do the look up
70+
// shadow_coord.z = float(index);
71+
//
72+
// // let the hardware do the comparison for us
73+
// return shadow2DArray(shadowMap, shadow_coord).x;
74+
//}
75+
3176
void main() {
3277
vec3 illumLightSum = vec3(0);
3378
vec3 normal = normalize(primNormal);
@@ -41,9 +86,11 @@ void main() {
4186

4287
float distance = length(lightDir);
4388
distance = distance * distance;
44-
lightDir = normalize(lightDir);
89+
lightDir = normalize(-lightDir);
4590

4691
float lambertian = max(dot(lightDir, normal), 0.0);
92+
lambertian = texture(p3d_Texture1, vec2(lambertian, 0.5)).x;
93+
4794
float specular = 0.0;
4895

4996
if (lambertian > 0.0) {
@@ -53,14 +100,17 @@ void main() {
53100
vec3 halfDir = normalize(lightDir + viewDir);
54101
float specAngle = max(dot(halfDir, normal), 0.0);
55102
specular = pow(specAngle, p3d_Material.shininess);
103+
specular = texture(p3d_Texture1, vec2(specular, 0.5)).x;
56104
}
57-
vec3 illumDiffuse = diffuseColor * lambertian * p3d_LightSource[i].color.xyz * 1 / distance;
58-
vec3 illumSpecular = diffuseColor * specStrength * specular * p3d_LightSource[i].color.xyz * 1 / distance;
59-
60-
illumLightSum += illumDiffuse+illumSpecular;
105+
vec3 illumDiffuse = diffuseColor * lambertian * p3d_LightSource[i].color.xyz * 1 / distance;
106+
vec3 illumSpecular = diffuseColor * specStrength * specular * p3d_LightSource[i].color.xyz * 1 / distance;
107+
//illumLightSum += (illumDiffuse+illumSpecular)*textureProjSoft(p3d_LightSource[i].shadowMap, shadow_uv[i], 0.0001, 0.2);
108+
illumLightSum += (illumDiffuse+illumSpecular)*textureProj(p3d_LightSource[i].shadowMap, shadow_uv[i]);
61109
}
62110

63111
vec3 colorGammaCorrected = pow(p3d_LightModel.ambient.xyz*diffuseColor+illumLightSum, vec3(0.49504950495));
112+
colorGammaCorrected = texture(p3d_Texture1, vec2(length(colorGammaCorrected.xyz), 0), 0).x*colorGammaCorrected;
113+
64114
// use the gamma corrected color in the fragment0
65-
outputColor = vec4(colorGammaCorrected, 1.0);
115+
outputColor = vec4(colorGammaCorrected, 1);
66116
}

assets/shaders/planets/planet.vert

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#version 430
22

3+
#pragma include "../utils/p3d_light_sources.glsl"
4+
35
// Uniform inputs
46
uniform mat4 p3d_ModelViewProjectionMatrix;
57
uniform mat4 p3d_ViewMatrixInverse;
@@ -8,7 +10,6 @@ uniform mat4 p3d_ModelViewMatrix;
810
uniform mat3 p3d_NormalMatrix;
911

1012
uniform sampler2D p3d_Texture0;
11-
uniform sampler2D p3d_Texture1;
1213

1314
layout(rgba32f) uniform readonly image3D vertexBufferEdge;
1415
layout(rgba32i) uniform readonly iimageBuffer triangleBuffer;
@@ -30,6 +31,7 @@ out float specStrength;
3031

3132
out vec3 cam_pos;
3233
out vec3 cam_dir;
34+
out vec4[4] shadow_uv;
3335

3436
void main() {
3537
ivec3 vertexIndex = imageLoad(triangleBuffer, gl_VertexID).xyz;
@@ -56,4 +58,8 @@ void main() {
5658

5759
cam_pos = p3d_ViewMatrixInverse[3].xyz;
5860
cam_dir = -p3d_ViewMatrixInverse[2].xyz;
61+
62+
for (int i = 0; i < p3d_LightSource.length; i++) {
63+
shadow_uv[i] = p3d_LightSource[i].shadowViewMatrix * (p3d_ModelViewMatrix * vertex);
64+
}
5965
}
149 Bytes
Loading

config/Config.prc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ load-display pandagl
2222
# A value of -2 for the origin means to center it on the screen,
2323
# while -1 lets the window manager choose the position.
2424

25-
framebuffer-srgb #t
25+
#framebuffer-srgb #t
2626
#framebuffer-multisample 1
2727

2828
win-origin -2 -2
@@ -34,7 +34,6 @@ win-size 1600 900
3434
fullscreen #f
3535
#threading-model Cull/Draw
3636

37-
3837
# The framebuffer-hardware flag forces it to use an accelerated driver.
3938
# The framebuffer-software flag forces it to use a software renderer.
4039
# If you set both to false, it will use whatever's available.
@@ -68,6 +67,8 @@ default-directnotify-level warning
6867
model-path $MAIN_DIR
6968
model-path $THIS_PRC_DIR/..
7069
model-path $THIS_PRC_DIR/../models
70+
model-path C:/Panda3D-1.10.6-x64
71+
model-path C:/Panda3D-1.10.6-x64/models
7172

7273
# This enable the automatic creation of a TK window when running
7374
# Direct.

0 commit comments

Comments
 (0)