Skip to content

Commit 21fdc63

Browse files
committed
Working planet fragment/vertex phong shader
1 parent cdefa76 commit 21fdc63

9 files changed

Lines changed: 90 additions & 67 deletions

File tree

Blobtory/Scripts/Main.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
from direct.task import Task
21
from panda3d.core import loadPrcFile, loadPrcFileData
3-
from direct.showbase.ShowBase import ShowBase, PTAFloat, \
4-
DirectionalLight, AntialiasAttrib, AmbientLight
2+
from direct.showbase.ShowBase import ShowBase, PTAFloat, AmbientLight
53

64
from Blobtory.Scripts.Pipeline.WindowCreator import WindowCreator
75

@@ -22,12 +20,12 @@ def __init__(self):
2220
super().__init__()
2321
self.winCreator = WindowCreator(self, enableRP=False, isFullscreen=False)
2422

25-
self.planetGen: PlanetGenerator = PlanetGenerator(self.winCreator, 128, 150)
23+
self.planetGen: PlanetGenerator = PlanetGenerator(self.winCreator, 128, 500)
2624
self.planetGen.cubeformer.mouseTime.setData(PTAFloat([10, 0, 0, 1]))
2725
self.accept("a", self.planetGen.RegenPlanet)
2826

2927
alight = AmbientLight('alight')
30-
alight.setColor((0.6, 0.6, 0.6, 1))
28+
alight.setColor((0.5, 0.5, 0.5, 1))
3129
alnp = self.render.attachNewNode(alight)
3230
self.render.setLight(alnp)
3331

Blobtory/Scripts/Pipeline/WindowCreator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,18 @@ def UpdateWindow(self, isFullscreen: bool, enableRP: bool):
8181
# filters.setBloom()
8282

8383
dlight = DirectionalLight('my dlight')
84-
dlight.setColor((0.8, 0.5, 0.5, 1))
84+
# dlight.setColor((0.8, 0.5, 0.5, 1))
8585
dlnp = self.base.render.attachNewNode(dlight)
86-
self.base.render.setAntialias(AntialiasAttrib.MAuto)
86+
self.base.render.setLight(dlnp)
87+
88+
dlight = DirectionalLight('my dlight')
89+
dlight.setColor((0.05, 0.05, 0.05, 1))
90+
dlnp = self.base.render.attachNewNode(dlight)
91+
dlnp.setHpr(0, 180, 0)
8792
self.base.render.setLight(dlnp)
8893

8994
#filters.setAmbientOcclusion()
9095
# filters.setVolumetricLighting(dlnp)
96+
self.base.render.setAntialias(AntialiasAttrib.MAuto)
9197
self.base.setBackgroundColor(0, 0.01, 0.1)
9298
self.base.setFrameRateMeter(True)

Blobtory/Scripts/planet_former/MarchingCubes.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import math
22

33
from panda3d.core import Texture, Shader, NodePath, LVecBase3i, ShaderAttrib, GeomEnums, GeomNode, \
4-
GeomVertexFormat, GeomVertexData, GeomTriangles, Geom, OmniBoundingVolume
4+
GeomVertexFormat, GeomVertexData, GeomTriangles, Geom, OmniBoundingVolume, Material, LColor
55

66
from assets.shaders.compute.includes import MarchTable
77
from Blobtory.Scripts.planet_former.CubeFormer import CubeFormer
@@ -123,8 +123,16 @@ def GenerateMesh(self):
123123
-self.size.getX()*0.5*self.cubeformer.spacing,
124124
-self.size.getY()*0.5*self.cubeformer.spacing,
125125
-self.size.getZ()*0.5*self.cubeformer.spacing)
126+
126127
self.winCreator.pipelineSwitcher.AddModelWithShaderGeneralName(self.geomPath, "assets/shaders/planets/planet")
127128
self.geomPath.set_shader_input('vertexBufferEdge', self.edgeVertexBuffer)
128129
self.geomPath.set_shader_input('triangleBuffer', self.triangleBuffer)
129130
self.geomPath.set_shader_input('normalBuffer', self.normalBuffer)
130131

132+
myMaterial = Material()
133+
myMaterial.setShininess(0.8) # Make this material shiny
134+
myMaterial.setDiffuse(LColor(0.95, 0.24, 0.06, 1)*0.1)
135+
myMaterial.setSpecular((1.1, 1.1, 1.1, 1))
136+
myMaterial.setAmbient((0.04, 0.01, 0.006, 1)) # Make this material blue
137+
self.geomPath.setMaterial(myMaterial)
138+

assets/shaders/compute/alphaNoiseSphere.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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*10;
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

@@ -57,7 +57,7 @@ void main() {
5757
float theta = rand((offset+i))*2*PI;
5858
float phi = rand((offset+i+200))*2*PI;
5959

60-
float cometSize = rand((offset+i))*20+4;
60+
float cometSize = rand((offset+i))*150+50;
6161

6262
vec3 cartCraterNormal = vec3(sin(theta)*cos(phi), sin(theta)*sin(phi), cos(theta));
6363
vec3 cartCraterPosCenter = (planetRadius*cometOffset-cometDiff+cometSize*sizeMatters)*cartCraterNormal;

assets/shaders/compute/cubemarcher.glsl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ void main() {
4343
if (cubeCorners[6].w < isoLevel) cubeIndex |= 64;
4444
if (cubeCorners[7].w < isoLevel) cubeIndex |= 128;
4545

46+
float wComb = 0;
47+
for (int i = 0; i < 8; i++) wComb += cubeCorners[i].w;
48+
4649
int i = 0;
4750
int triangleEdgeIndex = 1;
4851
do {
@@ -67,7 +70,7 @@ void main() {
6770

6871
vec3 normal = cross(v2-v0, v1-v0);
6972
//vec3 normal = normalize(v2+v0+v1);
70-
imageStore(normalBuffer, int(triangleIDIndex/3), vec4(normal, triangleIDIndex/1000));
73+
imageStore(normalBuffer, int(triangleIDIndex/3), vec4(normal, wComb));
7174

7275
i+=3;
7376
} while (triangleEdgeIndex != -1);

assets/shaders/planets/planet.frag

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
#version 130
22

33
#pragma include "../utils/p3d_light_sources.glsl"
4-
5-
#define MAX_ITERATION 20
4+
#pragma include "../utils/p3d_material.glsl"
65

76
// Input from vertex shader
8-
in vec2 texcoord;
7+
in vec3 viewspacePos;
8+
in vec4 fragPos;
99
in vec3 vertexNormal;
1010
in vec3 primNormal;
11-
in vec3 FragPos;
1211
in float num;
1312

14-
in vec3 pospos;
15-
1613
in vec3 cam_pos;
1714
in vec3 cam_dir;
1815

1916
out vec4 outputColor;
20-
const float atten = 0.5;
21-
const float shininess = 0.5;
22-
23-
const float specularStrength = 0.9;
24-
const vec3 specColor = vec3(1,1,1);
25-
const vec3 matColor = vec3(0,0.2,0);
2617

2718
vec3 remap(vec3 iMin, vec3 iMax, vec3 oMin, vec3 oMax, vec3 v) {
2819
vec3 t = smoothstep(iMin, iMax, v);
@@ -35,36 +26,49 @@ float remap(float iMin, float iMax, float oMin, float oMax, float v) {
3526
}
3627

3728
void main() {
38-
vec3 rgb_normal = (normalize(primNormal) + 0.5) * 0.5;
39-
40-
//outputColor = vec4(rgb_normal, 1)*clamp(vec4(dot(primNormal, )),0,1);
41-
//outputColor = vec4(rgb_normal, 1);//*clamp(primNormal, 0, 1).x;
42-
vec3 lightDir = p3d_LightSource[0].position.xyz;
43-
lightDir = vec3(0,-0.5,0.5);
44-
45-
float diff = max(dot(primNormal, lightDir), 0.0);
46-
vec3 diffuse = diff * vec3(0.1);//p3d_LightSource[0].color.xyz;
47-
48-
vec3 viewDir = normalize(cam_pos - FragPos);
49-
vec3 reflectDir = reflect(lightDir, normalize(primNormal));
50-
51-
float spec = pow(max(dot(-viewDir, reflectDir), 0.0), 32);
52-
vec3 specular = specularStrength * spec * specColor;
53-
54-
vec3 light = diffuse + specular;
55-
//if (any(lessThan(normalize(pospos), vec3(0)))) light = vec3(0);
56-
57-
vec3 result = (p3d_LightModel.ambient.xyz+light) * vec3(remap(0.5, 10, 0, 1, dot(pospos,vec3(1))));
58-
outputColor = vec4(result, 1);
59-
60-
//outputColor = vec4()
61-
//outputColor = vec4(rgb_normal, 1);// * smoothstep(0,1, dot(vertexNormal, lightDir)+0.9*dot(p3d_LightModel.ambient.xyz, vec3(1)));
62-
// vec3 diffuseReflection = atten * p3d_LightSource[0].color.xyz * max(normalize(primNormal), lightDir);
63-
// vec3 lightReflectDir = reflect(-lightDir, normalize(primNormal));
64-
// float lightSeeDir = max(0.0, dot(lightReflectDir, cam_dir));
65-
// vec3 shininessPower = vec3(pow(lightSeeDir.x, shininess), pow(lightSeeDir.y, shininess), pow(lightSeeDir.z, shininess));
66-
// float shininessPower = pow(lightSeeDir, (shininess));
67-
// vec3 specularReflect = atten * specColor * shininessPower;
68-
//
69-
// outputColor = vec4(matColor*(diffuseReflection+p3d_LightModel.ambient.xyz), 1);
29+
int mode = 1;
30+
//vec3 viewDir = normalize(viewspacePos.xyz-cam_pos);
31+
vec3 illumLightSum = vec3(0);
32+
vec3 normal = normalize(primNormal);
33+
34+
for (int i = 0; i < p3d_LightSource.length; i++) {
35+
vec3 lightDir = p3d_LightSource[i].position.xyz;
36+
if (p3d_LightSource[i].position.w != 0) lightDir -= viewspacePos;
37+
38+
float distance = length(lightDir);
39+
distance = distance * distance;
40+
lightDir = normalize(lightDir);
41+
42+
float lambertian = max(dot(lightDir, normal), 0.0);
43+
float specular = 0.0;
44+
45+
if (lambertian > 0.0) {
46+
vec3 viewDir = normalize(-viewspacePos);
47+
48+
// this is blinn phong
49+
vec3 halfDir = normalize(lightDir + viewDir);
50+
float specAngle = max(dot(halfDir, normal), 0.0);
51+
specular = pow(specAngle, p3d_Material.shininess);
52+
53+
// this is phong (for comparison)
54+
if (mode == 2) {
55+
vec3 reflectDir = reflect(-lightDir, normal);
56+
specAngle = max(dot(reflectDir, viewDir), 0.0);
57+
// note that the exponent is different here
58+
specular = pow(specAngle, p3d_Material.shininess/4.0);
59+
}
60+
}
61+
vec3 illumDiffuse = (p3d_Material.diffuse.xyz) * lambertian * p3d_LightSource[i].color.xyz * 1 / distance;
62+
vec3 illumSpecular = p3d_Material.diffuse.xyz*p3d_Material.specular * specular * p3d_LightSource[i].color.xyz * 1 / distance;
63+
64+
illumLightSum += illumDiffuse+illumSpecular;
65+
}
66+
67+
//vec3 reflectDir = reflect(-lightDir, normalize(primNormal));
68+
//float spec = pow(max(dot(viewDir, reflectDir), 0.0), 64);
69+
//vec3 specular = specularStrength * spec * specColor;
70+
71+
vec3 colorGammaCorrected = pow(p3d_LightModel.ambient.xyz*p3d_Material.ambient.xyz+illumLightSum, vec3(0.49504950495));
72+
// use the gamma corrected color in the fragment0
73+
outputColor = vec4(colorGammaCorrected, 1.0);
7074
}

assets/shaders/planets/planet.vert

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,46 @@
22

33
// Uniform inputs
44
uniform mat4 p3d_ModelViewProjectionMatrix;
5-
uniform mat4 p3d_ViewProjectionMatrix;
6-
uniform mat4 p3d_ModelMatrix;
75
uniform mat4 p3d_ViewMatrixInverse;
6+
uniform mat4 p3d_ModelMatrix;
7+
uniform mat4 p3d_ModelViewMatrix;
8+
uniform mat3 p3d_NormalMatrix;
89

910
layout(rgba32f) uniform readonly image3D vertexBufferEdge;
1011
layout(rgba32i) uniform readonly iimageBuffer triangleBuffer;
1112
layout(rgba32f) uniform readonly imageBuffer normalBuffer;
1213

13-
// Vertex inputs
14+
//Inputs
1415
in vec2 p3d_MultiTexCoord0;
15-
in vec3 p3d_Normal;
1616

1717
// Output to fragment shader
1818
out vec2 texcoord;
1919
out vec3 vertexNormal;
2020
out vec3 primNormal;
21-
out vec3 FragPos;
2221
out float num;
23-
out vec3 pospos;
22+
out vec4 fragPos;
23+
out vec3 viewspacePos;
2424

2525
out vec3 cam_pos;
2626
out vec3 cam_dir;
2727

2828
void main() {
2929
ivec3 vertexIndex = imageLoad(triangleBuffer, gl_VertexID).xyz;
3030
vec4 vertex = vec4(imageLoad(vertexBufferEdge, vertexIndex).xyz, 1);
31-
//vertex = vec4(gl_VertexID, gl_VertexID, gl_VertexID+1, 0);
3231
gl_Position = p3d_ModelViewProjectionMatrix*vertex;
32+
fragPos = gl_Position;
33+
34+
vec4 viewspacePos4 = p3d_ModelViewMatrix * vertex;
35+
viewspacePos = vec3(viewspacePos4) / viewspacePos4.w;
36+
3337
vec4 primNormalWVal = imageLoad(normalBuffer, int(gl_VertexID/3));
3438
primNormal = primNormalWVal.xyz;
35-
primNormal = mat3(transpose(inverse(p3d_ModelMatrix))) * primNormal;
39+
primNormal = p3d_NormalMatrix * primNormal;
40+
3641
vertexNormal = normalize(vertex.xyz);
37-
FragPos = vec3(p3d_ModelViewProjectionMatrix * vertex);
42+
3843
texcoord = p3d_MultiTexCoord0;
3944
num = primNormalWVal.w;
40-
pospos = vertex.xyz;
4145

4246
cam_pos = p3d_ViewMatrixInverse[3].xyz;
4347
cam_dir = -p3d_ViewMatrixInverse[2].xyz;

assets/shaders/utils/p3d_light_sources.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ uniform struct p3d_LightSourceParameters {
2525

2626
// Transforms view-space coordinates to shadow map coordinates
2727
mat4 shadowViewMatrix;
28-
} p3d_LightSource[];
28+
} p3d_LightSource[8];
2929

3030
// The sum of all active ambient light colors.
3131
uniform struct {

version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version = "0.0.1"
1+
version = "0.0.2"
22
print(version)

0 commit comments

Comments
 (0)