-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shadow casting component for aircraft
- Loading branch information
Showing
7 changed files
with
236 additions
and
2 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/resources/ShadowMaterial.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2024, Emanuel Rabina (http://www.ultraq.net.nz/) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package nz.net.ultraq.redhorizon.classic.resources | ||
|
||
import nz.net.ultraq.redhorizon.engine.graphics.SpriteMaterial | ||
|
||
/** | ||
* A material for drawing a sprite silhouette. | ||
* | ||
* @author Emanuel Rabina | ||
*/ | ||
class ShadowMaterial extends SpriteMaterial { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/shaders/Shadow.frag.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#version 410 core | ||
|
||
in vec4 v_vertexColour; | ||
in vec2 v_textureUVs; | ||
|
||
out vec4 fragmentColour; | ||
|
||
uniform sampler2D indexTexture; | ||
|
||
/** | ||
* Fragment shader main function, emits only a shadow colour for any pixel that | ||
* is not using the 0th palette index. | ||
*/ | ||
void main() { | ||
|
||
int index = int(texture(indexTexture, v_textureUVs).x * 256); | ||
vec4 colour = vec4(0, 0, 0, index == 0 ? 0 : 0.5); | ||
|
||
fragmentColour = colour * v_vertexColour; | ||
} |
36 changes: 36 additions & 0 deletions
36
redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/shaders/Shadow.vert.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#version 410 core | ||
|
||
in vec4 position; | ||
in vec4 colour; | ||
in vec2 textureUVs; | ||
|
||
out vec4 v_vertexColour; | ||
out vec2 v_textureUVs; | ||
|
||
layout (std140) uniform Camera { | ||
mat4 projection; | ||
mat4 view; | ||
}; | ||
uniform mat4 model; | ||
layout (std140) uniform SpriteMetadata { | ||
float frameStepX; | ||
float frameStepY; | ||
int framesHorizontal; | ||
int framesVertical; | ||
}; | ||
uniform int frame; | ||
|
||
/** | ||
* Vertex shader main function, mostly passes geometry information along to the | ||
* fragment shader. | ||
*/ | ||
void main() { | ||
|
||
gl_Position = projection * view * model * position; | ||
v_vertexColour = colour; | ||
|
||
// Adjust textureUVs to the location of the selected frame in the spritesheet | ||
float textureU = (frame % framesHorizontal) * frameStepX; | ||
float textureV = floor(frame / framesHorizontal) * frameStepY; | ||
v_textureUVs = textureUVs + vec2(textureU, textureV); | ||
} |
53 changes: 53 additions & 0 deletions
53
redhorizon-classic/source/nz/net/ultraq/redhorizon/classic/shaders/ShadowShader.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2024, Emanuel Rabina (http://www.ultraq.net.nz/) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package nz.net.ultraq.redhorizon.classic.shaders | ||
|
||
import nz.net.ultraq.redhorizon.classic.resources.ShadowMaterial | ||
import nz.net.ultraq.redhorizon.engine.graphics.Attribute | ||
import nz.net.ultraq.redhorizon.engine.graphics.Shader | ||
import nz.net.ultraq.redhorizon.engine.graphics.ShaderConfig | ||
import nz.net.ultraq.redhorizon.engine.graphics.Uniform | ||
|
||
/** | ||
* A shader to draw a silhouette of an existing sprite object. | ||
* | ||
* @author Emanuel Rabina | ||
*/ | ||
class ShadowShader extends ShaderConfig { | ||
|
||
final String name = 'Shadow' | ||
final String vertexShaderSource = getResourceAsText('nz/net/ultraq/redhorizon/classic/shaders/Shadow.vert.glsl') | ||
final String fragmentShaderSource = getResourceAsText('nz/net/ultraq/redhorizon/classic/shaders/Shadow.frag.glsl') | ||
final Attribute[] attributes = [Attribute.POSITION, Attribute.COLOUR, Attribute.TEXTURE_UVS] | ||
final Uniform[] uniforms = [ | ||
{ Shader shader, ShadowMaterial material, window -> | ||
shader.setUniformTexture('indexTexture', 0, material.texture) | ||
}, | ||
{ Shader shader, ShadowMaterial material, window -> | ||
if (material.spriteMetadataBuffer) { | ||
material.spriteMetadataBuffer.bind() | ||
} | ||
else { | ||
shader.setUniform('framesHorizontal', material.framesHorizontal) | ||
shader.setUniform('framesVertical', material.framesVertical) | ||
shader.setUniform('frameStepX', material.frameStepX) | ||
shader.setUniform('frameStepY', material.frameStepY) | ||
} | ||
shader.setUniform('frame', material.frame) | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters