Hology
Hology 文檔
Hology 文檔
  • 👋歡迎來到Hology文檔
  • Getting started
    • Hology Engine簡介
    • 第一步指南
    • 編輯器基礎指南
      • 在場景中飛行
      • 放置物件
      • 選擇物件
      • 變換
      • 分組物件
      • 複製
    • 入門遊戲模板 - 第三人稱射擊遊戲
  • Tutorials
    • 滾球 - 遊戲玩法編程
    • 角色動畫程式設計
    • Character AI behavior
  • Release
    • 遊戲發行
      • Discord Activities
      • 臉書即時遊戲
      • 上傳至 Itch.io
      • 使用 GitHub Pages
      • 發布到Steam
      • iOS 和 Android
  • Assets
    • 3D模型
      • 客製化碰撞形狀
      • 材質自訂分配
    • 材質
    • 紋理
    • 預製體
  • Gameplay
    • 演員(Actors)
      • 創建演員類
      • 演員參數
      • Actor 元件
      • Actor 的生命週期
      • 生成 Actors
      • 移動 Actors
    • 服務
      • 載入資產
    • Player input
    • Collision detection
    • Physics
      • Physics body types
      • Applying forces
      • Ray casting
    • Trigger volumes
    • Character movement
    • Pointer events
    • Animation
      • Animation system
      • Character Animation
      • Animation State Machine
    • Sound
      • Audio parameter
    • 世界
    • Navigation
  • Shaders
    • 著色器介紹
    • Creating shaders
    • 著色器參數
    • Typescript shaders
      • Types
      • Math functions
      • Attributes
      • Varying
      • Uniforms
      • Textures
      • Arrays
      • Select
      • Lighting
    • Painted materials
    • Water shader tutorial
  • Level design
    • 地形雕刻
    • 地形繪製
    • Grass
  • User Interfaces
    • 創建用戶界面UI
    • 使用 React
    • VR
  • Visual Effects
    • 視覺效果簡介
    • 視覺特效資產
  • Integrations
    • Arcweave
由 GitBook 提供支持
在本页
  • Creating a shader class
  • Using Typescript
  • Using GLSL
  • Exporting shaders
  • When to use Typescript or GLSL
  1. Shaders

Creating shaders

Shaders are created by defining TypeScript classes. The class can receive parameters and should return an instance of a material. The class should inherit from a base class such as Shader or NodeShader and override a specific method in each.

Creating a shader class

Shaders can be written in either Typescript or GLSL.

Using Typescript

To write a shader with typescript, extend from NodeShader and override the output method.


import { rgb, standardMaterial } from "@hology/core/shader-nodes";
import { NodeShader, NodeShaderOutput, Parameter } from "@hology/core/shader/shader";
import { Color } from "three";

export class ExampleShader extends NodeShader {
  @Parameter()
  color: Color = new Color(0xff0000)

  output(): NodeShaderOutput {
    return {
      color: standardMaterial({color: rgb(this.color)})
    }
  }
}

Using GLSL

To create a shader with GLSL, extend from the Shader class and override the build method, returning a new shader material.

import { Shader } from "@hology/core/shader/shader";
import { Color, ShaderMaterial, Material } from "three";

export class MyShader extends Shader {
  
  build(): Material {
    return new ShaderMaterial({
      vertexShader: `
        void main() {
          gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
        }
      `,
      fragmentShader: `
        void main() {
          gl_FragColor = vec4(1., 0., 0., 1.);
        }
      `
    })
  }
}

Exporting shaders

For shaders to be available in the editor, shaders have to be exported in the index.ts file in a configured directory.

import { ExampleShader } from './example-shader';
import { MyShader } from './my-shader';

export default {
  ExampleShader,
  MyShader,
}

The folder in which this index.ts file is, should be configured in the hology.config.json file in the root of your project's directory.

When to use Typescript or GLSL

While you could achive the same thing directly with GLSL as in Typescript, it may require significantly more code. With typescript, it is much easier to reuse code. In the above example, we use `standardMaterial()` which adds light calculations to your color which otherwise is non-trivial to calculate on your own.

GLSL could be useful if your material doesn't require light calculations and your are more familiar with GLSL or already have code snippets you want to reuse.

Using the node based approach with Typescript is more common in Hology Engine and tutorials.

上一页著色器介紹下一页著色器參數