Skip to content

Commit

Permalink
remove light and material
Browse files Browse the repository at this point in the history
  • Loading branch information
yukiny0811 committed Feb 2, 2024
1 parent e38b84d commit accee88
Show file tree
Hide file tree
Showing 14 changed files with 2 additions and 135 deletions.
26 changes: 0 additions & 26 deletions Sources/SwiftyCreatives/DataTypes/Light.swift

This file was deleted.

22 changes: 0 additions & 22 deletions Sources/SwiftyCreatives/DataTypes/Material.swift

This file was deleted.

1 change: 0 additions & 1 deletion Sources/SwiftyCreatives/Renderers/AddRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public class AddRenderer<
renderCommandEncoder?.setRenderPipelineState(renderPipelineState)

drawProcess.beforeDraw(encoder: renderCommandEncoder!)
drawProcess.updateAndDrawLight(encoder: renderCommandEncoder!)
drawProcess.update(camera: camera)
drawProcess.draw(encoder: renderCommandEncoder!)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ extension RendererBase {
encoder.setVertexBuffer(DefaultBuffers.default_f2, offset: 0, index: VertexBufferIndex.UV.rawValue)
encoder.setVertexBuffer(DefaultBuffers.default_f3, offset: 0, index: VertexBufferIndex.Normal.rawValue)
encoder.setVertexBuffer(DefaultBuffers.default_f4x4, offset: 0, index: VertexBufferIndex.CustomMatrix.rawValue)
encoder.setFragmentBuffer(DefaultBuffers.default_material, offset: 0, index: FragmentBufferIndex.Material.rawValue)
encoder.setFragmentBytes([1], length: Float.memorySize, index: FragmentBufferIndex.LightCount.rawValue)
encoder.setFragmentBuffer(DefaultBuffers.default_light, offset: 0, index: FragmentBufferIndex.Lights.rawValue)
encoder.setFragmentBuffer(DefaultBuffers.default_false, offset: 0, index: FragmentBufferIndex.HasTexture.rawValue)
encoder.setFragmentBuffer(DefaultBuffers.default_false, offset: 0, index: FragmentBufferIndex.IsActiveToLight.rawValue)
encoder.setFragmentBytes([0], length: Float.memorySize, index: FragmentBufferIndex.FogDensity.rawValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public class NormalBlendRenderer<
renderCommandEncoder?.setFragmentTexture(AssetUtil.defaultMTLTexture, index: FragmentTextureIndex.MainTexture.rawValue)

drawProcess.beforeDraw(encoder: renderCommandEncoder!)
drawProcess.updateAndDrawLight(encoder: renderCommandEncoder!)
drawProcess.update(camera: camera)
drawProcess.draw(encoder: renderCommandEncoder!)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ public class NormalBlendRendererVision: RendererBase {
renderCommandEncoder?.setFragmentTexture(AssetUtil.defaultMTLTexture, index: FragmentTextureIndex.MainTexture.rawValue)

drawProcess.beforeDraw(encoder: renderCommandEncoder!)
drawProcess.updateAndDrawLight(encoder: renderCommandEncoder!)
drawProcess.update()
drawProcess.draw(encoder: renderCommandEncoder!)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ class TransparentRenderer<

// MARK: - draw primitive
drawProcess.beforeDraw(encoder: renderEncoder)
drawProcess.updateAndDrawLight(encoder: renderEncoder)
drawProcess.update(camera: camera)
drawProcess.draw(encoder: renderEncoder)

Expand Down
9 changes: 0 additions & 9 deletions Sources/SwiftyCreatives/Resources/Shaders/AddShader.metal
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ vertex RasterizerData add_vertex (const Vertex vIn [[ stage_in ]],

fragment half4 add_fragment (RasterizerData rd [[stage_in]],
half4 c [[color(0)]],
const device Material &material [[ buffer(FragmentBuffer_Material) ]],
const device int &lightCount [[ buffer(FragmentBuffer_LightCount) ]],
const device Light *lights [[ buffer(FragmentBuffer_Lights) ]],
const device FrameUniforms_HasTexture &uniformHasTexture [[ buffer(FragmentBuffer_HasTexture) ]],
const device FrameUniforms_IsActiveToLight &isActiveToLight [[ buffer(FragmentBuffer_IsActiveToLight) ]],
const device FrameUniforms_FogDensity &fogDensity [[ buffer(FragmentBuffer_FogDensity) ]],
const device FrameUniforms_FogColor &fogColor [[ buffer(FragmentBuffer_FogColor) ]],
texture2d<half, access::sample> tex [[ texture(FragmentTexture_MainTexture) ]]) {
Expand All @@ -61,11 +57,6 @@ fragment half4 add_fragment (RasterizerData rd [[stage_in]],
resultColor = half4(rd.color);
}

if (isActiveToLight.value) {
float3 phongIntensity = calculatePhongIntensity(rd, material, lightCount, lights);
resultColor = half4(float4(resultColor) * float4(phongIntensity, 1));
}

resultColor = half4(createFog(rd.position.z / rd.position.w,
float4(resultColor),
fogDensity.value,
Expand Down
36 changes: 0 additions & 36 deletions Sources/SwiftyCreatives/Resources/Shaders/Functions.metal
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,6 @@
#include "Types.metal"
using namespace metal;

inline float3 calculatePhongIntensity(
RasterizerData rd,
const device Material &material,
const device int &lightCount,
const device Light *lights
) {
float3 totalAmbient = float3(0, 0, 0);
float3 totalDiffuse = float3(0, 0, 0);
float3 totalSpecular = float3(0, 0, 0);

float3 unitNormal = normalize(rd.surfaceNormal);
float3 unitToCameraVector = normalize(rd.toCameraVector);

for (int i = 0; i < lightCount; i++) {
float3 unitToLightVector = normalize(lights[i].position - rd.worldPosition);
float3 unitReflectionVector = normalize(reflect(-unitToLightVector, unitNormal));

float3 ambientColor = clamp(material.ambient * lights[i].ambientIntensity * lights[i].brightness * lights[i].color, 0.0, 1.0);
totalAmbient += ambientColor;

float3 diffuseness = material.diffuse * lights[i].diffuseIntensity;
float nDotL = max(dot(unitNormal, unitToLightVector), 0.0);
float3 diffuseColor = clamp(diffuseness * nDotL * lights[i].brightness * lights[i].color, 0.0, 1.0);
totalDiffuse += diffuseColor;

float3 specularness = material.specular * lights[i].specularIntensity;
float rDotV = max(dot(unitReflectionVector, unitToCameraVector), 0.0);
float specularExponential = pow(rDotV, material.shininess);
float3 specularColor = clamp(specularness * specularExponential * lights[i].brightness * lights[i].color, 0.0, 1.0);
totalSpecular += specularColor;
}
float3 phongIntensity = totalAmbient + totalDiffuse + totalSpecular;

return phongIntensity;
}

inline float4x4 createModelMatrix(
Vertex vIn,
const device FrameUniforms_ModelPos& uniformModelPos,
Expand Down
9 changes: 0 additions & 9 deletions Sources/SwiftyCreatives/Resources/Shaders/NormalShader.metal
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ vertex RasterizerData normal_vertex (const Vertex vIn [[ stage_in ]],
}

fragment half4 normal_fragment (RasterizerData rd [[stage_in]],
const device Material &material [[ buffer(FragmentBuffer_Material) ]],
const device int &lightCount [[ buffer(FragmentBuffer_LightCount) ]],
const device Light *lights [[ buffer(FragmentBuffer_Lights) ]],
const device FrameUniforms_HasTexture &uniformHasTexture [[ buffer(FragmentBuffer_HasTexture) ]],
const device FrameUniforms_IsActiveToLight &isActiveToLight [[ buffer(FragmentBuffer_IsActiveToLight) ]],
const device FrameUniforms_FogDensity &fogDensity [[ buffer(FragmentBuffer_FogDensity) ]],
const device FrameUniforms_FogColor &fogColor [[ buffer(FragmentBuffer_FogColor) ]],
texture2d<half, access::sample> tex [[ texture(FragmentTexture_MainTexture) ]]) {
Expand All @@ -59,11 +55,6 @@ fragment half4 normal_fragment (RasterizerData rd [[stage_in]],
resultColor = half4(rd.color.x, rd.color.y, rd.color.z, 1);
}

if (isActiveToLight.value) {
float3 phongIntensity = calculatePhongIntensity(rd, material, lightCount, lights);
resultColor = half4(float4(resultColor) * float4(phongIntensity, 1));
}

resultColor = half4(createFog(rd.position.z / rd.position.w,
float4(resultColor),
fogDensity.value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,7 @@ inline void InsertFragment(OITDataT oitData, half4 color, half depth, half trans
template <typename OITDataT>
void OITFragmentFunction(RasterizerData in,
OITDataT oitData,
const device Material &material,
const device int &lightCount,
const device Light *lights,
FrameUniforms_HasTexture uniformHasTexture,
FrameUniforms_IsActiveToLight isActiveToLight,
FrameUniforms_FogDensity uniformFogDensity,
FrameUniforms_FogColor uniformFogColor,
texture2d<half> tex) {
Expand All @@ -129,11 +125,6 @@ void OITFragmentFunction(RasterizerData in,
fragmentColor = tex.sample(textureSampler, float2(in.uv.x*tex.get_width(), in.uv.y*tex.get_height()));
}

if (isActiveToLight.value) {
float3 phongIntensity = calculatePhongIntensity(in, material, lightCount, lights);
fragmentColor = half4(float4(fragmentColor) * float4(phongIntensity, 1));
}

fragmentColor = half4(createFog(in.position.z / in.position.w,
float4(fragmentColor),
uniformFogDensity.value,
Expand Down Expand Up @@ -169,15 +160,11 @@ half4 OITResolve(OITData<NUM_LAYERS> pixelData) {
fragment FragOut<4>
OITFragmentFunction_4Layer(RasterizerData in [[ stage_in ]],
OITImageblock<4> oitImageblock [[ imageblock_data ]],
const device Material &material [[ buffer(FragmentBuffer_Material) ]],
const device int &lightCount [[ buffer(FragmentBuffer_LightCount) ]],
const device Light *lights [[ buffer(FragmentBuffer_Lights) ]],
const device FrameUniforms_HasTexture &uniformHasTexture [[ buffer(FragmentBuffer_HasTexture) ]],
const device FrameUniforms_IsActiveToLight &isActiveToLight [[ buffer(FragmentBuffer_IsActiveToLight) ]],
const device FrameUniforms_FogDensity &fogDensity [[ buffer(FragmentBuffer_FogDensity) ]],
const device FrameUniforms_FogColor &fogColor [[ buffer(FragmentBuffer_FogColor) ]],
texture2d<half, access::sample> tex [[ texture(FragmentTexture_MainTexture) ]]) {
OITFragmentFunction(in, &oitImageblock.oitData, material, lightCount, lights, uniformHasTexture, isActiveToLight, fogDensity, fogColor, tex);
OITFragmentFunction(in, &oitImageblock.oitData, uniformHasTexture, fogDensity, fogColor, tex);
FragOut<4> Out;
Out.aoitImageBlock = oitImageblock;
return Out;
Expand Down
11 changes: 1 addition & 10 deletions Sources/SwiftyCreatives/Sketch/Sketch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ open class Sketch: SketchBase, FunctionBase {
public var packet: SCPacket {
SCPacket(privateEncoder: privateEncoder!, customMatrix: getCustomMatrix())
}
public var LIGHTS: [Light] = [Light(position: f3(0, 10, 0),
color: f3.one,
brightness: 1,
ambientIntensity: 1,
diffuseIntensity: 1,
specularIntensity: 50)]

public init() {}

#if !os(visionOS)
Expand All @@ -52,10 +47,6 @@ open class Sketch: SketchBase, FunctionBase {
public func getCustomMatrix() -> f4x4 {
return customMatrix.reduce(f4x4.createIdentity(), *)
}
open func updateAndDrawLight(encoder: SCEncoder) {
encoder.setFragmentBytes([LIGHTS.count], length: Int.memorySize, index: FragmentBufferIndex.LightCount.rawValue)
encoder.setFragmentBytes(LIGHTS, length: Light.memorySize * LIGHTS.count, index: FragmentBufferIndex.Lights.rawValue)
}

#if os(macOS)
open func mouseMoved(with event: NSEvent, camera: MainCamera, viewFrame: CGRect) {}
Expand Down
1 change: 0 additions & 1 deletion Sources/SwiftyCreatives/Sketch/SketchBase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public protocol SketchBase: AnyObject {
func update()
#endif
func draw(encoder: SCEncoder)
func updateAndDrawLight(encoder: SCEncoder)
func beforeDraw(encoder: SCEncoder)
func preProcess(commandBuffer: MTLCommandBuffer)
func postProcess(texture: MTLTexture, commandBuffer: MTLCommandBuffer)
Expand Down
2 changes: 0 additions & 2 deletions Sources/SwiftyCreatives/Utils/DefaultBuffers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@ enum DefaultBuffers {
static let default_f3 = ShaderCore.device.makeBuffer(bytes: [f3.zero], length: f3.memorySize)
static let default_f4 = ShaderCore.device.makeBuffer(bytes: [f4.zero], length: f4.memorySize)
static let default_f4x4 = ShaderCore.device.makeBuffer(bytes: [f4x4.createIdentity()], length: f4x4.memorySize)
static let default_material = ShaderCore.device.makeBuffer(bytes: [Material(ambient: f3.zero, diffuse: f3.zero, specular: f3.zero, shininess: 0)], length: Material.memorySize)
static let default_light = ShaderCore.device.makeBuffer(bytes: [Light(position: f3.zero, color: f3.zero, brightness: 0, ambientIntensity: 0, diffuseIntensity: 0, specularIntensity: 0)], length: Light.memorySize)
}

0 comments on commit accee88

Please sign in to comment.