Skip to content

Commit

Permalink
Improvement: Normals and tangents (if not present) are only calculate…
Browse files Browse the repository at this point in the history
…d if the assigned material actually requires them.
  • Loading branch information
atteneder committed Feb 25, 2020
1 parent 8459872 commit 08eb93e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Normals and tangents (if not present) are only calculated if the assigned material actually requires them.

## [0.10.1] - 2020-02-24
### Added
- Experimental KTX / Basis Universal support was merged (off by default)
Expand Down
3 changes: 3 additions & 0 deletions Runtime/Scripts/GltFast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,9 @@ void CreatePrimitiveContexts( Root gltf ) {
}
context.primtiveIndex = i;
context.materials[primIndex] = primitive.material;

context.needsNormals |= primitive.material<0 || gltf.materials[primitive.material].requiresNormals;
context.needsTangents |= primitive.material>=0 && gltf.materials[primitive.material].requiresTangents;
}

primitiveContexts[i] = context;
Expand Down
9 changes: 4 additions & 5 deletions Runtime/Scripts/PrimitiveCreateContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ public override bool IsCompleted {
if(normals!=null) {
msh.normals = normals;
} else
if(topology==MeshTopology.Triangles || topology==MeshTopology.Quads) {
if( needsNormals && ( topology==MeshTopology.Triangles || topology==MeshTopology.Quads ) ) {
Profiler.BeginSample("RecalculateNormals");
msh.RecalculateNormals();
Profiler.EndSample();
}
Profiler.EndSample();
Profiler.BeginSample("SetColor");
Expand All @@ -87,15 +89,12 @@ public override bool IsCompleted {
if(tangents!=null) {
msh.tangents = tangents;
} else
if(topology==MeshTopology.Triangles || topology==MeshTopology.Quads) {
// TODO: Improvement idea: by only calculating tangents, if they are actually needed
if( needsTangents && uvs0!=null && (topology==MeshTopology.Triangles || topology==MeshTopology.Quads) ) {
Profiler.BeginSample("RecalculateTangents");
msh.RecalculateTangents();
Profiler.EndSample();
}
Profiler.EndSample();
// primitives[c.primtiveIndex] = new Primitive(msh,c.primitive.material);
// resources.Add(msh);

Profiler.BeginSample("Dispose");
Dispose();
Expand Down
2 changes: 2 additions & 0 deletions Runtime/Scripts/PrimitiveCreateContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace GLTFast {
abstract class PrimitiveCreateContextBase {
public int primtiveIndex;
public int[] materials;
public bool needsNormals;
public bool needsTangents;
public abstract bool IsCompleted {get;}
public abstract Primitive? CreatePrimitive();
}
Expand Down
4 changes: 2 additions & 2 deletions Runtime/Scripts/PrimitiveDracoCreateContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public override bool IsCompleted {
var mesh = DracoMeshLoader.CreateMesh(dracoMesh, out hasNormals, out hasTexcoords);
Profiler.EndSample();

if(!hasNormals) {
if(needsNormals && !hasNormals) {
Profiler.BeginSample("Draco.RecalculateNormals");
// TODO: Make optional. Only calculate if actually needed
mesh.RecalculateNormals();
Profiler.EndSample();
}
if(hasTexcoords) {
if(needsTangents && hasTexcoords) {
Profiler.BeginSample("Draco.RecalculateTangents");
// TODO: Make optional. Only calculate if actually needed
mesh.RecalculateTangents();
Expand Down
12 changes: 12 additions & 0 deletions Runtime/Scripts/Schema/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,17 @@ public AlphaMode alphaModeEnum {
/// lighting equation is evaluated.
/// </summary>
public bool doubleSided = false;

public bool requiresNormals {
get {
return extensions==null || extensions.KHR_materials_unlit==null;
}
}

public bool requiresTangents {
get {
return normalTexture!=null && normalTexture.index>=0;
}
}
}
}
2 changes: 1 addition & 1 deletion Runtime/Scripts/Schema/MeshPrimitive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class MeshPrimitive {
/// <summary>
/// The index of the material to apply to this primitive when rendering.
/// </summary>
public int material;
public int material = -1;

/// <summary>
/// The type of primitives to render. All valid values correspond to WebGL enums.
Expand Down

0 comments on commit 08eb93e

Please sign in to comment.