mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 02:59:37 +00:00 
			
		
		
		
	use shader uniforms for scaling cube
This commit is contained in:
		@ -218,6 +218,10 @@ class Renderer {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  func paint() throws {
 | 
					  func paint() throws {
 | 
				
			||||||
 | 
					    var uniforms = ShaderUniforms(
 | 
				
			||||||
 | 
					      model: .init(diagonal: .init(0.5, 0.5, 0.5, 1.0)),
 | 
				
			||||||
 | 
					      projView: matrix_identity_float4x4)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    guard let rt = layer.nextDrawable() else {
 | 
					    guard let rt = layer.nextDrawable() else {
 | 
				
			||||||
      throw RendererError.drawFailure("Failed to get next drawable render target")
 | 
					      throw RendererError.drawFailure("Failed to get next drawable render target")
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -239,6 +243,10 @@ class Renderer {
 | 
				
			|||||||
    encoder.setVertexBuffer(vtxBuffer,
 | 
					    encoder.setVertexBuffer(vtxBuffer,
 | 
				
			||||||
      offset: 0,
 | 
					      offset: 0,
 | 
				
			||||||
      index: ShaderInputIdx.vertices.rawValue)
 | 
					      index: ShaderInputIdx.vertices.rawValue)
 | 
				
			||||||
 | 
					    // Ideal as long as our uniforms total 4 KB or less
 | 
				
			||||||
 | 
					    encoder.setVertexBytes(&uniforms,
 | 
				
			||||||
 | 
					      length: MemoryLayout<ShaderUniforms>.stride,
 | 
				
			||||||
 | 
					      index: ShaderInputIdx.uniforms.rawValue)
 | 
				
			||||||
    encoder.drawIndexedPrimitives(
 | 
					    encoder.drawIndexedPrimitives(
 | 
				
			||||||
      type: .triangle,
 | 
					      type: .triangle,
 | 
				
			||||||
      indexCount: cubeIndices.count,
 | 
					      indexCount: cubeIndices.count,
 | 
				
			||||||
 | 
				
			|||||||
@ -12,10 +12,14 @@ struct FragmentInput {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
vertex FragmentInput vertexMain(
 | 
					vertex FragmentInput vertexMain(
 | 
				
			||||||
  uint vertexID [[vertex_id]],
 | 
					  uint vertexID [[vertex_id]],
 | 
				
			||||||
  device const ShaderVertex* vtx [[buffer(ShaderInputIdxVertices)]]
 | 
					  device const ShaderVertex* vtx [[buffer(ShaderInputIdxVertices)]],
 | 
				
			||||||
 | 
					  constant ShaderUniforms& u [[buffer(ShaderInputIdxUniforms)]]
 | 
				
			||||||
) {
 | 
					) {
 | 
				
			||||||
 | 
					  auto position = vtx[vertexID].position;
 | 
				
			||||||
 | 
					  position = u.projView * u.model * position;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  FragmentInput out;
 | 
					  FragmentInput out;
 | 
				
			||||||
  out.position = vtx[vertexID].position * float4(0.5, 0.5, 0.5, 1.0);
 | 
					  out.position = position;
 | 
				
			||||||
  out.normal   = vtx[vertexID].normal;
 | 
					  out.normal   = vtx[vertexID].normal;
 | 
				
			||||||
  out.texCoord = vtx[vertexID].texCoord;
 | 
					  out.texCoord = vtx[vertexID].texCoord;
 | 
				
			||||||
  return out;
 | 
					  return out;
 | 
				
			||||||
 | 
				
			|||||||
@ -11,7 +11,8 @@
 | 
				
			|||||||
#include <simd/simd.h>
 | 
					#include <simd/simd.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef NS_ENUM(NSInteger, ShaderInputIdx) {
 | 
					typedef NS_ENUM(NSInteger, ShaderInputIdx) {
 | 
				
			||||||
  ShaderInputIdxVertices = 0
 | 
					  ShaderInputIdxVertices = 0,
 | 
				
			||||||
 | 
					  ShaderInputIdxUniforms = 1
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct {
 | 
					typedef struct {
 | 
				
			||||||
@ -20,4 +21,9 @@ typedef struct {
 | 
				
			|||||||
  vector_float2 texCoord;
 | 
					  vector_float2 texCoord;
 | 
				
			||||||
} ShaderVertex;
 | 
					} ShaderVertex;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef struct {
 | 
				
			||||||
 | 
					  matrix_float4x4 model;
 | 
				
			||||||
 | 
					  matrix_float4x4 projView;
 | 
				
			||||||
 | 
					} ShaderUniforms;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif//SHADERTYPES_H
 | 
					#endif//SHADERTYPES_H
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user