skycraft: per-object specular color

This commit is contained in:
Paul Mathieu 2025-02-12 19:07:48 +01:00
parent 6966c5d45f
commit ee7f5e9595

View File

@ -15,12 +15,14 @@ uniform mat4 uView;
uniform vec3 uLightDirection; uniform vec3 uLightDirection;
uniform float uAmbiantLight; uniform float uAmbiantLight;
uniform vec3 uGlowColor; uniform vec3 uGlowColor;
uniform vec3 uSpecularColor;
varying highp vec2 vTextureCoord; varying highp vec2 vTextureCoord;
varying lowp vec3 vLighting; varying lowp vec3 vLighting;
varying lowp vec3 vRay; varying lowp vec3 vRay;
varying lowp vec3 vLightDir; varying lowp vec3 vLightDir;
varying lowp vec3 vNormal; varying lowp vec3 vNormal;
varying lowp vec3 vSpecularColor;
highp mat3 transpose(in highp mat3 inmat) { highp mat3 transpose(in highp mat3 inmat) {
highp vec3 x = inmat[0]; highp vec3 x = inmat[0];
@ -50,6 +52,7 @@ void main() {
vRay = -normalize((uModel * vec4(aPosition, 1.0)).xyz - camPos); vRay = -normalize((uModel * vec4(aPosition, 1.0)).xyz - camPos);
vLightDir = -uLightDirection; vLightDir = -uLightDirection;
vNormal = normal; vNormal = normal;
vSpecularColor = uSpecularColor;
} }
`; `;
@ -61,6 +64,7 @@ varying lowp vec3 vLighting;
varying lowp vec3 vRay; varying lowp vec3 vRay;
varying lowp vec3 vLightDir; varying lowp vec3 vLightDir;
varying lowp vec3 vNormal; varying lowp vec3 vNormal;
varying lowp vec3 vSpecularColor;
void main() { void main() {
highp vec4 color = texture2D(uSampler, vTextureCoord); highp vec4 color = texture2D(uSampler, vTextureCoord);
@ -69,7 +73,7 @@ void main() {
} }
lowp vec3 specularDir = 2.0 * dot(vLightDir, vNormal) * vNormal - vLightDir; lowp vec3 specularDir = 2.0 * dot(vLightDir, vNormal) * vNormal - vLightDir;
lowp float specularAmount = smoothstep(0.92, 1.0, dot(vRay, specularDir)); lowp float specularAmount = smoothstep(0.92, 1.0, dot(vRay, specularDir));
lowp vec3 specular = 0.8 * specularAmount * vec3(1.0, 1.0, 0.8); lowp vec3 specular = specularAmount * vSpecularColor;
gl_FragColor = vec4(vLighting * color.rgb + specular, color.a); gl_FragColor = vec4(vLighting * color.rgb + specular, color.a);
} }
`; `;
@ -86,6 +90,7 @@ export async function initWorldGl(gl: WebGLRenderingContext) {
const lightDirectionLoc = gl.getUniformLocation(program, 'uLightDirection'); const lightDirectionLoc = gl.getUniformLocation(program, 'uLightDirection');
const ambiantLoc = gl.getUniformLocation(program, 'uAmbiantLight'); const ambiantLoc = gl.getUniformLocation(program, 'uAmbiantLight');
const glowColorLoc = gl.getUniformLocation(program, 'uGlowColor'); const glowColorLoc = gl.getUniformLocation(program, 'uGlowColor');
const specularColorLoc = gl.getUniformLocation(program, 'uSpecularColor');
const positionLoc = gl.getAttribLocation(program, 'aPosition'); const positionLoc = gl.getAttribLocation(program, 'aPosition');
const normalLoc = gl.getAttribLocation(program, 'aNormal'); const normalLoc = gl.getAttribLocation(program, 'aNormal');
@ -123,12 +128,14 @@ export async function initWorldGl(gl: WebGLRenderingContext) {
numVertices, numVertices,
lightDirection, lightDirection,
glowColor, glowColor,
specularColor,
} = objectParams; } = objectParams;
gl.uniformMatrix4fv(modelLoc, false, new Float32Array(se3.product( gl.uniformMatrix4fv(modelLoc, false, new Float32Array(se3.product(
se3.translation(...position), orientation))); se3.translation(...position), orientation)));
gl.uniform3fv(lightDirectionLoc, lightDirection); gl.uniform3fv(lightDirectionLoc, lightDirection);
gl.uniform3fv(glowColorLoc, glowColor); gl.uniform3fv(glowColorLoc, glowColor);
gl.uniform3fv(specularColorLoc, specularColor);
gl.bindBuffer(gl.ARRAY_BUFFER, glBuffer); gl.bindBuffer(gl.ARRAY_BUFFER, glBuffer);
@ -240,7 +247,7 @@ export function getOrbitDrawContext(gl: WebGLRenderingContext) {
function getObjects(context, body, parentPosition = undefined) { function getObjects(context, body, parentPosition = undefined) {
const objects = []; const objects = [];
const {gl, glContext, player} = context; const {gl, glContext, player} = context;
const {position, orientation, glowColor} = body; const {position, orientation, glowColor, specularColor} = body;
if (body.glBuffer === undefined) { if (body.glBuffer === undefined) {
body.glBuffer = makeBufferFromFaces(gl, body.geometry); body.glBuffer = makeBufferFromFaces(gl, body.geometry);
@ -252,6 +259,7 @@ function getObjects(context, body, parentPosition = undefined) {
position, position,
glContext, glContext,
glowColor, glowColor,
specularColor,
}); });
if (parentPosition !== undefined) { if (parentPosition !== undefined) {
const orbitObject = makeOrbitObject(gl, context.orbitGlContext, body.orbit, parentPosition); const orbitObject = makeOrbitObject(gl, context.orbitGlContext, body.orbit, parentPosition);
@ -268,6 +276,7 @@ function getObjects(context, body, parentPosition = undefined) {
orientation: shipOrientation, orientation: shipOrientation,
position: shipPos, position: shipPos,
glContext, glContext,
specularColor: [0.8, 0.8, 0.8],
}); });
} }
@ -311,7 +320,7 @@ export function draw(context) {
].reduce(se3.product)); ].reduce(se3.product));
let lastGlContext; let lastGlContext;
for (const {position, orientation, geometry, glContext, glowColor} of objects) { for (const {position, orientation, geometry, glContext, glowColor, specularColor} of objects) {
if (glContext !== lastGlContext) { if (glContext !== lastGlContext) {
glContext.setupScene({ glContext.setupScene({
projectionMatrix: projMatrix, projectionMatrix: projMatrix,
@ -330,6 +339,7 @@ export function draw(context) {
numVertices: geometry.numVertices, numVertices: geometry.numVertices,
lightDirection, lightDirection,
glowColor: glowColor || [0, 0, 0], glowColor: glowColor || [0, 0, 0],
specularColor: specularColor || [0, 0, 0],
}); });
} }
} }